/* Create a namespace */
var Macy = {};

//try to eliminate IE 6 flicker
try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}

Macy.createNewsMask = function() {
	var boxHeight = $("div#sidebar div.newscallout").outerHeight();
	$("div#sidebar div.newscallout div.newsfade").css("height",boxHeight+"px");
	// or for homepage
	var homeBoxHeight = $("div.homepagesecondary div.homepagenews").outerHeight();
	$("div.homepagesecondary div.homepagenews div.newsfade").css("height",homeBoxHeight+"px");
}
Macy.createBlogMask = function() {
	var boxHeight = $("div#sidebar div.maskmodule").outerHeight();
	$("div#sidebar div.maskmodule div.modulemask").css("height",boxHeight+"px");
}

Macy.homepageFeatureArea = function() {
	$("div.homepage div#feature div.featurepreview").hover(
		function() {
			$(this).addClass("hover");
		},
		function() {
			$(this).removeClass("hover");
	}).click( function() {
		$("div#featureshadow").hide();
		$("div.homepage div#feature div.featurepreview, div.homepage div#feature div.featurenumber").fadeOut();
		$(this).next().fadeIn();
		$(this).next().children("a.btnclose").fadeIn();
		$(this).next().find("span").fadeIn();
	});
	
	$("div.homepage div#feature a.btnclose").click( function() {
		$("div.homepage div#feature div.featurepreview, div.homepage div#feature div.featurenumber").fadeIn("normal", function() {
			$("div#featureshadow").show();
		});
		
		$("div.homepage div#feature div.featurecontent, div.homepage div#feature div.featurecontent a.btnclose, div.homepage div#feature div.featurecontent div.featureitemnav span").fadeOut();
		return false;
	});
	
	$("div.homepage div#feature a.featurenavlink").click( function() {
		var newSectionId = $(this).attr("href");
		$("div.homepage div#feature div.featurecontent").not(newSectionId).find("span").fadeOut();
		$("div.homepage div#feature div.featurecontent").not(newSectionId).children("a.btnclose").fadeOut();
		$("div.homepage div#feature div.featurecontent").not(newSectionId).fadeOut();
		$("div.homepage div#feature div.featurecontent#"+newSectionId).fadeIn();
		$("div.homepage div#feature div.featurecontent#"+newSectionId).children("a.btnclose").fadeIn();
		$("div.homepage div#feature div.featurecontent#"+newSectionId).find("span").fadeIn();
		return false;
	});
	
}

/* Initialize everything when page is ready */
$(document).ready(function() {

	//Equalize heights of feature columns on subpages
	$("#featurecolumns .featuredescription").equalHeights();
	$(".subpage #featurecolumns .column").equalHeights();

	
	//Initialize Homepage Feature area functionality
	Macy.homepageFeatureArea();
	
  // Prepend toggle links
  $('.about div.bio').before("<p class=\"toggle\"><a href=\"#\">Show bio</a></p>");
  $('.toggle a').bind('click', function(e) {
    e.stopPropagation();
    e.preventDefault();
    $next = $(this).parent().next();
    $(this).html($next.is(':visible') ? "Show bio" : "Hide bio");
    $next.slideToggle();
  });

})

// Initialize when whole page is loaded
$(window).load( function() {
	var dlay = setInterval(function(){
		if(jQuery('.sIFR-replaced').length>=sIFR.replacements.length){
			Macy.createNewsMask();
			Macy.createBlogMask();
		}
		clearInterval(dlay);
	},100);
})
