/*-------------------------------------------------------------------- 
 * JQuery Plugin: "EqualHeights"
 * by:	Scott Jehl, Todd Parker, Maggie Costello Wachs (http://www.filamentgroup.com)
 *
 * Copyright (c) 2008 Filament Group
 * Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)
 *
 * Description: Compares the heights or widths of the top-level children of a provided element 
 		and sets their min-height to the tallest height (or width to widest width).
 * Version: 2.0, 08.01.2008
--------------------------------------------------------------------*/

$(document).ready(function () {

// Most Popular functions
 $("#mostPopularSections").hide();
 $("#liDocumentsLink").click(function() { 
  $("#mostPopularSections").hide();
  $("#mostPopularDocuments").show();
  $("#liDocuments").addClass("current");
  $("#liSections").removeClass("current");  
  return false;
 });   
 $("#liSectionsLink").click(function() { 
  $("#mostPopularDocuments").hide();
  $("#mostPopularSections").show();
  $("#liSections").addClass("current");
  $("#liDocuments").removeClass("current");    
  return false;
 });   
 
// Document view functions
 $("#textSize100").click(function() {textSize('100');});
 $("#textSize110").click(function() {textSize('110');});
 $("#textSize120").click(function() {textSize('120');});
 $("#textSize130").click(function() {textSize('130');});
 
//Archive collection
 $(".arch-link > a").each(function (i) {
    var myVal = $(this).html(); 
    var myId = "#" + this.className;
    $(myId).hide();
    var myUrl = this.href; 
    myUrl = myUrl.replace("\?page\=4a2cc4cc6", "\/do_archives_series\.html\?");
    var sId = myId.replace("arch-", "");
    $(this).click(function() {
        $(myId).toggle();
        $(myId).load(myUrl);
        return false;
    });
  });
   
// Country page notes   
 $("#countryNotes").hide();
 var noteVal = $("#countryNotesGet").html();
 $("#countryNotesGet").html(noteVal + '<img src="/images/furniture/plus.gif">'); 
 $("#countryNotesGet").click(function() {
    if ($("#countryNotes").is(":hidden")) {
      $("#countryNotes").show();
      var noteVal = $("#countryNotesGet > span").html();
      $("#countryNotesGet").html('<span>' + noteVal + '</span><img src="/images/furniture/minus.gif">' );
    } else {
      $("#countryNotes").hide();
      var noteVal = $("#countryNotesGet > span").html();
      $("#countryNotesGet").html('<span>' + noteVal + '</span><img src="/images/furniture/plus.gif">');      
    }
 }); 
 $(".infoBox > a").each(function (i) {
    var infoId = "#" + this.className;
    $(this).mouseover(function() {
        $(infoId).removeClass();
        $(infoId).addClass("info");
    });
    $(this).mouseout(function() {
        $(infoId).removeClass();
        $(infoId).addClass("info hidTag");
    });    
  });

// latest contributions carousel
  jQuery('#contributions').jcarousel({
      vertical: true,
      scroll:4
  });

// Country page GNA notes   
 $(".GNADiv").hide();
 var moreVal = 'Lire la suite';
 $("#gnaMore").html('<span>' + moreVal + '</span><img src="/images/furniture/plus.gif" alt="Lire la suite - agrandir">');
 $("#gnaMore").click(function() {
    var moreDiv = $("#gnaMore").attr("class");
    moreDiv = "#" + moreDiv;
    if ($(moreDiv).is(":hidden")) {
      $(moreDiv).show();
      var moreVal = $("#gnaMore > span").html();
      $("#gnaMore").html('<span>' + moreVal + '</span><img src="/images/furniture/minus.gif" alt="Lire la suite - reduire">' );
    } else {
      $(moreDiv).hide();
      var moreVal = $("#gnaMore > span").html();
      $("#gnaMore").html('<span>' + moreVal + '</span><img src="/images/furniture/plus.gif" alt="Lire la suite - agrandir">');      
    }
 });  
   
  // Pull quotes
 $("blockquote.pullquote > p:first-child").prepend('<img class="start" src="/images/furniture/quote_start.gif">');
 $("blockquote.pullquote > p.by").prepend('<span class="arial"> &#8211; </span>');   
 $("blockquote.pullquote > p.by").prev("p").append('<img class="end" src="/images/furniture/quote_end.gif">'); 

 // What's New page
 $("div#expand_header_stats").click(function() { hide_show_content("div#expand_content_stats","div#expand_button_stats")});
  
 $("div#expand_content_admin").hide();
 $("div#expand_header_admin").click(function() { hide_show_content("div#expand_content_admin","div#expand_button_admin")});
 
 $("div#expand_content_basics").hide();
 $("div#expand_header_basics").click(function() { hide_show_content("div#expand_content_basics","div#expand_button_basics")});
 
 $("div#expand_content_excom").hide();
 $("div#expand_header_excom").click(function() { hide_show_content("div#expand_content_excom","div#expand_button_excom")});

 $("div#expand_content_help").hide();
 $("div#expand_header_help").click(function() { hide_show_content("div#expand_content_help","div#expand_button_help")});
     
 $("div#expand_content_difference").hide();
 $("div#expand_header_difference").click(function() { hide_show_content("div#expand_content_difference","div#expand_button_difference")});
 
 $("div#expand_content_news").hide();
 $("div#expand_header_news").click(function() { hide_show_content("div#expand_content_news","div#expand_button_news")});
 
 $("div#expand_content_partners").hide();
 $("div#expand_header_partners").click(function() { hide_show_content("div#expand_content_partners","div#expand_button_partners")});
  
 $("div#expand_content_publications").hide();
 $("div#expand_header_publications").click(function() { hide_show_content("div#expand_content_publications","div#expand_button_publications")});
   
 $("div#expand_content_protection").hide();
 $("div#expand_header_protection").click(function() { hide_show_content("div#expand_content_protection","div#expand_button_protection")});
 
 $("div#expand_content_research").hide();
 $("div#expand_header_research").click(function() { hide_show_content("div#expand_content_research","div#expand_button_research")});

 $("div#expand_content_agenda").hide();
 $("div#expand_header_agenda").click(function() { hide_show_content("div#expand_content_agenda","div#expand_button_agenda")});
    
 $("div#expand_content_events").hide();
 $("div#expand_header_events").click(function() { hide_show_content("div#expand_content_events","div#expand_button_events")});
  
 $("div#expand_content_human_story").hide();
 $("div#expand_header_human_story").click(function() { hide_show_content("div#expand_content_human_story","div#expand_button_human_story")});

 //Inline galleries start
 var $oldGid='';
 $('body').append("<div id='inlineGalleries' style='height:0px;display:none;'></div>");
 $("a.inlineGallery").each(function (i) {
    $gid = "";
    $gid = this.href.substr(this.href.indexOf("gallery-")+8);
    $gid = $gid.replace(".html", "");
    //alert($gid +" "+$oldGid);
    if ($gid != $oldGid){
        $gidb=$gid;
        $("#inlineGalleries").append("<div id='inlineGid-" + $gid + "'></div>");        
        $("#inlineGalleries #inlineGid-"+$gid).load("home/do_inline_gallery.html?comid="+$gid, function(){
        $('#inlineGalleries .gallery a').lightBox();
      });
    } 
    $(this).click(function() {
      $gid = this.href.substr(this.href.indexOf("gallery-")+8);
      $gid = $gid.replace(".html", "");    
      if ($gid.length > 0){
        $("#inlineGalleries #inlineGid-"+ $gid +" a:first").trigger('click');
        return false;
      }
    });
    $oldGid=$gid;
  });
  //Inline galleries end

  //Inline videos start
  var $oldVid='';
  $('body').append("<div id='inlineVideosHP' style='height:0px;display:none;'></div>");
  $("a.inlineVideo").each(function (i) {
      $vid = "";
      $vid = this.href.substr(this.href.indexOf(".html#")+6);
      //alert($vid);
      if ($vid != $oldVid){
          $("#inlineVideosHP").append("<div id='b-" + $vid + "'></div>");
          $("#inlineVideosHP #b-"+$vid).load("home/do_inline_video.html?mlibid="+$vid);
      }
      $(this).colorbox({width:"678px", inline:true, href:"#b-"+$vid});      
      $oldVid=$vid;
  });
  //Inline videos end
});

