// track Page view
function trackPageView(url){
  try {
    pageTracker._trackPageview( $.address.value() );
  } catch(err) {};
}

// $.address.change(function(event) {  
// 	debug('address.change : '+ event.path );
// 	debug('event.path.hash : '+ event.path.hash );
// 	trackPageView($.address.hash());
// }); 

if(typeof $.address == 'function'){
	$.address.externalChange(function(event) {  
		debug('address.externalChange : '+ $.address.value() );
		debug('event.path : '+ event.path );
		debug('event.path.hash : '+ event.path.hash );
	
		/* 
		// relocate if url contain a hash tag
		if( location.hash.length > 0  ){
			debug("location.replace : location.hash : "+location.hash);
			location.replace( location.hash.replace(/#/,'') );
		} */
		if( location.hash.length > 0  ){
			hash = location.hash.replace(/#/,'');
			$.ajax({ url: hash, context: $("#menu a[href='"+hash+"'], #submenu a[href='"+hash+"']"), 
				success: function(data){ 
					trackPageView($.address.hash());
					ajaxSuccess(data, $("#menu a[href='"+hash+"'], #submenu a[href='"+hash+"']") ); 
				} 
			});
		}
	});
}

// function ajaxNavigationInit
function ajaxNavigationInit(){
	
	if(window.console && console.groupCollapsed && debugMode ){ 
		console.groupCollapsed("function ajaxNavigationInit()"); 
	}
	
	$("#menu a:not('.get-found #menu a'), #submenu a:not(.selected)").unbind('click').click(function(event){
		event.preventDefault();
		$(this).parents(".menu, .submenu").find('a').removeClass("loading selected on");
		$(this).addClass('loading selected on');

		$.ajax({ url: $(this).attr('href'), context: $(this), 
			success: function(data){
				ajaxSuccess(data, this);
				// $.address.queryString($(this).attr('href'));
			},
			error: function(){ $('body', this).removeClass('loading');  }
		});
		// return false;
	});

	// CLOSE debug group
	if(window.console && console.groupEnd && debugMode ){ console.groupEnd(); }
} // CLOSE ajaxNavigationInit


// ajax Responding Sucess callBack
function ajaxSuccess(data, caller){ 
	
	if(window.console && console.groupCollapsed && debugMode ){ 
		console.groupCollapsed("function ajaxSuccess();"); 
	}
	debug("ajaxSuccess args : \n id="+caller.attr('id')+" \n caller:"+caller+" \n\n href="+caller.attr('href')+" ;\n\n data:"+data);
	
	$.address.value( $(caller).attr('href') );
	if( location.hash.length > 0  ){
		hash = location.hash.replace(/#/,'');
		trackPageView($.address.hash());
	}
	
	$('#page-container').addClass('loading');
	debug('PAGE LOADING:'+$('#page-container').hasClass('loading'));

	$('#page').fadeOut('normal', function() {
	// Animation complete.
	// debug('#page fadeOut Complete');
	// debug("$('#page',data) : "+$('#page',data).html() );
	$('#page').html( $('#page',data).html() );

	// Ajout identifiant contextuel
	// debug("$('#page',data).attr('class') : "+$('#page',data).attr('class'));
	$('#page').attr('class', $('#page',data).attr('class'));
     
	/* Tweak pour l'url http://ypg/en/solutions/search-engine-marketing#/en/solutions/online-lead-generation/em
	* ne pas afficher le numéro de tel
	* */
	if($('#page',data).attr('class') == "solutions-accelerate-your-results-ranking-products-em"){
		$('#order-now').css("display","none");
	}else{
		$('#order-now').css("display","block");
	}

	if( $('#submenu',data).length > 0 ){
		var subMenuHtml = $('#submenu',data).html();
		var subMenuClass = $('#submenu',data).attr('class');
		// debug('subMenuClass : '+subMenuClass+' ; subMenuHtml : '+subMenuHtml);

		if( $('#submenu-container').length <= 0 ) {
			$('#menu-container').after("<div id='submenu-container' class='submenu-container'><div id='submenu' class='submenu' ></div></div>");
		}
		$('#submenu').html(subMenuHtml);
		$('#submenu').attr('class', subMenuClass);
		// debug('subMenuClass : '+subMenuClass+' ; subMenuHtml : '+subMenuHtml);
		ajaxNavigationInit();
	}else{
		$('#submenu-container').remove();
	}
	debug("$('#page') : "+$('#page'));
	pageInit();
	
	$('#page').fadeIn('slow', function(){ debug("End of page fadeIn"); });
	});// End off content FADEOUT
/*
	$('#slideshow').fadeOut('fast', function() {
		debug("#slideshow fadeOut Complete");
		$('#naviss').remove();
		$('#slideshow').html($('#slideshow',data).html()).attr('class', $('#slideshow',data).attr('class') );

		// Ajout identifiant contextuel
		$('#slideshow').attr('class', $('#slideshow',data).attr('class'));
		debug("#slideshow : "+$('#slideshow')+" - class: "+$('#slideshow').attr('class') );

		$('#banner').stop().animate( { height: $('#slideshow').height() }, 
			1400, 'circEaseInOut', 
			function(){
				debug("#banner animate callBack");
				$('#slideshow').fadeIn('slow', function(){ 
					debug("End of slideShow fadeIn"); 
					ssInit(); // Slideshow Init
				}); 
			}
		);
	}); // slideshow fadeOut END
	*/
	// CLOSE debug group
	if(window.console && console.groupEnd && debugMode ){ console.groupEnd(); }
} // End off ajax Responding callBack

