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

function initcampanha(){
	
	$(document).pngFix();
	
	var campanha = xml.documentElement.getElementsByTagName('campanha');
	itensCampanha = campanha[0].getElementsByTagName('item');
	
	totalimages = itensCampanha.length;
	image = [];
	imgCurrent = 0;
	openWidth = 292;
	openHeight = 433;
	closeWidth = 125;
	closeHeight = 179;
	
	$('.buttonNextCA, .buttonPrevCA').hover(
	
		function() {
			
			$(this).stop();
			$(this).animate({"backgroundColor": "#FFF", "color" : "#000"}, timeOver);
			
		},
	
		function() {
			
			$(this).stop();
			$(this).animate({"backgroundColor": "#000", "color" : "#fff"}, timeOver);
			
		}
	
	);
	
	$('.buttonZoomCA').hover(
	
		function() {
			
			$(this).stop();
			$(this).animate({"backgroundColor": "#000", "color" : "#fff"}, timeOver);
			
		},
	
		function() {
			
			$(this).stop();
			$(this).animate({"backgroundColor": "#fff", "color" : "#000"}, timeOver);
			
		}
	
	);
	
	createGalleryCA();
	
}

function createGalleryCA(){
	
	var idd = (totalimages<=9) ? "0" + totalimages : totalimages; 
	var imgTotal = document.getElementById('imgTotal');
	imgTotal.innerHTML = idd;
	
	for (var i = 0 ; i < totalimages ; i++){
		
		var imgContainer = document.createElement('div');
		imgContainer.className = "imgContainerCampanha";
		imgContainer.id = "imgContainerCampanha" + i;
		
		var id = (i<9) ? "0" + (i + 1) : (i + 1); 
		
		imgContainer.innerHTML = '<div id="imgInfoCA' + i + '" class="imgInfoCA"><div class="imgContNumberCA bold">' + id + '</div><div class="imgTextCA light">lorem ipsun</div><div class="imgCodeCA light">132123</div></div><img src="content/campanha/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('containerImagesCampanha');
		containerImages.appendChild(imgContainer);
		
		$('#imgInfoCA' + i).fadeOut(0);
		
	}
	
	moveGalleryCA(0, 0);
	
}

function galleryPositionCA(id){
	
	for (var i = 0 ; i < totalimages ; i++){
		
		var idd = (i-id);
		var init = openWidth - closeWidth;
		var top = openHeight - closeHeight;
		
		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 moveGalleryCA(id, time){
	
	galleryPositionCA(id);
	
	for (var i = 0 ; i < totalimages ; i++){
	
		if(i==id){
		
			$('#imgInfoCA' + i).delay(time).fadeIn(300);	
			
		}else{
			
			$('#imgInfoCA' + i).fadeOut(300);
			
		}
	
		$('#imgContainerCamapnha' + i).stop();
		$('#imgContainerCampanha' + 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 nextImageCA(){
	
	id = imgCurrent + 1;
	id = (id>totalimages-1) ?  totalimages-1 : id;
	moveGalleryCA(id, 800);
	
}

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