/*--------------------------------------------------------------------
 * 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
--------------------------------------------------------------------*/

jQuery(document).ready(function () {
	// soluciona warning en FB
	jQuery('.fb-comments').attr("href",window.location.href);

	// Elimina clase floatedPhoto, cuando la foto es la principal de una página, para que no salga el marco
	jQuery('.divPageSplashMain').find('div.fprte:first').each(function (i) {
			jQuery(this).removeClass("floatedPhoto");
			jQuery(this).children('.floatedPhotoImagebox').children(".photoCredit").html( jQuery(this).children(".photoCaption").html() );
			jQuery(this).children(".photoCaption").remove();
		});
	
	// Fotos con marco en página // #openDoc
 jQuery(".fprte").each(function (i) {
 		imagen = jQuery(this).find('img');
 		jQuery(this).width( imagen.width() );
 		
 	 	if( jQuery(this).width() < jQuery(this).height() ){
 			jQuery(this).removeClass("floatedPhoto").addClass("floatedPhotoHor");
 		}
 		photoCredit = (jQuery(this).children('.floatedPhotoImagebox').children('a').size() > 0)? jQuery(this).children('.floatedPhotoImagebox').children('a').children('img').attr("alt") : jQuery(this).children('.floatedPhotoImagebox').children('img').attr("alt");
 		jQuery(this).children('.floatedPhotoImagebox').children(".photoCredit").html( photoCredit );
    if( jQuery(this).children(".photoCaption").is(':empty') ) {
    	photoCaption = (jQuery(this).children('.floatedPhotoImagebox').children('a').size() > 0)? jQuery(this).children('.floatedPhotoImagebox').children('a').children('img').attr("title") : jQuery(this).children('.floatedPhotoImagebox').children('img').attr("title");
    	jQuery(this).children(".photoCaption").html( photoCaption );	
    }

  });

 // genera link de "leer más" en los listados de noticias que tengan header h4 o h5
 jQuery(".tt_news_more").each(function (i) {
   $cond=jQuery(this).parent().children('h4').children('a').attr("href");
        if($cond!=null)
            jQuery(this).attr("href",$cond);
        else
           jQuery(this).hide();
  });

 // Agrega #tx_ablinklist_hsw al final del enlace para pasar indicar el div contenedor que se debe utilizar en los detalles
 jQuery(".tx_ablinklist_catalogDetails").each(function (i) {
 		jQuery(this).children('a').attr("href", jQuery(this).children('a').attr("href") + "#tx_ablinklist_hsw");
  });
 // Agrega #tx_ablinklist_hsw al final del enlace para pasar indicar el div contenedor que se debe utilizar en los detalles
	jQuery(".tx_ablinklist_searchView_linkDetails").each(function (i) {
		jQuery(this).attr("href", jQuery(this).attr("href") + "#tx_ablinklist_hsw");
	});
  
// Most Popular functions
 jQuery("#mostPopularSections").hide();
 jQuery("#liDocumentsLink").click(function() { 
  jQuery("#mostPopularSections").hide();
  jQuery("#mostPopularDocuments").show();
  jQuery("#liDocuments").addClass("current");
  jQuery("#liSections").removeClass("current");  
  return false;
 });   
 jQuery("#liSectionsLink").click(function() { 
  jQuery("#mostPopularDocuments").hide();
  jQuery("#mostPopularSections").show();
  jQuery("#liSections").addClass("current");
  jQuery("#liDocuments").removeClass("current");    
  return false;
 });
 
 // elimina margen derecho de columnas de componentes
 jQuery("div.divPageStandardMain div#pageComponents div.firstBox").each(function(i){
 		jQuery(this).children("div.csc-default:odd").css('margin-right', '0');
 });
 
 // CHECK TO SEE IF SPOTLIGHT IS PRESENT
	var spotlight = jQuery('div#Spotlight_area').length;
	if (spotlight!=0) {
		 var contadorp = 0;
		 // genera link de "leer más" en los items del spotlight
		 jQuery("div#Spotlight_area div#Spotlight_container div#Spotlight_holder div.Spotlight_item").each(function (i) {
		 		if(jQuery(this).is(':empty')){
		 			jQuery(this).remove();
		 		}
		 		else {
		 			contadorp++;
		 			// temp-fix
		 			//jQuery(this).find('div.Spotlight_pad').append('<div class="Spotlight_pad_dots">...</div>');
		 			
		    	enlace = jQuery(this).find('div.Spotlight_pad a');
		    	imagen = jQuery(this).find('img');
		    	leer_mas = jQuery(this).find('div.read_more a');
		    	
		    	imagen.wrap(jQuery("<a/>").attr("href", enlace.attr("href") ));
		    	leer_mas.attr("href", enlace.attr("href"));
		    }
		  });
		
		// remueve paginas no utilizadas
		if( (contadorp / 3) != 4){
			inicio = Math.ceil(contadorp / 3) + 1;
			for(i=inicio; i<=4; i++){
				jQuery('div#Spotlight_area div#Spotlight_pips a#pip_'+i).remove();
			}
		}

		// GET NUMBER OF SPOTLIGHT PAGES
		var pages = parseInt(jQuery("div#Spotlight_pips a:last-child").attr('id').replace('pip_',''));
		var page = 1;
		var current_page = 1;

		// DISPLAY SPOTLIGHT BUTTONS
		jQuery("a#pip_1").addClass('on');
		jQuery("a.pip").css({"display":"inline-block"});
		jQuery("a#btnLeft").css({"display":"none"});
		jQuery("a#btnRight").css({"display":"block"});

		// SETUP SPOTLIGHT CLICK FUNCTIONS
		jQuery("a#btnLeft").click(function() {
			page = (current_page - 1);
			go_to(page,current_page,pages);
			current_page = page;
			return false;
		});
		jQuery("a#btnRight").click(function() {
			page = (current_page + 1);
			go_to(page,current_page,pages);
			current_page = page;
			return false;
		});
		jQuery("a.pip").click(function() {
			var page = parseInt(jQuery(this).attr('id').replace('pip_',''));
			if (page == current_page) {
				// DO NOTHING!
				return false;
			} else {
				go_to(page,current_page,pages);
				current_page = page;
			}
			return false;
		});
	}
 
// Document view functions
 jQuery("#textSize100").click(function() {textSize('100'); return false;});
 jQuery("#textSize110").click(function() {textSize('110'); return false;});
 jQuery("#textSize120").click(function() {textSize('120'); return false;});
 jQuery("#textSize130").click(function() {textSize('130'); return false;});
 
//Archive collection
 jQuery(".arch-link > a").each(function (i) {
    var myVal = jQuery(this).html(); 
    var myId = "#" + this.className;
    jQuery(myId).hide();
    var myUrl = this.href; 
    myUrl = myUrl.replace("\?page\=4a2cc4cc6", "\/do_archives_series\.html\?");
    var sId = myId.replace("arch-", "");
    jQuery(this).click(function() {
        jQuery(myId).toggle();
        jQuery(myId).load(myUrl);
        return false;
    });
  });
   
// Country page notes   
 jQuery("#countryNotes").hide();
 var noteVal = jQuery("#countryNotesGet").html();
 jQuery("#countryNotesGet").html(noteVal + '<img src="images/plus.gif">'); 
 jQuery("#countryNotesGet").click(function() {
    if (jQuery("#countryNotes").is(":hidden")) {
      jQuery("#countryNotes").show();
      var noteVal = jQuery("#countryNotesGet > span").html();
      jQuery("#countryNotesGet").html('<span>' + noteVal + '</span><img src="images/minus.gif">' );
    } else {
      jQuery("#countryNotes").hide();
      var noteVal = jQuery("#countryNotesGet > span").html();
      jQuery("#countryNotesGet").html('<span>' + noteVal + '</span><img src="images/plus.gif">');      
    }
 }); 
 jQuery(".infoBox > a").each(function (i) {
    var infoId = "#" + this.className;
    jQuery(this).mouseover(function() {
        jQuery(infoId).removeClass();
        jQuery(infoId).addClass("info");
    });
    jQuery(this).mouseout(function() {
        jQuery(infoId).removeClass();
        jQuery(infoId).addClass("info hidTag");
    });
  });

// latest contributions carousel FIX
/*  jQuery('#contributions').jcarousel({
      vertical: true,
      scroll:4
  });*/

// Country page GNA notes
 jQuery(".GNADiv").hide();
 var moreVal = 'Read more';
 jQuery("#gnaMore").html('<span>' + moreVal + '</span><img src="images/plus.gif" alt="Read more expand">');
 jQuery("#gnaMore").click(function() {
    var moreDiv = jQuery("#gnaMore").attr("class");
    moreDiv = "#" + moreDiv;
    if (jQuery(moreDiv).is(":hidden")) {
      jQuery(moreDiv).show();
      var moreVal = jQuery("#gnaMore > span").html();
      jQuery("#gnaMore").html('<span>' + moreVal + '</span><img src="images/minus.gif" alt="Read more contract">' );
    } else {
      jQuery(moreDiv).hide();
      var moreVal = jQuery("#gnaMore > span").html();
      jQuery("#gnaMore").html('<span>' + moreVal + '</span><img src="images/plus.gif" alt="Read more expand">');
    }
 });  
   
  // Pull quotes
 jQuery("blockquote.pullquote > p:first-child").prepend('<img class="start" src="images/quote_start.gif">');
 jQuery("blockquote.pullquote > p.by").prepend('<span class="arial"> &#8211; </span>');   
 jQuery("blockquote.pullquote > p.by").prev("p").append('<img class="end" src="images/quote_end.gif">'); 
});

