﻿// Qualify the HTML to allow stylesheets to style dynamic content appropriately
$('html').removeClass('unscripted').addClass('scripted');

$(document).ready(function () {
    // IE6 PNG fix trigger - but not on elements that it might break ;)
    if (window.DD_belatedPNG && $('html').hasClass('recipe')) {
        if ($('#recipeForm').length) {
            DD_belatedPNG.fix('#side *')
        }
        else DD_belatedPNG.fix('*')
    }

    /* 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; GW removed, prevents info bubble and directions
        } 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;
    });

    /* <snippet author="Barney" date="22/12/2010" description="Star ratings for submission"> */

    (function () {
        $('#leaveComment select.starSelect')
            .hide()
            .after('<ul class="rater"></ul>')

        for (var n = 1; n < 6; n++) {
            $("#leaveComment ul.rater")
                .append('<li><a href="#" title="' + n + ' out of 5 stars" class="star_' + n + '">' + n + '</a></li>');
        }

        $('#leaveComment ul.rater li a').live('click', function (e) {
            e.preventDefault();

            // Alter form accordingly
            $('#leaveComment select.starSelect option:nth-child(' + $(this).eq() + ')').click();

            // Visual feedback
            $('#leaveComment ul.rater a').removeClass('current-rating');
            $(this).addClass('current-rating');
        })
    } ())

    /* </snippet> */

    // 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;
    });

    /* 'share this' block */
    if (typeof Share !== 'undefined') {
        var el = $('#share ul').css({
            position: 'absolute',
            top: '-36px'
        }).hide().data('isVisible', false);

        $('a.trigger').click(function () {
            var isVisible = el.data('isVisible');
            if (isVisible) {
                el.hide();
            } else {
                el.show();
            };
            el.data('isVisible', !isVisible);
            if ($(this).parents('#share').length > 0) {
                return false;
            };
        });

        $('#share ul a').attach(Share);
    };
    /* end share */

    /* Print Shopping List */
    $("#printShopping").click(function () {
        $("body").addClass("printShopping");
    });
    $("#printRecipe").click(function () {
        $("body").removeClass("printShopping");
    });

    $($("a.toggleThis").attr("href") + ":not(.visible)").hide();
    $("a.toggleThis").click(function () {
        $($(this).attr("href")).slideToggle();
        return false;
    });

    /* Filter show/hide */
    $(".filterHide").hide();
    $(".filterShow").click(function () {
        $(".filterHide").show();
        $(this).hide();
    });

    //Load the rotation
    $('div#rotator ul li:first').addClass("show");
    theRotator();

});

/*ad rotation*/

function rotate() {
    //Get the first image
    var current = ($('div#rotator ul li.show') ? $('div#rotator ul li.show') : $('div#rotator ul li:first'));

    //Get next image, when it reaches the end, rotate it back to the first image
    var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div#rotator ul li:first') : current.next()) : $('div#rotator ul li:first'));

    //Set the fade in effect for the next image, the show class has higher z-index
    next.css({ opacity: 0.0 }).addClass('show').animate({ opacity: 1.0 }, 1000);

    //Hide the current image
    current.animate({ opacity: 0.0 }, 1000).removeClass('show');

};

function theRotator() {
    //Set the opacity of all images to 0
    $('div#rotator ul li').css({ opacity: 0.0 });

    //Get the first image and display it (gets set to full opacity)
    $('div#rotator ul li:first').css({ opacity: 1.0 });

    //Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
    setInterval('rotate()', 6000);

};
