﻿$(document).ready(function () {
    var $brandsFlyout = $("#brands .flyout");

    $("#menu .open").hoverIntent(
        {
            timeout: 150,
            sensitivity: 20,
            over: function (e) {
                //e.stopPropagation(); 

                $("a", this).addClass('active');

                var $flyout = $(this).children(".level1");

                $flyout.show();
            },
            out: function (e) {
                //e.stopPropagation(); 

                $("a", this).removeClass('active');

                var $flyout = $(this).children(".level1");
                $flyout.hide();
            }
        }
    );

    $("#brands .open").hoverIntent(
        {
            timeout: 150,
            sensitivity: 10,
            over: function (e) {
                //e.stopPropagation(); 
                $brandsFlyout.show();
                $brandsFlyout.position({
                    my: "center bottom",
                    at: "center top",
                    of: $(this)
                });


            },
            out: function (e) {
                //e.stopPropagation(); 

                $brandsFlyout.hide();
            }
        }
    );



    $('input, textarea').placeholder();

    // placeholder
    $("form").submit(function (e) {
        $form = $(this);

        var $inputs = $('.placeholder', this).each(function () {
            var $input = $(this);
            if ($input.val() === $input.attr('placeholder')) {
                if ($input.data('placeholder-password')) {
                    $input.hide().next().show().focus().attr('id', $input.removeAttr('id').data('placeholder-id'));
                } else {
                    $input.val('').removeClass('placeholder');
                }
            };
        });

        //var $val = $form.validate();
        var $valid = $form.valid();
        if ($valid)
            return true;

        e.preventDefault();
        //$form.showErrors();
    });

    //form setup
    $("form").each(function () {
        var settings = $(this).data('validator').settings;
        settings.ignore += ",:disabled,.ignore";
        //validator.ignore = ".ignore";

        //$(this).data('validator', validator)
    });

    //re-set all client validation    
    $.fn.resetValidation = function () {
        //reset jQuery Validate's internals        
        $(this).validate().resetForm();

        //reset unobtrusive validation summary, if it exists        
        $(this).find("[data-valmsg-summary=true]")
            .removeClass("validation-summary-errors")
            .addClass("validation-summary-valid")
            .find("ul")
            .empty();

        //reset unobtrusive field level, if it exists        
        $(this).find("[data-valmsg-replace]")
            .removeClass("field-validation-error")
            .addClass("field-validation-valid")
            .empty();
    };

    // activator
    $(this).find('input:.activator').each(function () {
        var $button = $(this);
        var $form = $button.closest('form');
        //        var validator = $form.data('validator');
        //        alert(validator.settings.ignore);
        //        $form.validate({
        //            ignore: ".ignore"
        //        });

        var $data = $(this).data("activator");

        var $target = $("#" + $data.id);
        if ($target) {
            
            var disable = null;
            var enable = null;

            if ($data.mode == "hide") {
                disable = function () {
                    $target.hide();
                    $target.find(":input").addClass("ignore");
                };
                enable = function () {
                    $target.show();
                    $target.find(":input").removeClass("ignore");
                };
            } else {
                disable = function () {
                    $target.attr("disabled", true);
                    $target.find(":input").addClass("ignore");
                };
                enable = function () {
                    $target.removeAttr("disabled");
                    $target.find(":input").removeClass("ignore");
                };
            }

            if ($button.is(":radio")) {
                if ($("input:radio:checked[@name=" + $button.attr("name") + "]").val() != $button.val()) {
                    disable();
                }

                $("input:radio[@name=" + $button.attr("name") + "]").change(function () {
                    $form.resetValidation();

                    if ($(this).val() == $button.val())
                        enable();
                    else
                        disable();
                });
            } else {
                if ($button.not(":checked")) {
                    disable();
                }

                $button.change(function () {
                    $form.resetValidation();

                    if ($(this).is(':checked'))
                        disable();
                    else
                        enable();
                });
            }
        }
    });

});

 
