// JavaScript Document
var currentPage = -1;
var timeOver = (BrowserDetect.browser!="Explorer") ? 300 : 0;
var timeFade = (BrowserDetect.browser!="Explorer") ? 800 : 0;
var timeInit = (BrowserDetect.browser!="Explorer") ? 1000 : 0;
var menu = [];
menu[0] = ["home", false];
menu[1] = ["marca", false];
menu[2] = ["colecao", false];
menu[3] = ["midia", false];
menu[4] = ["lojas", false];
menu[5] = ["contato", false];
menu[6] = ["multimarcas", false];

var mn =[];
mn["home"] = 0;
mn["marca"] = 1;
mn["colecao"] = 2;
mn["midia"] = 3;
mn["lojas"] = 4;
mn["contato"] = 5;
mn["multimarcas"] = 6;

var bgs = [];
bgs['home'] = 0;
bgs['marca'] = 1;
bgs['release'] = 2;
bgs['midia'] = 3;
bgs['lojas'] = 4;
bgs['contato'] = 5;
bgs['multimarcas'] = 6;
bgs['campanha'] = 7;
bgs['lookbook'] = 8;
bgs['lookbookav'] = 8;
bgs['anteriores'] = 9;

var frombt = false;
var xml = null;
var playlistForm = [];
var playlist = [];
var currmusic = 0;
var musicState = 0;
var ms = 0;
var imgAtual = ["", 0, "retrato", 400];
var itens = null;
var itensLength = 0;

var xmllook = null;
var itenslook = null;
var itenslookLength = 0;
var lookbookCriado = false;

var xmllook_av = null;
var itenslook_av = null;
var itenslookLength_av = 0;

$(document).ready(function(){
	
	$(document).pngFix();
	window.onresize = resize;
	posicionaContainer(0);
	
	$('.background_site').cycle({
		fx: 'fade',
		timeout: 0
	});
	currmusic = 0;
	
	xml = xmlLoader("content/radio/playlist.xml");
	itens = xml.documentElement.getElementsByTagName('item');
	itensLength = itens.length;
	
	xmlook = xmlLoader("content/lookbook/lookbook.xml");
	itenslook = xmlook.documentElement.getElementsByTagName('item');
	itenslookLength = itenslook.length;
	
	xmlook_av = xmlLoader("content/lookbook/lookbook_altoverao.xml");
	itenslook_av = xmlook_av.documentElement.getElementsByTagName('item');
	itenslookLength_av = itenslook_av.length;
	
	for (var i=0 ; i<itensLength ; i++){
	
		playlistForm.push(i);
		
	}
	
	while(playlistForm.length>0){
		
		var rnd = Math.floor(Math.random() * playlistForm.length);
		
		playlist.push([
						itens[playlistForm[rnd]].getElementsByTagName('url')[0].childNodes[0].nodeValue,
						itens[playlistForm[rnd]].getElementsByTagName('artista')[0].childNodes[0].nodeValue,
						itens[playlistForm[rnd]].getElementsByTagName('musica')[0].childNodes[0].nodeValue
					 ]);
					 
		playlistForm.splice(rnd, 1);
		
	}
	
	$('#jquery_jplayer_1').jPlayer({
	 	ready: function () {
			
			changeMusic(playlist[currmusic]);
			setVolume(3);
			toggleMusic(1);
			
    	},
 		solution: 'html, flash',
		supplied: "mp3", 
		volume: 0.75
	});
	
	$("#jquery_jplayer_1").bind($.jPlayer.event.ended, function(event) {
		 
    	changeMusic(playlist[currmusic]);
		
  	});
	
	$("#videolightbox").hide();
	$("#lightbox").hide();
	$("#opaque").hide();
	
	$('.botao_menu').hover(
	
		function() {
			
			if(currentPage!=$(this).attr("name")){
			
				$(this).children('#img0').stop(true, true).fadeOut(timeOver);
				$(this).children('#img1').stop(true, true).fadeIn(timeOver);
			
			}
			
		},
	
		function() {
			
			if(currentPage!=$(this).attr("name")){
			
				$(this).children('#img0').stop(true, true).fadeIn(timeOver);
				$(this).children('#img1').stop(true, true).fadeOut(timeOver);
			
			}
			
		}
	
	);
	
	// Configura a troca do Menu
	$('#menu').children('.botao_menu').each(function(){
		$(this).click(function(){
			
			if (!$(this).hasClass('ativo')){
				
				// Troca BG do Site
				if ($(this).attr('name') != 2){
					
					var slideProximo = Number($(this).attr('name'));
					var slideAnterior = Number($('.background_site div:visible').attr('name'));
					
					setTimeout(function(){
						$('.background_site').cycle('destroy');
						posicionaContainer(0);
						$('.background_site').cycle({
							fx: 'fade',
							timeout: 0,
							startingSlide: slideAnterior
						});
						$('.background_site').cycle(slideProximo);
					}, 0);
				}
				
				$('#menu').children('.ativo').each(function(){
					$(this).stop(true, true).children('.coracao_menu').fadeOut(300);
					$(this).stop(true, true).animate({"color": "#f5f0f0"}, 300);
					$(this).toggleClass('ativo');
				});
				
				$(this).stop(true, true).children('.coracao_menu').fadeIn(300);
				$(this).stop(true, true).animate({"color": "#e9f1cb"}, 300);
				$(this).toggleClass('ativo');
				
			}
		});
		
		$(this).hover(function(){
			if (!$(this).hasClass('ativo')){
				$(this).stop(true, true).children('.coracao_menu').fadeIn(300);
				$(this).stop(true, true).animate({"color": "#e9f1cb"}, 300);
			}
		}, function(){
			if (!$(this).hasClass('ativo')){
				$(this).stop(true, true).children('.coracao_menu').fadeOut(300);
				$(this).stop(true, true).animate({"color": "#f5f0f0"}, 300);
			}
		});
		
	});
	
	
	$('.fechar, #opaque').click(function(){
		
		$("#lightbox").fadeOut(300);
		$("#videolightbox").fadeOut(300);
		$("#opaque").delay(300).fadeOut(300);
		$("#videocontent").html('');
		
		window.setTimeout(function() {
				
			if(musicState==1){
				
				toggleMusic(1);
				
			}
		
		}, 600);
	});
	
	$('#fotolightbox, #overlay').click(function(){
		
		$("#fotolightbox").fadeOut(300);
		$("#overlay").delay(300).fadeOut(300);
		setTimeout(function(){
			//$("#fotolightbox").html("");
		}, 300);
		
	});
	
	$('.background_site').stop(true, true).fadeOut(0);
	$('.content_container').stop(true, true).fadeOut(0);
	$('.header_container').stop(true, true).fadeOut(0);
	$('.rodape').stop(true, true).fadeOut(0);
	$('#loader_content').stop(true, true).fadeOut(0);
	
});

