﻿/* ********************************************** */
//
//  Namespace: SecretOrange
//
/* ********************************************** */
SecretOrange = {};

SecretOrange.GetDimensions = function (selector, dimensionsOut) {

    var jqObject = $(selector);

    if (jqObject == null || jqObject.length == 0)
        return;

    var v = function (element, css) {
        return Math.max(0, parseInt(element.css(css)) || 0);   // Performing a Math.max here as Chrome was reporting negative margins for some reason         
    }

    var e = jqObject;
    var width = v(e, "width") + v(e, "padding-left") + v(e, "padding-right") + v(e, "margin-left") + v(e, "margin-right");
    var height = v(e, "height") + v(e, "padding-top") + v(e, "padding-bottom") + v(e, "margin-top") + v(e, "margin-bottom");

    dimensionsOut.Height = height;
    dimensionsOut.Width = width;
}


SecretOrange.PostJSON = function (url, data, callback) {
    $.post(url, data, callback, "json");
}

SecretOrange.GetForm = function (param) {
    var type = typeof (param);
    var form;
    switch (type) {
        case "string":
            form = $("#" + param);
            break;
        case "object":
            var tagName = $(param).get(0).tagName.toLowerCase();
            form = tagName == "form" ? $(param) : $(param).parents('form:first');
            break;
    }
    return form;
};






/* ********************************************** */
//
//  Namespace: SecretOrange.Dialog
//
/* ********************************************** */
SecretOrange.Dialog = {};

/* Private fields */
SecretOrange.Dialog._ConfirmElementClicked = null;
SecretOrange.Dialog._ConfirmDialog = null;
SecretOrange.Dialog._ConfirmCallback = null;

SecretOrange.Dialog._TempModalPopUpId = "TempModalPopUp";
SecretOrange.Dialog._TempModalPopUpSelector = "#" + SecretOrange.Dialog._TempModalPopUpId;

SecretOrange.Dialog.RequestAndPop = function (url, data, format, method, options) {

    var httpMethod = method == "post" ? $.post : $.get; // Default 

    $(SecretOrange.Dialog._TempModalPopUpSelector).remove();

    var html = "<div id=\"" + SecretOrange.Dialog._TempModalPopUpId + "\" style=\"display:block;padding:0px;\"><div style=\"text-align:center;padding-top:50px;\">Loading...</div></div>";

    $("body").append(html);

    var modalOptions = {
        resizable: true,
        position: "center",
        modal: true,
        draggable: false
    };

    /*
    if (noclose) {
    options.closeOnEscape = false;
    options.draggable = false;
    options.dialogClass = "hide";
    }*/


    $(SecretOrange.Dialog._TempModalPopUpSelector).dialog(modalOptions);

    if (url.indexOf("html:") == 0) {
        $(SecretOrange.Dialog._TempModalPopUpSelector).html(url.substr(5));
        SecretOrange.Dialog.AutoResize();
    } else {
        httpMethod(url, data, function (data) {
            $(SecretOrange.Dialog._TempModalPopUpSelector).html(data);
            SecretOrange.Dialog.AutoResize();
        }, format);
    }



    return false;
}

SecretOrange.Dialog.PostAndPop = function (url, data, format) {
    SecretOrange.Dialog.RequestAndPop(url, data, format, "post");
}

SecretOrange.Dialog.GetAndPop = function (url, format) {
    SecretOrange.Dialog.RequestAndPop(url, {}, format, "get");
}

SecretOrange.Dialog.PostAndPopJson = function (url, data) {
    SecretOrange.Dialog.PostAndPop(url, data, "json");
}

SecretOrange.Dialog.GetAndPopJson = function (url) {
    SecretOrange.Dialog.GetAndPop(url, "json");
}

SecretOrange.Dialog.PostAndPopHtml = function (url, data) {
    SecretOrange.Dialog.PostAndPop(url, data, "html");
}

SecretOrange.Dialog.GetAndPopHtml = function (url) {
    SecretOrange.Dialog.GetAndPop(url, "html");
}

SecretOrange.Dialog.AutoResize = function () {
    var dialog = new Object();

    dialog.Width = -1;
    dialog.Height = -1;

    SecretOrange.GetDimensions("#jq-resize", dialog);

    if (dialog.Width > 0) {
        $(SecretOrange.Dialog._TempModalPopUpSelector).dialog("option", "width", dialog.Width);
    }

    if (dialog.Height > 0) {
       // $(SecretOrange.Dialog._TempModalPopUpSelector).dialog("option", "height", dialog.Height + 50);
    }

    $(SecretOrange.Dialog._TempModalPopUpSelector).dialog('option', 'position', $(SecretOrange.Dialog._TempModalPopUpSelector).dialog('option', 'position'));
}