function hide_show_content(content,button) {
 	if ($(content).is(":hidden")) {
		$(content).slideDown("normal");
		$(button).html('<img src="/images/furniture/expd_button_close.jpg">');
	} else {
		$(content).slideUp("normal");
		$(button).html('<img src="/images/furniture/expd_button.jpg">');
	} 
}

// fuction used to change text sizes in open document template
function textSize(MyNum) {
    $("#openDoc").css("font-size", MyNum + '%');
}

$.fn.equalHeights = function(px) {
	$(this).each(function(){
		var currentTallest = 0;
		$(this).children().each(function(i){
			if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
		});
		// for ie6, set height since min-height isn't supported
		if ($.browser.msie && $.browser.version == 6.0) { $(this).children().css({'height': currentTallest}); }
		$(this).children().css({'min-height': currentTallest}); 
	});
	return this;
};

function checkColumns(){
	var hH1 = $("#header").outerHeight();
	var hC1 = $("#content").outerHeight() + $("#header").outerHeight();
	var hC2 = $("#rightColumn").outerHeight();
	
	if(hC2 > hC1){
		$("#content").height((hC2 - hH1) + "px");
	}
}

function autoCenterFooter(){
	var totalWidth = 0;
	$("#footerHolder li").each(function(){
		totalWidth = $(this).outerWidth() + totalWidth;
	});
	
	$("#footerHolder").width(totalWidth + "px");
}

