﻿$(document).ready(function() {

    $('a.trackMe').click(function() {
        try {
            pageTracker._trackPageview($(this).attr('rel'));
            //TODO: anvend pageTracker._trackEvent('<Box navn>', '<Link navn>'); istedet
        } catch (err) { }
    });

    defaultButton($(".newsLetterTxtWrapper .newsLetterTxt"), $(".newsLetterBtn"));

    // Clickable LIs on recipeLists and filters
    $(".recipeList li, .recipeFilterList li").click(function() {
        liLink = $(this).find('h3 a').attr("href");
        location.href = liLink;
    });

    // Shopping lists, mark as bought
    $(".shoppingListPage dt").click(function() {
        $(this).next("dd").trigger("click");
    });
    $(".shoppingListPage dd").click(function() {
        if ($(this).prev('dt').html() != "") {
            $(this).toggleClass('bought').prev('dt').toggleClass('bought').addClass('border');
        } else {
            $(this).toggleClass('bought').prev('dt').addClass('bought');
        }
        if ($(this).next().html() && $(this).next().hasClass("bought") == false) {
            $(this).parent().children('dd.bought:first, dt.bought:first').delay(1000).slideUp(500).hide(function() {
                $(this).removeClass('border').fadeIn().appendTo($(this).parent('dl'));
            });
        }
        if ($(this).parent().children('dd:not(.bought)').length == 0) {
            $(this).parent().delay(1500).slideToggle("medium", function() {
                $(this).prev(".heading").addClass("strikethrough");
            });
        }
    });
    // Shopping lists, collapse
    $(".shoppingListPage .heading").click(function() {
        $dlElem = $(this).next('dl');
        $dlElem.slideToggle('medium').toggleClass("hidden");
        noOfChildren = $dlElem.children('dd:not(.bought)').length;
        if ($dlElem.hasClass("hidden")) {
            if (noOfChildren > 0) {
                $(this).append('<span class="noOfItems">(' + noOfChildren + ')</span>').children(".noOfItems").hide().delay(200).fadeIn(400).fadeOut(200).fadeIn(400).fadeOut(200).fadeIn(400);
            }
        } else {
            $(this).children(".noOfItems").remove();
        }
    });

});



// default button for form
function defaultButton(elm, btn) {
    $(elm).keypress(function(e) {
        if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
            $(btn).trigger('click');
            return false;
        } else {
            return true;
        }
    });
}

/* Reset form fields on focus*/
function ResetFormfield(elm, defaultValue) {
    $(elm).focus(function() {
        if (defaultValue != "") {
            if (this.value == defaultValue) {
                this.value = "";
                $(this).addClass('focus');
            }
        }
        else {
            if (this.value == this.defaultValue) {
                this.value = "";
                $(this).addClass('focus');
            }
        }
    }).blur(function() {
        if (defaultValue != "") {
            if (!this.value.length) {
                this.value = defaultValue;
                $(this).removeClass('focus');
            }
        }
        else {
            if (!this.value.length) {
                this.value = this.defaultValue;
                $(this).removeClass('focus');
            }
        }


    });
}
