﻿// Have to check this because IE is all like "I'm a special snowflake, I deserve different treatment!"
var OnChangeEventName = $.browser.msie ? "propertychange" : "change";

 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() {
    // Any element with a title shows it's title inside of the control panel tips div
    $("*[title]:not([rel='post']):not(" + overlayTextSelector + ")").not($(".buttons a")).hover(
        function() {
            $(controlPanelTipsSelector).html($(this).attr("title"));
        },
        function() {
            $(controlPanelTipsSelector).html("");
        }
    );

    // 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();
    });

    $(delayedShowOverlaySelector).click(function() {
        $(this).filter(overlayTextSelector).each(function() {
            $(pleaseWaitSelector).find("p").text($(this).attr("title"));
        });
        DelayedShowOverlay();
    });

    // Any link that has rel post will POST to it's href
    $("a[rel='post']").click(
        function(e) {
            e.preventDefault();
            if (confirm($(this).attr('title'))) {
                var f = document.createElement('form');
                f.style.display = 'none';
                this.parentNode.appendChild(f);
                f.method = 'post';
                f.action = this.href;
                f.submit();
            }
            else {
                HideOverlay();
            }
        }
    );

    if (!($.browser.msie && $.browser.version < 7)) {
        $(window).scroll(function() {
            var element = $("#content-right");
            var windowJQuery = $(window);
            var windowTop = windowJQuery.scrollTop();
            var difference = windowTop - headerHeight;
            if (difference > 0) {
                var top = windowTop + 15 - headerHeight;
                element.css("top", top + "px");
            }
            else {
                element.css("top", "0px");
            }
        });
    }

    $(":input[type!='hidden']:not([type='submit']):not([type='button']):not(.no-focus):first").focus();
});

function DelayedShowOverlay() {
    $(overlaySelector).oneTime(delayOverlay, overlayShowTimer, ShowOverlay);
}

function ShowOverlay() {
    if (!cancelled) {
        var overlay = $(overlaySelector);
        var pleasewait = $(pleaseWaitSelector);
        overlay.show();
        pleasewait.show();
        overlay.css("height", $(document).height());
        
        if ($.browser.msie && $.browser.version < 7) {
            pleasewait.css("top", $(window).height() / 2);
            pleasewait.css("left", $(window).width() / 2);
        }
    }
};

function HideOverlay() {
    var overlay = $(overlaySelector);
    overlay.hide();
    cancelled = true;
};

function DelaySelector(selector, url, data, selectorCallback) {
    ///	<summary>
    ///		Show visual wait and execute the action at url.
    ///	</summary>
    /// <param name="selector" type="Jquery.Selector">JQuery Selector of the select.</param>
    ///	<param name="url" type="String">The URL of the HTML page to load.</param>
    ///	<param name="data" optional="true" type="Map">Key/value pairs that will be sent to the server.</param>
    ///	<param name="callback" optional="true" type="Function">The function called when the AJAX request is complete.  It should map function(responseText, textStatus) such that this maps the options for this AJAX request.</param>
    
    if ($('#DelaySelector' + selector.id).length == 0) {
        var imageX = new Image();
        imageX.src = "/Content/Images/Jquery.Treeview/Spinner.gif";
        imageX.id = "DelaySelector" + selector.id;             
        selector.after(imageX);

        $.post(url, data, function(d) { HideDelaySelector(selectorCallback, d, $('#DelaySelector' + selector.id)); });

    }
};

function HideDelaySelector(selectorCallback, data, jquerySelector) {
    selectorCallback(data);
    jquerySelector.remove();
};

