var totalSections=null;
var loadedSections=null;
var currentSectionID=null;
var showAll=true;
$(function(){
	
	prepareMenu();
	
	
	if (showAll) {
		pages= $('<ul id="pages"></ul>').appendTo("#web");
		
		currentSectionID= $("#web .section").attr("id");
		var currentSection=$("#web #content").html();
		
		totalSections=$(".globalMenu li[class~=load] a").size();
		loadedSections=0;
		
		$("#loader").show();
		$("#loader .container").css({
			marginTop: (windowHeight/2-20)+'px'
		})
		$("body").css({overflow : 'hidden'});
	
		$(".globalMenu li[class~='load'] a").each(function() {
			var id=$(this).attr("rel");
			$('<li class="page" id="section_'+id+'">&nbsp;</li>').appendTo(pages);
			
			var $pageContainer=$("#section_"+id);
			var href=$(this).attr("href");	
			
			// LOAD ALL SECTIONS 
			loadSection(href,$pageContainer)
			
			
			/*
			$(this).click(function() {					
				$.scrollTo("#web #pages #section_"+id, 1000, {easing:'easeInOutExpo'});
				closeNews();
				
				
				var $pageContainer=$("#section_"+id);			
				var href=$(this).attr("href");	
	
				if ($pageContainer.html().length<20) {
					$("#loader").show();
					loadSection(href,$pageContainer)
					$("#loader").hide();
				}
				
				return false;
			});
			*/
		})
		
		
		$(".globalMenu li[class~='load'] a").live('click',function() {
			//alert($('.globalMenu').parent().parent().parent().attr("id"));
			var id=$(this).attr("rel");
								
			$.scrollTo("#web #pages #section_"+id, 1000, {easing:'easeInOutExpo'});		
			if (isdefined("closeNews")) {
				closeNews();
			}
							
			var $pageContainer=$("#section_"+id);			
			var href=$(this).attr("href");	
	
			if ($pageContainer.html().length<20) {
				$("#loader").show();
				loadSection(href,$pageContainer)
				$("#loader").hide();
			}
			
			return false;
		});

		
		
		//Copy current section 
		$("#web #pages #section_"+currentSectionID).html(currentSection);	
		//$.scrollTo("#web .pages #section_"+currentSectionID, 500);
		
		
		$("#content").remove();
	}
})



function loadSection(href,pageContainer,forceReload) {
	
	$.ajax({
		type: "GET",
		url: href,
		data: {json: 1},
		timeout: 30000,
		dataType: 'json',
		success: function(res,status) {		
			
			/* ASSIGN CONTENT */
			pageContainer.html(res.html);
					
			finishLoad();
			
			/* LOAD CSS */
			$(res.css).each(function() {
				if ($("head link[href='"+this+"']").size()==0) {
					$("head").append('<link rel="stylesheet" href="'+this+'" type="text/css" />');					
				}
			})
			
									
			/* LOAD JS */
			$(res.js).each(function() {
				if ($("head script[src='"+this+"']").size()==0) {
					$.getScript(this);
				}
			})

			prepareMenu();	
	
		},
		error: function() {
			finishLoad();			
		}
	})
	
}


function finishLoad() {
	loadedSections++;
	if (loadedSections>=totalSections) {
		$("#loader").fadeOut("fast");
		$("pages").css({height : 'auto'});
		$("body").css({overflow : 'auto'});
		
		$("img").load(function() {
			position();
		})		
		
		resize();
		$(".webHeader .playerControl").show();
	}
}


function position() {
	$.scrollTo("#web #pages #section_"+currentSectionID, 0, {easing:'easeInOutExpo'});
}


function prepareMenu() {
	$(".webHeader .logoB").css("cursor","pointer");
	//$(".webHeader .globalMenu li").hide();
	/*
	$(".webHeader").hover(
		function() {
			$(".webHeader .globalMenu li",$(this).parent()).each(function() {
				$(this).slideDown();
			});
		},
		function() {			
			$(".webHeader .globalMenu li",$(this).parent()).each(function() {
				$(this).slideUp();
			});
		}
	)	
	*/
	
}
