var contentInitialBottom = null;

// parseUri 1.2.2
// (c) Steven Levithan <stevenlevithan.com>
// MIT License

function parseUri (str) {
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};

function commonSettings(){
	
	 /*
  	Détection de la largeur de l'écran
	et appel d'une css supplémentaire si width < 1280
  */
// 	alert("screen.width : " + screen.width);
  
//	if(screen.width < 1280){
//		$("body").addClass("c1024");		
//	}else if($.browser.msie && jQuery("window").width() < 1280){
//		$("body").addClass("c1024");
//	}
	
	if($(window).width() < 1200){
	//	alert("width : " + $(window).width());
		$("body").addClass("c1024");
		
		// ajuste la marge-droite du bloc de tri dans la liste produits pour aligner correctement
		$("#productsSortFormP").css("margin-right", 260);
		
		// if we are at home page, we have to shift the top of the right column
		var parts = parseUri(window.location);
		if (parts['directory']=='/' && (parts['file']=='' || parts['file']=='index.php')) {
			$('#colonneInfos').css('top', '408px');
		}

	} else {
		// ajuste la marge-droite du bloc de tri dans la liste produits pour aligner correctement
		$("#productsSortFormP").css("margin-right", 201);
	}
  
   /*
   		Gére l'affichage et le masquage des sous-rubriques
		du menu haut div#menu-haut
   */
	$('#menu-haut > ul > li > ul').hide();
	if($.browser.msie && $.browser.version.substr(0,1) < 8){
	
		// bugs de superpositions des divs et des images
		// on ne déroule pas les menus
		
		// on modifie le lien sur Encyclopedie : http://localhost/202-encyclopedie-bienfaits-du-the
		$('#menu-haut > ul > li:nth-child(2) > a').attr("href", baseDir + "202-encyclopedie-bienfaits-du-the");
		
		// et sur glossaire : http://localhost/100-definition-mot-a
		$('#menu-haut > ul > li:nth-child(4) > a').attr("href", baseDir + "100-definition-mot-a");
		
/*		$('#menu-haut > ul > li:nth-child(2), #menu-haut > ul > li:nth-child(4)').click( function(){
	
			//$(this).css("border","solid 1px #f00");
			
			if($(this).children('ul').css("display") != 'block'){
				
				//$(this).children('ul').css("color","#f00");
				//$(this).children('ul').css("display","block");
				
				$("#menu-haut").after("<div id=\"sousMenuDialog\"><p>DIALOG</p></div>");
				
				$(this).children('ul').css("display","block").css("top","60");
				//$(this).children('ul').css("position",relative);
				
				$("#sousMenuDialog").attr("title",$(this).children('a').text());
				$("#sousMenuDialog").html($(this).children('ul'));
				
				$("#lstGlossaire").css("margin-left","90px");
				
				$("#sousMenuDialog").dialog({
					height: 320,
					width: 300	
				});
			}
		});*/
	}
	else{
		$('#menu-haut > ul > li').hover(
			function(){
			
				if($(this).children('ul').css("display") != 'block'){
					$(this).children('ul').slideDown('fast',function(){});
				};
			},
			function(){
				$(this).children('ul').slideUp('fast',function(){});
				
			}
		);
	}
	
	/*
   		Gére l'affichage et le masquage du formulaire de login
   */
/*
	 $('#formLogin').hide();
	 
	 $('#lienIdentification').toggle(
	 	function(){
			$('#formLogin').show();
	 	},
		function(){	  
			$('#formLogin').hide();	
	 	}
	);
*/	 
	
	decalageFooter();
	decalageHContent();	
}

function decalageFooter(){
	var positionColonneInfos = getBottomPosition($("#colonneInfos")[0]);
	var positionContenu = getBottomPosition($("#contenu")[0]);
	if (contentInitialBottom==null)
		contentInitialBottom = positionContenu;
	
	var positionBandeauHaut = getBottomPosition($("#bandeau-haut")[0]);

	
	// if we are in content page (CMS) and initial content is higher than right column then don't change it
	var parts = parseUri(window.location);
	if (parts['directory'].indexOf('/content/')>=0 && contentInitialBottom>positionColonneInfos) {
		return;
	}
	
	// if we are in content page (glossary / encyclopedia) and content is higher than right column then don't change it
	if ((parts['directory'].indexOf('/glossaire/')>=0 ||
		 parts['directory'].indexOf('definition')>=0 ||
		 parts['directory'].indexOf('encyclopedie')>=0) && positionContenu>positionColonneInfos) {
		return;
	}
	
	if(positionColonneInfos > contentInitialBottom){
		$("#contenu").height(positionColonneInfos - positionBandeauHaut );
	} else {
		$("#contenu").height(contentInitialBottom - positionBandeauHaut );
	}
}
function decalageFooterForTreePages(){
	var positionColonneInfos = getBottomPosition($("#colonneInfos")[0]);
	var positionContenu = getBottomPosition($("#contenu")[0]);
	
	var positionBandeauHaut = getBottomPosition($("#bandeau-haut")[0]);
	if(positionColonneInfos > positionContenu){
		$("#contenu").height(positionColonneInfos - positionBandeauHaut );
//	} else {
//		$("#contenu").height(positionContenu - positionBandeauHaut );
	}
}
function getBottomPosition(element){

	var position = $(element).position();
	var height = $(element).height();
	
	return (position.top + height);
}

function decalageHContent() {
	// get info column position
	var x2 = $($("#colonneInfos")[0]).position().left;
	// get menu-gauche column x / width
	var x1 = $($("#menu-gauche")[0]).position().left + $($("#menu-gauche")[0]).width();
	var contentWidth = x2-x1-20;
	// for small display, also increment the margin-left for alignement purpose
	if (contentWidth<700) {
//		$("#hackContent").css("margin-left", "30px");
		$("#hackContent").css("margin-left", "16px");
		contentWidth -= 15;
	}
//	alert("x1 = " + x1 + " and x2 = " + x2 + " and width : " + contentWidth);
	$("#hackContent").width(contentWidth);
}




