// JavaScript Document
var currentPage = -1;
var timeOver = 300;
var menu = [];
menu[0] = ["home", false];
menu[1] = ["primart", false];
menu[2] = ["colecao", false];
menu[3] = ["campanha", false];
menu[4] = ["lookbook", false];
menu[5] = ["release", false];
menu[6] = ["atacado", false];
menu[7] = ["contato", false];
menu[8] = ["lojas", false];

var mn =[];
mn["home"] = 0;
mn["primart"] = 1;
mn["colecao"] = 2;
//mn["campanha"] = 3;
mn["lookbook"] = 4;
mn["release"] = 5;
mn["atacado"] = 6;
mn["contato"] = 7;
mn["lojas"] = 8;

var fu = true;
var imgCount = 0;
var currentImg = 0;
var currentImage = 1;
var totalImagebg = 4;

var xml = null;
// JavaScript Document

$(document).ready(function(){
	
	xml = xmlLoader("xml/dados.xml");
	
	$(document).pngFix();
	window.onresize = resize;
	posicionaContainer(0);
	
	$('.naccordion, .subMenu').address();
	$.address.strict(false);
	
	$('#conteudo').fadeOut(0);
	$('.backgroundSite img').fadeOut(0);
	$('.lojas').fadeOut(0);
	$('.rodape').fadeOut(0);
	$('.shareIconBg').fadeOut(0);
	
	$('#zoomGaleria').fadeOut(0);
	
	$('.accordion').next().hide();
	
	$('.naccordion, .accordion, .textoLojas').hover(
	
		function() {
			
			var id = $(this).attr('ID');
			
			if(menu[id][1] == false){
				
				$(this).stop(true, true).animate({"backgroundColor": "#FFF", "color" : "#000"}, timeOver);
				
			}
			
		},
	
		function() {
			
			var id = $(this).attr('ID');
			
			if(menu[id][1] == false){
				
				$(this).stop(true, true).animate({"backgroundColor": "#000", "color" : "#fff"}, timeOver);
				
			}
			
		}
	
	);
	
	
	$('.textoSubMenu').hover(
	
		function() {
			
			if(currentPage!=$(this).attr('ID')){
				
				$(this).stop(true, true).animate({"backgroundColor": "#000", "color" : "#fff"}, timeOver);
				
			}
			
		},
	
		function() {
			
			if(currentPage!=$(this).attr('ID')){
				
				$(this).stop(true, true).animate({"backgroundColor": "#fff", "color" : "#000"}, timeOver);
				
			}
			
		}
	
	);
	
	$('.facebookRodape').hover(
	
		function() {
			
			$(this).children('.shareIconBg').stop(true, true).fadeIn(timeOver);
			
		},
	
		function() {
			
			$(this).children('.shareIconBg').stop(true, true).fadeOut(timeOver);
			
		}
	
	);
	
	for (var i = 1 ; i < totalImagebg + 1 ; i++){
		
		var imgs = loadImg("content/background/bg" + i + ".jpg", i, imgLoaded);
		
	}
	
});

function siteLoader(){
	
	$('#logoLoad').fadeOut(0);
	$('#barraLoad').animate({"width": 0}, 0);
	
	$('#logoLoad').fadeIn(500);
	
}

function initSite(){

	$('.backgroundSite #img' + currentImg).fadeIn(800);
	$('.rodape').delay(1000).fadeIn(800);
	$('.lojas').delay(1500).fadeIn(800);
	$('#conteudo').delay(2000).fadeIn(800);
	
	window.setTimeout(function() {
		
		var value = $.address.value();
		value = value.replace("/","");
		value = (value=='') ? 'home' : value;
		
		if(mn[value]!=undefined){
			
			trocaArea(mn[value], value + ".php");
			
		}else{
			
			trocaArea(0, "home.php");
			fu = false;
			
		}
		
	}, 2500);
	
}

$.address.change(function(event) {
	
	if(fu!=true){
		
		var value = event.value;
		value = value.replace("/","");
		
		if(mn[value]!=undefined){
		
			trocaArea(mn[value], value + ".php");
			
		}else{
			
			trocaArea(0, "home.php");
			
		}
		
	}
	
});

