/* correctly handle PNG transparency in Win IE 5.5 & 6.
------------------------------------------------------------------------------*/
function correctPNG()
{
   var arVersion = navigator.appVersion.split("MSIE")
   var version = parseFloat(arVersion[1])
   if ((version >= 5.5) && (version < 7) && (document.body.filters))  
   {
      for(var i=0; i<document.images.length; i++)
      {
         var img = document.images[i]
         var imgName = img.src.toUpperCase()
         if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
         {
            var imgID = (img.id) ? "id='" + img.id + "' " : ""
            var imgClass = (img.className) ? "class='" + img.className + "' " : ""
            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
            var imgStyle = "display:inline-block;" + img.style.cssText 
            if (img.align == "left") imgStyle = "float:left;" + imgStyle
            if (img.align == "right") imgStyle = "float:right;" + imgStyle
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
            var strNewHTML = "<span " + imgID + imgClass + imgTitle
            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
            + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
            img.outerHTML = strNewHTML
            i = i-1
         }
      }
   }    
}
//window.attachEvent("onload", correctPNG);
$(function(){
/* headline
------------------------------------------------------------------------------*/
	if(!$("#news").length)
	{
		$(".home div.right").css("margin-top","0");
	}
	
/* flash
------------------------------------------------------------------------------*/
	/*if($("#carte").length)
	{
		if(!siteEditEnabled){
			$("#carte").flash({
				width:482,
				height:122,
				flashvars:{
					//image:"img/worldwide_presence.png",
					xml:flashBannerConfigUrl
				},
				wmode:"transparent",
				src:flashMediaUrl},{
					version:8
				}
			);
		}
	}*/
	
		
/* Manage logo version by language
------------------------------------------------------------------------------*/
	if($("#logo .logo img").length)
	{
		var locale = $("html").attr("lang");
		
		/* only for country sites */
		/* The goal of the following code is to replace the name of the logo image 
		 * The name of the country is exctracted from the URL.
		 * The final name is : <default logo name>_<locale>_<country name>.<default extension>
		 * */
		var pageUrl = document.location;	
		// Regular expression which get the first word after the string /<locale>/
		//var expReg = new RegExp("\/"+locale+"\/([^\/]+)\/","i");	
		
		var expReg = new RegExp("http\:\/\/[^\/]+[\.]{1}([^\.\/]+)(\/|$)","i"); 
		// with the url : http://www.rhodia.ru/
		// http\:\/\/ <=> "http://"
		// [^\/]+ <=> "www.rhodia"
		// [\.]{1} <=> "."
		// ([^\.\/]+) <=> "ru"
		// (\/|$)  <=> "/" or ""
		// this expression should return "ru"
		
		var resultArr = expReg.exec(pageUrl); // resultArr contains the result of the reg ex
		// if the extraction is ok build the new name
		if(resultArr && resultArr.length == 3){
			// rename logo
			$('#logo .logo img').attr("src",$('#logo .logo img').attr("src").replace("logo_rhodia","logo_rhodia_"+locale+"_"+resultArr[1]));		 
		}
		
		
		
	}
/* remove component title link on articles details  
------------------------------------------------------------------------------*/
	$("#content .inside .contentdetail .title a").each(function() {
		$(this).removeAttr("href");
	});
	
/* manage top panels 
------------------------------------------------------------------------------*/
	$("#country, #qaccess, #sign").addClass("flottant").hide();
	$(".country a").click(function(){
		$("#qaccess, #sign, #country").hide();
		$(".sign a, .qaccess a").removeClass("close");
		if($(this).attr("class") == "close")
		{
			$($(this).attr("href")).hide();
			$(this).removeClass("close");
			$("#logo a.logo").show();
		}
		else
		{
			$($(this).attr("href")).show();
			$(this).addClass("close");
			$("#logo a.logo").hide();
		}
		$(this).blur();
		return(false);
	});
	
	$(".sign a").click(function(){
		$("#qaccess, #sign, #country").hide();
		$(".country a, .qaccess a").removeClass("close");
		if($(this).attr("class") == "close")
		{
			$($(this).attr("href")).hide();
			$(this).removeClass("close");
			$("#logo a.logo").show();

		}
		else
		{
			$($(this).attr("href")).show();
			$(this).addClass("close");
			$("#logo a.logo").hide();

		}
		$(this).blur();
		return(false);
	});
	
	$(".qaccess a").click(function(){
		$("#qaccess, #sign, #country").hide();
		$(".country a, .sign a").removeClass("close");
		if($(this).attr("class") == "close")
		{
			$($(this).attr("href")).hide();
			$(this).removeClass("close");
			$("#logo a.logo").show();
		}
		else
		{
			$($(this).attr("href")).show();
			$(this).addClass("close");
			$("#logo a.logo").hide();
		}
		$(this).blur();
		return(false);
	});
	var h = 0;
	$(".prelease a").each(function() {
		if($(this).parent().parent().attr("class")=='paddingLeft')
			h = Math.max($(this).height(), h);
	});
	$(".prelease a").each(function() {
		if($(this).parent().parent().attr("class")=='paddingLeft')
			$(this).height(h);
	});
	/*
	$("body").click(function(){
		$("h1 span").show();
		$(".country a, .sign a, .qaccess a").removeClass("close");
		$("#country, #qaccess, #sign").hide();
	});
	*/
/* faq
------------------------------------------------------------------------------*/
	$("#faq dd, #faq dt").addClass("off");
	$("#faq dt").hover(function(){
		$(this).addClass("hover");
	},function(){
		$(this).removeClass("hover");
	}).click(function(){
/*		$(this).addClass("visited"); */
		$(this).toggleClass("off");
		$(this).next().toggleClass("off");
	});
/* drop-down menu navigation
------------------------------------------------------------------------------*/
	$("#navigation li").hover(function(){
		$(this).addClass("hover");
	},function(){
		$(this).removeClass("hover");	
	});
/* text input
------------------------------------------------------------------------------*/
	var keywords = $("#keywords").val();
	$("#keywords").focus(function(){
		if($(this).val() == keywords)
			$(this).val("");
	});
	$("#keywords").blur(function(){
		if($(this).val() == "")
			$(this).val(keywords);
	});

	var searchBox = $("#search").val();
	$("#search").focus(function(){
		if($(this).val() == searchBox)
			$(this).val("");
	});
	$("#search").blur(function(){
		if($(this).val() == "")
			$(this).val(searchBox);
	});
	
	
	var search_in = $("#search_in").val();
	$("#search_in").focus(function(){
		if($(this).val() == search_in)
			$(this).val("");
	});
	$("#search_in").blur(function(){
		if($(this).val() == "")
			$(this).val(search_in);
	});
	
	
	
	var login = $("#login").val();
	$("#login").focus(function(){
		if($(this).val() == login)
			$(this).val("");
	});
	$("#login").blur(function(){
		if($(this).val() == "")
			$(this).val(login);
	});
	
	var password = $("#password").val();
	$("#password").focus(function(){
		if($(this).val() == password)
			$(this).val("");
	});
	$("#password").blur(function(){
		if($(this).val() == "")
			$(this).val(password);
	});
/* increase and decrease font size
------------------------------------------------------------------------------*/
	/*
	$(".sign").after('<ul class="police">'+
			'<li><a class="decrease" href="#" title="decrease the size"><img src="img/tmoins.gif" alt="decrease the character\'s size" /></a></li>'+
			'<li><a class="standard" href="#" title="standard size"><img src="img/tzero.gif" alt="standard character size" /></a></li>'+
			'<li><a class="increase" href="#" title="increase the size"><img src="img/tplus.gif" alt="increase the character\'s size" /></a></li>'+
		'</ul>');
	*/
	$("a.decrease").click(function(){
		$("body").removeClass("increase").removeClass("normal").addClass("decrease");
		$(this).blur();
		return(false);
	});
	$("a.increase").click(function(){
		$("body").removeClass("decrease").removeClass("normal").addClass("increase");
		$(this).blur();
		return(false);
	});
	$("a.standard").click(function(){
		$("body").removeClass("decrease").removeClass("increase");
		$(this).blur();
		return(false);
	});
	$(".top .cleft").addClass("bottom");
/* print action
------------------------------------------------------------------------------*/
	/*
		$(".pictos").prepend('<li><a class="print" href="#" title="Print"><img src="img/print.gif" alt="Print" /></a></li>');
	*/
	$(".print").click(function(){
		window.print();
		$(this).blur();
		return(false);
	});
/* 
------------------------------------------------------------------------------*/
	$("#nav>ul>li").hover(function(){
		$(this).addClass("hover");
		if($.browser.msie && $.browser.version == "6.0")
		{
			$("#content select").css("visibility","hidden");
		}
	},function(){
		$(this).removeClass("hover");
		$("#content select").css("visibility","visible");
	});
/* carousel
------------------------------------------------------------------------------*/
	$(".switch_init").removeClass("switch_init");
	
	
	if($("#mycarousel").length)
	{
		$("#mycarousel").jcarousel({
			scroll:1,
			start:1,
			visible:1,
			wrap:"both",
			auto:10
		});
	}
/* latest press release and events
------------------------------------------------------------------------------*/
	$(".prelease_init").removeClass("prelease_init");
	$(".prelease>ul>li").addClass("off");
	// UPDATE RHODIA
	
	$(".prelease>ul>li:first").removeClass("off").addClass("on");
	$(".prelease>ul>li>a").click(function(){
		// put off class on all LI
		$(".prelease>ul>li").removeClass("off").removeClass("on").addClass("off");
		// put on class on LI containing clicked anchor
		$(this).parent().removeClass("off").addClass("on");
		$(this).blur();
		return(false);
	});
	
/* related links
------------------------------------------------------------------------------*/
	$(".relatedLinks>ul>li").addClass("off");
	$(".relatedLinks>ul>li:first").removeClass("off").addClass("on");
	$(".relatedLinks>ul>li>a").click(function(){
		// put off class on all LI
		$(".relatedLinks>ul>li").removeClass("off").removeClass("on").addClass("off");
		// put on class on LI containing clicked anchor
		$(this).parent().removeClass("off").addClass("on");
		$(this).blur();
		return(false);
	});	
/*
------------------------------------------------------------------------------*/
	/*
									$(".news .roll").after('<ul class="navig">'+
										'<li><a id="prev" href="#"><img src="img/prev_off.gif" alt="Précédent" title="" /></a></li>'+
										'<li><a id="next" href="#"><img src="img/next_off.gif" alt="Suivant" title="" /></a></li>'+
									'</ul>')
	*/
	if($(".news .roll").length){
		$(".news .roll").cycle({ 
		    fx:     "fade", 
		    timeout: 0, 
		    speed:   1000, 
		    timeout: 10000, 
		    next:   "#next", 
		    prev:   "#prev" 
		});
	}
	$("#next, #prev").hover(function(){
			$(this).children().attr("src",$(this).children().attr("src").replace("_off","_on"));
		},function(){
			$(this).children().attr("src",$(this).children().attr("src").replace("_on","_off"));
		}).click(function(){
		$(this).blur();
	});
/*
------------------------------------------------------------------------------*/
/*
	$("#country .arrow").toggle(function(){
		$("#country").css("height","500px");
		$("#country .hide").show();
	},function(){
		$("#country .hide").hide();	
		$("#country").css("height","120px");
	});
*/
/*
------------------------------------------------------------------------------*/
	$(".profile li").click(function(){
		$(".profile li").not(this).removeClass("on");
		$(this).toggleClass("on");
		$(this).blur();
	});
/*
------------------------------------------------------------------------------*/
	$(".tabs").not(":first").hide();
	$("#tabs li").click(function(){
		$("#tabs li").removeClass("on");
		$(this).addClass("on");
		$(".tabs").hide();
		$($(this).children().attr("href")).show();
		$(this).children().blur();
		return false;
	});
/* table
------------------------------------------------------------------------------*/
  $('#content .inside td').addClass("tableothercells");
  $('#content .inside tr td:first-child').removeClass("tableothercells").addClass("tablefirstcolumn");
  $('#content .inside tr:first-child td').removeClass("tableothercells").removeClass("tablefirstcolumn").addClass("tablefirstline");    
  $('.datatable td').addClass("tableothercells");
  $('.datatable tr td:first-child').removeClass("tableothercells").addClass("tablefirstcolumn");
  $('.datatable tr:first-child td').removeClass("tableothercells").removeClass("tablefirstcolumn").addClass("tablefirstline");
/*
------------------------------------------------------------------------------*/

	if(!siteEditEnabled)
	{
		correctPNG();
	}
/*
------------------------------------------------------------------------------*/
/* popin
------------------------------------------------------------------------------*/
	$("#masque, #popin #close a, #popin a.button").click(function(){
		$("html").removeClass("popin");
		$(this).blur();
		return(false);
	});

/* PRESS RELEASE AND EVENT CONTEXTUALISATION SYSTEM
------------------------------------------------------------------------------*/
	if(!siteEditEnabled){
		// initialisation
		var allowedPageTypes = ["event-index-page","pr-index-page"];
		var deniedUniverse = "news_center";
		
		// retrieve variable values fom current page
		var pageType = $("meta[name='tcmPageType']").attr("content");
		var universe = $("body").attr("class");
		if( ((pageType==allowedPageTypes[0]) || (pageType==allowedPageTypes[1]) ) && (universe != deniedUniverse)&& ($(".inside .search_results .arrows a").length) ){
			var rootHref;	
		  	// init URL base
			rootHref = $(".inside .search_results .arrows a").get(0).href;
			rootHref = rootHref.substr(0,rootHref.lastIndexOf("/")+1);
			  
			// init detail if hashname (bookmark support)
		    var initialHash = $.history.getCurrent();
			if(initialHash != '') {
				// show content detail
				showContentDetail(rootHref + initialHash);
			}
	
		
			// available for navigation (prev/next) in the browser
			$(window).history(function(e, hash){
				if(hash == '') {
					// hide content detail
					hideContentDetail();
				}
				if(hash != '') {
					// show content detail
					showContentDetail(rootHref + hash);
				}
			});
		
			// available when click on the content detail link (anchor: a)
			$(".inside .search_results .arrows a").click(function(){	
				var url = $(this).attr("href");
			    // get content detail from the detail page and load it + hide the current content
			    showContentDetail(url);
			    
			   	// calculate the hashName for the detail page
				var tab = url.split("/");
				var hachName = tab[tab.length -1];
				
				// navigate to the new keyword and change the URL in the user's browser
				$.history.add(hachName);
			 	return false;	
			});
		}
	}

	

});

function showContentDetail(url){
	
	$("#content .inside").removeClass("inside").addClass("insidehidden");
	$("body").css("cursor","progress");
	$("#content .insidehidden").hide();
	$("<div class='detailtmp'></div>").appendTo("#content");
	$("#content .detailtmp").load(url + " #content .inside", function(){
		$("body").css("cursor","default");	
	});
}

function hideContentDetail(){
	$("#content div.detailtmp").remove();
	$("#content .insidehidden").removeClass("insidehidden").addClass("inside");	
	$("#content .inside").show();
}


