/* Global Variables */

var loggedIn = false;
var zipcode = "Your Zip Code";

/* jquery controls */
$(document).ready(function(){



$("ul#pastaShapes li:nth-child(odd)").addClass("nth-child-odd");


    
    selectNav();
    
	$("#login_red").mouseover(function(){
    	$(this).removeClass().addClass("signInButtonRed_hover");
    }).mouseout(function(){
    	$(this).removeClass().addClass("signInButtonRed");		
    });
	
	$("#submitBtn").mouseover(function(){
    	$(this).removeClass().addClass("buttonSubmit_hover");
    }).mouseout(function(){
    	$(this).removeClass().addClass("buttonSubmit");		
    });	
	
	var delayfade = {};
    /* toggle control */
    $('#loginBtn,#signin_button,#closeLogin').click(function()
    {
        $('#login_controls').toggle();
    });

   // $('.printItemLink').facebox();
    $('.sendtoFriendItemLink').facebox();

    $('.cookTime').click(function(){showCookTimes();});
});


function reloadSearchContent(url, terms)
{
$('<div id="pagingAlert"/>').insertBefore("#pagination");
$("#pagination").hide();
$("#pagingAlert").text("Loading...").fadeIn(30);
$.get(url, function(data)
    {
        $('#searchContent').html(data);
    });

}


function reloadFeaturedRecipeContent(url, isForward, current)
{

$('<div id="pagingAlert"/>').insertBefore("#pagination");
$("#pagination").hide();
$("#pagingAlert").text("Loading...").fadeIn(30);

$.get(url, function(data)
    {
        $('#featuredRecipeContent').html(data);
        if ( current == undefined )
        {
            $(".recipeContent").hide();
            $(".recipeContentFirst").show();
            current = $(".active").html();
        }
        if ( isForward )
        {
        	
        	refreshNextRecipeContent(current);
        }
        else
        {
        	refreshPreviousRecipeContent(current);
        }
    });

}


function showPreviousRecipe(first, previousPageUrl)
{
	var current = $(".active").html();
    if ( current == first )
    {
    	if ( current != 1)
    	{
    		reloadFeaturedRecipeContent(previousPageUrl, false, current);
    	}
    }
    else
    {
    	refreshPreviousRecipeContent(current);
    }   
}

function refreshPreviousRecipeContent(current)
{
	if ( current != 1 ) { current--; }
    
    var showRecipe = "#recipe" + current;
    var recipeSelected = "#showRecipe" + current;
    
    $(".recipeContent").hide();
    $(showRecipe).fadeIn('slow');
    $(".active").addClass("showRecipe").removeClass("active");
    $(recipeSelected).addClass("active");
}


function showNextRecipe(pageLast, last, nextPageUrl)
{
	var current = $(".active").html();
    if ( current == pageLast )
    {
    	if ( current != last)
    	{
    		reloadFeaturedRecipeContent(nextPageUrl, true, current);
    	}
    }
    else
    {
    	refreshNextRecipeContent(current, last);
    }   
}

function refreshNextRecipeContent(current, last)
{
	if ( current != last ) { current++; }
    
    var showRecipe = "#recipe" + current;
    var recipeSelected = "#showRecipe" + current;
    
    $(".recipeContent").hide();
    $(showRecipe).fadeIn('slow');
    $(".active").addClass("showRecipe").removeClass("active");
    $(recipeSelected).addClass("active");
}

function doSiteSearch(form)
{

    var ok = true;

    if (form == 'siteSearch')
    {
        if ($('form[name=siteSearch] input[type=checkbox]:not(input[name=all]):checked').length == 0)
        {
            ok = false;
        }
    }

    if (ok && $('#' + form + "  #searchField").val() != 'Enter Keywords e.g. chicken')
    {
        $('#' + form).submit();
    }
    else if (!ok)
    {
        $('#siteSearchError').show();
    }
}

function clickAllSiteSearch()
{
    if ($('input[name=all]:checked').val() == 'true')
    {
        $('form[name=siteSearch] input[type=checkbox]').each(function()
        {
            $(this).attr('checked', 'true');
        });

    }

}

function checkSiteSearchAll()
{
    if ($('form[name=siteSearch] input[type=checkbox]:not(input[name=all]):checked').length != 3)
    {
        $('input[name=all]').removeAttr('checked');
    }
}
function clickclear(thisfield, defaulttext)
{
    if (thisfield.value == defaulttext)
    {
        thisfield.value = "";
    }
}
function clickrecall(thisfield, defaulttext)
{
    if (thisfield.value == "")
    {
        thisfield.value = defaulttext;
    }
}

function processSendtoFriend()
{
    var form = $('form[name=sendToFriend]');
    $.post(form.attr("action"), form.serialize(), function(data) {
        $.facebox(data);
    });
}


function pause(secs) 
{
	var date = new Date();
	var curDate = null;
	
	do { curDate = new Date(); } 
		while(curDate-date < (secs * 1000));
} 

function isImageLoaded(img) {

    if (!img.complete) 
    {
        return false;
    }
    
    if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) 
    {
        return false;
    }

    return true;
}

function printSomething(printWhat,printPath){
	if (printWhat == 'article'){
			window.open(printPath, "articlePrint", "menubar=no,width=650,height=600,toolbar=no,scrollbars=yes,status=yes,resizable=yes");
		}
	else if (printWhat == 'coupon'){
			window.open(printPath, "couponPrint", "menubar=no,width=400,height=200,toolbar=no,status=no");
		}
	else if (printWhat == 'recipe'){
			window.open(printPath, "recipePrint", "menubar=no,width=650,height=600,toolbar=no,scrollbars=yes,status=yes,resizable=yes");
		}
	
}

function showCookTimes()
{
	$.facebox('<div><br /><br /><img src="' + contextPath + 'images/articles/pasta-cook-times.jpg" alt="Pasta Cook Times" /><br /></div>');
}

function disableCookTimeLink()
{
	$(".cookTime").click(function() { return false; }).attr('href', '#').attr( 'style', 'text-decoration: none;color: #000; cursor: text;');
}

function printStuff(){
		window.print();
		window.close();		
}

