﻿/** GLOBAL VARIABLES **/
var subMenuInterval;
var iCounterIncrease = 0;
var sTimeout;

/** LOCALIZED VARIABLES **/
var sLang = $('html').attr("lang");
var sLocalizedProceed = "Proceed to the website";
var sLocalizedCancel = "Cancel";
if (sLang == "fr")
{
	sLocalizedProceed = "Accéder au site Internet";
	sLocalizedCancel = "Annuler";
}

	
var addthis_config =
{
	ui_508_compliant: true,
	ui_click: true,
	services_exclude: 'print'
}

$(document).ready(function() {
	var xt = new XitiDocTracker ();

	slotNews();
	if( navigator.userAgent.match(/iPad/i) != null || navigator.userAgent.match(/iPod/i) != null || navigator.userAgent.match(/iPhone/i) != null) displaySubMenuIpad();
	else displaySubMenu();
	displayLinkArrow();
	changeClassLink();
	outgoingMessage();
	increase_decrease_size();
	removeSearchLabel();
	printPage();
	teaser9mai();
	slotWorldwide();
	slotSubsidiaries();
	toogleDiv();
	showSlider();
	slider();
	displayImageSet();
	managePager();
	displayModal();
	redirectIeRSS();
	geoLocRedirect();
	displayPageContent();
	//displayOnce();
});

function displayOnce()
{
	if($.cookie('first_visit') == null){
	
		var oLink = $('div#slotNews a.displayPageContent');

		if (oLink.length > 0 )
		{
			var sAjaxPageUrl = oLink.attr('href');
			
			$.ajax({
				url: sAjaxPageUrl,
				context: document.body,
				success: function(data){
					var response = $(data).find('td#middleArea').html();
					response = "<div class=\"popupInner2\">" + response + "<span class=\"clear\">&nbsp;</span>";
					$(this).easyPopup({sHtml:response});
					
					$.cookie('first_visit', 'true', { expires: 90 });
				}
			});
		}
	}
}

function displayPageContent()
{
	$('a.displayPageContent').click(function(e){
		e.preventDefault();
		
		var sAjaxPageUrl = $(this).attr('href');
		
		$.ajax({
			url: sAjaxPageUrl,
			context: document.body,
			success: function(data){
				var response = $(data).find('td#middleArea').html();
				response = "<div class=\"popupInner2\">" + response + "<span class=\"clear\">&nbsp;</span>";
				//alert(response);
				$(this).easyPopup({sHtml:response});
			}
		});
	});
}

function geoLocRedirect()
{
	var cookie = $.cookie('SanofiCookie-redirect');
	
	if (cookie == null) {
	
		var pathArray = document.URL;
		//alert(pathArray);
		if (pathArray == "http://www.sanofi.com" || pathArray == "http://www.sanofi.com/"
												 || pathArray == "http://www.sanofi.com/index.aspx"
												 || pathArray == "www.sanofi.com"
												 || pathArray == "www.sanofi.com/"
												 || pathArray == "www.sanofi.com/index.aspx"
												 || pathArray == "http://sanofi.com"
												 || pathArray == "http://sanofi.com/"
												 || pathArray == "http://sanofi.com/index.aspx"
												 || pathArray == "sanofi.com"
												 || pathArray == "sanofi.com/"
												 || pathArray == "sanofi.com/index.aspx"){
			if ( navigator.appName == "Microsoft Internet Explorer" )
				var baseLang = navigator.userLanguage;
			else
				var baseLang = navigator.language;
			
			if ( baseLang != 'fr' )
				window.location.href = "http://en.sanofi.com";
			
			//make cookie
			$.cookie('SanofiCookie-redirect', 'true', {domain: '.sanofi.com'});
		}
	}
}

function redirectIeRSS()
{
	$("a").click(function(e){
		var slink = this.href;
		var reg=new RegExp("rss-feed-press.aspx?","g");
		if(slink.match(reg)){
			if ( navigator.appName == "Microsoft Internet Explorer" ){
				e.preventDefault();
				window.location.href = "/rss/rss-feed-press2.aspx";
			}
			
		}
	});
}

function displayModal()
{
	$('a.modal').click(function(e){
		e.preventDefault();
		
		var sContainerId = $(this).attr('rel');
		$(this).easyPopup({containerId:sContainerId});
	});
}

