$(function()
{
	initialise();
	gestionFavoris();
	$('#iTvScroller').iTvScroller(); 
	
	if (jQuery.url.attr("anchor")==null)
	{
		if  ($.browser.msie || $.browser.safari) 
		{
			url="contents/templates/principale/actualites.php";
		}
		else
		{
			url="contents/templates/principale/designActualites.php";
		}
		hash=url;

		//hash = hash.replace(/^.*#/, '');
		jQuery.historyLoad(hash);
		
		return false;
	}	
	
});

function initialise()
{
	jQuery.fn.center = function (absolute) {
	    return this.each(function () {
	        var t = jQuery(this);
	
	        t.css({
	            position:    absolute ? 'absolute' : 'fixed', 
	            left:        '50%', 
	            top:        '50%', 
	            zIndex:        '99'
	        }).css({
	            marginLeft:    '-' + (t.outerWidth() / 2) + 'px', 
	            marginTop:    '-' + (t.outerHeight() / 2) + 'px'
	        });
	
	        if (absolute) {
	            t.css({
	                marginTop:    parseInt(t.css('marginTop'), 10) + jQuery(window).scrollTop(), 
	                marginLeft:    parseInt(t.css('marginLeft'), 10) + jQuery(window).scrollLeft()
	            });
	        }
	    });
	};
	
	
	$.historyInit(pageload, "jquery_history.html");

	$("#viderPanier").live("click",function(e)
	{		
		appelCache("contents/templates/boutique/viderPanier.php","#panier")
		self.tb_remove();
		return false;
	});
	

	$("#supprimerArticle").live("click",function(e)
	{		
		appelCache(this.href,"#panier")
		self.tb_remove();
		return false;
	});
	
	
	
	$(".inactif").live("click",function(e)
	{
		return false;
	});
	

	$("a").unbind("click");
	$("a").unbind("mouseover");
	

	$("#wait").hide();
	$("li.animatedMenu").mouseover(function(e){
		if  ($.browser.msie || $.browser.safari) 
		{
			$(this).css({cursor:'hand'});
		}
		else
		{
			var h=$(this).height()+300;
			var id=$(this).attr("id");			
			$("#div"+id).stop().animate({height:h},{queue:false, duration:600, easing: 'easeOutBounce'});
		}	
	});

	$("li.animatedMenu").mouseout(function(e){
		if  ($.browser.msie || $.browser.safari) {
		}
		else
		{
			
			var id=$(this).attr("id");
			$("#div"+id).stop().animate({height:'0'},{queue:false, duration:600, easing: 'easeOutBounce'});
		}	
	});

	//tempo
		
	$("#CadreInternaute").everyTime(180000,function(i){
			$.ajax({
			  url: "contents/templates/banniereGauche/cadreInternaute.php",
			  cache: false,
			  success: function(html){
				$("#CadreInternaute").html(html);
			  }
			})
	});
		
	
	$("a[rel=homePage]").live("click",function()
	{
		
		if  ($.browser.msie || $.browser.safari) 
		{
			url="contents/templates/principale/actualites.php";
		}
		else
		{
			url="contents/templates/principale/designActualites.php";
		}
		hash=url;
		
		//hash = hash.replace(/^.*#/, '');
		jQuery.historyLoad(hash);
		return false;
	});
	

	$("a[rel=interne]").live("click",function()
	{		
		gestionInternaute();
		
		if (this.className=="jqbookmark")
		{	
			return true;
		}

		if (this.rel=="cache")
		{
			return true;
		}	

		if (this.className=="thickbox")
		{	
			return true;
		}
	
		if (this.className=="ancre")
		{
			document.location=this.href;
			return false;
		}
			hash=this.href;
			//hash = hash.replace(/^.*#/, '');
			jQuery.historyLoad(hash);
			return false;
	});	

	$("area").live("click", function(e)
	{
		appel(this.href,"#principal");
		return false;		
	});
	
	//Fix Errors - http://www.learningjquery.com/2009/01/quick-tip-prevent-animation-queue-buildup/
	
	
	//When mouse rolls over
	$("a").mouseover(function(e)
	{
		$(this).css({cursor:'hand'});	
	});
	

	
	//When mouse is removed
	
	$("#divMenu").click(function(e)
	{
	 	$("#divMenu").animate({top:'0'});	 	
 	 	$("#divMenu").animate({left:'0'});
			
		$("#divMenu").hide();
	});
	
	$("li").live("click",function(e){
		if  ($.browser.msie || $.browser.safari) 
		{
			$("#wait").center();
			$("#wait").show();
			var id=$(this).attr("id");
			var pos=$("#"+id).position();
 		 	$("#divMenu").html($("#div"+id).html());
 		 	
 		 	$("a[rel=interne]").click(function()
			{					
				//appel(this.href,"#principal");				
				return true;		
			});	 
			$("area").click(function(e)
			{
				appel(this.href,"#principal");
				return false;		
			});
			$("#divMenu").animate({left:pos.left},1);
			$("#divMenu").animate({top:pos.top+120},1);
			$("#divMenu").show(1);
			$("#wait").hide();
		}
	});			

	$("body").click(function()
	{
		$("#divMenu").hide();
		$('#detailNews').hide();
	});
	
	$("a.tooltiplink").simpletooltip({ showEffect: "slideDown", hideEffect: "slideUp" });
	
			
}

