﻿var controlPanelTipsSelector = "#control-panel-tips-title";
var headerHeight = 177;
var overlaySelector = "#overlay";
var pleaseWaitSelector = "#please-wait";
var overlayShowTimer = "overlayShowTimer";
var delayedShowOverlaySelector = ":submit[class!='no-overlay'], a[class!='no-overlay']:not([rel='external'])";
var delayOverlay = 2000;
var cancelled = false;
var overlayTextSelector = ".overlay-text";


// NOTES FOR OVERLAY
// - To not have an overlay on a link or input, give it the class "no-overlay"
// - To add a text between "Please wait..." and the gif, add a title and the class "overlay-text" to an element
//
//



///<reference path="Jquery-vsdoc.js" />
// Methods to apply when the document is read
$(document).ready(function() {
    // Make the cancel links into buttons for people that have javascript
    $("a[rel='cancel']").each(function(e) {
        var element = $(this);
        var button = document.createElement("input");
        button.type = "button";
        button.value = element.html();
        $(button).click(function() {
            window.location.href = element.attr("href");
        });
        element.replaceWith(button);
    });

    // Any link that has rel external opens a new tab / window
    $("a[rel='external']").click(
        function(e) {
            e.preventDefault();
            window.open(this.href);
        }
    );

    $("[class^='expands-']").click(function(e) {
        var element = $(this);
        var className = element.attr("class");
        var id = className.replace("expands-", "");
        $("#" + id).slideToggle();
    });

    // Any link that has rel post will POST to it's href
    $("a[rel='post']").click(
        function(e) {
            e.preventDefault();
            var title = $(this).attr('title')
            var hasTitle = typeof title != "undefined" && title != null && title != "";
            if (!hasTitle || confirm(title)) {
                var f = document.createElement('form');
                f.style.display = 'none';
                this.parentNode.appendChild(f);
                f.method = 'post';
                f.action = this.href;
                f.submit();
            }
        }
    );

    // Prevent to search an empty string
    $("#search-box #go-search").click(function(e) {
        if ($("#search-box #faq-search").val() == "") {
            e.preventDefault();
            $("#search-box #faq-search").focus();
        }
    });

    // Create click events for unimportant options
    $(".unimportant legend").click(function() {
        var legend = $(this);

        legend.each(function() {
            var temp = $(this).toggleClass("closed").parent().children().not("legend");
            temp.slideToggle("slow");
        });
    });
    $(".unimportant:not(.opened) legend").toggleClass("closed").parent().children().not("legend").hide();

    $(":input[type!='hidden']:not([type='submit']):not([type='button']):not(.no-focus):first").focus();

    $("#productTreeView.treeview input[type=checkbox]").change(function() {
        var chekVal = $(this).attr('checked');
        $(this).parent().parent().children("ul").children().each(function() {
            $(this).children().each(function() {
                $(this).children("input[type=checkbox]").each(function() {
                    if ($(this).attr('disabled') == false) {
                        $(this).attr('checked', chekVal);
                    }
                });
            });
        });
        HandleTreeViewChange(this)
    });

    $("#faq-search, #faq-search1").autocomplete({
        source: function(req, add) {
            $.getJSON("/AutoComplete/Search", { q: req.term }, function(data) { add(data); });
        },
        delay: 200
    });

});

function HandleTreeViewChange(element) {
    var parentLi = $(element).parent().parent().parents("li:first");
    if (parentLi.find("> ul input:checked").length > 0) {
        parentLi.find("> span input[type=checkbox]").attr("checked", true);
    }
    else {
        parentLi.find("> span input[type=checkbox]").attr("checked", false);
    }
}


$(document).ready(function() {
    $("#go-search").click(function(e) {
        if ($("#go-search-box").val() == "") {
            e.preventDefault();
        }
    });
});