function managePager()
{
	var aUrl = window.location.href.slice(window.location.href).split('#page_');
	var sUrl = "";
	if (aUrl.length > 1)
	{
		sUrl = "page_"+aUrl[1];
	}
	var sPageId = "";
	var oLiParent = null;
	var oUl = null;
	var oLiSelected = null;
	var oDivToHide = null;
	var oDivToShow = null;
	var aPageNumber = sUrl.split('_');
	var sPageNumber = aPageNumber[1];
	
	if( sUrl != "undefined" && sUrl != null && sUrl != "" )
	{
		var aPageNumber = sUrl.split('_');
		var sPageNumber = aPageNumber[1];
		
		sPageId = "#"+sUrl;
		oDivToHide = $('td#middleArea').find('div.pageVisible');
		oDivToShow = $('td#middleArea').find('div' + sPageId);
		
		oDivToHide.removeClass('pageVisible');
		oDivToHide.addClass('pageHidden');
		oDivToShow.removeClass('pageHidden');
		oDivToShow.addClass('pageVisible');
		
		$('table.pagination tr td.middle ul li:nth-child('+sPageNumber+')').addClass('selected');
	}
	else
	{
		$('table.pagination tr td.middle ul li:first').addClass('selected');
	}
	
	$('table.pagination tr td.middle ul li a').click(function(e){
		e.preventDefault();
		
		sPageId = $(this).attr('href');
		oLiParent = $(this).parent();
		oUl = oLiParent.parent();
		oLiSelected = oUl.find('li.selected');
		
		oDivToHide = $('td#middleArea').find('div.pageVisible');
		oDivToShow = $('td#middleArea').find('div' + sPageId);
		
		oDivToHide.removeClass('pageVisible');
		oDivToHide.addClass('pageHidden');
		oDivToShow.removeClass('pageHidden');
		oDivToShow.addClass('pageVisible');
		
		oLiSelected.removeClass('selected');
		oLiParent.addClass('selected');
	});
	
	
}

function displayImageSet()
{
	$('div div.desc ul li.zoom a').click(function(e){
		e.preventDefault();
		var sPage = $(this).attr('href');
		$(this).easyPopup({ajaxPageUrl:sPage, displayCloseLabel:false, popupWidth:638});
	});
	
	$('div a.zoom').click(function(e){
		e.preventDefault();
		var sPage = $(this).attr('href');
		$(this).easyPopup({ajaxPageUrl:sPage, displayCloseLabel:false});
	});
}

function slider(){
	$('ul.allSlides').cycle({
		fx					: 'scrollHorz',
		next				: '.next', 
		prev				: '.prev',
		prevNextClick		: slider_actionPause,
		timeout				: 5000,
		speed				: 800

	});
}

function slider_actionPause(isNext, zeroBasedSlideIndex, slideElement) 
{
	$('div.slider ul.allSlides').cycle('pause');
	setTimeout(function() { $('div.slider ul.allSlides').cycle('resume'); }, 10000);
}

function slider_buildPagination(idx, slide) {
	return '<li><span class="forVoice">'+(idx+1)+'</span><a href="#" class="bullet">&nbsp;</a></li>';
}

function showSlider(){

	$('a.showMore').click(function(e){
		e.preventDefault();
		var sId, sTitle, sData;
		
		sId = $(this).attr("rel");
		sTitle = $(this).html();
		//sData = '<div id="'+sId+'" class="slider">' + $( 'div#'+sId ).html() + '</div>';

		$(this).easyPopup({containerId:sId, popupTitle:sTitle, displayOuterCloseButton:true, displayInnerCloseButton:false, displayCloseLabel:false, popupWidth:480});
		slider();
	});
}

function toogleDiv()
{
	
	$('a.hasDesc').click(function(e) {
		e.preventDefault();
		var sRel = $(this).attr('rel');
		var oDiv = $('div#'+sRel);

		var sClass = oDiv.attr('class');
		
		if ( sClass == 'collapsed' ) {
			oDiv.slideDown(200);
			oDiv.attr('class', 'expanded');
		}
		else
		{
   			oDiv.slideUp(200);
			oDiv.attr('class', 'collapsed');
		}
	});
}


function slotSubsidiaries()
{
	$('div#slotSubsidiaries form#selectSubsidiaries').submit(function(e){
		e.preventDefault();
		var sOutgoingPage = '/test/disclaimer_subsidiary.aspx';
		var sHref = $('div#slotSubsidiaries form#selectSubsidiaries select').val();
		
		if (sHref != "-1")
		{
			var sContent = '<div class="actions">';
			sContent += '<a href="'+sHref+'" class="goto" target="_blank"><span><span>' + sLocalizedProceed + '</span></span></a>';
			sContent += '<a href="#" class="popupClose"><span><span>' + sLocalizedCancel + '</span></span></a>';
			sContent += '</div>';
			
			var o = $(this).easyPopup({ajaxPageUrl:sOutgoingPage, displayOuterCloseButton:false, sHtml:sContent, addClass:"outgoing"});
			closeOnProceed(o);
		}
	});
}