function trocaArea(id, url){
	
	if(id!=currentPage){
	
		if(url!=""){
			
			//$("#content").stop();
			$("#content").fadeOut(800);
			
			window.setTimeout(function() {
				
				var cnt = document.getElementById("content");
				cnt.innerHTML = '';
				
				$("#content").load(url, function() {
					
					switch (id){
						
						case 0: { inithome(); break; }
						case 1: { initprimart(); break; }
						//case 3: { initcampanha(); break; }
						case 4: { initlookbook(); break; }
						case 5: { initrelease(); break; }
						case 6: { initatacado(); break; }
						case 7: { initcontato(); break; }
						case 8: { initlojas(); break; }
				
					}
					
					$("#content").fadeIn(800, function(){contentPosition(800);});
					 
				});
				
			}, 1000);
			
		}
		
		for (var i = 0 ; i < 9 ; i++){
			
			var cor1 = (i==3||i==4||i==5) ? "#FFF" : "#000";
			var cor2 = (i==3||i==4||i==5) ? "#000" : "#FFF";
			
			if(i==id){
				
				$('#' + i).stop();
				$('#' + i).animate({"backgroundColor": cor2, "color" : cor1}, timeOver);
				menu[i][1] = true;
				$('#' + i).css("cursor", "default");
				
			}else{
				
				if((id==3||id==4||id==5) && i==2){
						
					menu[i][1] = true;
					$('#' + i).css("cursor", "default");
						
				}else{
					
					if(id==2 && (i==3||i==4||i==5)){
						
						
					}else{
							
						$('#' + i).stop();
						$('#' + i).animate({"backgroundColor": cor1, "color" : cor2}, timeOver);
						menu[i][1] = false;
						$('#' + i).css("cursor", "pointer");
						
					}
					
				}
				
			}
			
		}
		
		if(id==3||id==4||id==5){
					
			$('#' + 2).stop();
			$('#' + 2).animate({"backgroundColor": "#FFF", "color" : "#000"}, timeOver);
			menu[2] = true;
			$('#' + 2).css("cursor", "default");
					
		}
		
		if(id!=2 && id!=3 && id!=4 && id!=5){
			
			$('.accordion').next().hide('slow');
			
		}else{
			
			$('.accordion').next().show('slow');
			
		}
		
		currentPage = id;
		
		if(id!=2 && fu!=true){
	
			changeBGimage();	
		
		}
	
	}
	
	$.address.value(menu[id][0]);
	fu = false;

	//var content = document.getElementById("content");
	//alert(content.offsetWidth);
	
}

function posicionaContainer(time){
	
	//$('.backgroundSite img').stop();
	//$('.backgroundSite').stop();
	//$('.zoomGaleria').stop();
	//$('.conteudo').stop();
	//$('.rodape').stop();
	
	var scale = 0;
	var cheight = 0;
	var cwidth = 0;
	
	widthW = getWindowWidth();
	widthW = (widthW<1000) ? 1000 : widthW;
	
	heightW = getWindowHeight();
	heightW = (heightW<600) ? 600 : heightW;

	$('.rodape').animate({"width": widthW, "top": (heightW - 35)}, time);
	$('.lojas').animate({"top": (heightW - 59)}, time);

	var prop = 1000/600;
	if(getWindowWidth()/getWindowHeight()>prop){
	
		scale = getWindowWidth()/1000;	
		
	}else{
		
		scale = getWindowHeight()/600;
		
	}
	
	cheight = 600*scale;
	cheight = (cheight<600) ? 600 : cheight;
	
	cwidth = 1000*scale;
	cwidth = (cwidth<1000) ? 1000 : cwidth;
	
	$('.backgroundSite img').attr({height:cheight, width:cwidth});
	
	var margemV = (getWindowWidth() - cwidth) / 2;
	margemV = (margemV<0) ? 0 : margemV;
	
	var margemH = (getWindowHeight() - cheight) / 2;
	margemH = (margemH<0) ? 0 : margemH;
	
	$('.backgroundSite img').animate({"margin-left": margemV, "margin-top": margemH}, time);
	$('#loaderP').animate({"margin-top": (heightW / 2) - 50}, time);
	
	var mtw = widthW / 2;
	var mth = heightW / 2;
	
	$('#loaderZoom').animate({"margin-top": mth - 18, "margin-left": mtw - 18}, time);
	
	$('.backgroundSite').animate({"width": widthW, "height": heightW}, time);
	$('.zoomGaleria').animate({"width": widthW, "height": heightW}, time);
	$('#internaAtacado').animate({"width": widthW}, time);
	
	var prop = 1067/1600;
	if(getWindowWidth()/getWindowHeight()>prop){
	
		scale = getWindowWidth()/1067;	
		
	}else{
		
		scale = getWindowHeight()/1600;
		
	}
	
	cheight = 1600*scale;
	cheight = (cheight<1600) ? 1600 : cheight;
	
	cwidth = 1067*scale;
	cwidth = (cwidth<1067) ? 1067 : cwidth;
	
	$('.zoomGaleria img').attr({height:cheight, width:cwidth});
	
	contentPosition(0);
	
}

