﻿/// <reference path="jquery-1.4.4-vsdoc.js" />


$(function () {

    //hide by default
    $(".otherTitleContainer").css("display", "none");

    $("#Title").change(function () {
        if ($(this).val() === "Other") {
            $(".otherTitleContainer").css("display", "block");
        }
        else {
            $(".otherTitleContainer").css("display", "none");
        }
    });


    var delContainer = $("#delivery-address-container");
    delContainer.css("display", "none");

    var inContainer = $("#invoice-address-container");
    inContainer.css("display", "none");

    $("#DeliveryAddressNeeded").change(function () {
        if ($(this).is(":checked")) {
            delContainer.slideDown();
        }
        else {
            delContainer.slideUp();
        }
    });

    $("#InvoiceAddressNeeded").change(function () {
        if ($(this).is(":checked")) {
            inContainer.slideDown();
        }
        else {
            inContainer.slideUp();
        }
    });

});

$(function () {

    $(".wizard-step:first").fadeIn(); //show first step
    $("#register").hide();
    $("#submit").hide();

    // attach backStep button handler
    // hide on first step
    $("#back-step").hide().click(function () {
        var $step = $(".wizard-step:visible"); // get current step
        if ($step.prev().hasClass("wizard-step")) {
            $step.hide().prev().fadeIn();
            $("#next-step").show();
            $("#register").hide();
            $("#submit").hide();

            if (!$step.prev().hasClass("wizard-step")) {
                $("#back-step").hide();
            }
        }
    });

    //attach nextstep button
    $("#next-step").click(function () {

        var $step = $(".wizard-step:visible");

        var validator = $("form").validate();
        var anyError = false;
        $step.find("input").each(function () {
            if (!validator.element(this)) {
                anyError = true;
            }
        });

        var titleError = ValidateTitle();
        if (anyError || titleError)
            return false;

        if ($step.next().hasClass("confirm")) {
            $("#register").show();
            $("#next-step").hide();
            $.post("Register/confirm", $("form").serialize(), function (r) {
                $(".wizard-step.confirm").html(r);
            });
        }

        if ($step.next().hasClass("checkDetails")) {
            $("#submit").show();
            $("#next-step").hide();
            $.post("PlotRegistration/CheckDetails", $("form").serialize(), function (r) {
                $(".wizard-step.checkDetails").html(r);
            });
        }

        if ($step.next().hasClass("wizard-step")) {
            $step.hide().next().fadeIn();
            $("#back-step").show();
        }
        else {
            $("form").submit();
        }
    });

});

function ValidateTitle() {

    var titleOption = $("#Title");
    var OtherTitle = $("#TitleOther");

    if (titleOption.val() == "") {
        titleOption.next("span").removeClass("field-validation-valid").addClass("field-validation-error").html("Must select a valid title.");
        return true;
    }
    else {
        titleOption.next("span").removeClass("field-validation-error").addClass("field-validation-valid");
    }

    if (titleOption.val() == "Other") {
        if (OtherTitle.val().trim() == "") {
            OtherTitle.next("span").removeClass("field-validation-valid").addClass("field-validation-error").html("You must specify a title.");
            return true;
        }
        else {
            OtherTitle.next("span").removeClass("field-validation-error").addClass("field-validation-valid");
        }
    }

    return false;
}

$(document).ready(function () {
    var $h2 = $(".downloads-holder h2");
    $h2.siblings().hide();

   $h2.click(function () {
        $(this).siblings().slideToggle("fast");

    });


//    var zIndexNumber = 1000;
//    $('div').each(function () {
//        $(this).css('zIndex', zIndexNumber);
//        zIndexNumber -= 10;
//    });

//    $(".downloads-holder").removeAttr("style");

});


$(function () {
    var $nav = $("#nav .dropdown");

    $nav.find("li").removeClass("selected");
    var url = location.pathname;
    $nav.find("li").has("a[href=" + url + "]").addClass("selected").parents("li.main").addClass("selected");


    var subNav = $nav.find("li.selected").first().contents().clone().appendTo("#menuRight");

    $("#menuRight ul li ul").has("li.selected").addClass("showSub");

});





