/**
 * AJAX navigation 
 * REZO ZERO
 * @author Ambroise Maupate
 */

function displayLoader() 
{
	if ( $.browser.msie ) {
	 	if ( $.browser.version == "8.0") {
    		$("#ajax_loader").show();
    	}
 	} else {
    	$("#ajax_loader").fadeIn();
 	}
}

function hideLoader() 
{
	if ( $.browser.msie ) {
	 	if ( $.browser.version == "8.0") {
    		$("#ajax_loader").hide();
    	}
 	} else {
    	$("#ajax_loader").fadeOut();
 	}
}

function loadData( query ){
	
	 displayLoader();

	$("html, body").stop(true, true);
	$(".vignette_content").remove();
	currentCarroussel = null;
	
	if (vignettesToPush != null) {
		
		// On remonte les vignettes
		if (query != "none") {
			$(vignettesToPush).animate({top: '-='+(vignetteContentHeight)}, {duration:500, easing:"easeOutCubic"});
			var timer = setTimeout(function(){
				// Whole animation complete
				vignettesToPush = null;
				vignetteContentHeight = 0;
				generateVignetteContent( query );
			}, 500);
			return;
		}
		else{
			$.scrollTo( 0, 500, {'axis':'y'});
			
			$(vignettesToPush).animate({top: '-='+(vignetteContentHeight)}, {duration:1000, easing:"easeOutCubic"});
			var timer = setTimeout(function(){
				// Whole animation complete
				vignetteContentHeight = 0;
				vignettesToPush = null;
				
				generateTopPageLinks();
				document.title = "REZO ZERO";
				
				resetOpenedVignette();
				
				hideLoader();
				
			}, 1000);
			return;
		}
	}
	else {
		generateVignetteContent(query);
	}
}

function resetOpenedVignette () 
{
	$('.vignette.openned').find(".inner_vignette").fadeIn();
	$('.vignette.openned').find(".inner_vignette_image").fadeIn().css("top","0px");
	$('.vignette.openned').removeClass("openned");
	addEventsOnVignettes();
}

function generateVignetteContent ( query ) 
{
	resetOpenedVignette();
	
	// AJAX + JSON
	if (query != "" && query != "none") 
	{		
		$.get('get_work.php?page='+query+'&entire=1', function(data, textStatus, xhr) {
			
			//called when successful
			var short_name_link = query;
			var datahref = "http://www.rezo-zero.com/#!/"+short_name_link;
			var fbhref = "http://www.rezo-zero.com/?_escaped_fragment_="+short_name_link;
            
			
			if (jQuery.inArray(query, hidden_pages) > -1) {
				// If is page
				
				$("#pages_menu a").removeClass("current");
				$("#pages_menu a."+query).addClass("current");
				
				query = "heading";
			}
            else {
				$("#pages_menu a").removeClass("current");
				$("#pages_menu a.reset").addClass("current");
			}
			
			if ($('#'+query).hasClass("petite")) {
				rz_find_rows($('#'+query).get(0), 1);
			}
			else if ($('#'+query).hasClass("grande")) {
				rz_find_rows($('#'+query).get(0), 2);
			}
			else if (query == "heading") {
				rz_find_rows($('#'+query).get(0), 2);
			}
			
			var work_object = data;
			var top = $('#'+query).offset().top;
			top += $('#'+query).height();
            
			$('#'+query).addClass("openned");
			$('#'+query).find(".inner_vignette").fadeOut(100);
			$('#'+query).find(".inner_vignette_image").fadeOut(100);
			$('#'+query).after('<div class=\"vignette_content '+query+'\" style=\"top:'+top+'px\"></div>');
			
			// Inject AJAX html response into DOM
			$(".vignette_content").html(data);
			
			// Specials
			//rz_create_fb_like(fbhref, $(".vignette_content .work_informations .links .tweet").get(0));
			document.title = "REZO ZERO — "+($(".vignette_content .work_title").text());
			
			if (query == "heading") {
				$(".vignette_content .links, .vignette_content .work_categories").hide();
				$(".work_informations .text_infos").css("width","100%");
			}
			else{
				$(".vignette_content .links, .vignette_content .work_categories").show();
			}
			
			// EVENTS
			$(".vignette_content .close_content > a").click(function(){
				$.history.load("!/none");
			});
			
			if ($(".vignette_content .uipack_carroussel ul li").length > 0 ) 
			{
				// CARROUSSEL
				$(".vignette_content").slideDown({duration:600, easing:"easeOutCubic", complete:function(){
					currentCarroussel = new UIPack_Carroussel("carroussel00");
					
					// use videoJS for html5
					VideoJS.setupAllWhenReady();
					
					$(".vignette_content img").animate({opacity:1}, 500);
					vignetteContentHeight = $(".vignette_content").height() + 10;
					
					$(vignettesToPush).animate({top: '+='+(vignetteContentHeight)});
					
					set_currentwork_image(); // set vignette image as image_src for Facebook
					FB.XFBML.parse();
					
					var timer = setTimeout(function(){
						$.scrollTo( $(".vignette_content").offset().top, 700, {'axis':'y'}); 
						generateTopPageLinks();
						
						hideLoader();
						
					}, 500);
					
				}});
			}
			else {
				// OR NOT CARROUSSEL
				$(".vignette_content .uipack_carroussel").css("height", "72px");
				
				$(".vignette_content").slideDown( {duration:600, easing:"easeOutCubic", complete:function(){
					$.scrollTo( $(".vignette_content").offset().top, 700, {'axis':'y'}); 
					vignetteContentHeight = $(".vignette_content").height() + 10;
					
					$(vignettesToPush).animate({top: '+='+(vignetteContentHeight)}, 200);

					var timer = setTimeout(function(){ 
						generateTopPageLinks();
						FB.XFBML.parse();
						
						hideLoader();
						
					}, 200);
				}});
				
			}
		});
	}
	else{	
		hideLoader();
	}
}


