/* Global.js */

// remove IE6 image flickering
try {document.execCommand('BackgroundImageCache', false, true);}
catch(e) {}


jQuery(document).ready(function(){
	//text resizer
	jQuery("#txtResizer span").eq(0).css("color", "#666");
	jQuery("#txtResizer span").bind("click", function(){
		var newSize = jQuery(this).prevAll("span").length;
		jQuery("#txtResizer span").css("color", "#036").eq(newSize).css("color", "#666");
		jQuery("#txtResizeArea").css("font-size", (newSize*0.2+1)+"em");
	});

	//initialize popup links
	jQuery("a.popup").addClass("pop").attr("title", "[Opens in new window]").bind("click", function(){
		var popup = window.open(jQuery(this).attr("href"), "_blank", "height=500,width=600,scrollbars=yes");
		popup.focus();
		return false;
	});
	jQuery("a.popup2").attr("title", "[Opens in new window]").bind("click", function(){
		var popup = window.open(jQuery(this).attr("href"), "_blank", "height=500,width=600,scrollbars=yes");
		popup.focus();
		return false;
	});

	//don't submit search form with blank query
	jQuery("#sitesearch").submit(function(){
		return ($.trim(jQuery("#searchbox").val()) != "");
	});

	//prints the pages url on screen used on print pages
	jQuery("#linkLocation").html(location.href);

	//convert add-this link lists to form
	if (jQuery(".getRSS .getRSSList a").length > 0) {
		var str = "";
		jQuery(".getRSS .getRSSList a").each(function() {
			str += "<option value='" + jQuery(this).attr("href") + "'>" + jQuery(this).text() + "</option>";
		});
		jQuery(".getRSS .getRSSList").after("<form target='_top' action='#' method='post'><table cellspacing='10' summary=''><tr><td><label for='reader'>Add Southern Living to:</label></td><td><select name='reader' id='reader'>" + str + "</select></td><td><input type='image' src='http://img4.southernliving.com/static/i/btn_add.gif' alt='Add' /></td></tr></table></form>");
		jQuery(".getRSS form").bind("submit", function(){
			jQuery(this).attr("action", jQuery(this).find("option:selected").val());
		});
		jQuery(".getRSS .getRSSList").remove();
	}
	if (jQuery(".addTo .getRSSList a").length > 0) {
		var str = "";
		jQuery(".addTo .getRSSList a").each(function() {
			str += "<option value='" + jQuery(this).attr("href") + "'>" + jQuery(this).text() + "</option>";
		});
		jQuery(".addTo .getRSSList").after("<form target='_top' action='#' method='post'><table cellspacing='4' summary=''><tr><td><label for='reader'>Add Southern Living to:</label></td><td><select name='reader' id='reader'>" + str + "</select></td><td><input type='image' src='http://img4.southernliving.com/static/i/btn_add.gif' alt='Add' /></td></tr></table></form>");
		jQuery(".addTo form").bind("submit", function(){
			jQuery(this).attr("action", jQuery(this).find("option:selected").val());
		});
		jQuery(".addTo .getRSSList").remove();
	}

});

//hide popunders from certain sites
var dref = document.referrer.toLowerCase();
if (dref.indexOf("google") > -1 || dref.indexOf("yahoo") > -1 || dref.indexOf("facebook") > -1 || dref.indexOf("primosearch") > -1 || dref.indexOf("righthealth") > -1 || dref.indexOf("alot") > -1 || dref.indexOf("digg") > -1 || dref.indexOf("reddit") > -1 || dref.indexOf("twitter") > -1 || dref.indexOf("stumbleupon") > -1 || dref.indexOf("cnn") > -1 || dref.indexOf("msn") > -1 || dref.indexOf("huffingtonpost") > -1) {
	adConfig.setPopups(false);
}


// initialize the calendar thumbnail module (flex module js copied from MHI)
function initCalTN(calFile) {
	var monthsAbbr = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
	var now = new Date();
	jQuery(".calTN2:not(:has(.date))").prepend("<div class='date'><div class='month'>" + monthsAbbr[now.getMonth()] + "</div><div class='day'>" + now.getDate() + "</div></div>");
	$.ajax({
		type: "GET",
		url: calFile,
		dataType: "xml",
		success: function(data){
			var currentDay = jQuery(data).find("day").eq(now.getDate() - 1);
			var calLink = jQuery(data).find("month").attr("url");
			jQuery(".calTN:not(:has(a)) h3").prepend(jQuery(data).find("title").eq(0).text() + " ");
			jQuery(".calTN2:not(:has(a))").find("p").remove().end().append("<a href='" + calLink + "' class='title'>" + jQuery(currentDay).find("title").text() + "</a><p>" + jQuery(currentDay).find("short-deck").text() + " <a href='" + calLink + "' class='more'>&raquo; See Full Calendar</a></p>");
		}
	});
}