$(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.toLowerCase();
	/*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");
	}*/
	if (previousPage.indexOf("room-galleries.myhomeideas.com") > -1 && previousPage.indexOf("view=gallery") > -1) {
		insertBackToTab("Back to Gallery");
	}
	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").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 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(){
			if ($(belt).find(":animated").length == 0) {
				$(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(){
			if ($(belt).find(":animated").length == 0) {
				$(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 = ["http://img4.myhomeideas.com/i/legacy/designassistant/0506_sunset_minibath6_s.jpg", "http://img4.myhomeideas.com/i/2009/06/rg-thumbnails/SU0404030b_1_s.jpg", "http://img4.myhomeideas.com/i/2009/06/SU0210110c_2-s.jpg", "http://img4.myhomeideas.com/i/2009/06/rg-thumbnails/SU03101133_1_s.jpg", "http://img4.myhomeideas.com/i/2009/06/rg-thumbnails/turquoise-tower-s.jpg", "http://img4.myhomeideas.com/i/legacy/design/sl/03/02/SL0302081u_1a_s.jpg", "http://img4.myhomeideas.com/i/legacy/design/su/03/10/SU0310116k_1_s.jpg", "http://img4.myhomeideas.com/i/legacy/design/su/00/06/SU0006118o_1_s.jpg", "http://img4.myhomeideas.com/i/2009/06/rg-thumbnails/SU0403084t_1_s.jpg", "http://img4.myhomeideas.com/i/legacy/design/su/04/06/SU0406087o_1_s.jpg"];
	var rand = Math.floor(Math.random() * rgImgs.length);
	$(".mf-rg .imgcont img").attr("src", rgImgs[rand]);
	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) {}

//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);
}