jQuery(document).ready(function() {
	
	/* Dropdown Menu Hover */
	jQuery('li.dropdown').hover(function() {
		jQuery(this).find('.dropdown-container').css({left: "-63px"});
	},
	function() {
		jQuery(this).find('.dropdown-container').css({left: "-10000px"});
	});
	
	/* Download Link Hover */
	jQuery('#download-treemap1').hover(function() {
		jQuery(this).parent().css({backgroundPosition: '-940px 0'});
	},
	function() {
		jQuery(this).parent().css({backgroundPosition: '0 0'});
	});
	
	jQuery('.single-countryprofile #download-treemap1').hover(function() {
		jQuery(this).parent().css({backgroundPosition: '-620px 0'});
	},
	function() {
		jQuery(this).parent().css({backgroundPosition: '0 0'});
	});
	
	/* View All Link Hover */
	jQuery('.print-link').hover(function() {
		jQuery(this).parent().css({backgroundPosition: '-780px 0'});
	},
	function() {
		jQuery(this).parent().css({backgroundPosition: '0 0'});
	});
	
	/* Print Link Hover */
	jQuery('.view-all').hover(function() {
		jQuery(this).parent().parent().css({backgroundPosition: '-620px 0'});
	},
	function() {
		jQuery(this).parent().parent().css({backgroundPosition: '0 0'});
	});
	
	/* Show Table Button */
	
	jQuery('#table-treemap1').click(function() {
		jQuery('.container-table').slideToggle(function() {
			tm.refresh();
		});
		jQuery(this).toggleClass('table-treemap-selected');
		if(jQuery(this).html() == 'Show Table') { jQuery(this).html('Hide Table'); }
		else { jQuery(this).html('Show Table'); }
	});
	
	/* Table Sorter */
	
	jQuery.tablesorter.addParser({
		id: "commaDigit",
		is: function(s, table) {
			var c = table.config;
			return jQuery.tablesorter.isDigit(s.replace(/,/g, ""), c);
		},
		format: function(s) {
			return jQuery.tablesorter.formatFloat(s.replace(/,/g, ""));
		},
		type: "numeric"
	});
	
	jQuery('table.sort').tablesorter(); 
	
	jQuery('.home .table tbody tr').live('click', function() {
		window.location = jQuery(this).children('td:first-child').children('a').attr('href');
	});
	
	jQuery('#assessment-more').css({backgroundPosition: '3px 100%'});
	jQuery('#assessment-more').click(function() {
		jQuery(this).prev().slideToggle(function() {
			if(jQuery('#assessment-more').css('backgroundPosition') == "3px 100%") {
				jQuery('#assessment-more').css({backgroundPosition: '-988px 100%'});
			}
			else {
				jQuery('#assessment-more').css({backgroundPosition: '3px 100%'});
			}
		});
	});
	
	jQuery('ul.children').hide();
	jQuery('li.current_page_item ul.children, li.current_page_ancestor ul.children, li.current-menu-ancestor ul.children').show();


	//back to top scroll function. Any link with a hash (#) will scroll to that id on the page
	jQuery('a[href*=#]').click(function() {
		
		if(jQuery(this).hasClass('tab-link')) { return false; }
		
		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {
			var $target = jQuery(this.hash);
			$target = $target.length && $target || jQuery('[name=' + this.hash.slice(1) +']');
			if ($target.length) {
				var targetOffset = $target.offset().top;
				targetOffset = targetOffset - 35;
				jQuery('html,body').animate({scrollTop: targetOffset}, 500);
				return false;
			}
		}
	});

	if(jQuery("#backtotop").length!=0) {
		/* Back to top scroll */
		topYloc = parseInt(jQuery("#backtotop").css("top").substring(0,jQuery("#backtotop").css("top").indexOf("px")));
		jQuery(window).scroll(function () { 
			var scrollTop = jQuery(document).scrollTop();
			scrollTop = parseInt(scrollTop);
			var offset = topYloc+scrollTop+"px";
			jQuery("#backtotop").animate({top:offset},{duration:500,queue:false});
		});
	}
	 
});
