/* Global.js */
var monthsAbbr = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

// remove IE6 image flickering
try {document.execCommand('BackgroundImageCache', false, true);}
catch(e) {}

//convert add-this link lists to form
function convertRSSList1() {
	if ($(".getRSS .getRSSList a").length > 0) {
		var str = "";
		$(".getRSS .getRSSList a").each(function() {
			str += "<option value='" + $(this).attr("href") + "'>" + $(this).text() + "</option>";
		});
		$(".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>");
		$(".getRSS form").bind("submit", function(){
			$(this).attr("action", $(this).find("option:selected").val());
		});
		$(".getRSS .getRSSList").remove();
	}
}
function convertRSSList2() {
	if ($(".addTo .getRSSList a").length > 0) {
		var str = "";
		$(".addTo .getRSSList a").each(function() {
			str += "<option value='" + $(this).attr("href") + "'>" + $(this).text() + "</option>";
		});
		$(".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>");
		$(".addTo form").bind("submit", function(){
			$(this).attr("action", $(this).find("option:selected").val());
		});
		$(".addTo .getRSSList").remove();
	}
}
//Validate and submit the free newsletter form
(function($) {
	$.fn.freeNewsletterValidate = function(){
		return this.each(function(){
			var $this = $(this);
			$this.find("form").submit(function(){
				var emailPattern = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
				if (!emailPattern.test($this.find("input[name=email]").val())) {
					var errorMsg = $('<div></div>').attr('style', 'color:#c00').text('Please enter a valid email address.');
					if ($this.find('.dek').text().indexOf('Please enter a valid email address.') == -1) {
						$this.find('.dek').append(errorMsg);
					}
					return false;
				} else {
					omniNewsProxy($this.find("input[name=sub]").val());
					return true;
				}
			});
		});
	};
})(jQuery);

$(document).ready(function(){
	//text resizer
	$("#txtResizer span").eq(0).css("color", "#666");
	$("#txtResizer span").bind("click", function(){
		var newSize = $(this).prevAll("span").length;
		$("#txtResizer span").css("color", "#036").eq(newSize).css("color", "#666");
		$("#txtResizeArea").css("font-size", (newSize*0.2+1)+"em");
	});

	//initialize popup links
	$("a.popup").addClass("pop").attr("title", "[Opens in new window]").bind("click", function(){
		var popup = window.open($(this).attr("href"), "_blank", "height=500,width=600,scrollbars=yes");
		popup.focus();
		return false;
	});
	$("a.popup2").attr("title", "[Opens in new window]").bind("click", function(){
		var popup = window.open($(this).attr("href"), "_blank", "height=500,width=600,scrollbars=yes");
		popup.focus();
		return false;
	});

	//don't submit search form with blank query
	$("#sitesearch").submit(function(){
		return ($.trim($("#searchbox").val()) != "");
	});

	//prints the pages url on screen used on print pages
	$("#linkLocation").html(location.href);

	//convert add-this link lists to form
	convertRSSList1();
	convertRSSList2();
	
	$('.newsSignup').freeNewsletterValidate();
	$('.newsSignupTn').freeNewsletterValidate();

});

//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 now = new Date();
	$(".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 = $(data).find("day").eq(now.getDate() - 1);
			var calLink = $(data).find("month").attr("url");
			$(".calTN:not(:has(a)) h3").prepend($(data).find("title").eq(0).text() + " ");
			$(".calTN2:not(:has(a))").find("p").remove().end().append("<a href='" + calLink + "' class='title'>" + $(currentDay).find("title").text() + "</a><p>" + $(currentDay).find("short-deck").text() + " <a href='" + calLink + "' class='more'>&raquo; See Full Calendar</a></p>");
		}
	});
}

// initialize the calendar thumbnail module
function initCal150(calFile) {
	var now = new Date();
	$(".cal150").empty().prepend("<div class='date'>" + monthsAbbr[now.getMonth()] + " " + now.getDate() + ", " + now.getFullYear() + "</div>");
	$.ajax({
		type: "GET",
		url: calFile,
		dataType: "xml",
		success: function(data){
			var currentDay = $(data).find("day").eq(now.getDate() - 1);
			var calLink = $(data).find("month").attr("url");
			var monthTitle = $(data).find("month > title").text();
			var image = $(currentDay).find("img[width=150]");
			if (image.length == 0) {
				image = $(currentDay).find("img");
			}
			$(".cal150").append('<h3><a href="' + calLink + '">' + monthTitle + '</a></h3><div class="img"><a href="' + calLink + '"><img src="' + image.attr("url") + '" width="150" height="150" alt="' + image.attr("alt") + '" /></a></div><h4><a href="' + calLink + '">' + $(currentDay).find("title").text() + '</a></h4><div class="deck">' + $(currentDay).find("short-deck").text() + ' <a href="' + calLink + '" class="teaserLink">more</a></div>');
		}
	});
}
