/* ------------------------------------------------- *
 * Global jQuery functions
 * ------------------------------------------------- */
 
 var totalImages=0;
 var swapLimit = 2;
 
$(document).ready(function(){
$("#frame").hide().parent().addClass('loading');
$('#gallery').hide();

// Homepage gallery support
	var i=0;
	
	$("#frame ul").reorder();
	$('#gallery li a img').each(function(){
		totalImages++;
		if(i<swapLimit){
		$(this).attr('src',images[i].original)
		i++;
		}
		
	});
	
	
	
	$('#frame ul').cycle({delay:50, speed:500, timeout:5000, sync:0, fit:1, after:swapImageAt});
	$('#gallery').show();
});

function replaceAtIndex(index_value)
{
var replace_img = $('#gallery li a img').get(index_value);
$(replace_img).attr('src',images[index_value].original); 



}


function swapImageAt(currSlideElement, nextSlideElement, options, forwardFlag) {

var current_index = $(nextSlideElement).index();

if(current_index+swapLimit<totalImages)
{
replaceAtIndex(current_index+swapLimit);
}

}
/* ------------------------------------------------- *
 * Additional JS functions
 * ------------------------------------------------- */
// Images swap
function swapImages(){
	
}
 
// List reorder
(function($){
	$.fn.reorder = function(){
		// random array sort from
		// http://javascript.about.com/library/blsort2.htm
		function randOrd() { return(Math.round(Math.random())-0.5); }
		return($(this).each(function() {
			var $this = $(this),
					$children = $this.children(),
					childCount = $children.length;
			if (childCount > 1) {
				$children.remove();
				var indices = [];
				for (i=0;i<childCount;i++) { indices[indices.length] = i; }
				indices = indices.sort(randOrd);
				$.each(indices,function(j,k) { $this.append($children.eq(k)); });
			}
		}));
};
})(jQuery);
 
$(window).load(function(){
	
	//setTimeout('swapImages',200);

// Image preloading
	$("#frame").parent().removeClass('loading');
	$("#frame").fadeIn(500);

// Homepage image behaviour
	var galWidth = ($('.container').innerWidth())-20;
	var winHeight = ($(window).height())-119;
	$("#frame").parent().css({
		'min-height':'0',
		'height':winHeight+'px'
	});
	$('#frame ul').css('height',winHeight+'px');
	$('#frame img').css({
		'width':galWidth+'px',
		'height':'auto'
	});
	
	var imgFirst = $('#frame li').eq(0).find('img');
	var imgHeight = imgFirst.height();
	var newHeight = (winHeight-imgHeight)/2;
	imgFirst.css('margin-top',newHeight+'px');

	$(window).bind('resize',function(){
		var galWidth = ($('.container').innerWidth())-20;
		var winHeight = ($(window).height())-119;
		$('#frame').parent().css('height',winHeight+'px');
		$('#frame ul').css('height',winHeight+'px');
		$('#frame img').css({
			'width':galWidth+'px',
			'height':'auto'
		});

		var imgFirst = $('#frame li').eq(0).find('img');
		var imgHeight = imgFirst.height();
		var newHeight = (winHeight-imgHeight)/2;
		imgFirst.css('margin-top',newHeight+'px');
	});
});

/*
var timerstart;

$(document).ready(function(){
timerstart = new Date().getTime();
});
$(window).load(function(){
elapsed = new Date().getTime() - timerstart;
console.log('elapsed : '+elapsed);
	
});

*/