function slotWorldwide()
{
	$('div#slotWorldwideWebsites form#selectCountry').submit(function(e){
		e.preventDefault();
		var sOutgoingPage = '/test/disclaimer_subsidiary.aspx';
		var sHref = $('div#slotWorldwideWebsites form#selectCountry select').val();
		
		if (sHref != "-1")
		{
			var sContent = '<div class="actions">';
			sContent += '<a href="'+sHref+'" class="goto" target="_blank"><span><span>' + sLocalizedProceed + '</span></span></a>';
			sContent += '<a href="#" class="popupClose"><span><span>' + sLocalizedCancel + '</span></span></a>';
			sContent += '</div>';
			
			var o = $(this).easyPopup({ajaxPageUrl:sOutgoingPage, displayOuterCloseButton:false, sHtml:sContent, addClass:"outgoing"});
			closeOnProceed(o);
		}
	});
}

function teaser9mai()
{
	$('a.teaser9mai').click(function(e){
		e.preventDefault();
		var sId, sTitle, sContent, sLang, sRel;
		
		sRel = ($(this).attr("rel")).split('__');
		sId = sRel[0];
		sLang = sRel[1];
		sTitle = $(this).attr("title");
		if (sLang == "en")
		{
			sContent = "<div id='"+sId+"'>";
			sContent += "<h2>The Bird of Hope</h2>";
			sContent += "<p><strong>Sanofi's new icon represents the hope that it brings to the 7 billion people around the world and its focus on the patient.</strong></p>";
			sContent += '<iframe width="640" height="390" src="http://www.youtube.com/embed/nhJ4ungbWoE?rel=0" frameborder="0" allowfullscreen></iframe>';
			sContent += "<p>As Sanofi's strategy is built on three key principles (Innovation, Adaptability, Growth) the icon is made of three shapes that make a whole: a planet with the bird of hope in the center. These three shapes structure our ambition, which puts the patient at the center of our activities. The bird illustrates the hope we offer to our patients.  The three different shapes show the diversity of our teams, cultures, know-how and locations around the world. This new icon represents the hope that we bring to the approximately 7 billion people around the world; our focus on the patient, our desire to working tirelessly, and our passion and confidence, to enhance life. </p>";
			sContent += "<p>The four colors (white, blue, green, ochre) represent life: air - water - earth - fire. </p>"
			sContent += '</div>';
		}
		else
		{
			sContent = "<div id='"+sId+"'>";
			sContent += "<h2>L'Oiseau de l'Espoir</h2>";
			sContent += "<p><strong>Le nouveau logo de Sanofi symbolise l'espoir que nous apportons aux 7 milliards de personnes qui pourraient un jour avoir besoin de nos soins et notre engagement en faveur des patients.</strong></p>";
			sContent += '<iframe width="640" height="390" src="http://www.youtube.com/embed/IqBgiO-_Cd8?rel=0" frameborder="0" allowfullscreen></iframe>';
			sContent += "<p>La strat&eacute;gie de Sanofi &eacute;tant articul&eacute;e autour de trois grands principes (innovation, adaptation, croissance), ce logo est construit de trois formes constituant un tout : une plan&egrave;te avec l'oiseau de l'espoir en son centre. Ces trois formes structurent notre ambition qui consiste &agrave; placer le patient au coeur de nos activit&eacute;s. L'oiseau symbolise l'espoir que nous apportons aux patients. Ces trois formes illustrent &eacute;galement la diversit&eacute; de nos &eacute;quipes, leurs diff&eacute;rentes cultures, savoir-faire et localisations g&eacute;ographiques. Ce nouveau logo repr&eacute;sente l'espoir que nous apportons &agrave; pr&egrave;s de 7 milliards de personnes qui pourraient un jour avoir besoin de soins ; il symbolise notre souci constant du patient, notre volont&eacute; d'am&eacute;liorer la vie ainsi que la passion et la confiance avec lesquelles nous poursuivons infatigablement cet objectif. </p>";
			sContent += "<p>Les couleurs (bleu, vert, ocre et blanc) repr&eacute;sentent la vie : l'eau, la terre, le feu et l'air.</p>"
			sContent += '</div>';
		}
		
		$(this).easyPopup({displayOuterCloseButton:true, displayInnerCloseButton:false, displayCloseLabel:false, sHtml:sContent});
	});
}

