﻿$(document).ready(function () {
    //Front page functionality
    //var killSwitch = "0";
    //if (killSwitch == "1") { theEnd(); }

    //End Of Recipes
    //$("#nextRecipes").append("<img height='83' width='69' id='end' class='queueImage' src='uploads/small/end.jpg' alt=''>");

    //Initial Load
    gradeLoad();
    loadCurrentRecipeInfo();
    loadSetPast();

    $(".homeRating").click(function () {
        theRating = $(this).attr("rel");
        gradeMe();
    });

    $(".homeInstEnd").click(function () {
        gradeMeEmpty();
    })
});

function gradeLoad()
{
    theCurrent = $(".queueImage:nth-child(1)").attr("id");

    $(".queueImage:nth-child(1)").appendTo("#nextRecipes");
    $("#homeMainRecipeImage").attr({ src: "/uploads/large/" + theCurrent.replace("i_", "") + ".jpg", "class": theCurrent });
    $(".printLink").attr({ href: "/print_recipe.aspx?r=" + theCurrent.replace("i_", "") });

    //Note contest has ended.  Replaced if logic with 'true'.  Change back to false for normal operations.
    if (validateCookie(theCurrent.replace("i_", ""), getCookie("gmrc")) == "true") {
        //   theEnd();
        $(".homeMid").addClass("homeMidEnd");
        $("#homeTheRatings").hide();
        $(".homeInst").hide(); $(".homeInstEnd").show();
        $(".queueImage").removeClass("L");
        $(".queueImage:nth-child(3)").addClass("L");
    }
    else {
        $(".queueImage").removeClass("L");
        $(".queueImage:nth-child(3)").addClass("L");
    }
}

function gradeMe() {
    theCurrent = $(".queueImage:nth-child(1)").attr("id");
    thePast = $("#homeMainRecipeImage").attr("class");

    checkMoveOnRecipe();

    //Submit grade
    $.post("submitGrade.aspx", { r: thePast.replace("i_", ""), g: theRating });
    //alert("Recipe ID: " + thePast + " | The Rating: " + theRating);

    //Continue with image transitions
    $(".queueImage:nth-child(1)").fadeOut("medium", function() {
        if ($(".queueImage:nth-child(1)").attr("id") != "end")
            $(".queueImage:nth-child(1)").appendTo("#nextRecipes").show();
        else
            $(".queueImage:nth-child(1)").remove();
        //Note contest has ended.  Replaced if logic with 'true'.  Change back to false for normal operations.    
        if (validateCookie(theCurrent.replace("i_", ""), getCookie("gmrc")) == "ture")
            theEnd();
        else {
            $(".queueImage").removeClass("L");
        }
    });

    foContent();
}

function gradeMeEmpty()
{
     theCurrent = $(".queueImage:nth-child(1)").attr("id");
     thePast = $("#homeMainRecipeImage").attr("class");
     
     checkMoveOnRecipe();

    //Continue with image transitions
    $(".queueImage:nth-child(1)").fadeOut("medium", function()
    {
        $(".queueImage").removeClass("L");

        if ($(".queueImage:nth-child(1)").attr("id") != "end")
            $(".queueImage:nth-child(1)").appendTo("#nextRecipes").show();
        else
            $(".queueImage:nth-child(1)").remove();
    });
    //Note contest has ended.  Replaced if logic with 'true'.  Change back to false for normal operations.
    if (validateCookie(theCurrent.replace("i_", ""), getCookie("gmrc")) != "true") {
        $(".homeMid").removeClass("homeMidEnd");
        $("#homeTheRatings").fadeIn("medium");
        $(".homeInst").show();
        $(".homeInstEnd").hide();
    }
    
    foContent();
}
function checkMoveOnRecipe()
{
    if ($(".queueImage:nth-child(1)").attr("id") == "end")
        theEnd();
}

function setCurrent()
{
    //Note contest has ended.  Replaced if logic with 'true'.  Change back to false for normal operations.
    if (validateCookie(theCurrent.replace("i_", ""), getCookie("gmrc")) == "true")
        theEnd();

    $("#homeMainRecipeImage").attr({ src: "/uploads/large/" + theCurrent.replace("i_", "") + ".jpg", "class": theCurrent});
    $("#homeMainRecipeImage").fadeIn("medium");
}

function setPast() {
    
    if(thePast != "end")
        $("#ratedImage").attr({ src: "/uploads/mid/" + thePast.replace("i_", "") + ".jpg", "class": thePast });
    else
        $("#ratedImage").attr({ src: "/images/clear.gif", "class": thePast });

    if (thePast != "end")
    {
        $("#getVoted").load("getVoted.aspx?r=" + thePast.replace("i_", "") + " #getVoted");
        $("#getVoted").show();
    }

    if (thePast == "end")
    {
        $("#getVoted").hide();
    }

    $("#ratedImage").fadeIn("medium");
}

//This sets the highest rated image on page load
function loadSetPast()
{
    //$("#getVoted").load("setGetVoted.aspx", "#getVoted");
    $("#getVoted").html($('#getVotedH').html());

    $("#ratedImage").attr("src", $("#ratedImageH").attr("src"));
    //$("body").hide();
    //alert($('#getVotedH').html());
}

function foContent()
{
    $("#homeMainRecipeImage").fadeOut("medium", function()
    {
        setCurrent();
    });

    $("#ratedImage").fadeOut("medium", function()
    {
        setPast();
    });

    $("#singleRight").fadeOut("medium", function()
    {
        loadCurrentRecipeInfo();
    });
}

function loadCurrentRecipeInfo()
{
    if (theCurrent != "end")
    {
        $("#getRecipes").load("getCurrentRecipe.aspx?r=" + theCurrent.replace("i_", "") + " #getRecipe");
        $("#singleRight").fadeIn("medium");
        $(".printLink").attr({ href: "/print_recipe.aspx?r=" + theCurrent.replace("i_", "") });
    }
}

function theEnd()
{
    $(".homeMid").addClass("homeMidEnd");
    $("#homeTheRatings").fadeOut("medium");
    $(".homeInst").hide(); $(".homeInstEnd").show();
}

function validateCookie(recipe, token)
{
    var value = (function()
    {
        var val = null;
        var target = "targets/validateCookie.aspx?r=" + recipe + "&t=" + token + "&n=" + Math.random();

        $.ajax(
        {
            'async': false,
            'global': false,
            'url': target,
            'success': function(data)
            {
                val = data;
            }
        });

        return val;
    })();

    return value;
}
function getCookie(c_name)
{
    if (document.cookie.length > 0)
    {
        c_start = document.cookie.indexOf(c_name + "=");

        if (c_start != -1)
        {
            c_start = c_start + c_name.length + 1;
            c_end = document.cookie.indexOf(";", c_start);

            if (c_end == -1)
                c_end = document.cookie.length;
            
            return unescape(document.cookie.substring(c_start, c_end));
        }
    }
    return "";
}