// SPOTLIGHT FUNCTION TO SCROLL SPOTLIGHT AREA
function go_to(page,current_page,pages) {

	// CATCH ALL
	if (page < 1) {
		page = 1;
	} else if (page > pages) {
		page = pages;
	}
	
	if (current_page == 1) {

		// LEAVING FIRST PAGE, ACTIVATE THE LEFT TAB
		jQuery("a#btnLeft").css({"display":"block"});

	} else if (current_page == pages) {

		// LEAVING LAST PAGE, ACTIVATE THE RIGHT TAB
		jQuery("a#btnRight").css({"display":"block"});

	}

	jQuery("div#Spotlight_pips a").removeClass('on');
	jQuery("a#pip_"+page).addClass('on');

	var left = (0 - ((page * 618) - 618));
	jQuery("div#Spotlight_holder").animate(
		{"left": left + "px"},
		"normal",
		"linear",
		function() {
			if (page == 1) {

				// DISABLE THE LEFT TAB
				jQuery("a#btnLeft").css({"display":"none"});

			} else if (page == pages) {

				// DISABLE THE RIGHT TAB
				jQuery("a#btnRight").css({"display":"none"});
			}
		}
	);
}

// fuction used to change text sizes in open document template
function textSize(MyNum) {
    jQuery("#openDoc").css("font-size", MyNum + '%');
}

