// JavaScript Document

var playerWindow;

$(document).ready(function () {
	$("#search_input").bind("focus", function () { 
		if ($("#search_input").val() == "Search The Blog") $("#search_input").val("");
	});
	$("#search_input").bind("blur", function () { 
		if ($("#search_input").val() == "") $("#search_input").val("Search The Blog"); 
	});
	$(".search-btn a").bind("click", function () { submitSearch(); });
	//
	var numButtons = $("#main_nav").children().length;
	for (var i=1; i<=numButtons; i++) {
		// get the main nav button
		var nav = $("#nav"+i);
		var nava = $("#nav"+i+ " a");
		// attach custom data to the button
		nav.data("custom",{index: i});
		var subnav = $("#subnav"+i);
		// check to see if there is a subnav for this button
		var hassub = subnav.length > 0;
		var isActive = nava.hasClass("active");		
		// if there is a subnav...
		if (hassub > 0) {
			subnav.hide();
			subnav.css("top",nav.position().top + nav.innerHeight());
			subnav.css("left",nav.position().left);
			// attach events to link			
			nav.mouseover(function () {
				showSubnav($(this).data("custom").index);		   	
			});
			nav.mouseout(function () {
				startSubnavTimer();		   	
			});
			subnav.mouseover(function () {
				stopSubnavTimer();		   	
			});
			subnav.mouseout(function () {
				startSubnavTimer();		   	
			});
		// else if there is not a subnav...
		} else {
			// attach events to link
			nav.mouseover(function () {
				hideSubnav();
			});
		}
	} 
});

var currentSubnav = 0;
var timer = 0;
function showSubnav (which) {
	hideSubnav();
	var subnav = $("#subnav" + which);
	var nav = $("#nav"+which);
	subnav.css("top",nav.position().top + nav.innerHeight());
	subnav.css("left",nav.position().left);
	which == currentSubnav ? subnav.show() : subnav.fadeIn(200);
	currentSubnav = which;
}
function hideSubnav () {
	if (currentSubnav > 0) $("#subnav" + currentSubnav).hide();
	stopSubnavTimer();
}
function startSubnavTimer () {
	timer = setTimeout("hideSubnav()",1000);	
}
function stopSubnavTimer () {
	clearTimeout(timer);	
}

function submitSearch () {
	if (($("#search_input").val() == "Search The Blog") || ($("#search_input").val() == "")) {
		alert("Please enter a search term");
		$("#search_input").focus();
		return false;
	} else {
		$("#searchform").submit();
	}
}

function writeShoppingCartNotice (num_items, total, url) {
	$("body").prepend("<div id=\"shopping_cart_alert\"><p>You have " + num_items + " in your shopping cart for a total of $" + total + ". <a href=\"" + url + "\">Click here</a> to view/edit your cart and checkout.</p></div>");	
	$("#shopping_cart_alert").bind("click", function () { document.location.href = url; });
}

function openPlayer () {
	var w = window.open("http://www.oceansedgemusic.com/popup_player.html","playerWindow","status=0, toolbar=0, location=0, menubar=0, directories=0, resizable=0, scrollbars=0, width=380, height=560");
	if (w == null) {
		alert("It's possible that you have a popup-blocker enabled on your browser that prevented the music player window from opening. Check your browser preferences to allow popups for OceansEdgeMusic.com");
	} else {
		w.focus();
	}
}