function gestionInternaute()
{
    $.ajax({
			   type: "GET",
			   cache:false,
			   url: "contents/templates/internaute/gestionInternaute.php",
			   error:function(msg){
			     
		 		}
		 });		
}

// appel ajax sans retour d'affichage et sans gestion du retour arriere du browser
function appelCache(url,target)
{	
	$("#wait").center();
    $.ajax({
			   type: "GET",
			   cache:false,
			   url: "contents/templates/internaute/gestionInternaute.php",
			   error:function(msg){
			     
		 		}
		 });	
	$.ajax({
	  url: url,
	  asynchrone:false,
	  cache: false,
	  timeout: (6 * 1000),
	  error: function( objAJAXRequest, strError ){
		$( "#response" ).text
		(
			"Error! Type: " + strError
		);
		},
	  success: function(html){		 	  	
	  	$("#wait").hide();	  
	    $(target).html(html);

	  },
	  error: function(msg, string){
	   $("#wait").hide();
		//alert( "Cett epage n'existe pas, veuillez nous en excuser");
	  }
	});
}

function appel(url,target)
{	
	//$("#wait").center();
	$("#wait").show();
		$.ajax({
		  url: url,
		  asynchrone:true,
		  cache: false,
		  timeout: (15 * 1000),
		  error: function( objAJAXRequest, strError ){
			$( "#response" ).text
			(
				"Error! Type: " + strError
			);
			},
		  success: function(html){
		  
		  	$("#wait").hide();
		    $(target).html(html);
		  },
		  error: function(msg, string)
		  {
			   $("#wait").hide();
				//alert( "Cette page n'existe pas, veuillez nous en excuser");
		  }
		});		    
		$("#divMenu").hide();
		
		  $.ajax({
			   type: "GET",
			   cache:false,
			   url: "contents/templates/internaute/gestionInternaute.php",
			   error:function(msg){
			     
		 		}
		 });		
		try {
				var pageTracker = _gat._getTracker("UA-10580110-1");
				pageTracker._trackPageview(url);
				} catch(err) {}			 
		}	


function PopupCentrer(page,largeur,hauteur,options) 
{
  var top=(screen.height-hauteur)/4;
  var left=(screen.width-largeur)/4;
  options="scrollbars=no,menubar=no,toolbar=no,resizable=no";
  fen=window.open(page,"Canaris","top="+top+",left="+left+",width="+largeur+",height="+hauteur+","+options);

}

function scrolling()
{
	//Repete(4000);
    //document.getElementById("canaris").style.visibility="visible";
}


function pageload(hash) {
	// hash doesn't contain the first # character.
		hash = hash.replace(/&/, '?');
		if(hash) {
			appel(hash,"#principal");
			//$("#principal").load(hash);
		} else {
			// start page
			document.location="index.php";
		}
		
}

function gestionFavoris()
{
    if(window.opera) {
        if ($("a.jqbookmark").attr("rel") != ""){ // don't overwrite the rel attrib if already set
            $("a.jqbookmark").attr("rel","sidebar");
        }
    }
   
    $("a.jqbookmark").click(function(event){
        event.preventDefault(); // prevent the anchor tag from sending the user off to the link
        var url = this.href;
        var title = this.title;
       
        if (window.sidebar) { // Mozilla Firefox Bookmark
            window.sidebar.addPanel(title, url, "");
        } else if( window.external ) { // IE Favorite
            window.external.AddFavorite( url, title);
        } else if(window.opera) { // Opera 7+
            return false; // do nothing - the rel="sidebar" should do the trick
        } else { // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)
            alert('Unfortunately, this browser does not support the requested action, please bookmark this page manually.');
        }
    });
}

function setHomepage()
{
 if (document.all)
    {
        document.body.style.behavior='url(#default#homepage)';
  		document.body.setHomePage('http://www.fcerdre.com');
    }
    else if (window.sidebar)
    {
	    if(window.netscape)
    	{
        	try
   			{  
            	netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");  
         	}  
         catch(e)  
         {  
    		alert("L'action a échoué");  
         }
    	}
    }	
    var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components. interfaces.nsIPrefBranch);
    prefs.setCharPref('browser.startup.homepage','http://www.fcerdre.com');
 }