$(document).ready(function(){
	// navigation drop downs
	$(".nav li ul").show();
	$(".nav li:has(ul)").bind("mouseover", function(){
		$(this).find("ul").css("left", "0");
		var overhang = $(this).offset().left + 200 - $("body").width();
		if (overhang > 0) {
			$(this).find("ul").css("left", (-overhang-5) + "px");
		}
    }).bind("mouseout", function(){
		$(this).find("ul").css("left", "-999em");
    });
	$(".nav a").bind("focus", function(){
		$(this).parents("li").find("ul").css("left", "0");
    }).bind("blur", function(){
		$(this).parents("li").find("ul").css("left", "-999em");
    });
	$(".nav li:has(ul)").each(function() {
		$(this).css("z-index", 5000 - $(this).prevAll("li").length);
	});

	//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;
	});

	//text resizer
	$(".resizer").append("Text Size: <a class='size1 current'>A</a> <a class='size2'>A</a> <a class='size3'>A</a>").find("a").click(function() {
		$(this).addClass("current").siblings().removeClass("current");
		if ($(this).hasClass("size1")) {
			$(".content, .recirc, .footer").css("fontSize", "1em");
		}
		else if ($(this).hasClass("size2")) {
			$(".content, .recirc, .footer").css("fontSize", "1.15em");
		}
		else if ($(this).hasClass("size3")) {
			$(".content, .recirc, .footer").css("fontSize", "1.3em");
		}
	});

	//convert email addresses from spans to mailto: links (reduce spam)
	$("span.email").each(function(){
		$(this).replaceWith("<a href='mailto:" + $(this).text() + "'>" + $(this).text() + "</a>");
	});

    //Slideshow photo overlay
	$("div.fromSite a").bind("mouseover focus", function(){
		var bottomPosition = ($(".slideBox").height() - 415) + "px";
		$(".photoOverlay").css("bottom", bottomPosition).show();
	})
	$(".photoOverlay a.close").click(function(){
		$(".photoOverlay").hide();
	});

	//Back to Tab from Room pages
	var previousPage = document.referrer;
	if (previousPage.indexOf("coastalliving.com") > -1) {
		insertBackToTab("Back to Coastal Living");
	}
	else if (previousPage.indexOf("cookinglight.com") > -1) {
		insertBackToTab("Back to Cooking Light");
	}
	else if (previousPage.indexOf("southernaccents.com") > -1) {
		insertBackToTab("Back to Southern Accents");
	}
	else if (previousPage.indexOf("southernliving.com") > -1) {
		insertBackToTab("Back to Southern Living");
	}
	else if (previousPage.indexOf("sunset.com") > -1) {
		insertBackToTab("Back to Sunset");
	}
	else if (previousPage.indexOf("room-galleries.myhomeideas.com") > -1) {
		if (previousPage.indexOf("view=") > -1) {
			insertBackToTab(previousPage.indexOf("view=Gallery") > -1 ? "Back to Gallery" : "Back to Rooms");
		}
	}
	else if (previousPage.indexOf("myhomeideas.com") > -1 && previousPage.indexOf("room-galleries.myhomeideas.com") == -1) {
		insertBackToTab("Back to Room");
	}
	function insertBackToTab(str) {
		$("div.fromtab").addClass("selected").append("<div class='inner'>&laquo; <a onclick='history.go(-1);'>" + str + "</a></div>");
	}

	// tooltip on project tools
	$(".ptools .tooltip").each(function(){
		$(this).addClass("tooltip2").parent("li").hover(function(){
			$(this).find(".tooltip2").show();
		}, function(){
			$(this).find(".tooltip2").hide();
		});
	});
	
	//validate search form
	$("#searchbox, #searchbox2").closest("form").submit(function(){
		var srchStr = $(this).find("input:text").val();
		srchStr = jQuery.trim(srchStr);
		if (srchStr != "" && srchStr != "Search MyHomeIdeas.com") {
			if (typeof omniSearch == 'function') {
				omniSearch(srchStr);
			}
			return true;
		}
		return false;
	});
});

// addTo RSS module - convert links to form (home page)
function addTo() {
	$(".addTo").each(function(){
		if ($(this).find(".getRSSList a").length > 0) {
			var str = "";
			$(this).find(".getRSSList a").each(function() {
				str += "<option value='" + $(this).attr("href") + "'>" + $(this).text() + "</option>";
			});
			$(this).find(".getRSSList").after("<form target='_top' action='#' method='post'><label for='reader'>Add MyHomeIdeas to:</label> <select name='reader' id='reader'>" + str + "</select> <input type='image' src='" + imgPath + "/btn_add.gif' alt='Add' /></form>");
			$(this).find("form").bind("submit", function(){
				$(this).attr("action", $(this).find("option:selected").val());
			});
			$(this).find(".getRSSList").remove();
		}
	});
}

