﻿
var SUCCESS = 1;
var FAILURE = -1;

// Track loaded URLs
var loadedCss = {};
var loadedJs = {};
var getParams = {};
var postParams = {};

function getParamsToUrl() {
    var url = "";

    for (var name in getParams) {
        url += "&" + name + "=" + getParams[name];
    }

    return url;
}

function mergeGetParams(params) {
    if (!params) return;
    $.extend(getParams, params);
}

function mergePostParams(params) {
    if (!params) return;
    $.extend(getParams, params);
}

// Applies a semi-blocked-out loading area
// over a part of the page that is loading
function loadWithLoader(target, url, callback) {

    // Setup the loader cover
    var offset = target.offset();
    var width = target.width();
    var height = target.height();
    $("body").append("<div class='loading-block'></div>");
    var loader = $(".loading-block").width(width).height(height);
    loader.offset(offset);

    target.css("opacity", 0.5);

    // Load the url
    target.load(url, function () {
        target.css("opacity", 1);
        loader.remove();

        // Call any function callback
        if (callback) callback();
    });
}

// Applies a semi-blocked-out loading area
// over a part of the page that is loading
function postWithLoader(target, url, params, callback) {

    // Setup the loader cover
    var offset = target.offset();
    var width = target.width();
    var height = target.height();
    $("body").append("<div class='loading-block'></div>");
    var loader = $(".loading-block").width(width).height(height);
    loader.offset(offset);

    target.css("opacity", 0.5);

    // Load the url
    $.post(url, params, function (data) {
        target.css("opacity", 1);
        loader.remove();

        if (callback)
            callback(data);
    });
}

// Creates a floating message window for the user
// [requires: jquery.blockUI.js]
function showMessage(body, hideClose, leftAlign, width, height) {
    if (!hideClose) $("#modal-close").show();
    else $("#modal-close").hide();

    $("#modal-body").get(0).innerHTML = body;
    $("#modal-body").css("text-align", leftAlign ? "left" : "center");

    $.blockUI({ message: $("#modal"),
                fadeIn: 0,
                fadeOut: 0,
                css: { cursor: "inherit", 
                       background: "none", 
                       border: "none" },
                overlayCSS: { cursor: "inherit" }
                });

    if (!hideClose) $('.blockOverlay').click($.unblockUI);

    if (width) $("#modal").css("width", width);
    if (height) $("#modal").css("height", height);
}

// Creates a floating notice message that fades in and out
function showNotice(body, delayTime, fadeInTime, fadeOutTime) {
    $("#modal-close").hide();
    $("#modal-body").get(0).innerHTML = body;

    $.blockUI({ showOverlay: false,
                fadeIn: fadeInTime ? fadeInTime : 500,
                fadeOut: fadeOutTime ? fadeOutTime : 500,
                timeout: delayTime ? delayTime : 6000,
                message: $("#modal"), 
                css: { cursor: "inherit", background: "none", border: "none"} });
}

// Loads in an external CSS file
function getCss(url) {
    // Don't reload a URL
    if (loadedCss[url]) return false;

    $("head").append("<link>");
    var css = $("head").children(":last");
    css.attr({
        rel: "stylesheet",
        type: "text/css",
        href: url
    });

    // Mark as loaded
    loadedCss[url] = true;
    return true;
}

// Loads in an external javascript file
function getScript(url, callback) {

    // Don't reload a URL
    if (!loadedJs[url]) {
        $.getScript(url, callback);

        // Mark as loaded
        loadedJs[url] = true;
    }
    else if (callback) {
        callback();
    }
}

// Scrolls to a named anchor with the given speed
function scrollToAnchor(name, speed, callback) {
    var target = $("a[name=" + name + "]");
    scrollTo(target, speed, function () {
        //window.location.hash = name;
        if (callback) callback();
    });
}

// Scrolls to an element with the given speed
function scrollTo(target, speed, callback) {
    speed = speed ? speed : 1100;
    var destination = target.offset().top - ($(window).height() / 5);
    $("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination }, speed, function () {
        if (callback) callback();
    });
}

// Used to store input elements that have been focused
if (typeof (focusedElements) == "undefined")
    focusedElements = {};

$(function () {
    $("input.auto").focus(function () { 
        onFocus($(this));
    });

    $("input.auto").change(function () {
        $(this).addClass("focused");
        focusedElements[$(this).attr("name")] = true;
    });

    $("input.auto").blur(function () {
        onBlur($(this));
    });
});

function onFocus(target) {
    target.addClass("focused");
    if (focusedElements[target.attr("name")]) return;
    target.val("");
    focusedElements[target.attr("name")] = true;
}

function onBlur(element) {
    var target = $(element);
    //target.removeClass("focused");
}

(function ($) {

    $.fn.extend({
        loadWithLoader: function (url, callback) {
            // Setup the loader cover
            var target = $(this);
            var offset = target.offset();
            var width = target.width();
            var height = target.height();
            $("body").append("<div class='loading-block'></div>");
            var loader = $(".loading-block").width(width).height(height);
            loader.offset(offset);

            target.css("opacity", 0.5);

            // Load the url
            target.load(url, function () {
                target.css("opacity", 1);
                loader.remove();

                // Call any function callback
                if (callback) callback();
            });
        },
        postWithLoader: function (url, params, callback) {
            // Setup the loader cover
            var target = $(this);
            var offset = target.offset();
            var width = target.width();
            var height = target.height();
            $("body").append("<div class='loading-block'></div>");
            var loader = $(".loading-block").width(width).height(height);
            loader.offset(offset);

            target.css("opacity", 0.5);

            // Load the url
            $.post(url, params, function (data) {
                if (data["Type"] && data.Type == 1 && data["Html"]) target.html(data.Html);
                else target.html(data);
                target.css("opacity", 1);
                loader.remove();

                // Call any function callback
                if (callback) callback(data);
            });
        }
    });

})(jQuery);
