var currSection="home";
var currSez="home";
var currGender="man";
var showAnimation="slide";

//DEFINE VARS FOR CONFIG.XML
var datetime_start;
var showphase_params;
var show_gender;
var url_live;
var url_postlive;
var main_bg;
var man_bg;
var woman_bg;

var linkWoman=false;
  


////JQTOUCH			
var jQT = $.jQTouch({
	icon: 'themes/dolcegabbana/'+theBrand+'_logo.png',
	startupScreen: 'themes/dolcegabbana/'+theBrand+'_startup.png',
	slideSelector:'.slider' ,
	fixedViewport:true,
	fullScreen:true,
	statusBar: 'black',
	preloadImages: [
			'themes/dolcegabbana/images/loader.gif',
            'themes/dolcegabbana/digits/0.png',
            'themes/dolcegabbana/digits/1.png',
            'themes/dolcegabbana/digits/2.png',
            'themes/dolcegabbana/digits/3.png',
            'themes/dolcegabbana/digits/4.png',
            'themes/dolcegabbana/digits/5.png',
            'themes/dolcegabbana/digits/6.png',
            'themes/dolcegabbana/digits/7.png',
            'themes/dolcegabbana/digits/8.png',
            'themes/dolcegabbana/digits/9.png'
           ]

});

function initConfig(){	
		
	//START PARSING XML
	$.ajax({
		type: "GET",
		url: rootUrl+theBrand+"/XML/config/data.xml",
		dataType: "xml",
		cache:'false',
		success: function(xml){
				//CYCLE TROUGHT XML ELEMENTS
				$(xml).find("Item").each(function(){
					datetime_start=new Date($(this).find("datetime_start").text());
					showphase_params=$(this).find("showphase_params").text().replace(/^\s*|\s*$/g,'');
					//showphase_params="countdown";
					show_gender=$(this).find("show_gender").text().replace(/^\s*|\s*$/g,'');
					url_live=$(this).find("url_live").text().replace(/^\s*|\s*$/g,'');
					url_postlive=$(this).find("url_postlive").text().replace(/^\s*|\s*$/g,'');
					main_bg=$(this).find("img_main").text().replace(/^\s*|\s*$/g,'');
					man_bg=$(this).find("img_man").text().replace(/^\s*|\s*$/g,'');
					woman_bg=$(this).find("img_woman").text().replace(/^\s*|\s*$/g,'');
					
						
				 });
			},
		beforeSend:startInit,
		complete:finishInit
	});
	//END AJAX
};

//PARSER ONSTART FUNCTION
function startInit(){
	$('.loader').show();

}

//PARSER ONCOMPLETE FUNCTION
function finishInit(){
	$('.loader').hide();
	setSection();
}


function setSection(){
	console.info(imgUrl+main_bg);
		switch(showphase_params){
			
			//CONTENT
			case "content":
			case "contents":
				//set Backgound dynamically
				//alert(rootUrl+theBrand+main_bg);
				
				$('body').load('content.html', function() {
					if (show_gender=="woman"){
						//VA ALLA SCELTA MAN/WOMAN
						jQT.goTo('.first', "fade");
						$("#select").css("background-image","url("+imgUrl+main_bg+")");
					}else{
						//VA A MAN    
						$("#select").css("background-image","url("+imgUrl+man_bg+")");       
						console.log("gotoMan");
						jQT.goTo('#man', "fade");
					}
					
				
				
				});
			break;
			
			//LIVE o RECORDED
			case "live":
			case "recorded":
				if (show_gender=="woman"){
					linkWoman=true;
				}
				//LOAD LIVE or RECORDED PAGES
				window.location=showphase_params+".html";
			break;
			
			//COUNTDOWN - COMING SOON
			case "countdown":
				if (show_gender=="woman"){
					linkWoman=true;
				}
				$('body').load(showphase_params+".html", function() {
					jQT.goTo('.first', "fade");
				});
			break;
			
			default:
				jQT.goTo('.first', showAnimation);
			break;
		
		}

	javascript:window.scrollTo(0,0);
}
	
	//MENU
