/*-------------------------------------------------------------------- 
 * 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');});

 // News images fix
 $("#openDoc img").parent("p").addClass("inline");
 $("#openDoc img").parent("a").parent("p").addClass("inline");
 
 
	// Donate page buttons
	$(".donate-form").click(function() {
		$(this).find("form").submit();
	}); 


 

// Mini galleries should browse only their own images
 $('.tx-wtgallery-pi1').each(function() {
      var rand = Math.floor(Math.random()*99999);
      //alert(rand);

      $(this).find('a.lightbox').each(function() {
          $(this).attr("rel", "lightbox" + rand);
     });

 });
 


   // Highlight first two word of headings
 /* $('#divPageSplash .regular-left h2,#divStorySwap h2,.wtgallery_gallerydesc_wrapper h3').each(function(){
    var me = $(this)
       , t = me.text().split(' ');
    me.html( '<span class="hilite">'+t.shift()+" "+t.shift()+'</span> '+t.join(' ') );
  }); */

 

 
//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/plus.gif">'); 
 $("#countryNotesGet").click(function() {
    if ($("#countryNotes").is(":hidden")) {
      $("#countryNotes").show();
      var noteVal = $("#countryNotesGet > span").html();
      $("#countryNotesGet").html('<span>' + noteVal + '</span><img src="images/minus.gif">' );
    } else {
      $("#countryNotes").hide();
      var noteVal = $("#countryNotesGet > span").html();
      $("#countryNotesGet").html('<span>' + noteVal + '</span><img src="images/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 = 'Read more';
 $("#gnaMore").html('<span>' + moreVal + '</span><img src="images/plus.gif" alt="Read more expand">');
 $("#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/minus.gif" alt="Read more contract">' );
    } else {
      $(moreDiv).hide();
      var moreVal = $("#gnaMore > span").html();
      $("#gnaMore").html('<span>' + moreVal + '</span><img src="images/plus.gif" alt="Read more expand">');      
    }
 });  
   
  // Pull quotes
 $("blockquote.pullquote > p:first-child").prepend('<img class="start" src="images/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/quote_end.gif">');

 

});

// 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 + 12) + "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") == "enter search"){
			$(this).attr("value", "");
		}
	});
	$("#txtSearch").blur(function(){
		if($(this).attr("value") == ""){
			$(this).attr("value", "enter search");
		}
	});
	
	$("ul.sf-menu").superfish({
		autoArrows: false,
		dropShadows: false
	});
	
	if(document.getElementById("divStorySwap")){
	
	  // SPLASH TABS onCLICK FUNCTION
		$("#divStoryNav a").click(function(e){

                    e.preventDefault();
                    
			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(e){
			e.preventDefault();
			if($("#btnSlideLeft").hasClass("enabled") && $("#divSpotlightSlideHolder").css("left") != "0px"){
				$("#btnSlideLeft").removeClass("enabled");
				$("#divSpotlightSlideHolder").animate(
					{"left": "+=307px"},
					"normal",
					"linear",
					function(){
						$("#btnSlideLeft").addClass("enabled");
					}
				);
			}
		});  
		$("#btnSlideRight").click(function(e){
			e.preventDefault();
			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').nyroModal({addImageDivTitle: true});	
	$('.gallery a').nyroModal({addImageDivTitle: true});

	
});





//======= WORLD MAP ROLL-OVER ===========
addEvent(window, 'load', mainOnLoad);
function mainOnLoad() {
    preloadImages(
    	'fileadmin/templates/img/worldmap_africa.gif',
    	'fileadmin/templates/img/worldmap_asia.gif',
    	'fileadmin/templates/img/worldmap_europe.gif',
    	'fileadmin/templates/img/worldmap_america.gif',
    	'fileadmin/templates/img/worldmap_mena.gif'
    );
    // Apply javascript to rollover map
    if(!document.getElementById) { return; }
    
    $("#m_americas").mouseover(function(){SwapImage ('document.map','document.map','fileadmin/templates/img/worldmap_americas.gif','#2');});
    $("#m_americas").mouseout(SwapImgRestore);
    
    $("#m_africa").mouseover(function(){SwapImage('document.map','document.map','fileadmin/templates/img/worldmap_africa.gif','#3');});
    $("#m_africa").mouseout(SwapImgRestore);
    
    $("#m_europe").mouseover(function(){SwapImage('document.map','document.map','fileadmin/templates/img/worldmap_europe.gif','#4');});
    $("#m_europe").mouseout(SwapImgRestore);
    
    $("#m_asia").mouseover(function(){SwapImage('document.map','document.map','fileadmin/templates/img/worldmap_asia.gif','#5');});
    $("#m_asia").mouseout(SwapImgRestore);
    
    $("#m_mena").mouseover(function(){SwapImage('document.map','document.map','fileadmin/templates/img/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;
}