function openFotoLightBox(url){
	
	$('#fotolightbox img').attr({src:imgAtual[0]});
	
	widthW = getWindowWidth();
	widthW = (widthW<1000) ? 1000 : widthW;
	
	heightW = getWindowHeight();
	heightW = (heightW<600) ? 600 : heightW;
	
	var lbh = heightW / 600;
	var lbw = imgAtual[3] * lbh;
	var margemlbV = (widthW - lbw) / 2;
	$("#fotolightbox").stop(true, true).animate({"width": lbw, "height": heightW, "left": margemlbV, "top": 0}, 0);
	
	$("#overlay").fadeIn(300);
	$("#fotolightbox").delay(300).fadeIn(300);
	
}

function changeMusic(data){
	
	$("#msc").html(data[2]);
	$("#aut").html(data[1]);
	
	$("#jquery_jplayer_1").jPlayer("setMedia", {mp3: "content/radio/" + data[0]});
	$("#jquery_jplayer_1").jPlayer("play");
	
	currmusic++;
	currmusic = (currmusic>playlist.length) ? 0 : currmusic;
	
}

function toggleMusic(value){
	
	if(value==1){
		
		$('#jquery_jplayer_1').jPlayer("play");
		$('.btplay').stop(true, true).fadeOut(timeOver);
		$('.btpause').stop(true, true).fadeIn(timeOver);
		ms = 1;
		
	}else{
		
		$('#jquery_jplayer_1').jPlayer("pause");
		$('.btplay').stop(true, true).fadeIn(timeOver);
		$('.btpause').stop(true, true).fadeOut(timeOver);
		ms = 0;
		
	}
	
}

function setVolume(value){
	
	setBar(value);
	value = value * 0.25;
	$('#jquery_jplayer_1').jPlayer("volume", value);
	
}

function setBar(id){
	
	for(var i=1 ; i<=4; i++){
		
		if(i<=id){
			
			$('#bar'+ i).stop(true, true).animate({"backgroundColor": "#34548f"}, 200);
			
		}else{
			
			$('#bar'+ i).stop(true, true).animate({"backgroundColor": "#fff"}, 200);
			
		}
		
	}
	
}

function siteLoader(){
	
	changeBGimage();
	$('#loader_content').stop(true, true).fadeIn(500);
	var imgs = loadImg("content/background/bg" + 1 + ".jpg", 0, imgLoaded);
	
}

