/* 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");

    $('#bannerCycle').cycle({ 
        fx:     'fade', 
        speed:  400, 
        timeout: 7000,
		cleartype:  1,
		cleartypeNoBg: 1,
		next:   '.btnRight', 
        prev:   '.btnLeft' 
    });
    
    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 clickclear(thisfield, defaulttext)
{
    if (thisfield.value == defaulttext)
    {
        thisfield.value = "";
    }
}
function clickrecall(thisfield, defaulttext)
{
    if (thisfield.value == "")
    {
        thisfield.value = defaulttext;
    }
}

function processPostLogin(data)
{
	var path = location.href;
	var item = path.substring(path.lastIndexOf('/') + 1)
	
    if (data.message == "Success")
    {
        $('#login_controls').hide();
        reloadLoginHeader();
		
        reloadFavorites();

    }
    else
    {
        $('#loginErrorMsg').html(data.message);
        $('#loginError').show();
    }
}

function reloadFavorites()
{
	$.getJSON(contextPath + "users/login/content-update", function(data)
	{
	    $.each(data, function(i,item){
	    	//alert("Fav: " + item.key);
	    	$('.' + item.type + "_" + item.key + "_removeFavorite").show();
	        $('.' + item.type + "_" + item.key + "_addFavorite").hide();
	    });
	});	
}

function reloadLoginHeader()
{
    $.get(contextPath + "tiles/header/login-header-include", function(data)
    {
        $('#login_header_include').html(data);
        $('#closeLogin').click(function()
        {
            $('#login_controls').toggle();
        });

    });
}

function login()
{
    var options =
    {
        dataType : 'json',
        success : processPostLogin
    };

    $('#loginForm').ajaxSubmit(options);
}

function reloadContentBottom(url, terms)
{
$('<div id="pagingAlert"/>').insertBefore("#pagination");
$("#pagination").hide();
$("#pagingAlert").text("Loading...").fadeIn(30);
$.get(url, function(data)
    {
        $('#contentBottom').html(data);
        setRoundedStyle();
    });

}


function addToFavorites(type, key)
{
	//alert("add " + type + ", key = " + key);
    if (loggedIn)
    {
        $.getJSON(contextPath + type + "/" + key + "/favorite", function(data)
        {
            $.facebox(data.message); 
            $('.' + type + "_" + key + "_removeFavorite").show();
            $('.' + type + "_" + key + "_addFavorite").hide();
            reloadLoginHeader();
        });
    }
    else
    {
	if ( contextPath != "/" )
	{		
        $.facebox('Please login to add an article or recipe to your Favorites. <br/> <br/>If you are a current member, please <a href="' + contextPath + '/users/login">login</a>. <br/> <br/>If you are not yet a member, please <a href="' + contextPath + '/users/registration">register</a> to enjoy coupons, other online promotions and store your favorite recipes and articles.');
	} else {
        $.facebox('Please login to add an article or recipe to your Favorites. <br/> <br/>If you are a current member, please <a href="/users/login">login</a>. <br/> <br/>If you are not yet a member, please <a href="/users/registration">register</a> to enjoy coupons, other online promotions and store your favorite recipes and articles.');
	}
    }
}

function removeFromFavorites(type, key)
{
    if (loggedIn)
    {
        $.getJSON(contextPath + type + "/" + key + "/not_favorite", function(data)
        {
            $.facebox(data.message);
            $('.' + type + "_" + key + "_removeFavorite").hide();
            $('.' + type + "_" + key + "_addFavorite").show();
            reloadLoginHeader();
            
            // If this is the favorites page (inside div with id = recipeFavoritesContainer) then remove the recipe from the DOM.
            $('#recipeFavoritesContainer ' + '#' + type + "_" + key ).fadeOut('slow');
            reloadLoginHeader();
        });
    }
    else
    {
        $.facebox('Remove from Favorites: Not logged on.  Become a member');
    }
}



function processSendtoFriend()
{
    var form = $('form[name=sendToFriend]');
    $.post(form.attr("action"), form.serialize(), function(data) {
        $.facebox(data);
    });
}

function searchProduct()
{
    if ( 
				! $('#prodLocatorZip').val().match(/^\d{5}$/)) {
        $('#prodLocatorError').show();
		return false;
    }
    else
    {
				$('#sideProdLocator').submit(); 
    }
}


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();		
}

