/*************************************************

   Sodexo changes: Each time this file is updated by the 
           design agency, also make the following change(s):

      openPrint('/en/print-page.aspx');  //change link
      $.getJSON("/en/includes/json/googlemap.txt", { }, function(data){   //path to map.json file used by google map
      activity names to match that of the ones in the Country Activities category: $GetKeywordDescription(on-site-service-solutions), $GetKeywordDescription(motivation-solutions) and $GetKeywordDescription(personal-home-solutions)   // in google map
      resizeFlash functions at the bottom of the code
      script for input search - added script for calling the search results page; added passing of the current publication's language (pubLanguage hidden field)


*************************************************/

Cufon.replace('div.home div#center div h2,p.welcome,a.link-google-map, a.dinMedium, h2.dinMedium, div.video-blue h2, div#right div.actions h2',  { fontFamily: 'DIN-Medium' });
Cufon.replace('div.standard div#right div.generic-bloc_right h2, div.bloc-right-segments h2, div.bloc-right-segments h2 a , h3.dinMedium, ul.subnav-1 li a.dinMedium, div.standard div#center div.news-right h2', { fontFamily: 'DIN-Medium' });
Cufon.replace('li.media2 a, div#center h1', { fontFamily: 'DIN-Regular' });
Cufon.replace('li.media1 a, .link-press a', { fontFamily: 'Sodexho', hover:{ color:'#08436a'} });
Cufon.replace('ul#nav li a.dinBold', { fontFamily: 'DIN-Bold', hover:{ color:'#000'} });
Cufon.replace('div.hunger h2', { fontFamily: 'Sodexho', hover:{ color:'#08436a'} });
Cufon.replace('span.sodexo, div.video-green h2 ', { fontFamily: 'Sodexho'});