function contentPosition(time){
	
	var cntW = document.getElementById('content').offsetWidth + 190;
	var cntH = document.getElementById('content').offsetHeight;
	
	var margemVc = (getWindowWidth() - cntW) / 2;
	margemVc = (margemVc<0) ? 0 : margemVc;
	
	var margemHc = (getWindowHeight() - cntH) / 2;
	margemHc = (margemHc<0) ? 0 : margemHc;
	
	$('.conteudo').animate({"margin-left": margemVc, "margin-top": margemHc}, time);
	
}

function imgLoaded(e){
	
	var img = (BrowserDetect.browser!="Explorer") ? this : e;

	imgCount++;
	
	var ww = getWindowWidth();
	ww = (ww / totalImagebg) * imgCount;
	
	//$('#loaderpct').animate({"margin-left": ww - 30}, 500);
	$('#barraLoad').animate({"width": ww}, 500);
	
	if(imgCount==totalImagebg){
		
		window.setTimeout(function() {
		
			$('#logoLoad').fadeOut(300);
			initSite();
		
		}, 2000);
		
	}
		
}

function changeBGimage(){
	
	var rnd = (Math.random() * 3) + 1;
	rnd = rnd.toFixed(0);
	var newimg = Math.abs(currentImg - 1);

	if(imgCount>=4 && rnd!= currentImage){
		
		$('.backgroundSite #img' + newimg).attr({src:newImg[rnd].src});
		
		$('.backgroundSite #img' + currentImg).fadeOut(800)
		$('.backgroundSite #img' + newimg).fadeIn(800);
		
		currentImage = rnd;
		currentImg = newimg;
		
	}
	
}

var py = 0;
var t;
var timer_is_on = false;

function moveZoom(){
	
	var wh = getWindowHeight();	
	var yy = mouseY / wh ;
	//var yy = (mouseY - (wh/2));
	//yy /= wh;
	
	var ht = $('.zoomGaleria img').attr("height");
	//var mt = $('.zoomGaleria img').css("margin-top");
	//mt = mt.replace("px", "");
	
	var py = yy * (ht - wh);
	//py += yy * (wh/20);
	//py = (py < 0) ? 0 : py;
	//py = (py > (ht - wh)) ? (ht - wh) : py;

	$('.zoomGaleria img').animate({"margin-top": -py}, 0);
	
}

function timedCount(){
	
	if(timer_is_on==true){
	
		t = setTimeout("timedCount()", 1);
		moveZoom();
		
	}
	
}

function openZoom(area){
	
	var url = 'content/' + area + '/zoom/' + imgCurrent + '.jpg';
	$('.zoomGaleria img').animate({"margin-top": 0}, 0);
	$('.zoomGaleria img').attr({src:url});
	
	timer_is_on = true;
	timedCount();
	$('#zoomGaleria').fadeIn(800);
	
}

function closeZoom(){
	
	timer_is_on = false;
	$('#zoomGaleria').fadeOut(800);
	
}

function resize(){
	
	posicionaContainer(0);
 
}
