$(document).ready(function(){
    makeHelpText();
    designFix();
    unLabelizer();
    validateForm();
    recipeActions();
    showHideExtras();
    checkAll();
});

function makeHelpText() {
    $("div.aside div.theBox a.helpLink").click(function(e){
        e.preventDefault();
        theValue = $(this).parents(".theBox").find(".searchButton").val();
        $(this).parents(".theBox").find(".searchBox").hide();
        $(this).parents(".theBox").find(".helpBox").show();
        $(this).parents(".theBox").find(".searchButton").val("Tillbaks");
        $(this).parents(".theBox").find(".searchButton").click(function(e){
            if($(this).val() == "Tillbaks"){
                $(this).parents(".theBox").find(".searchBox").show();
                $(this).parents(".theBox").find(".helpBox").hide();
                $(this).val(theValue);
                return false;
            }
        });
        return false;
    });
}
function designFix() {
    $("ul.resultsList li:even").addClass("even")
}

function unLabelizer(){
    $(".recipe label").each(function(){
        $(this).hide();
        theInput = $("#" + $(this).attr("for"));
        if($.trim(theInput.val()) == "" || $.trim(theInput.val()) == $(this).text()) {
            theInput.val($(this).text());
            theInput.addClass("passive");
        }
        theInput.removeClass("error")
        theInput.focus(function(){
            theLabel = $("label[for=" + $(this).attr("id") + "]");
            if(theLabel.text() == $(this).val()){
                $(this).val("");
                $(this).removeClass("passive");
                $(this).removeClass("error");
            }
        });
        theInput.blur(function(){
            theLabel = $("label[for=" + $(this).attr("id") + "]");
            if($.trim($(this).val()) == ""){
                $(this).val(theLabel.text());
                $(this).addClass("error");
            } else if($(this).hasClass("email")) {
                var mailPattern = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
                if((mailPattern).test($(this).val()) == false){
                    $(this).addClass("error");
                } else {
                    $(this).removeClass("error");
                }
            }
        });
    });
}
function validateForm() {
    $(".commentIt :submit,.recommendIt :submit").click(function(event){
        var returnVal = true
        var mailPattern = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
        $(this).parents(".part").find("textarea,:text").each(function(){
            theLabel = $("label[for=" + $(this).attr("id") + "]");
            if($(this).val() == theLabel.text() || $.trim($(this).val()) == "" ){
                $(this).addClass("error");
                returnVal = false ;
            } else if($(this).hasClass("email") && (mailPattern).test($(this).val()) == false){
                $(this).addClass("error");
                returnVal = false ;

            }
        })
        return returnVal;
    });
}
function recipeActions() {
    $("#commentIt").click(function(){
        $(".recommendIt").hide();
        $(".commentIt").show();
        unLabelizer()
        adaptMainDivs( 400 );
        return false;
    });
    $("#recommendIt").click(function(){
        $(".commentIt").hide();
        $(".recommendIt").show();
        unLabelizer()
        adaptMainDivs( 400 );
        return false;
    });
    $("#printIt").click(function(){
        window.print();
        return false;
    });
}
function showHideExtras() {
    $("tr.extra").hide();
    $("#extrasButton").click(function(){
        $("tr.extra").show();
        $(this).hide();
        adaptMainDivs( 400 );
        return false;
    })
}
function checkAll() {
    $(".theBox ul :checkbox:not(:first)").click(function(){
        $(this).parents("ul").find(":checkbox:first").removeAttr("checked")
    })
    $(".theBox ul :checkbox:first").click(function(){
        $(this).parents("ul").find(":checkbox:not(:first)").removeAttr("checked")
    })
}

window.onload = function() {
    adaptMainDivs( 400 );
}


//window.onresize = function() {
//    adaptMainDivs( 400 );
//}

//window.onscroll = function() {
//    adaptMainDivs( 400 );
//}