$(window).load(function(){
/** ************************** script for tabulation *********************** */ 
	$("div.css-panes div.bloc div.sous-niv").css('display','none'); 
	//$("ul.css-tabs li:last a").css('background','none');
	
	$("span.bt_more").click(function(){
		if($(this).siblings("div.sous-niv").css('display') == 'block'){
			$(this).find('a').css('background-position', '-90px -93px');
		}else{
			$(this).find('a').css('background-position','-90px -74px');
		}
	    $(this).siblings("div.sous-niv").slideToggle("slow");
    });
	$("ul.css-tabs li a.current").parent('li').prev().find("a").addClass('nobck');
	$("TopSearchButton").val('0');	
	
	switch($("ul.css-tabs li").length)
	{
		case 5:$("ul.css-tabs").addClass("css-tabs-5");break;
		case 4:$("ul.css-tabs").addClass("css-tabs-4");break;
		case 3:$("ul.css-tabs").addClass("css-tabs-3");break;
		case 2:$("ul.css-tabs").addClass("css-tabs-2");break;
	}
	
	$("div.css-panes").eq(0).addClass("firstDivCssTab");  
	
/** *********** script for press release detail (DOM) ************************ */
$("div.release-detail div.section p:last-child").css('margin','0');
	
/** ***** script for select country ***************** */
$("ul.index-top-countries li a").click(function(){
	$("div.bloc-countries div.list").hide();
	$("ul.index-top-countries li").removeClass("activ");
	$(this).parent().addClass("activ");
	var list = ($(this).parent().index())+1;
	$("div.bloc-countries div.list"+list).show();
});
$("a.link-coutries").hover(function(){
	$("div.bloc-countries").css('display','block');
});
$("div.bloc-countries").hover(function(){
}, function(){$("div.bloc-countries").css('display','none');
});	

/** *************** script for input search ********************* */	
$("#SearchInput").focus(function(){
    // if ($(this).val().toLowerCase() == "search") {
         $(this).val("");
         $(this).addClass("actif");
    // }
});
$("#SearchInput").blur(function(){
    if ($(this).val() == "") {
        $(this).val("search");
        $(this).removeClass("actif");
    }
});
$("#SearchInput").keypress(function(event){
   if (event.which == '13') {
      location.href = $("#SearchResultsPage").val() + "?q=" + encodeURIComponent($("#SearchInput").val()) + "&lr=lang_" + $("#pubLanguage").val();    
   }
});

$("#TopSearchButton").live('click', function () {
    //alert($("#SearchInput").val());  
    //alert($("#SearchResultsPage").val());
    location.href = $("#SearchResultsPage").val() + "?q=" + encodeURIComponent($("#SearchInput").val()) + "&lr=lang_" + $("#pubLanguage").val();
}); 
    
/*** function resize flash ***/
function resize(){
	$('.animation').animate({height: '360px' }, 2000, function() { });
}    

/** ****************** script for navigation *********************** */
var indexli;
$("div.div-subnav-1").css({bottom:'37px',top:'auto',display:'none'});
$("ul#nav > li:last").css('background','none');

/* calcul space for each item nav li */
var largMenu=0;

$("ul#nav > li").each(function() { largMenu +=$(this).width(); }); 
largMenu -= 36; // we dont resize the first item's space

var esp;
var l2;var larg2;
var NbItemsFirstLevel = ($("ul#nav > li").length)-1;
esp = Math.round((523-largMenu)/NbItemsFirstLevel) ;
for( var i=1; i<7; i++){
 l2 = $("ul#nav > li:eq("+i+")").width();
 var larg2 = (l2 + esp) - 2;
 $("ul#nav > li:eq("+i+")").css('width',larg2) ;
}

var largMenu2=0;
$("ul#nav > li").each(function() { largMenu2 +=$(this).width(); }); 
largMenu2 -= 36;

espLast = 513 - largMenu2; 
largLast = ($("ul#nav > li:last").width()) + espLast;
$("ul#nav > li:last").css('width',largLast);

/** ******************************************* */
$("ul#nav > li:last a, ul.css-tabs  li:last a ").addClass("last");
if( ($(".css-panes > div.bloc > div").length == 1) && ($(".css-panes div.bloc div.sous-niv").length == 0) ) $(".firstDivCssTab > div.bloc > div").addClass("noGradient");

$(".div-subnav-1").each(function () {
	$(this).parent().eq(0).hoverIntent({
	timeout: 250,
	over: function () { 
		var current = $(".div-subnav-1:eq(0)", this);
		current.slideDown(100);
		current.css('display','block');
		$("#nav ul.subnav-2, #nav ul.subnav-33, .subnav-3").css('display','none');
		$("#nav li").removeClass("nobck");
		$(this).find("a.dinBold").css('color','#0189E5');
		$(this).addClass('activ');
		$("#nav").addClass("bck-menu-on");
		check();
		},
	out: function () { 
			var current = $(".div-subnav-1:eq(0)", this);
			current.hide();
			$(this).removeClass('activ');
			$("#nav").removeClass("bck-menu-on");
			check();
			$("#nav li").removeClass("nobck");
			check();
		}
	});
});
function check(){
	$("ul#nav li.activ").each(function () {
		$("#nav").addClass("bck-menu-on");
		$(this).prev("li").addClass("nobck");
	});
}

	$("ul.subnav-1 > li a").click(function() {
		if( $(this).parent().find("ul.subnav-2").length == 2 ) { $(this).parent().find("ul.subnav-2").addClass("list-div-nav");$(this).parent().find("ul.subnav-2").eq(0).find(".subnav-3").addClass('subnav-332');$(this).parent().find("ul.subnav-2:last").addClass('subnav-22'); } 
		if($(this).parent().find("ul.subnav-2 > li").length > 8 ){ $(this).parent().find("ul.subnav-2").removeClass("list-div-nav"); $(this).parent().find("ul.subnav-2").addClass("subnav-33");$(this).parent().find("ul.subnav-2").removeClass("subnav-2");  }
		$("ul.subnav-33").css("display","none");
		$(this).parent().find("ul.subnav-33").css("display","block");
		$("ul.subnav-2").css("display","none");
		$(this).parent().find("ul.subnav-2").toggle();
	});
	$("ul.subnav-33").hover(function() {
	},function() {
		$(this).css('display','none');
	});
		
	$("ul.subnav-3").each(function () {
		$(this).parent().eq(0).hoverIntent({
		timeout: 200,
		over: function () {
			$("ul.subnav-2 li").removeClass("activ");
			
			$("ul.subnav-3").css("display","none");
			$(this).find("ul.subnav-3").css("display","block");
			$(this).addClass("activ2");
			$(this).parent().parent().parent().addClass("activ");
			$(this).parent().parent().parent().find("a").addClass("activa");
			},
		out: function () { 
			$("ul.subnav-2 > li").removeClass("activ2");	
			$("ul.subnav-2 > li a").removeClass("activa");	
			$(this).find("ul.subnav-3").css("display","none");
			}
		});	
	});

	$("ul.subnav-2").css("display","none");

/** ******** print ****************************************************** */
$("ul#tools li.print a").click(function() {
   openPrint('/en/print-page.aspx');
});
/** *********** font Resizing ****************************************** */
var originalFontSize = $('html').css('font-size');
    $(".resetFont").click(function(){
    $('html').css('font-size', originalFontSize);
  });
  // Increase Font Size
  $(".increaseFont").click(function(){
    var currentFontSize = $('html').css('font-size');
    var currentFontSizeNum = parseFloat(currentFontSize, 10);
    var newFontSize = currentFontSizeNum*1.2;
    $('html').css('font-size', newFontSize);
    return false;
  });
  // Decrease Font Size
  $(".decreaseFont").click(function(){
    var currentFontSize = $('html').css('font-size');
    var currentFontSizeNum = parseFloat(currentFontSize, 10);
    var newFontSize = currentFontSizeNum*0.8;
    $('html').css('font-size', newFontSize);
    return false;
  });
	
/** *************** SEO change placement *********************** */
$("p.description.seoChgt").insertAfter("div#center h1");
$("p.introduction-title.seoChgt").insertAfter("div#center h1");

/******************** html structure *******************************/
if($("div#right").length == 0) $("div#content").addClass('fullpage');

/** *************** faq ****************************************** */
 if($("ul.list-faq")) $("ul.list-faq div.level2").css('display','none');
 if($("ul.list-faq")) $("ul.list-faq div.level1").css('display','none');
 if($("ul.list-faq")) $("div.level1 > ul > li:last h3").css('border-bottom','0 none'); 

 $("ul.list-faq h2").click(function(){
     if ($(this).siblings("div.level1").css('display') == 'block') {
         $(this).css('background-position', '-31px -26px');
     }
     else 
         if ($(this).siblings("div.level1").css('display') == 'none') {
             $(this).css('background-position', '-33px -173px');
         }
     $(this).siblings("div.level1").slideToggle("slow");
 });
	
$("ul.list-faq h3").click(function(){
		if($(this).siblings("div.level2").css('display') == 'block'){
			$(this).css('background-position','-33px -129px');
			$(this).find('a').css('color','#1e7ab9');	
		}else if($(this).siblings("div.level2").css('display') == 'none') {
			$(this).css({backgroundPosition: '-33px -76px', padding:'0 0 0 19px'});
			$(this).find('a').css('color','#9f5da5');	
		}
	    $(this).siblings("div.level2").slideToggle("slow");
    });
	
/** ************ end document ready **************************** */
});

