// JavaScript Document

function resizescreen()
{	
	var w = $(window).width();
	var lin_w = img_w = img_c = img_d = 0;

	if (w < 1200){
		$("body").addClass('smallscreen');
	} else {
		$("body").removeClass('smallscreen');
	}	
	
	var width_img = 285;
	var conut_img_line = parseInt((w -280) / width_img);
	//$(".photolist").css({"width", (conut_img_line * width_img) + 'px'});
	//alert(width_img + " " + conut_img_line + " " + w);
	$(".photolist").width(conut_img_line * width_img);
	$(".quoteblock p.quote").width(w - conut_img_line * width_img - 50);
}
		
$(window).resize(function() { resizescreen(); } );
$(document).ready(function() { resizescreen(); 
		
	var photos = $('.photolist_base img');
	var queue = new Array(0, 1, 2, 3, 4, 5, 6, 7);

	var w = $(window).width();
	var queue_length = parseInt (w / 285) 

	for (var ind = 0; ind < queue_length; ind++) $(photos[queue[ind]]).clone().addClass('queue_image_'+ind).css( { 'height' : '190px' } ).appendTo('#img_line');

	setTimeout(function() { updatePhotolistCircular(photos.length - 1, queue, 0, photos); }, 2000);

	// circular update photo fade
	function updatePhotolistCircularFade(max, haystack, next, photolist) {
		rmInd = haystack[next];
		next = next == queue_length -1 ? 0 : next + 1;
		addInd = getIndex(max, haystack, 1);
		updateQueue(rmInd, addInd, haystack);
		margin = -parseInt($(photolist[addInd]).css('width'));
		$('#img_line img.queue_image_'+rmInd).css( { 'margin-left' : margin, } ).before($(photolist[addInd]).clone().addClass('queue_image_'+addInd).appendTo('#img_line'));
		$('#img_line img.queue_image_'+rmInd).fadeOut( 400, function() {$('#img_line img.queue_image_'+rmInd).remove();});

		setTimeout(function() { updatePhotolistCircularFade(max, haystack, next, photolist); }, 2000);				
	}

	// circular update photo
	function updatePhotolistCircular(max, haystack, next, photolist) {
		rmInd = haystack[next];
		next = next == queue_length -1 ? 0 : next + 1;
		addInd = getIndex(max, haystack, 1);
		updateQueue(rmInd, addInd, haystack);
		$('#img_line img.queue_image_'+rmInd).before($(photolist[addInd]).clone().addClass('queue_image_'+addInd).css( { 'width' : '0px', 'height' : '190px' } ).appendTo('#img_line'));
		$('#img_line img.queue_image_'+rmInd).animate( { 'width' : 'toggle', 'height' : '190px' }, 400 );
		$('#img_line img.queue_image_'+addInd).animate( { 'width' : $(photolist[addInd]).css('width') }, 400, function() {
			$('#img_line img.queue_image_'+rmInd).remove();
		});

		setTimeout(function() { updatePhotolistCircular(max, haystack, next, photolist); }, 2000);				
	}

	// random update photo
	function updatePhotolistRandom(max, haystack, photolist) {
		rmInd = getIndex(max, haystack, 0);
		addInd = getIndex(max, haystack, 1);
		updateQueue(rmInd, addInd, haystack);
		$('#img_line img.queue_image_'+rmInd).before($(photolist[addInd]).clone().addClass('queue_image_'+addInd).css( { 'width' : '0px', 'height' : '190px' } ).appendTo('#img_line'));
		$('#img_line img.queue_image_'+rmInd).animate( { 'width' : 'toggle', 'height' : '190px' }, 700 );
		$('#img_line img.queue_image_'+addInd).animate( { 'width' : $(photolist[addInd]).css('width') }, 700, function() {
			$('#img_line img.queue_image_'+rmInd).remove();
		});

		setTimeout(function() { updatePhotolistRandom(max, haystack, photolist); }, 3700);
	}

	function getIndex(max, haystack, in_arr) {
		var photo_index = getRandomInt(max);
		if (in_arr) while (in_array(photo_index, haystack)) photo_index = getRandomInt(max);
		else while (!in_array(photo_index, haystack)) photo_index = getRandomInt(max);
		return photo_index;
	}

	function getRandomInt(max) {
		return Math.floor(Math.random() * max);
	}

	function updateQueue(delInd, setInd, haystack) {
		for (var key in haystack) if (haystack[key] === delInd) haystack[key] = setInd;
	}

	function in_array(needle, haystack) {
		for(var key in haystack) if (haystack[key] === needle) return true;
		return false;
	}
});