function initMenu(gender){	
	currGender=gender;
            
	if(gender=="man"){
			$("."+gender+"image").css("background-image","url("+imgUrl+man_bg+")");
		}else{
			$("."+gender+"image").css("background-image","url("+imgUrl+woman_bg+")");
		}
	//hide all menus
	$("#"+gender+" li").hide();
	
	
	//START PARSING XML
	$.ajax({
		type: "GET",
		url: rootUrl+theBrand+"/"+gender+"/XML/menu/data.xml",
		dataType: "xml",
		cache:'false',
		success: function(xml){     
			console.log("MENU "+rootUrl+theBrand+"/"+gender+"/XML/menu/data.xml");
				//CYCLE TROUGHT XML ELEMENTS
				var counter=1;
				var bg="";
				$(xml).find("Item").each(function(){
					//BACKGROUND IMAGES
								  
						
					
					
					var menu=$(this);
					var nome=menu.attr("id");
					var lbl="";
					var isVisible=menu.find("visible").text().replace(/^\s*|\s*$/g,'');
					//alert(isVisible);
					
					//Process only if Visible = true
					if(isVisible=="True"){
						menu.find("lang").each(function(){
							if($(this).attr("language")=="en"){
								lbl=$(this).find("dictionary").text();
							}
						});
						
						var tipo=menu.find("template").text().replace(/^\s*|\s*$/g,'');
						
						if(tipo=="video" && !isIphone() && !isAndroid){}else{
							if(!tipo){tipo="text"}
							
							if (counter %2 == 0){
								$("#"+gender+" .edgetoedge").append('<li class="arrow odd"><a class='+tipo+' rel="'+nome+'"  tipo="'+tipo+'">'+lbl+'</a></li>');
							}else{
								$("#"+gender+" .edgetoedge").append('<li class="arrow"><a  class='+tipo+' rel="'+nome+'" tipo="'+tipo+'">'+lbl+'</a></li>');
							}
							counter ++;
						}
					}
				 });
			},
		beforeSend:null,
		complete:setMenuActions
	});
	//END AJAX
};
		
	jQuery.fn.seqfx = function()
	{
		$(this).fadeOut("fast").fadeIn("slow", function()
		{
			$(this).next().seqfx();
		});
	};
	
function setMenuActions(){

	// $("#"+currGender+" .edgetoedge li:first").seqfx();

	
	//GALLERY 
	$("#"+currSection+" .edgetoedge li .gallery").click(
		function(){
			var rel=$(this).attr("rel");
			jQT.goTo('#detail', showAnimation);
			currSez=$(this).html();
			setTitle()
			loadGalleryXml(rel);
		}
	);
	
	//VIDEO SECTION
	$("#"+currSection+" .edgetoedge li .video").click(
		function(){
			var rel=$(this).attr("rel");
			//jQT.goTo('#video', showAnimation);
			loadVideo(rel);
			//currSez=$(this).html();
		}
	);
	
	//TEXT SECTION
	$("#"+currSection+" .edgetoedge li .text").click(
		function(){
			var rel=$(this).attr("rel");
			jQT.goTo('#text', showAnimation);
			currSez=$(this).html();
			setTitle()
			loadText(rel);
		}
	);
}

function setTitle(){
	floaterTop();
	$(".title").html(currSez);
}

	 

  
function goLive(){
	$('body').load(showphase_params+'.html', function() {
		jQT.goTo('.first', showAnimation);
	});
}

function floaterTop(){
	$('.floater').css("top", $(this).scrollTop());
}

function isIphone(){
	var agent=navigator.userAgent.toLowerCase();
	var is_iphone = ((agent.indexOf('iphone')!=-1));
	if (is_iphone) { return true; }
	else{return false}
}
function isAndroid(){
	var agent=navigator.userAgent.toLowerCase();
	var is_android = ((agent.indexOf('android')!=-1));
	if (is_android) { return true; }
	else{return false}
}