$(function(){
	$("#txtSearch").click(function(){
		if($(this).attr("value") == "saisir votre recherche"){
			$(this).attr("value", "");
		}
	});
	$("#txtSearch").blur(function(){
		if($(this).attr("value") == ""){
			$(this).attr("value", "saisir votre recherche");
		}
	});
	
	$("ul.sf-menu").superfish({
		autoArrows: false,
		dropShadows: false
	});
	
	if(document.getElementById("divStorySwap")){
	
	  // SPLASH TABS onCLICK FUNCTION
		$("#divStoryNav a").click(function(){
			var s = $(this);
			if(!s.parent("div").hasClass("selected")){
				var i = s.attr("id");
				i = i.charAt(9);
				$("#divStoryNav div").attr("class","disabled");
				$(".divStoryContainer").each(function(){
					if($(this).css("display") == "block"){
						$(this).fadeOut("normal");
					}
				});
				$("#divStory" + i).fadeIn("normal");
				s.parent("div").removeClass("disabled");
				s.parent("div").addClass("selected");
				return false;
			}
		});
		
		// SPLASH TABS onMOUSEOVER FUNCTION	
		$("#divStoryNav a").mouseover(function(){
			var y = $(this);
			if(y.parent("div").hasClass("disabled")){
          y.parent("div").removeClass("disabled");
			}
		});
		
		// SPLASH TABS onMOUSEOUT FUNCTION
		$("#divStoryNav a").mouseout(function(){
			var y = $(this);
			if ((!y.parent("div").hasClass("disabled")) && (!y.parent("div").hasClass("selected"))) {
			    y.parent("div").addClass("disabled");
			}
		});
		
	}
	
	if(document.getElementById("divSpotlightSlide")){
		var numPanels = $(".divSpotlightPane").length;
		var holderWidth = (numPanels * 307);
		$("#divSpotlightSlideHolder").css("width", holderWidth + "px")
		
		$("#btnSlideLeft").click(function(){
			if($("#btnSlideLeft").hasClass("enabled") && $("#divSpotlightSlideHolder").css("left") != "0px"){
				$("#btnSlideLeft").removeClass("enabled");
				$("#divSpotlightSlideHolder").animate(
					{"left": "+=307px"},
					"normal",
					"linear",
					function(){
						$("#btnSlideLeft").addClass("enabled");
					}
				);
			}
		});
		$("#btnSlideRight").click(function(){
			if($("#btnSlideRight").hasClass("enabled") && $("#divSpotlightSlideHolder").css("left") != ("-" + (holderWidth - 614) + "px") ){
				$("#btnSlideRight").removeClass("enabled");
				$("#divSpotlightSlideHolder").animate(
					{"left": "-=307px"},
					"normal",
					"linear",
					function(){
						$("#btnSlideRight").addClass("enabled");
					}
				);
			}
		});
	}
	
	if(document.getElementById("divSpotlightSlide")){
		$("#secondary").equalHeights();
	}
	
	$("#footer li:last").css("padding-right","0px");
	
	checkColumns();
	
	autoCenterFooter();
});