function printPage()
{
	$('div#toolsContent ul li.print a').click(function(e){
		e.preventDefault();
		window.print();
	});
}

function removeSearchLabel() {
	var $element = $('form#search input#searchText'),
	value = $('form#search input#searchText').attr('value');

	// Add focus/blur label behavior to search box.
	$element.bind('focus', function () {
		if ($element.val() === value) {
			$element.val('');
		}
	});
	$element.bind('blur', function () {
		if ($element.val() === '' || $element.val() === value) {
			$element.val(value);
		}
	});
	$element.trigger('blur');
};

function outgoingMessage()
{
	$('a.goOut').click(function(e){
		var sRel = $(this).attr('rel');
		var sHref = $(this).attr('href');
		
		if (sRel != "")
		{
			e.preventDefault();
			var sContent = '<div class="actions">';
			sContent += '<a href="'+sHref+'" class="goto" target="_blank"><span><span>' + sLocalizedProceed + '</span></span></a>';
			sContent += '<a href="#" class="popupClose"><span><span>' + sLocalizedCancel + '</span></span></a>';
			sContent += '</div>';
			
			var o = $(this).easyPopup({ajaxPageUrl:sRel, displayOuterCloseButton:false, sHtml:sContent, addClass:"outgoing"});
			closeOnProceed(o);
		}
	});
	
	$('body.r_tools div.countryList div.col ul li a').click(function(e){
		var sRel = $(this).attr('rel');
		var sHref = $(this).attr('href');

		sRel = '/test/disclaimer_subsidiary.aspx';
		
		e.preventDefault();
		var sContent = '<div class="actions">';
		sContent += '<a href="'+sHref+'" class="goto" target="_blank"><span><span>' + sLocalizedProceed + '</span></span></a>';
		sContent += '<a href="#" class="popupClose"><span><span>' + sLocalizedCancel + '</span></span></a>';
		sContent += '</div>';
		
		var o = $(this).easyPopup({ajaxPageUrl:sRel, displayOuterCloseButton:false, sHtml:sContent, addClass:"outgoing"});
		closeOnProceed(o);
		
	});
}

function closeOnProceed(o)
{
	$('a.goto').click(function(e){
		o.easyPopup.close();
	});
}

function changeClassLink()
{
	var oA = $('table#tableContent ul li a');
	
	oA.each(function(){

		if (($(this).attr('class').toLowerCase() == 'pdf') || ($(this).attr('class') == 'link') || ($(this).attr('class') == 'Excel') ||
			($(this).attr('class') == 'word') || ($(this).attr('class').toLowerCase() == 'calendar') || ($(this).attr('class') == 'goOut') ||
			($(this).attr('class').toLowerCase() == 'conference') || ($(this).attr('class') == 'teleconf') || ($(this).attr('class') == 'mp3') || 
			($(this).attr('class').toLowerCase() == 'rss') || ($(this).attr('class') == 'ourServices_mail') || ($(this).attr('class') == 'ourServices_contact') || 
			($(this).attr('class') == 'ourServices_glossary') || ($(this).attr('class') == 'ourServices_rss') || ($(this).attr('class') == 'ourServices_orderDoc') || 
			($(this).attr('class') == 'ourServices_calendar') || ($(this).attr('class') == 'ourServices_calculator'))
		{
			var oLi = $(this).parent();
			var oLi2 = $(this).parent().parent();
			var oLi3 = $(this).parent().parent().parent();
			if (oLi.hasClass('link'))
			{
				if (($(this).attr('class').toLowerCase() == 'rss') || ($(this).attr('class').toLowerCase() == 'pdf') ||
					($(this).attr('class').toLowerCase() == 'conference') || ($(this).attr('class').toLowerCase() == 'calendar') || ($(this).attr('class').toLowerCase() == 'mp3'))
					oLi.attr('class', $(this).attr('class').toLowerCase());
				else
					oLi.attr('class', $(this).attr('class'));
			}
			if (oLi2.hasClass('link'))
			{
				if (($(this).attr('class').toLowerCase() == 'rss') || ($(this).attr('class').toLowerCase() == 'pdf') ||
					($(this).attr('class').toLowerCase() == 'conference') || ($(this).attr('class').toLowerCase() == 'calendar') || ($(this).attr('class').toLowerCase() == 'mp3'))
					oLi2.attr('class', $(this).attr('class').toLowerCase());
				else
					oLi2.attr('class', $(this).attr('class'));
			}
			if (oLi3.hasClass('link'))
			{
				if (($(this).attr('class').toLowerCase() == 'rss') || ($(this).attr('class').toLowerCase() == 'pdf') ||
					($(this).attr('class').toLowerCase() == 'conference') || ($(this).attr('class').toLowerCase() == 'calendar') || ($(this).attr('class').toLowerCase() == 'mp3'))
					oLi3.attr('class', $(this).attr('class').toLowerCase());
				else
					oLi3.attr('class', $(this).attr('class'));
			}
		}
	});
	
}