function initSite(){
	
	$('.background_site').stop(true, true).fadeIn(1000);
	$('.content_container').stop(true, true).delay(500).fadeIn(timeInit);
	$('.header_container').stop(true, true).delay(500).fadeIn(timeInit);
	$('.rodape').stop(true, true).delay(800).fadeIn(timeInit);
	
}

function currentAddress(){
	
	var value = $.address.value();
	return value;
	
}

$.address.change(function(event) {
	
	var value = event.value;
	mvalue = value.split("/");
	
	if(mvalue[1]!=""){
		
		if(mn[mvalue[1]]!=undefined){
			
			changeContent(mn[mvalue[1]], menu[mn[mvalue[1]]][0] + ".php");
			
			if (mn[mvalue[1]] == 2){
				$('#submenu').fadeIn(300);
			} else {
				$('#submenu').fadeOut(300);
			}
				
		}
		
	} else {
		
		$(".botao_menu:eq(0)").stop(true, true).children('.coracao_menu').fadeIn(0);
		$(".botao_menu:eq(0)").stop(true, true).animate({"color": "#e9f1cb"}, 0);
		
		$('#submenu').fadeOut(0);
		trocaArea(0);
		changeContent(0, "home.php");
		
	}	
	
	if(mn[mvalue[1]] == 2){
		
		if(mvalue[2]!=undefined && mvalue[2]!=""){
			
			if(smn[mvalue[2]]!=undefined){
				
				tm = (frombt==true) ? 0 : timeFade;
				changeContentColecao(smn[mvalue[2]], submenu[smn[mvalue[2]]][0] + ".php", tm);
				frombt = false;
					
			}
			
		}
			
	}
	
}).externalChange(function(event) {
	
	var tmm = (BrowserDetect.browser!="Explorer") ? 800 : 0;
	
	if(currentPage==2){
		
		if(currSubPage==2 && lookbookCriado==true){
			
			moveGalleryInit(tmm);
			
		}else if(currSubPage==3){
			
			anterioresInit = true;
			toggle_anterioresInit(tmm);	
			
		}else if(currSubPage==1){
			
			campanhaInit = true;
			toggle_campanhaInit(tmm);
			
		}
		
	}
	
	var value = event.value;
	mvalue = value.split("/");
	
	if(mvalue[1] != ""){
		$('.botao_menu:eq('+mn[mvalue[1]]+')').stop(true, true).children('.coracao_menu').fadeIn(0);
		$('.botao_menu:eq('+mn[mvalue[1]]+')').stop(true, true).animate({"color": "#e9f1cb"}, 0);
		$('.botao_menu:eq('+mn[mvalue[1]]+')').toggleClass('ativo');
		
		if (mvalue[1] == 'colecao'){
			$('#submenu').fadeIn(300);
			
			if (mvalue[2] != "" && mvalue[2] != undefined){
				trocaBG(bgs[mvalue[2]]);
			}
			
		} else {
			$('#submenu').fadeOut(300);
			
			trocaBG(bgs[mvalue[1]]);
		}
		
	} else {
		
		$(".botao_menu:eq(0)").stop(true, true).children('.coracao_menu').fadeIn(0);
		$(".botao_menu:eq(0)").stop(true, true).animate({"color": "#e9f1cb"}, 0);
		$(".botao_menu:eq(0)").toggleClass('ativo');
		$('#submenu').fadeOut(0);
		
	}
	
});

function trocaArea(id, bt){
	
	if(id!=currentPage){
	
		frombt = bt;
		$.address.value(menu[id][0]);
	
	}
	
}

function trocaBG(prox){
	
	if (prox != Number($('.background_site div:visible').attr('name'))){
		
		var slideAnterior = $('.background_site div:visible').attr('name');
		var slideProximo = prox;
		$('.background_site').cycle('destroy');
		$('.background_site').cycle({
			fx: 'fade',
			timeout: 0,
			startingSlide: slideAnterior
		});
		$('.background_site').cycle(slideProximo);
	}	
}

