// JavaScript Document
var totalimages;
var image = [];
var imgSpace = 2;
var imgCurrent = 0;
var timeOver = 300;
var openWidth;
var openHeight;
var closeWidth;
var closeHeight;
var itensLookbook;

function initlookbook(){
	
	$(document).pngFix();
	
	var lookbook = xml.documentElement.getElementsByTagName('lookbook');
	itensLookbook = lookbook[0].getElementsByTagName('item');

	totalimages = itensLookbook.length;
	image = [];
	imgCurrent = 0;
	openWidth = 198;
	openHeight = 433;
	closeWidth = 117;
	closeHeight = 256;
	
	$('.buttonNextLB, .buttonPrevLB').hover(
	
		function() {
			
			$(this).stop(true, true).animate({"backgroundColor": "#FFF", "color" : "#000"}, timeOver);
			
		},
	
		function() {
			
			$(this).stop(true, true).animate({"backgroundColor": "#000", "color" : "#fff"}, timeOver);
			
		}
	
	);
	
	$('.buttonZoomLB').hover(
	
		function() {
			
			$(this).stop(true, true).animate({"backgroundColor": "#000", "color" : "#fff"}, timeOver);
			
		},
	
		function() {
			
			$(this).stop(true, true).animate({"backgroundColor": "#fff", "color" : "#000"}, timeOver);
			
		}
	
	);
	
	createGalleryLB();
	
}

function createGalleryLB(){
	
	var idd = (totalimages<=9) ? "0" + totalimages : totalimages; 
	var imgTotal = document.getElementById('imgTotal');
	imgTotal.innerHTML = idd;
	
	for (var i = 0 ; i < totalimages ; i++){
	
		var thumb = (itensLookbook[i].getElementsByTagName('thumb')[0].childNodes[0]!=undefined) ? itensLookbook[i].getElementsByTagName('thumb')[0].childNodes[0].nodeValue : "";
		var zoom = (itensLookbook[i].getElementsByTagName('zoom')[0].childNodes[0]!=undefined) ? itensLookbook[i].getElementsByTagName('zoom')[0].childNodes[0].nodeValue : "";
		var desc = (itensLookbook[i].getElementsByTagName('descricao')[0].getElementsByTagName('ditem')!=undefined) ? itensLookbook[i].getElementsByTagName('descricao')[0].getElementsByTagName('ditem') : [];
		
		var descNr = desc.length;
		
		var descricaoConc = "";
		
		for (var j = 0 ; j < descNr ; j++){
		
			var descricao = (desc[j].childNodes[0]!=undefined) ? desc[j].childNodes[0].nodeValue : "";
			descricaoConc += descricao + "<br />";
			
		}
		
		var imgContainer = document.createElement('div');
		imgContainer.className = "imgContainerLookbook";
		imgContainer.id = "imgContainerLookbook" + i;
		
		var id = (i<9) ? "0" + (i + 1) : (i + 1); 
		
		//<div class="imgContNumberLB bold">' + id + '</div>
		//<div class="imgCodeLB light">132123</div>
		
		imgContainer.innerHTML = '<div id="imgInfoLB' + i + '" class="imgInfoLB"><div class="imgTextLB medium">' + descricaoConc + '</div></div><img src="content/lookbook/thumb/' + i + '.jpg" width="100%" height="100%"/>';
		imgContainer.style.top =  0 + "px";
		imgContainer.style.left =  ((closeWidth + imgSpace) * i) + "px";
		imgContainer.style.width = closeWidth + "px";
		imgContainer.style.height = closeHeight + "px";
		//imgContainerLookbook.style.zIndex = (10 + i);
		
		image[i] = [(closeWidth + imgSpace) * i, 0, closeWidth, closeHeight];
		
		var containerImages = document.getElementById('containerImagesLookBook');
		containerImages.appendChild(imgContainer);
		
	}
	
	moveGalleryLB(0, 0);
	
}

function galleryPositionLB(id){
	
	for (var i = 0 ; i < totalimages ; i++){
		
		var idd = (i-id);
		var init = openWidth - closeWidth;
		var top = 0;
		
		if(idd>0){
			
			if(idd==1){
			
				image[i] = [(openWidth + imgSpace) * idd, top, closeWidth, closeHeight];
			
			}else{
				
				image[i] = [init + ((closeWidth + imgSpace) * idd), top, closeWidth, closeHeight];
				
			}
			
		}else{
			
			image[i] = [(openWidth + imgSpace) * idd, 0, openWidth, openHeight];
			
		}
		
	}
	
}

function moveGalleryLB(id, time){
	
	galleryPositionLB(id);
	
	for (var i = 0 ; i < totalimages ; i++){
	
		if(i==id){
		
			$('#imgInfoLB' + i).delay(time).fadeIn(300);	
			
		}else{
			
			$('#imgInfoLB' + i).fadeOut(300);
			
		}
	
		$('#imgContainerLookbook' + i).stop();
		$('#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"});
		
	}
	
	var idd = (id<9) ? "0" + (id + 1) : (id + 1); 
	var imgNumber = document.getElementById('imgNumber');
	imgNumber.innerHTML = idd;

	imgCurrent = id;
	
}

function nextImageLB(){
	
	id = imgCurrent + 1;
	id = (id>totalimages-1) ?  totalimages-1 : id;
	moveGalleryLB(id, 800);
	
}

function prevImageLB(){
	
	id = imgCurrent - 1;
	id = (id<0) ?  0 : id;
	moveGalleryLB(id, 800);
	
}