// initialize the calendar thumbnail module (right column)
function initCalTN(calFile) {
	var monthsAbbr = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
	var now = new Date();
	$(".calTN2 .adCal").after("<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 h3").prepend($(data).find("title").eq(0).text() + " ");
			$(".calTN2").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 belt (channel pages)
function initBelt() {
	$(".belt:not(.isBelt):has(ul:odd)").each(function(){
		$(this).addClass("isBelt");
		$(this).find("ul:last li").insertAfter($(this).find("ul:first li:last"));
		$(this).find("ul:last").remove();
		$(this).find("ul").after("<div class='pagi'><a class='prev'>&laquo;</a> <a class='next'>&raquo;</a> <strong>Click to Browse</strong></div>");
		var belt = $(this);
		$(this).find(".prev").click(function(){
			$(belt).find("li:last").insertBefore($(belt).find("li:first"));
			$(belt).find("ul").animate({marginLeft:"-=160px"}, 0).animate({marginLeft:"+=160px"}, 1000);
		}).end().find(".next").click(function(){
			$(belt).find("ul").animate({marginLeft:"-=160px"}, 1000, null, function(){
				$(belt).find("li:first").insertAfter($(belt).find("li:last"));
				$(belt).find("ul").animate({marginLeft:"+=160px"}, 0);
			});
		});
	});
}

// choose one random image to display on RG right column module
function rotateRgImg() {
	var rgImgs = new Array();
	rgImgs[0] = new Array("http://img4.myhomeideas.com/i/legacy/designassistant/0506_sunset_minibath6_s.jpg", "http://www.myhomeideas.com/room-galleries/bathroom-storage-00400000045385/");
	rgImgs[1] = new Array("http://img4.myhomeideas.com/i/2009/06/rg-thumbnails/SU0404030b_1_s.jpg", "http://www.myhomeideas.com/room-galleries/master-bedroom-pivotal-addition-10000000783209/");
	rgImgs[2] = new Array("http://img4.myhomeideas.com/i/2009/06/SU0210110c_2-s.jpg", "http://www.myhomeideas.com/room-galleries/colorful-playroom-10000000783148/");
	rgImgs[3] = new Array("http://img4.myhomeideas.com/i/2009/06/rg-thumbnails/SU03101133_1_s.jpg", "http://www.myhomeideas.com/room-galleries/dining-room-cool-contemporary-10000000783249/");
	rgImgs[4] = new Array("http://img4.myhomeideas.com/i/2009/06/rg-thumbnails/turquoise-tower-s.jpg", "http://www.myhomeideas.com/room-galleries/hallway-mirror-10000000733352/");
	rgImgs[5] = new Array("http://img4.myhomeideas.com/i/legacy/design/sl/03/02/SL0302081u_1a_s.jpg", "http://www.myhomeideas.com/room-galleries/slipcover-solutions-10000000783319/");
	rgImgs[6] = new Array("http://img4.myhomeideas.com/i/legacy/design/su/03/10/SU0310116k_1_s.jpg", "http://www.myhomeideas.com/room-galleries/kitchen-2003-idea-house-10000000783192/");
	rgImgs[7] = new Array("http://img4.myhomeideas.com/i/legacy/design/su/00/06/SU0006118o_1_s.jpg", "http://www.myhomeideas.com/room-galleries/great-idea-deck-10000000783067/");
	rgImgs[8] = new Array("http://img4.myhomeideas.com/i/2009/06/rg-thumbnails/SU0403084t_1_s.jpg", "http://www.myhomeideas.com/room-galleries/living-room-display-10000000783207/");
	rgImgs[9] = new Array("http://img4.myhomeideas.com/i/legacy/design/su/04/06/SU0406087o_1_s.jpg", "http://www.myhomeideas.com/room-galleries/garden-different-strokes-10000000783218/");
	var rand = Math.floor(Math.random() * rgImgs.length);
	$(".mf-rg .imgcont a").attr("href", rgImgs[rand][1]).find("img").attr("src", rgImgs[rand][0]);
	return true;
}

// collapse and expand the left nav lists on search / RG pages
function toggleList() {
	$("ul.toggleList").each(function(){
		$(this).find("li:eq(4)").before("<li class='showLink'><a>Show More</a></li>");
		$(this).find("li:gt(4)").hide();
		$(this).find("li.showLink a").click(function(){
			$(this).parent("li").toggleClass("less").nextAll().toggle();
			$(this).text($(this).text()=="Show More" ? "Show Less" : "Show More");
		});
	});
	$("ul.toggleList2").each(function(){
		$(this).prev().before("<a class='showLink2'><img src='" + imgPath + "/x.gif' alt='expand/collapse' width='14' height='14' /></a>");
		$(this).find("li").hide();
		$(this).prev().prev("a.showLink2").click(function(){
			$(this).toggleClass("less").next().next("ul.toggleList2").find("li").toggle();
		});
	});
}

/* Remove IE6 image flickering */
try {document.execCommand ('BackgroundImageCache', false, true);}
catch (error) {}