function changeContent(id, url){
	
	if (id == 2){
		$('.submenu').fadeIn(300);
	} else {
		$('.submenu').fadeOut(300);
	}
	
	if(id!=currentPage){
		
		if(url!=""){
			
			$("#secoes_container").stop(true, true).fadeOut(timeFade);
			
			window.setTimeout(function() {
				
				var cnt = document.getElementById("secoes_container");
				cnt.innerHTML = '';
				
				$("#secoes_container").load(url, function() {
					
					switch (id){
						
						case 0: { inithome(); break; }
						case 1: { initmarca(); break; }
						case 2: { initcolecao(); break; }
						case 3: { initmidia(); break; }
						case 4: { initlojas(); break; }
						case 5: { initcontato(); break; }
						case 6: { initmultimarcas(); break; }
						
					}
					
					$("#secoes_container").stop(true, true).fadeIn(timeFade);
					 
				});
				
			}, timeFade);
			
		}
		
		for(var i=0 ; i<menu.length; i++){
			
			if(i==id){
				
				
				
				$('#menu' + i).children('#img0').stop(true, true).fadeOut(timeOver);
				$('#menu' + i).children('#img1').stop(true, true).fadeIn(timeOver);
				
			}else{
				
				$('#menu' + i).children('#img0').stop(true, true).fadeIn(timeOver);
				$('#menu' + i).children('#img1').stop(true, true).fadeOut(timeOver);
				
			}
			
		}
		
		if(id==2){
			
			$('.rodapeBar1, .rodapeBar2').stop(true, true).animate({"backgroundColor": "#fff"}, timeFade);
			
		}else{
			
			$('.rodapeBar1, .rodapeBar2').stop(true, true).animate({"backgroundColor": "#f7426e"}, timeFade);
			
		}
		
		currentPage = id;
		
	}
	
}

function posicionaContainer(time){
	
	var scale = 0;
	var cheight = 0;
	var cwidth = 0;
	
	widthW = getWindowWidth();
	widthW = (widthW<1000) ? 1000 : widthW;
	
	heightW = getWindowHeight();
	heightW = (heightW<600) ? 600 : heightW;
	
	var lbh = heightW / 600;
	var lbw = imgAtual[3] * lbh;
	var margemlbV = (widthW - lbw) / 2;
	$("#fotolightbox").stop(true, true).animate({"width": lbw, "height": heightW, "left": margemlbV, "top": 0}, time);
	
	//$('.site_container').stop(true, true).animate({"width": widthW - 5, "height": heightW}, time);
	
	var prop = 1920/1280;
	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;
	
	$('.background_site 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;
	
	$('.background_site img').stop(true, true).animate({"margin-left": margemV, "margin-top": margemH}, time);
	$('.background_site').stop(true, true).animate({"width": widthW, "height": heightW}, time);
	$('#opaque').stop(true, true).animate({"width": widthW, "height": heightW}, time);
	$('#overlay').stop(true, true).animate({"width": widthW, "height": heightW}, time);
	$('.site_container').stop(true, true).animate({"width": widthW, "height": heightW}, time);
	
	$('.background_site').children('div').each(function(){
		$(this).stop(true, true).animate({"width": widthW, "height": heightW}, time);
	});
	
	var margemLV = (getWindowWidth() - 157) / 2;
	margemLV = (margemLV<0) ? 0 : margemLV;
	
	var margemLH = (getWindowHeight() - 157) / 2;
	margemLH = (margemLH<0) ? 0 : margemLH;
	
	$('#loader_content').stop(true, true).animate({"left": margemLV, "top": margemLH}, time);
	
	
	
	
	var margemMenuV = (getWindowHeight() - 600) / 2;
	margemMenuV = (margemMenuV<0) ? 0 : margemMenuV;
	
	$('.header_container').stop(true, true).animate({"top": margemMenuV}, time);
	
	
	
	contentPosition(0);
	
}

function contentPosition(time){
	
	var cntW = 1000;
	var cntH = 546;
	
	var margemVc = (getWindowWidth() - cntW) / 2;
	margemVc = (margemVc<0) ? 0 : margemVc;
	
	var margemHc = (getWindowHeight() - cntH) / 2;
	margemHc = (margemHc -40 <0) ? 0 : margemHc - 40;
	
	$('.content_container').stop(true, true).animate({"margin-left": margemVc, "margin-top": margemHc}, time);
	$('.header_container').stop(true, true).animate({"margin-left": margemVc}, time);
	
}

function imgLoaded(e){
	
	var img = (BrowserDetect.browser!="Explorer") ? this : e;

	var ww = 70;
	var tp = 49;
	
	$('.progresso_loader').animate({"width": 84}, 1500);
	
	window.setTimeout(function() {
		
		$('#loader_content').fadeOut(300);
		initSite();
		
	}, 3000);
	
}

function changeBGimage(){
	
	var rnd = (Math.random() * 2) + 1;
	rnd = rnd.toFixed(0);
	
	//$('.background_site #img0').attr({src:"content/background/bg" + rnd + ".jpg"});
	
}

function resize(){

	posicionaContainer(0);
 
}
