/*-------------------------------------------------------------------- 
 * 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 () {

// 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;
 });   
 
// Document view functions
 jQuery("#textSize100").click(function() {textSize('100');});
 jQuery("#textSize110").click(function() {textSize('110');});
 jQuery("#textSize120").click(function() {textSize('120');});
 jQuery("#textSize130").click(function() {textSize('130');});
 
//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
  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">'); 
});

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

jQuery.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 (jQuery.browser.msie && jQuery.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").click(function(){
		if(jQuery(this).attr("value") == "поиск"){
			jQuery(this).attr("value", "");
		}
	});
	jQuery("#txtSearch").blur(function(){
		if(jQuery(this).attr("value") == ""){
			jQuery(this).attr("value", "поиск");
		}
	});
	
	jQuery("ul.sf-menu").superfish({
		autoArrows: false,
		dropShadows: false
	});
	
	if(document.getElementById("divStorySwap")){
	
	jQuery(".divStoryContainer").hide();
	jQuery(".divStoryContainer").eq(0).show();
	
	for(i = 0; i < 4; i++){
		jQuery("#divStoryNav img").eq(i).attr("src", jQuery(".divStoryContainer img").eq(i).attr("src"));
	}
	
	  // 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 = jQuery("#divStoryNav a").index(this);
				jQuery("#divStoryNav div").attr("class","disabled");
				jQuery(".divStoryContainer").each(function(){
					if(jQuery(this).css("display") == "block"){
						jQuery(this).fadeOut("normal");
					}
				});
				jQuery(".divStoryContainer").eq(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");
			}
		});
		
	}
	
	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");
					}
				);
			}
		});
		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");
					}
				);
			}
		});
	}
	
	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) {
		jQueryli = document.getElementById(element);
	} else if (document.all) {
		jQueryli = document.all[element];
	} else if (document.layers) {
		jQueryli = document.layers[element];
	}
	if(status == true) {
		jQueryli.className = "highlight";
	} else {
		jQueryli.className = "none";
	}
}
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;
}