// action: can be a function or url
SecretOrange.Dialog.Confirm = function (confirmText, action, callback, onCancel) {

    // Check for stock callbacks
    if (callback == "close") // Perform confirmed action and simply close dialog
        callback = function (ele, dialog, json) { dialog.dialog("close"); };

    if (callback == "reload") // Perform confirmed action and reload the current page
        callback = function (ele, dialog, json) { window.location.reload(true); };

    if (callback == "redirect") // Perform confirmed action and redirec the current page
        callback = function (ele, dialog, json) { window.location.href = json.Data; };

    // Hold a global reference to the callback
    SecretOrange.Dialog._ConfirmCallback = typeof (callback) != "undefined" ? callback : null;

    // Create the confirm dialog html
    var dialogHtml = "<div style='display:none;' class='popup-container'>" + confirmText + "</div>";

    if (confirmText.indexOf("#") == 0)
        dialogHtml = confirmText;


    // Create the actual dialog and hold a reference to it
    SecretOrange.Dialog._ConfirmDialog = $(dialogHtml).dialog({
        resizable: false,
        modal: true,
        buttons: {
            "OK": function () {
                if (typeof (action) == "function") {
                    action();

                    if (SecretOrange.Dialog._ConfirmCallback != null)
                        SecretOrange.Dialog._ConfirmCallback(SecretOrange.Dialog._ConfirmElementClicked, SecretOrange.Dialog._ConfirmDialog, null);

                } else {

                    // Perform the ajax operation
                    SecretOrange.PostJSON(action, {}, function (json) {

                        if (SecretOrange.Dialog._ConfirmCallback != null)
                            SecretOrange.Dialog._ConfirmCallback(SecretOrange.Dialog._ConfirmElementClicked, SecretOrange.Dialog._ConfirmDialog, json);

                        SecretOrange.Dialog._ConfirmElementClicked = null;
                        SecretOrange.Dialog._ConfirmDialog = null;
                        SecretOrange.Dialog._ConfirmCallback = null;
                    });
                }
            },
            Cancel: function () {
                if (typeof (onCancel) != "undefined")
                    onCancel();

                $(this).dialog("close");
            }
        }
    });
}

SecretOrange.Dialog.ConfirmAndPostJson = function (confirmText, url, data, callback) {
    SecretOrange.Dialog.Confirm(confirmText, function () {
        $.post(url, data, callback, "json");
    }, callback);
}

SecretOrange.Dialog.ConfirmAndGetJson = function (confirmText, url, callback) {
    SecretOrange.Dialog.Confirm(confirmText, function () {
        $.get(url, data, callback, "json");
    }, callback);
}

SecretOrange.Dialog.ConfirmAnPostHtml = function (confirmText, url, data, callback) {
    SecretOrange.Dialog.Confirm(confirmText, function () {
        $.post(url, data, callback, "html");
    }, callback);
}

SecretOrange.Dialog.ConfirmAndGetHtml = function (confirmText, url, callback) {
    SecretOrange.Dialog.Confirm(confirmText, function () {
        $.get(url, data, callback, "html");
    }, callback);
}

SecretOrange.Dialog._ConfirmElementClicked = null;
SecretOrange.Dialog._ConfirmDialog = null;
SecretOrange.Dialog._ConfirmCallback = null;

SecretOrange.Dialog.InitConfirmationsWithAction = function (selector, confirmText, action) {
    SecretOrange.Dialog.InitConfirmations(selector, confirmText, null, action);
}

SecretOrange.Dialog.InitConfirmations = function (selector, confirmText, callback, action) {

    $(selector).click(function () {
        // Hold a reference to the element that was originally clicked
        SecretOrange.Dialog._ConfirmElementClicked = $(this);

        var ajaxUrl = SecretOrange.Dialog._ConfirmElementClicked.attr("data-ajax-url");

        SecretOrange.Dialog.Confirm(confirmText, ajaxUrl, callback);
    });
}



/* ********************************************** */
//
//  Namespace: SecretOrange.Utils
//
/* ********************************************** */
SecretOrange.Utils = {};

SecretOrange.Utils.RefreshPage = function () {
    document.location.reload();
};



(function ($) {
    $.fn.outerHTML = function () {
        return $(this).clone().wrap('<div></div>').parent().html();
    }
})(jQuery);



// jQuery Input Hints plugin
// Copyright (c) 2009 Rob Volk
// http://www.robvolk.com

jQuery.fn.inputHints = function () {
    // hides the input display text stored in the title on focus
    // and sets it on blur if the user hasn't changed it.

    // show the display text
    $(this).each(function (i) {
        $(this).val($(this).attr('title'))
            .addClass('hint');
    });

    // hook up the blur & focus
    return $(this).focus(function () {
        if ($(this).val() == $(this).attr('title'))
            $(this).val('')
                .removeClass('hint');
    }).blur(function () {
        if ($(this).val() == '')
            $(this).val($(this).attr('title'))
                .addClass('hint');
    });
};


(function ($) {
    // VERTICALLY ALIGN FUNCTION
    $.fn.vAlign = function () {
        return this.each(function (i) {
            var ah = $(this).height();
            var ph = $(this).parent().height();
            var mh = Math.ceil((ph - ah) / 2);
            $(this).css('margin-top', mh);
        });
    };
})(jQuery);