function displayLinkArrow()
{
	$('li.link a').hover(
		function()
		{
			var oLi = $(this).parent();
			oLi.addClass('active');
		},
		function()
		{
			var oLi = $(this).parent();
			oLi.removeClass('active');
		}
	);
}

function displaySubMenu()
{
	$('div.menu ul li').hover(
		function()
		{
			var oSubMenu = $(this).find('div.subMenu');
			var sSubMenuClass = $(oSubMenu).attr('class');

			
			if (sSubMenuClass != 'undefined' && sSubMenuClass != '' && sSubMenuClass != null)
			{
				if (oSubMenu.hasClass('forVoice'))
				{
					$(this).addClass('active');
					sTimeout = setInterval(function(){
						oSubMenu.show(0);
						oSubMenu.attr('class', 'subMenu selected');
					}, 200);
				}
			}
		},
		function()
		{
			clearTimeout(sTimeout);
			var oSubMenu = $(this).find('div.subMenu');
			var sSubMenuClass = $(oSubMenu).attr('class');
			if (sSubMenuClass != 'undefined' && sSubMenuClass != '' && sSubMenuClass != null)
			{
				$(this).removeClass('active');
				oSubMenu.hide(0);
				oSubMenu.attr('class', 'subMenu forVoice');
			}
		}
	);
}

function displaySubMenuIpad(){

	$("body").click( function(){
		//INIT menu displaying (hide) and disable li element active class
		$("body").find('div.subMenu').each( function(){ $(this).attr("class","subMenu forVoice")  });			
		$("body").find('div.menu ul li').each( function(){ $(this).removeClass("active");  });			
	});

	$('div.menu ul li a').click(function(){ 
		var oParent = $(this).parent();
		
		//2ND click
		if( oParent.hasClass("active") ){
			return true;
		}
	
		//INIT menu displaying (hide) and disable li element active class
		$("body").find('div.subMenu').each( function(){ $(this).attr("class","subMenu forVoice")});			
		$("body").find('div.menu ul li').each( function(){ $(this).removeClass("active");});
		
		
		//active class on li parent element
		oParent.addClass("active");
		
		//show submenu
		oParent.find('div.subMenu').attr('class','subMenu selected');
		
		$('div.menu ul li div.subMenu a').click(function(){
			window.location = "http://" + window.location.hostname + ":" + window.location.port + $(this).attr('href');
		});
		return false;
	});
}

function slotNews()
{
	$('div#slotFocus div.content ul.list').cycle({ 
		fx					:'fade', 
		speed				:200, 
		timeout 			:5000, 
		pager				:'ul#slotFocus-pagination',
		pagerAnchorBuilder	: slotNews_buildPagination,
		prevNextClick		: slotNews_actionPause,
		activePagerClass	: 'selected'
	});
}

function slotNews_actionPause(isNext, zeroBasedSlideIndex, slideElement) 
{
	$('div#slotFocus div.content ul.list').cycle('pause');
	setTimeout(function() { $('div#slotFocus div.content ul.list').cycle('resume'); }, 10000);
}

function slotNews_buildPagination(idx, slide) {
	return '<li><a href="#">'+(idx+1)+'</a></li>';
};

function increase_decrease_size()
{
	$("li.textSize a").click(function(e){
		e.preventDefault();
		
		var sClass = $(this).attr('class');
		var divMasterContainer_current = $('#masterContainer').css("font-size");
		var divMasterContainerNum = parseFloat(divMasterContainer_current, 10);
		
		if ( sClass == "increase")
		{
			divMasterContainer_new = divMasterContainerNum + 1;
			iCounterIncrease ++;
		}
		else if( sClass == "decrease")
		{
			if ( iCounterIncrease > 0)
			{
				divMasterContainer_new = divMasterContainerNum - 1;
				iCounterIncrease --;
			}
		}
		
		if ( iCounterIncrease >= 0 )
		{
			$('#masterContainer').css('font-size', divMasterContainer_new);
		}
		
	});
}

