$j(document).ready(function () {

     
    $j("#date").DatePicker({
        format: "d/m/Y",
        date: $j("#date").val(),
        current: $j("#date").val(),
        starts: 1,
        position: "b",
        onBeforeShow: function () {
            $j("#date").DatePickerSetDate($j("#date").val(), true);
        }, onChange: function (formated, dates) {
            $j("#date").val(formated);
            $j("#date").DatePickerHide();
        }
    });
    $j(".sideButton").click(function () {
        $j("#date").DatePickerShow();
        return false;
    });
    $j(window).load(function () {
        if ($j("#allVenues").attr("checked")) {
            $j(".checkbox").attr("checked", "checked");
        }
    });
    $j("#allVenues").click(function () {
        if ($j("#allVenues").attr("checked")) {
            $j(".checkbox").attr("checked", "checked");
        } else {
            $j(".checkbox").attr("checked", "");
        }
    });
    var bindBehaviors = function (scope) {
        $j(".availability-popup").click(function () {
            window.open($j(this).attr("href"));
            return false;
        });
        $j(".view-details").click(function () {
            $j(this).parent().parent().parent().slideUp(500, function () {
                $j(this).addClass("resultContainerExpanded");
                $j(".view-details", this).hide();
                $j(this).slideDown(1000);
            });
            return false;
        });
        $j(".hideDetails").click(function () {
            $j(this).parent().parent().parent().parent().slideUp(500, function () {
                $j(this).removeClass("resultContainerExpanded");
                $j(".view-details", this).show();
                $j(this).slideDown(500);
            });
            return false;
        });
        $j(".additionalServiceCheckbox").click(function () {
            if ($j(this).attr("checked") == true) {
                $j(this).closest("form").find(".exVatInput").val(($j(this).closest("form").find(".exVatInput").val() * 1) + ($j(this).attr("rel") * 1));
                $j(this).closest("form").find(".incVatInput").val(($j(this).closest("form").find(".incVatInput").val() * 1) + ($j(this).attr("rel") * 1.175));
            } else {
                $j(this).closest("form").find(".exVatInput").val(($j(this).closest("form").find(".exVatInput").val() * 1) - ($j(this).attr("rel") * 1));
                $j(this).closest("form").find(".incVatInput").val(($j(this).closest("form").find(".incVatInput").val() * 1) - ($j(this).attr("rel") * 1.175));
            }
            $j(this).closest("form").find(".exVatInput").format({
                format: "#,###.00",
                locale: "gb"
            });
            $j(this).closest("form").find(".incVatInput").format({
                format: "#,###.00",
                locale: "gb"
            });
            $j(this).closest("form").find(".exVAT").html("Total cost: &pound;" + $j(this).closest("form").find(".exVatInput").val());
            $j(this).closest("form").find(".incVAT").html("Total cost: &pound;" + $j(this).closest("form").find(".incVatInput").val());
            $j(this).closest("form").find(".exVAT").fadeOut("fast", function () {
                $j(".exVAT").fadeIn("fast");
            });
            $j(this).closest("form").find(".incVAT").fadeOut("fast", function () {
                $j(".incVAT").fadeIn("fast");
            });
            $j(this).closest("form").find(".exVatInput").format({
                format: "####.00",
                locale: "gb"
            });
            $j(this).closest("form").find(".incVatInput").format({
                format: "####.00",
                locale: "gb"
            });
        });
    };
    var cacheValue = $j.DOMCached.get("recordSet");
    if (cacheValue && window.location.hash == "#results") {
        $j("#col2AvailabilityContent").html(cacheValue);
        $j("#delegatesMain").val($j.DOMCached.get("delegates"));
        $j("#date").val($j.DOMCached.get("date"));
        $j("#duration").val($j.DOMCached.get("duration"));
        $j("#roomLayout").val($j.DOMCached.get("roomLayout"));
        $j(".checkbox").attr("checked", "checked");
    }


    //Sets the messages that appear on the form labels to nowt
    jQuery.validator.messages.required = "";

    //
    // front end validation of form submission
    // submitHandler: fires when successful form sent through
    //
    $j('#availabilitySearchForm').validate({
        invalidHandler: function(e, validator) {
            var errors = validator.numberOfInvalids();
            if (errors) {
                var message = errors == 1
                    ? 'You must tell us how many people are coming to your event.'
                    : errors + ' fields have not been completed. They have been highlighted above.';
                $j("div.error span").html(message);
                $j("div.error").show();
            } else {
                $j("div.error").hide();
            }
        },
        rules: {
            delegates: "required"
        },
        submitHandler: function(form) {
          _gaq.push(['_trackPageview', '/left-search-form']);
          // do other stuff for a valid form
           $j("#col2AvailabilityContent").ajaxStart(function () {
            $j(this).empty().html('<img src="/images/ajax-loader.gif" align="center" />');
        });
        var data = {
            "venue[]": [],
            __submit_check: [],
            duration: [],
            date: [],
            delegates: [],
            roomLayout: [],
            ajax: []
        };
        $j("input:checked").each(function () {
            data["venue[]"].push($j(this).val());
        });
        data.__submit_check.push($j("#__submit_check").val());
        data.ajax.push($j("#ajax").val());
        data.duration.push($j("#duration").val());
        data.date.push($j("#date").val());
        data.delegates.push($j("#delegatesMain").val());
        data.roomLayout.push($j("#roomLayout").val());
        $j.ajax({
            type: "POST",
            url: "/availability-checker/search",
            dataType: "html",
            data: data,
            success: function (records) {
                $j("#col2AvailabilityContent").html(records);
                $j("#breadcrumbImage").attr("src", "/images/availability/breadcrumb-availability.png");
                window.location.hash = "#results";
                $j.DOMCached.flush_all();
                $j.DOMCached.set("recordSet", records);
                $j.DOMCached.set("venue", data["venue[]"]);
                $j.DOMCached.set("date", data.date);
                $j.DOMCached.set("roomLayout", data.roomLayout);
                $j.DOMCached.set("duration", data.duration);
                $j.DOMCached.set("delegates", data.delegates);
                Shadowbox.setup();
                bindBehaviors(this);
            }
        });
        return false;
       }
     });
    bindBehaviors(this);
});