/* ------------------------------------------------- *
 * Global jQuery functions
 * ------------------------------------------------- */
 
 	// galery global info
		var imgIndex = 0;
		var maxImagesNo;
		var meX = 0;
		var swapLimit = 0 ;
	// image replacement
	
		var replaceQueue = new Array();
		var replacedList = new Array();
		//arrayName.splice(arrayIndex,1);

		var resizeTimer;
		var loadAtStart = 3;

$(document).ready(function(){

		$('#gallery2').hide();
		
		imgs = $('#gallery2 li');
		maxImagesNo = imgs.length;				
		$('#gallery2 li').addClass('loading');
		$('#gallery2 img').hide();
		
		$('#viewport').addClass('loading');

		var maxW = (maxImagesNo+1)*$('#gallery2 li:first').width()+40;

		$('#gallery2').css({'width': maxW+'px'});
		

		
		resizeViewport();
});


$(window).load(function(){
	updateGallery();
//	$('#viewport').removeClass('loading');
//	$('#gallery2').fadeIn(1000);
resizeViewport();

});


$(window).bind('resize',onResizeHandler);



function onResizeHandler(){
    if (resizeTimer) clearTimeout(resizeTimer);
       resizeTimer = setTimeout(resizeViewport, 50);
}

function resizeViewport(){
	
	var windowWidth = $(window).width();
	var windowHeight = $(window).height();

	var margin_top = 114; 
	var margin_bottom = 20;
	var margin_left = 20;
	var margin_right = 20;

	$('#viewport').css({width:windowWidth - (margin_left+margin_right), height: windowHeight - (margin_top+margin_bottom) })
	
										
	$('.loaded').each(function(){
		fixInto(this,'#viewport');
	});
	
	
}
function fixInto (img,container){

	var windowWidth = $(container).innerWidth();// 15 is a value in pixels to accomodate image title
	var windowHeight = $(container).innerHeight();

	var ratio  = $(img).data('meta').r;

	var mt = (windowHeight - (windowWidth/ratio) )/2;
	$(img).css({height: windowWidth/ratio+'px',width :(windowWidth)+'px',marginTop:mt+'px'});	
	
}


function addToQueue(index){
	
	if(index==maxImagesNo || replaceQueue[index]==1){
		return;
	}
	else
	replaceQueue[index]=1;
	
}

function onFirstImageReplaced(){
	
	$('#viewport').removeClass('loading');
	$('#gallery2').css({zIndex:500}).fadeIn(500);
	$('#gallery2 ').before('<div id="nav">').cycle({delay:50, speed:1000, timeout:10000, sync:0, fit:1, next:'#sig', prev:'#ant', pager:'#nav',  before:prepImage, after:nextImageCycle});
	
}
function replaceImages(){
	
//	return;


	for(var i = 0;i<replaceQueue.length;i++)
	{
		
		if(replacedList[i] == 'replaced'){
			continue;
		}
		
		
		
		var replace_img = $('#gallery2 li img').get(i);
		$(replace_img).data('meta',{r:images[i].ratio,'index':i});
		//$(replace_img).load(function(){
		
			
			
			$(replace_img).load(function(){
			
					
				//var width =replace_img.width;
				//var height = replace_img.height;
				
				
				//$(this).data('size',{w:width,h:height});
				fixInto(this,'#viewport');
				
				$(this).parent().parent().removeClass('loading');
				$(this).addClass('loaded');
				$(this).show();
				
				if($(this).data('meta').index == loadAtStart-1){
					onFirstImageReplaced();
				}
			});
	
			
			$(replace_img).attr('src',images[i].large); 
			
			
			replacedList[i] = 'replaced';
		

	}
}
//common-project
function updateGallery(){
	
	
	for(i=0;i<loadAtStart;i++)
	{
		addToQueue(i);	
	}

	replaceImages();	

													//	7000
	
}
var loopdone = false;
function prepImage(currSlideElement, nextSlideElement, options, forwardFlag){
	
	if(loopdone)
	return;
	
	if(maxImagesNo>imgIndex+loadAtStart)
	{
		addToQueue(imgIndex+loadAtStart);
		replaceImages();
	}
}

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

		imgIndex++;
		
		if(imgIndex==maxImagesNo-1){
		imgIndex = 0;
		loopdone = true;
		}
		
		
	
}

/*
var timerstart;

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

*/
