// JavaScript Document

//the last moused over .nav-item
var currentBtn = "";

//the current open menu
var overMenu = "";

//the image of the activated nav btn
var overBtn = "";

//the non moused-over image for current activated nav btn
var previousBtn = "";

var timeout    = 300;
var closetimer = 0;

$(document).ready( function() {
	
	$('input[title]').each(function() {
        if ($(this).val() === '') {
            $(this).val($(this).attr('title'));
        }

        $(this).focus(function() {
            if ($(this).val() === $(this).attr('title')) {
                $(this).val('').addClass('focused');
            }
        });

        $(this).blur(function() {
            if ($(this).val() === '') {
                $(this).val($(this).attr('title')).removeClass('focused');
            }
        });
    });
	
	var currentPage = $(".selected").attr("id");
	var displayPageHighlight = turl + "/images/common/" + currentPage + "-rollover.gif";
	
	if(currentPage == "recipes")
	{
		var displayPageHighlight = turl + "/images/common/" + currentPage + "-selected.gif";
	}
	
	$(".selected").children().children().attr("src",displayPageHighlight);
	
	$(".nav-item").mouseover(function() {
		
		cancelMenuTimer();
		
		$(overMenu).hide();
		
		$(overBtn).attr("src", previousBtn);
		$(currentBtn).css("z-index", "0");
		
		var currentItem = $(this).attr("id");
		
		var imgPath = turl + "/images/common/" + currentItem + "-rollover.gif";
		$(this).css("z-index", "300");
		
		overBtn = $(this).children().children();
		previousBtn = overBtn.attr("src");
		
		overBtn.attr("src", imgPath);
		overMenu = document.getElementById(currentItem + "-menu-over");
		$(overMenu).show();	
		currentBtn = $(this);
		
	});
	
	$(".nav-item").mouseout(function() {
		closetimer = window.setTimeout(hideMenu, timeout);
	});
	
	$(".over-menu").mouseout(function() {
		closetimer = window.setTimeout(hideMenu, timeout);						   						  
	});
	
	$(".over-menu").mouseover(function() {
		cancelMenuTimer();
	});
	
	$(".product-nav-item").mouseover( function () {
		$(this).css("background-color", "#ccc");
	});
	$(".product-nav-item").mouseout( function () {
		$(this).css("background-color", "#fff");
	});
	
	var subMenuName = $(".selected").attr("id") + "-menu";
	
	var subMenu = document.getElementById(subMenuName);
	
	$(subMenu).show();
	
	$("#search-btn").click(function(){
		query = $("#search").val();
		document.location = "/?s=" + query;
	});
	
	$('#search').keypress(function(event) {
  		if (event.keyCode == '13') 
		{
			query = $("#search").val();
			document.location = "/?s=" + query;
			
     		event.preventDefault();
   		}
	});

    $("#search-btn-local").click(function(){
        query = $("#search-local").val();
        document.location = "/?s=" + query;
    });

    $('#search-btn-local').keypress(function(event) {
        if (event.keyCode == '13')
        {
            query = $("#search-local").val();
            document.location = "/?s=" + query;

            event.preventDefault();
        }
    });

});

function cancelMenuTimer()
{
	if(closetimer)
    {  
		window.clearTimeout(closetimer);
      	closetimer = null;
	}
}

function hideMenu()
{
	$(overMenu).hide();	
	$(overBtn).attr("src", previousBtn);
	$(currentBtn).css("z-index", "0");
}

