$(function() {   
		var oldrubid;
		var newrubid;
        var theWindow        = $(window),
            $bg              = $("#bg"),
            aspectRatio      = $bg.attr("relwidth") / $bg.attr("relheight");

        function resizeBg() {

                if ( (theWindow.width() / theWindow.height()) < aspectRatio ) {
                    $bg
                        .removeClass()
                        .addClass('bgheight');
                } else {
                    $bg
                        .removeClass()
                        .addClass('bgwidth');
                }

        }

        theWindow.resize(function() {
                resizeBg();
				var windowwidth = $(this).width();
				var marginleft = windowwidth - 870 - 50;
				if(marginleft<0){
					marginleft = 0;
				}
				if(marginleft>220){
					marginleft = 220;
				}
				$("div.container").css("margin-left", marginleft + "px");
        }).trigger("resize");
		function scrollSite(){
		if($("div.navtitle.current").length>0){
			var currentposition = $("div.navtitle.current").position();
			/*$("#site").animate({
				marginTop: "-" + currentposition.top + "px"	
			});*/
			//$("#site").css("paddingBottom", theWindow.height() - $("div.navtitle.current").outerHeight() + "px");
			//$("#site").css("paddingTop", ($("div.navtitle.current").outerHeight()/2) +75 + "px");
			$('html, body').animate({
					scrollTop: currentposition.top-75
			}, 500, function(){ menuMove(); });
		}
		}
		scrollSite();
	
		$("a.ajaxify").live("click", function(){
			$(".contentcontainer").html("");
			var therubID = $(this).attr("relrubid");
			$("#contentcontainer_"+therubID).load($(this).attr("relajax"), function() {
				$("div.navtitle").removeClass("current");
				$("div.content").removeClass("current");
				$("a.navtitle").mouseover();
				$("div#navtitle_"+therubID).addClass("current");
				$("#content_"+therubID).addClass("current");
				if(oldrubid != therubID){
					$(".content").hide();
					$("#content_"+therubID).slideDown();

				}
				scrollSite();
				if($("body").find("div.content.current div.teamgallery").length>0){
					initGallery();
					//alert($("body").find("div.teamgallery").length);
				}
				 addthis.toolbox("#toolbox");
				oldrubid = therubID;
			});
			$.ajax({
			   type: "GET",
			   url: "pagetheme.php",
			   data: "rubID=" + therubID,
			   success: function(data){
				   //alert(data.color);
				  // $("div.navtitle a").css("color", "none");
				   //$("div.navtitle.current a").css("color", "#"+data.color);
				   $("#toolbar #toolbarcontent").css("background-color", "#"+data.color);
				   //Cufon.replace('div.navtitle', {hover: true});
				   $("img#bg").attr("src", data.image);
				   $("img#logovisuel").attr("src", "img/visuel-logo.png");
				  
			   },
				dataType: "json"
			});
			return false;
		});
		
		$("img#logovisuel, a.closehome").live("click", function(){
			$.ajax({
			   type: "GET",
			   url: "pagetheme.php",
			   data: "rubID=1",
			   success: function(data){
				   //alert(data.color);
				  // $("div.navtitle a").css("color", "none");
				   //$("div.navtitle.current a").css("color", "#"+data.color);
				   $("#toolbar #toolbarcontent").css("background-color", "#666666");
				   // COMMENTE Cufon.replace('div.navtitle', {hover: true});
				   $("img#bg").attr("src", data.image);
				   $("img#logovisuel").attr("src", "img/visuel-logo-color.png");
				   $("div.navtitle").removeClass("current");
					$("div.content").removeClass("current");
					//Cufon.replace('div.navtitle', {hover: true});
					$(".content").slideUp();
					$(".content").hide();
					
					$('html, body').animate({
						scrollTop:0
					}, 500);
					oldrubid = 1;
			   },
				dataType: "json"
			});
			return false;
		});
		$("img#logovisuel").click();
		
		$(".content").hide();
		
		if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod' ||  navigator.platform == 'IE6'){
			$("#bg").css("position", "absolute");
			$("#header").css("position", "absolute");
		}
		
		function menuMove(){
			
			var scrollValue = $(document).scrollTop();
			if(navigator.platform == 'iPad' || navigator.platform == 'iPhone' || navigator.platform == 'iPod' ||  navigator.platform == 'IE6'){
				$("#bg").css("top", (scrollValue) + "px");
				$("#header").css("top", (scrollValue) + "px");
			}
			
			if($("a#toolbarbtn").hasClass("opened")){
				scrollValue = 0-scrollValue;
			}else{
				scrollValue = -35-scrollValue;
			}
			$("#toolbar").css("bottom", (scrollValue) + "px");
			
			//alert(screenHeight);
		}	
		$(window).bind("scroll",function(){ menuMove();	});
		
		$("a.boxinforollover").live("click", function(){
			return false;
		});
});
