﻿$(document).ready(function() {

    /* Main Navigation Sub-Menus */
    $(".mainNav li").bind("mouseover focus", function() {
        $(this).children("ul").show();
    });
    $(".mainNav li").bind("mouseout blur", function() {
        $(this).children("ul").hide();
    });

    /* Login Form */
    if ($("#login .genericErrors").length == 0) {
        $("#login fieldset").hide();
    };
    $(".topNav a.login").click(function() {
        if ($("#login fieldset").is(":visible")) {
            $("#login fieldset").slideUp();
        } else {
            $("#login fieldset").slideDown();
        }
        return false;
    });

    /* Search input */
    $(".searchInput").focus(function() {
        if ($(this).val() == "Search") {
            $(this).val("");
        }
    });
    $(".searchInput").blur(function() {
        if ($(this).val() == "") {
            $(this).val("Search");
        }
    });

    /* Recipe Search Filter */
    $("#breadcrumb .toggle").click(function() {
        if ($("#breadcrumb fieldset div.hidden").is(":visible")) {
            $("#breadcrumb fieldset div.hidden").slideUp("slow");
            $(this).text("More options").removeClass("toggleOpen");
        } else {
            $("#breadcrumb fieldset div.hidden").slideDown("slow");
            $(this).text("Less options").addClass("toggleOpen");
        }
    });

    /* Definition Lists */
    $("#content #main dl dd").hide();
    $("#content #main dl dt").click(function() {
        $(this).toggleClass("showing").next().toggle();
    });

    /* Table Striping */
    $("#content #main table tr:even").addClass("even");

    /* Recipe and Store Finders */
    $("#finders div:not(.open) fieldset").css("height", "0");
    var isAnimating = true;
    $("#finders div:not(.open)").hover(
        function() {
            $(this).children("fieldset").animate({
                "height": "15em"
            }, { queue: false });
        },
        function() {
            $(this).children("fieldset").animate({
                "height": "0"
            }, { queue: false });
        }
    );

    /* Store Search */
    $(".store:not(:first) .details").hide();
    $(".store h2").click(function() {
        if ($(this).parent(".store").children(".details").is(":visible")) {
            return false;
        } else {
            $(".store .details").slideUp();
            $(this).parent(".store").children(".details").slideDown();
        }
    });

    /* Star Rating */
    /* shows error in development, due to paths */
    $("fieldset.rate ul").remove();
    $("fieldset.rate h2").after('<ul class="rater"></ul>');
    var n = 1;
    while (n < 6) {
        $("fieldset.rate ul.rater").append('<li><a href="#" title="' + n + ' out of 5 stars" class="star_' + n + '">' + n + '</a></li>');
        n++;
    }
    $("fieldset.rate ul a").click(function() {
        var val = $(this).text();
        var id = $(".pageid").text();
        $.ajax({
            type: "POST",
            url: "/_WebServices/Rating.svc/SetAnonRating",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: $.toJSON({ sitePageId: id, rating: val }),
            success: function() {
                $("fieldset.rate ul.rater").replaceWith('<span class="star-' + val + '" title="You rated this ' + val + '/5 Stars">' + val + '</span>');
                $("fieldset.rate h2+span").after('<p>Thanks for rating this recipe!</p>');
            },
            error: function() {
                alert("There was an error. Please try again.");
            }

        });
        return false;
    });

    // Go To Login anchors
    $("a[href*='#login']").click(function() {
        $("html, body").animate({ scrollTop: 0 }, "slow", function() {
            if ($("#login fieldset").is(":hidden")) {
                $("#login fieldset").slideDown();
            }
        });
        return false;
    });

});