// JavaScript Document
var totalimages;
var image = [];
var imgSpace = 5;
var imgCurrent = 0;
var openWidth;
var openHeight;
var closeWidth;
var closeHeight;
var itensGallery;

totalimages = 20
imgCurrent = 0;
openWidth = 200;
openHeight = 133;
closeWidth = 130;
closeHeight = 87;
xini = 301 - (openWidth / 2);
yini = 10;

function createGallery(divId){
	
	var gallery = xml.documentElement.getElementsByTagName('flickr');
	var photosets = gallery[0].getElementsByTagName('photoset');
	
	if(photosets!=undefined && photosets.length > 0){
	
		itensGallery = photosets[0].getElementsByTagName('item');
		totalimages = (itensGallery.length<20) ? itensGallery.length : 20;
	
	}else{
	
		totalimages = 0;
		
	}
	
	$("#" + divId).css("overflow", "hidden");
	$("#" + divId).css("position", "relative");
	
	for (var i = 0 ; i < totalimages ; i++){
		
		var anexo = (itensGallery[i].getElementsByTagName('photo')[0].childNodes[0]!=undefined) ? itensGallery[i].getElementsByTagName('photo')[0].childNodes[0].nodeValue : "";
		var descricao = (itensGallery[i].getElementsByTagName('descricao')[0].childNodes[0]!=undefined) ? itensGallery[i].getElementsByTagName('descricao')[0].childNodes[0].nodeValue : "";
		var titulo = (itensGallery[i].getElementsByTagName('titulo')[0].childNodes[0]!=undefined) ? itensGallery[i].getElementsByTagName('titulo')[0].childNodes[0].nodeValue : "";
		
		var imgContainer = document.createElement('div');
		imgContainer.className = "imgContainerLookbook";
		imgContainer.id = "imgContainerLookbook" + i;
		imgContainer.style.position = "absolute";
	
		imgContainer.innerHTML = '<a href="'+ anexo +'" class="homegallery"><img src="php/timthumb.php?src=' + anexo + '&h=120&w=180" width="100%" height="100%"/></a>';
		imgContainer.style.top =  0 + "px";
		imgContainer.style.left =  xini + ((closeWidth + imgSpace) * i) + "px";
		imgContainer.style.width = closeWidth + "px";
		imgContainer.style.height = closeHeight + "px";
		
		image[i] = [(closeWidth + imgSpace) * i, 0, closeWidth, closeHeight, 0];
		
		var containerImages = document.getElementById(divId);
		containerImages.appendChild(imgContainer);
		
		$('#imgContainerLookbook' + i).css("display", "none");
		
	}
	
	var vlr = Math.floor(totalimages / 2);
	moveGallery(vlr, 0);
	
	$(".homegallery").colorbox({initialWidth:500, initialHeight:300, opacity:0.9, transition:"elastic", arrowKey:false, onComplete:putTitle});
	
}

function putTitle(){
	
	var cnt = document.getElementById("cboxLoadedContent");
	cnt.style.marginBottom = "30px";
	
}

function galleryPosition(id){
	
	for (var i = 0 ; i < totalimages ; i++){
		
		var idd = (i-id);
		var init = openWidth - closeWidth;
		var top = yini + ((openHeight - closeHeight) / 2) ;
		
		if(idd>0){
			
			if(idd==1){
			
				image[i] = [xini + ((openWidth + imgSpace) * idd), top, closeWidth, closeHeight , 1];
			
			}else{
				
				image[i] = [xini + (init + ((closeWidth + imgSpace) * idd)), top, closeWidth, closeHeight, 0];
				
			}
			
		}else if(idd==0){
			
			image[i] = [xini + ((openWidth + imgSpace) * idd), yini, openWidth, openHeight, 1];
			
		}else{
			
			if(idd==-1){
			
				image[i] = [xini + (init + ((openWidth + imgSpace) * idd)), top, closeWidth, closeHeight, 1];
			
			}else{
				
				image[i] = [xini + ((0) + ((closeWidth + imgSpace) * idd)), top, closeWidth, closeHeight, 0];
				
			}
			
		}
		
	}
	
}

function moveGallery(id, time){
	
	galleryPosition(id);
	
	for (var i = 0 ; i < totalimages ; i++){
		
		if(image[i][4]==1){
			
			$('#imgContainerLookbook' + i).stop(true, true);
			$('#imgContainerLookbook' + i).fadeIn(time);
			$('#imgContainerLookbook' + i).animate({"left": image[i][0], "top": image[i][1], "width": image[i][2], "height": image[i][3]}, {queue:false, duration: time, easing:"quartEaseInOut"});
			
		}else{
			
			$('#imgContainerLookbook' + i).stop(true, true);
			$('#imgContainerLookbook' + i).fadeOut(time);
			$('#imgContainerLookbook' + i).animate({"left": image[i][0], "top": image[i][1], "width": image[i][2], "height": image[i][3]}, {queue:false, duration: time, easing:"quartEaseInOut"});
			
		}
		
	}
	
	imgCurrent = id;
	
}

function nextImage(){
	
	id = imgCurrent + 1;
	id = (id>totalimages-1) ?  totalimages-1 : id;
	moveGallery(id, 800);
	
}

function prevImage(){
	
	id = imgCurrent - 1;
	id = (id<0) ?  0 : id;
	moveGallery(id, 800);
	
}
