shutter_wait_time = 5000;
images_wait_time = 5000;

function camera_init(){
    counter = create_counter();
    camera_start();
    $("#shutter-next").click(function(){

	    clearTimeout(timer_timeout);
	    camera_next(counter());
	});
}

function camera_start(position){
    timer_timeout = setTimeout(function(){
	    camera_next(counter());
	}, shutter_wait_time);
}

function create_counter(){
    var internal_counter = 0;
    return counter;

    function counter(way){
	if (way == null)
	    way = 1;
	if (way == 0)
	    internal_counter--;
	if (way == 1)
	    internal_counter++;
	if (internal_counter >= nr_images)
	    internal_counter = 0;
	if (internal_counter < 0)
	    internal_counter = nr_images - 1;
	return internal_counter;
    };
}


function camera_next(shutter_image){
    var blind = $("#shutter-blind");
    var shutter_time = 100;
    blind.animate({'left': 550},
		  shutter_time,
		  '',
		  function(){
		      $("#camera-image").attr('src', shutter_images[shutter_image]);
		      blind.animate({'left': 267},
				    shutter_time,
				    '',
				    function(){
					timer_timeout = setTimeout(function() { camera_next(counter()) }, shutter_wait_time);
				    });
		  });
}


function delayStartInit(){
	
    $("#images").cycle({
	    fx: 'scrollLeft',
		next: '#image-next',
		prev: '#image-prev',
		timeout: 15000,
		easing: 'easeInOutBack'
	});
}

function start_images_init(){
	setTimeout('delayStartInit()',1000);
}

function start_images_init1(){
    images_counter = create_counter();
    current_image = 1;
    images_start();
    $("#image-next").click(function(){
	    clearTimeout(images_timeout);
	    image_next(images_counter(1));
	});
    $("#image-prev").click(function(){
	    clearTimeout(images_timeout);
	    image_next(images_counter(0));
	});
}

function images_start(position){
    images_timeout = setTimeout(function(){
	    image_next(images_counter(1));
	}, images_wait_time);
}


function other_image()
{
    if (current_image == 1)
	return 2;
    return 1;
}
function switch_image()
{
}
function get_image(img)
{
    return $("#start-image-" + img);
}

function image_next(which_image){

    var curr = get_image(current_image);
    var other = get_image(other_image());
    current_image = current_image == 1 ? 2 : 1;

    curr.css({display: 'none'});
    other.attr({src: start_images[which_image]});
    other.css({display: 'block'});
    
    image_timeout = setTimeout(function() { image_next(images_counter(1)) }, images_wait_time);
}