/** ************** google map ********************************** */	
var map, type_icon;
var arrMarkers = new Array();
var arrInfoWindows = new Array();
var activity = new Array();
var country = new Array();
var activity_country = new Array();
var geocoder;

function mapInit(){
	geocoder = new google.maps.Geocoder();
    var centerCoord = new google.maps.LatLng(0, 0);
    var mapOptions = {
        zoom: 1,
        center: centerCoord,
        mapTypeControl: false,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("map"), mapOptions);
    
	parseJson();
}

function parseJson(){
	clearMarkers();

    $.getJSON("/en/includes/json/googlemap.txt", { }, function(data){   
        $.each(data.places, function(i, item){
            var sizeIcon = 30; // manage icon's sprite
            if (item.activity == "On-site Service Solutions")     
                type_icon = 0; 
            if (item.activity == "Motivation Solutions") 
                type_icon = 173;
            if (item.activity == "Personal and Home Services") 
                type_icon = 355;
            if (item.activity == "country") {
                type_icon = 535;
                sizeIcon = 35
            }
            var zoomLevel = map.getZoom();
			var marker = new google.maps.MarkerImage();
            marker = new google.maps.MarkerImage('/en/Images/564828_sprite-icon-googlemap.png', new google.maps.Size(sizeIcon, 34), new google.maps.Point(0, type_icon));
           
			 marker = new google.maps.Marker({
						position: new google.maps.LatLng(item.lat, item.lng),
						map: map,
						icon: marker
					});
					
			 marker.setVisible(false);
			  if (zoomLevel == 1) {  // show only markers "sodexo in your
										// country"
				if (item.activity == "country") {
				  marker.setVisible(true);
				}
			}
			else if (zoomLevel != 1) { // show every markers
				 marker.setVisible(true);
				}
				
			country.push(item.country);
			activity.push(item.activity);
			activity_country[i]=[item.activity,item.country];
			arrMarkers.push(marker);
			
            var infowindow = new google.maps.InfoWindow({
                content: "<div style='width:405px; height:220px;' class='infoWindowContent " + item.activity + "'>" + item.description + "</div>",
                maxWidth: 420,
                'pixelOffset': new google.maps.Size(0, 20)
            });
            arrInfoWindows[i] = infowindow;
			
            google.maps.event.addListener(marker, 'click', function(){
                for (x = 0; x < arrInfoWindows.length; x++) {
                    arrInfoWindows[x].close();
                }
                //map.zoom = item.zoom;
                map.setCenter(new google.maps.LatLng(item.lat, item.lng));
                infowindow.open(map, marker);
               // map.setZoom(item.zoom); 
            });
        });
        
        checkUrl();	//check if there is a value for country in url
        
        $("ul.listMarkers a").live("click", function(){
            var i = $(this).attr("rel");
            for (x = 0; x < arrInfoWindows.length; x++) {
                arrInfoWindows[x].close();
				//arrMarkers[x].setVisib1le(true);
            }
            arrInfoWindows[i].open(map, arrMarkers[i]);
        }); 
		google.maps.event.addListener(map, 'zoom_changed', function(){		
			 var Country = $(".selectCountry").val();
	         var Activity = $(".selectActivity").val();
	         sortMarkers(Country,Activity);
        });  
		
        $(".selectActivity").change(function(){ // filter for activity
            var Country = $(".selectCountry").val();
            var Activity = $(".selectActivity").val();
            sortMarkers(Country,Activity);
        });
        
        $(".selectCountry").change(function(){ // filter for country
        	var Country = $(".selectCountry").val(); // value de country 
        	var countryName = $(".selectedTxt").html(); // name of the country
        	
            var Activity = $(".selectActivity").val();
            
            sortMarkers(Country,Activity);
            if(map.getZoom() == 1){ /*openMarkerCountry(Country);*/ }
          
            codeAddress(countryName);

        });
        
        function sortMarkers(Country,Activity){
        	for (x = 0; x < activity_country.length; x++) { 
        		if( ( map.getZoom() == 1 && activity_country[x][1] == Country && activity_country[x][0] == "country" ) ){
        			arrMarkers[x].setVisible(true); 
        		}
        		else if( ( map.getZoom() == 1 && Country == "0" && Activity == "0" && activity_country[x][0] == "country" ) ){
        			arrMarkers[x].setVisible(true); 
        		}
        		else if( ( map.getZoom() != 1 && activity_country[x][0] == Activity && activity_country[x][1] == Country) | (map.getZoom() != 1 && Country == 0 && Activity == activity_country[x][0]) | (map.getZoom() != 1 && activity_country[x][1] == Country && Activity == 0) | ( map.getZoom() != 1 && Country == 0 && Activity == 0 ) ){
        			arrMarkers[x].setVisible(true); 
        		}
        		else {
                    arrMarkers[x].setVisible(false);
                }
        	 }
        }
        
        function openMarkerCountry(Country){
        	for (x = 0; x < activity_country.length; x++) {
        	   arrInfoWindows[x].close(map, arrMarkers[x]);
        		if( activity_country[x][1] == Country && activity_country[x][0] == "country" ){
        			arrInfoWindows[x].open(map, arrMarkers[x]);
        		}
        	}
        }
        
        function checkUrl() {
        	var url = location.search;
        	if ( url != "" )
        	{
        		var UrlCountryVal = url.split('country=');
        		countrySearch = UrlCountryVal[1];
        		$('.selectCountry').getSetSSValue(countrySearch);
        		var countryName =  $(".selectedTxt").html();
        		var Country = $(".selectCountry").val();
                var Activity = $(".selectActivity").val();
                sortMarkers(Country,Activity);
                codeAddress(countryName);
        	}
        }
        
        function codeAddress(Country) {
            var address = Country;
            var bounds = new google.maps.LatLngBounds();

            geocoder.geocode( { 'address': address}, function(results, status) {
              if (status == google.maps.GeocoderStatus.OK) {
                bounds = results[0].geometry.bounds;
                map.fitBounds(bounds);
              } else {
                //alert("Geocode was not successful for the following reason: " + status);
            }
        }); }
        
    });
}
function show_hide_markers(){
	 for (x = 0; x < arrMarkers.length; x++) {
          arrMarkers[x].setVisible(true);
     }
}
function clearMarkers(){
	 for (x = 0; x < arrMarkers.length; x++) {
          arrMarkers[x].setMap(null);
     }
}
/** ********** print ********************** */
function openPrint(fichier) {
  //var ff=window.open(fichier,"printpage","width=1024,left=30,top=20,scrollbars=yes,menubar=yes,toolbar=yes,resizable=true ");   
  window.print();
 }


/* resize flash */
function expandLoader (tgt) { $("#"+tgt).height (360); $('div.animation').animate({ height: '360' }, 500, function() { }); }
function collapseLoader (tgt) { $("#"+tgt).height (130); $('div.animation').animate({ height: '130' }, 500, function() { }); }
function testExpand () {expandLoader('loader');  }
function testCollapse () {collapseLoader('loader'); }
