﻿$(function () {
    if ($("#left").length != 0) {
        $("body").addClass("withmenu");
    }

    $("table.stripe tr:even").addClass("alt");

    $("#print").bind("click", function () { print(); });


    // Catfish

    if ($("#catfish").length != 0) {
        var catfishHeight = $("#catfish").css("height").replace("px", "");
        $("#catfish").css("height", 0);
        $("#catfish").css("display", "none");
        $('#catfish').delay(2000).animate({ height: catfishHeight }, 800, "swing");

        $("#catfish-close").bind("click", function () {
            $('#catfish').animate({ height: 0 }, 600, "swing");
        });

        $("#catfish-hide").bind("click", function () {
            $('#catfish').animate({ height: 0 }, 600, "swing");
            $.get("/Forms/HideRegistrationPrompt");
        });
    }


    // Related pages functions

    if ($("#related h4").length != 0) {

        $("#related h4").each(function () {
            if ($(this).hasClass("expanded")) {
                $(this).next("ul").show();
            } else {
                $(this).next("ul").hide();
            }
            $(this).css("cursor", "pointer");
            $(this).bind("click", function () {
                if ($(this).hasClass("expanded")) {
                    $(this).removeClass("expanded");
                    $(this).addClass("collapsed");
                    if ($.browser.msie) { $(this).next("ul").hide() } else { $(this).next("ul").slideUp(); }
                } else {
                    $(this).removeClass("collapsed");
                    $(this).addClass("expanded");
                    if ($.browser.msie) { $(this).next("ul").show() } else { $(this).next("ul").slideDown(); }
                }
            });
        });
    }


    // Guide navigator functions

    if ($("#guidemininav").length != 0) {
        $("#guidemininav").show();
        $("#guidemininav").mouseenter(function () { $("#guidemininavhover").fadeTo(1000, 0.75); });
        $("#guidemininav").mouseleave(function () { $("#guidemininavhover").hide(); });
        $("#guidemininav").click(function () { $("#guidenav").show(); });
        $("#guidenavclose").click(function () { $("#guidenav").hide(); });
    }


    // Recruitment form functions

    $("textarea[wordcount]").live("keyup", function () {
        // For speed we just count the number of spaces. This doesn't give an exact work count but it's near enough.

        var maxwords = parseInt($(this).attr("wordcount"));
        var text = $.trim($(this).val().replace(/\s/g, ' '));
        var currentwords = text.length > 0 ? text.split(' ').length : 0;

        $(this).next("span.wordcount").text("Word count: " + currentwords);

        if (currentwords > maxwords) {
            $(this).next("span.wordcount").addClass("error");
        } else {
            $(this).next("span.wordcount").removeClass("error");
        }
    });
    $("textarea[wordcount]").trigger("keyup");

    $("#addentry").bind("click", function () {
        var newIndex = $("div.entry").length;
        var newEntry = $("div.entry:first").clone();

        newEntry.find("input, select, textarea").each(function () {
            $(this).val("");
            $(this).attr("id", $(this).attr("id").replace("_0_", "_" + newIndex + "_"));
            $(this).attr("name", $(this).attr("name").replace("[0]", "[" + newIndex + "]"));
            $(this).removeClass("input-validation-error");
        });

        $("div.entry:last").after(newEntry);
        $("textarea[wordcount]").trigger("keyup");
    });

    if ($("input.date").length > 0) {
        $("input.date").live("blur", function () {
            $(this).removeClass("input-validation-error");
            if ($(this).val().length > 0) {
                var reg = /^[0-3]?[0-9]\/[01]?[0-9]\/[12][90][0-9][0-9]$/;
                if (!reg.test($(this).val())) $(this).addClass("input-validation-error");
            }
        });
        $("form").bind("submit", function (event) {
            var valid = true;
            var reg = /^[0-3]?[0-9]\/[01]?[0-9]\/[12][90][0-9][0-9]$/;
            $("input.date").each(function () {
                if ($(this).val().length > 0 && !reg.test($(this).val())) {
                    $(this).addClass("input-validation-error");
                    valid = false;
                }
            });
            if (!valid) {
                event.preventDefault();
                alert("The date you have entered is not valid. You need to use the format dd/mm/yyyy.");
            }
        });
    }

    $("#agree").bind("keypress", function (e) {
        if (e.keyCode == 13) {
            return false;
        }
    });



    // Admin functions

    $("#pagetree li a:first-child").mouseenter(function () { ShowTreeTools(this); });

    if ($("#pagereorder").length != 0) {
        $("#pagereorder").sortable();

        $("form").submit(function () {
            // At the point the form is submitted, put the new ID order into the hidden weights field
            var neworder = "";
            $("#pagereorder div").each(function () {
                if (neworder != "") { neworder += "|"; }
                neworder += $(this).attr("id");
            });
            $("#neworder").val(neworder);
        });
    }

    $("tr .focusicon img").hide();
    $("tr").mouseenter(function () { $(this).find(".focusicon img").show(); });
    $("tr").mouseleave(function () { $(this).find(".focusicon img").hide(); });
});


function ShowTreeTools(a) {
    $(a).after($("#pagetreetools"));

    $($("#pagetreetools a")[0]).attr("href", "/Pages/Edit/" + $(a).attr("pageid"));
    $($("#pagetreetools a")[1]).attr("href", "/Pages/ReOrder/" + $(a).attr("parentid"));
    $($("#pagetreetools a")[2]).attr("href", "/" + $(a).attr("link"));
    $($("#pagetreetools a")[3]).attr("href", "/Pages/Delete/" + $(a).attr("pageid"));

    $("#pagetreetools").hide();
    $("#pagetreetools").fadeIn();
    $("#pagetreetools").css("left", $(a).outerWidth() + 15);
}

function ConfirmBooking() {
    if (!($("#chkAccept").is(":checked") || $("#chkDecline").is(":checked"))) {
        alert("Please choose a response by selecting one of the statements.");
        return false;
    } else {
        if ($("#txtName").attr("value").length == 0) {
            alert("Please enter your name.");
            return false;
        } else {
            return true;
        }
    }
}


function ShowVideo() {
    // For IE6 just let the link work normally
    if (!($.browser.msie && $.browser.version == "6.0")) {
        $("#videooverlay").show();

        $('#videomask').css({ 'width': $(window).width(), 'height': $(window).height() });
        $("#videomask").show();

        $("#videomask, #videoclose").click(function () {
            $("#videooverlay").hide();
            $("#videomask").hide();
            $("#videooverlay iframe").attr("src", $("#videooverlay iframe").attr("src"));
        });

        $(window).resize(function () {
            $('#videomask').css({ 'width': $(window).width(), 'height': $(window).height() });
        });

        return false;
    }
}