$.fn.equalHeights = function(px) {
	jQuery(this).each(function(){
		var currentTallest = 0;
		jQuery(this).children().each(function(i){
			if (jQuery(this).height() > currentTallest) { currentTallest = jQuery(this).height(); }
		});
		// for ie6, set height since min-height isn't supported
		if ($.browser.msie && $.browser.version == 6.0) { jQuery(this).children().css({'height': currentTallest}); }
		jQuery(this).children().css({'min-height': currentTallest}); 
	});
	return this;
};

function checkColumns(){
	var hH1 = jQuery("#header").outerHeight();
	var hC1 = jQuery("#content").outerHeight() + jQuery("#header").outerHeight();
	var hC2 = jQuery("#rightColumn").outerHeight();
	
	if(hC2 > hC1){
		jQuery("#content").height((hC2 - hH1) + "px");
	}
}

function autoCenterFooter(){
	var totalWidth = 0;
	jQuery("#footerHolder li").each(function(){
		totalWidth = jQuery(this).outerWidth() + totalWidth;
	});
	
	jQuery("#footerHolder").width(totalWidth + "px");
}

jQuery(function(){
		jQuery('#txtSearch').keyup(function(e) {
			if(e.keyCode == 13) {
				jQuery("#tx_acnurmodalwjquery_pi1_modal").click(); // al presionar ENTER hace click en la imagen de busqueda
			}
		});
	
	jQuery("ul.sf-menu").superfish({
		autoArrows: false,
		dropShadows: false
	});
	
	if(document.getElementById("divStorySwap")){
	
	  // SPLASH TABS onCLICK FUNCTION
		jQuery("#divStoryNav a").click(function(){
			var s = jQuery(this);
			if(!s.parent("div").hasClass("selected")){
				var i = s.attr("id");
				i = i.charAt(9);
				jQuery("#divStoryNav div").attr("class","disabled");
				jQuery(".divStoryContainer").each(function(){
					if(jQuery(this).css("display") == "block"){
						jQuery(this).fadeOut("normal");
					}
				});
				jQuery("#divStory" + i).fadeIn("normal");
				s.parent("div").removeClass("disabled");
				s.parent("div").addClass("selected");
				return false;
			}
		});
		
		// SPLASH TABS onMOUSEOVER FUNCTION	
		jQuery("#divStoryNav a").mouseover(function(){
			var y = jQuery(this);
			if(y.parent("div").hasClass("disabled")){
          y.parent("div").removeClass("disabled");
			}
		});
		
		// SPLASH TABS onMOUSEOUT FUNCTION
		jQuery("#divStoryNav a").mouseout(function(){
			var y = jQuery(this);
			if ((!y.parent("div").hasClass("disabled")) && (!y.parent("div").hasClass("selected"))) {
			    y.parent("div").addClass("disabled");
			}
		});
		
	}
	
	// BORRAR
	if(document.getElementById("divSpotlightSlide")){
		var numPanels = jQuery(".divSpotlightPane").length;
		var holderWidth = (numPanels * 307);
		jQuery("#divSpotlightSlideHolder").css("width", holderWidth + "px")
		
		jQuery("#btnSlideLeft").click(function(){
			if(jQuery("#btnSlideLeft").hasClass("enabled") && jQuery("#divSpotlightSlideHolder").css("left") != "0px"){
				jQuery("#btnSlideLeft").removeClass("enabled");
				jQuery("#divSpotlightSlideHolder").animate(
					{"left": "+=307px"},
					"normal",
					"linear",
					function(){
						jQuery("#btnSlideLeft").addClass("enabled");
					}
				);
			}
		return false;
		});
		jQuery("#btnSlideRight").click(function(){
			if(jQuery("#btnSlideRight").hasClass("enabled") && jQuery("#divSpotlightSlideHolder").css("left") != ("-" + (holderWidth - 614) + "px") ){
				jQuery("#btnSlideRight").removeClass("enabled");
				jQuery("#divSpotlightSlideHolder").animate(
					{"left": "-=307px"},
					"normal",
					"linear",
					function(){
						jQuery("#btnSlideRight").addClass("enabled");
					}
				);
			}
		return false;
		});
	}
	
	if(document.getElementById("divSpotlightSlide")){
		jQuery("#secondary").equalHeights();
	}
	
	jQuery("#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";
	}
}
// FIX
/*jQuery(function() {
	jQuery('a.lightbox').lightBox();
	jQuery('.gallery a').lightBox();	
});*/
//======= WORLD MAP ROLL-OVER ===========
addEvent(window, 'load', mainOnLoad);
function mainOnLoad() {
    preloadImages(
    	'images/worldmap_africa.gif',
    	'images/worldmap_asia.gif',
    	'images/worldmap_europe.gif',
    	'images/worldmap_america.gif',
    	'images/worldmap_mena.gif'
    );
    // Apply javascript to rollover map
    if(!document.getElementById) { return; }
    
    jQuery("#m_americas").mouseover(function(){SwapImage ('document.map','document.map','images/worldmap_americas.gif','#2');});
    jQuery("#m_americas").mouseout(SwapImgRestore);
    
    jQuery("#m_africa").mouseover(function(){SwapImage('document.map','document.map','images/worldmap_africa.gif','#3');});
    jQuery("#m_africa").mouseout(SwapImgRestore);
    
    jQuery("#m_europe").mouseover(function(){SwapImage('document.map','document.map','images/worldmap_europe.gif','#4');});
    jQuery("#m_europe").mouseout(SwapImgRestore);
    
    jQuery("#m_asia").mouseover(function(){SwapImage('document.map','document.map','images/worldmap_asia.gif','#5');});
    jQuery("#m_asia").mouseout(SwapImgRestore);
    
    jQuery("#m_mena").mouseover(function(){SwapImage('document.map','document.map','images/worldmap_mena.gif','#6');});
    jQuery("#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;
}