// Photo Galleries
function MenuHoverEffect(element, status) {
	if (document.getElementById) {
		$li = document.getElementById(element);
	} else if (document.all) {
		$li = document.all[element];
	} else if (document.layers) {
		$li = document.layers[element];
	}
	if(status == true) {
		$li.className = "highlight";
	} else {
		$li.className = "none";
	}
}
$(function() {
	$('a.lightbox').lightBox();
	$('.gallery a').lightBox();	
	$('.photo_gallery a').lightBox();	
});
//======= WORLD MAP ROLL-OVER ===========
addEvent(window, 'load', mainOnLoad);
function mainOnLoad() {
    preloadImages(
    	'/images/furniture/worldmap_africa.gif',
    	'/images/furniture/worldmap_asia.gif',
    	'/images/furniture/worldmap_europe.gif',
    	'/images/furniture/worldmap_america.gif',
    	'/images/furniture/worldmap_mena.gif'
    );
    // Apply javascript to rollover map
    if(!document.getElementById) { return; }
    
    $("#m_americas").mouseover(function(){SwapImage ('document.map','document.map','/images/furniture/worldmap_americas.gif','#2');});
    $("#m_americas").mouseout(SwapImgRestore);
    
    $("#m_africa").mouseover(function(){SwapImage('document.map','document.map','/images/furniture/worldmap_africa.gif','#3');});
    $("#m_africa").mouseout(SwapImgRestore);
    
    $("#m_europe").mouseover(function(){SwapImage('document.map','document.map','/images/furniture/worldmap_europe.gif','#4');});
    $("#m_europe").mouseout(SwapImgRestore);
    
    $("#m_asia").mouseover(function(){SwapImage('document.map','document.map','/images/furniture/worldmap_asia.gif','#5');});
    $("#m_asia").mouseout(SwapImgRestore);
    
    $("#m_mena").mouseover(function(){SwapImage('document.map','document.map','/images/furniture/worldmap_mena.gif','#6');});
    $("#m_mena").mouseout(SwapImgRestore);
}
function preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.p) d.p=new Array();
    var i,j=d.p.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.p[j]=new Image; d.p[j++].src=a[i];}}
}
function SwapImage() {
  var i,j=0,objStr,obj,swapArray=new Array,oldArray=document.SwapImgData;
  for (i=0; i < (SwapImage.arguments.length-2); i+=3) {
    objStr = SwapImage.arguments[(navigator.appName == 'Netscape')?i:i+1];
    if ((objStr.indexOf('document.layers[')==0 && document.layers==null) ||
        (objStr.indexOf('document.all[')   ==0 && document.all   ==null))
      objStr = 'document'+objStr.substring(objStr.lastIndexOf('.'),objStr.length);
    obj = eval(objStr);
    if (obj != null) {
      swapArray[j++] = obj;
      swapArray[j++] = (oldArray==null || oldArray[j-1]!=obj)?obj.src:oldArray[j];
      obj.src = SwapImage.arguments[i+2];
  } }
  document.SwapImgData = swapArray; //used for restore
}
function SwapImgRestore() { //v2.0
  if (document.SwapImgData != null)
    for (var i=0; i<(document.SwapImgData.length-1); i+=2)
      document.SwapImgData[i].src = document.SwapImgData[i+1];
}
function addEvent(obj,type,fn) {
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}
function email(user, domain, ext, subject) {
  location.href = 'mailto:' + user + '@' + domain + '.' + ext + '?subject=' + subject;
}
