var Cookie = {
	set: function(c_name,value,expiredays){
		var exdate = new Date();
		exdate.setDate(exdate.getDate()+expiredays);
		document.cookie= c_name + "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
	},
	get: function(c_name){
		if(document.cookie.length>0){
			c_start=document.cookie.indexOf(c_name + "=");
			if (c_start!=-1){ 
				c_start=c_start + c_name.length+1; 
				c_end=document.cookie.indexOf(";",c_start);
				if (c_end==-1) c_end=document.cookie.length;
					return unescape(document.cookie.substring(c_start,c_end));
			}
		}
		return "";
	}
}

jQuery.prototype.overlay = function(opacity, isLink) {
	var overlay = this.clone();
	var link = this.find('a').attr('href');
	overlay.html('');
	overlay.css('z-index', '1');
	overlay.css('opacity', opacity);
	overlay.addClass('overlay');
	overlay.height(this.height());
	overlay.width(this.width());
	overlay.css('paddingLeft', this.css('paddingLeft'));
	overlay.css('paddingRight', this.css('paddingRight'));
	//overlay.css('filter', 'alpha(opacity=10)');
	this.after(overlay);
	
	this.css('background-color', 'transparent');
	if(isLink) {
		overlay.parent().css('cursor', 'pointer');
		overlay.parent().mouseover(function(event){
			overlay.css('opacity', opacity - .45);
			jQuery(this).find('h2').addClass('black');
		});
		overlay.parent().mouseout(function(event){
			overlay.css('opacity', opacity);
			jQuery(this).find('h2').removeClass('black');
		});
		overlay.parent().click(function(event){
			window.location.replace(link);
		});
	}
	
}

jQuery.prototype.curtain = function() {
	this.height(jQuery(document).height());
	this.width(jQuery(document).width());
	if(Cookie.get('foundation-curtain') == '') {
		this.show();
		var thisObj = this;
		setTimeout(function() {thisObj.slideUp(5000);}, 1000);
		Cookie.set('foundation-curtain', 'yes', 1);
	} else {
		this.hide();
	}
}

jQuery(document).ready(function(){
    var i=0;
    var currentMonth = null;
    
    
    
    jQuery("#content .box .text").each(function() {
    	if(!jQuery('.imageOnly').length) {
	    	var opacity = .85;
	    	var isLink = false;
	    	if(!jQuery(this).parent().hasClass('post-box')) {
	    		opacity = .6;
	    		isLink = true;
	    	}
	    	//if(jQuery('#home').length > 0 || jQuery('.post-teaser').length > 0) opacity = .8;
	    	jQuery(this).overlay(opacity, isLink);
    	}
    });
    
    jQuery('#content .images-list li').each(function() {
    	galleryOverlay = jQuery(document.createElement("div"));
    	galleryOverlay.addClass('gallery-overlay');
    	galleryOverlay.height(jQuery(this).height());
    	galleryOverlay.width(jQuery(this).width());
    	jQuery(this).css('cursor', 'pointer');
    	galleryOverlay.css('opacity', .8);
    	galleryOverlay.css('marginTop', '-' + jQuery(this).height() + 'px');
    	jQuery(this).append(galleryOverlay);
    	jQuery(this).mouseover(function(event){
    		jQuery(this).find('.gallery-overlay').hide();
    	});
    	jQuery(this).mouseout(function(event){
    		jQuery(this).find('.gallery-overlay').show();
    	});
    });
    
    jQuery(".curtain").curtain();
    
    jQuery(".discover").click(function() {
    	jQuery(document).scrollTo('max', 1000);
    	return false;
    });
    
    jQuery('#content table tr').each(function() {
    	if(!jQuery(this).parents('table').hasClass('links')) {
	    	jQuery(this).find('td:first').addClass('first-column');
	    	var text = jQuery(this).find('td:eq(1)').text();
	    	jQuery(this).find('td:eq(1)').text('');
	    	jQuery(this).find('td:eq(1)').append('<span>' + text + '</span>');
    	}
    });
    
    var tdClasses = {0: '', 1: 'cbs', 2: 'foundation', 3: 'global-fund'}
    jQuery('#content table.links td').each(function(index){
    	jQuery(this).addClass(tdClasses[index]);
    });
    
    jQuery("#menu > li").each(
        function(){
            jQuery(this).attr('id', 'bt' + i++);
            if(jQuery(this).find('ul:first').length){
                monClone=jQuery(this).find('ul:first').clone();
                monClone.attr('id', 'submenu_' + jQuery(this).attr('id'));
                monClone.addClass('submenu');
                monClone.attr('style', 'display: none;');
                if(jQuery(this).hasClass('current_page_item') || jQuery(this).hasClass('current_page_ancestor')) {
                    monClone.show();
                }
                jQuery(this).find('ul:first').remove();
                jQuery("#submenucontent").html(monClone);
                jQuery('.submenu').each(function(item) {
                    jQuery(this).find('li:first').addClass('first');
                });
            }
        }
    );
    
    var content = jQuery('#content[class^=submenu_bt]');
    if(content.length) {
    	jQuery('#' + content.attr('class')).show();
    }
    
    jQuery(" #menu li").hover(
        function(){
            if(jQuery(this).attr('id') != 'bt8') {
                jQuery('#menucontent .submenu').each(function(item) {
                    jQuery(this).hide();
                });
                jQuery('#submenu_' + jQuery(this).attr('id')).css('display', 'inline');
            }
        }, 
        function(){}
    );
    
    /*4th level menu*/
    jQuery('.last-level .current_page_item ul').show();
	if(jQuery('.last-level ul .current_page_item').length) {
		jQuery('.last-level ul .current_page_item').parent().show();
	}
})
