﻿function RunAjaxRequestByDDLInstance(cmb, additionalParams) {
    // it is the same like LinkButton behavior
    RunAjaxRequestByLinkButtonInstance(cmb, additionalParams);
}

function RunAjaxRequestByLinkButtonInstance(link, additionalParams) {
    RunAjaxRequestByLinkButton($(link).attr("id").replace(/[_]/g, '$'), $(link).attr("refAjaxPanel"), additionalParams);
}

function RunAjaxRequestByLinkButton(controlUniqueId, panelId, additionalParams) {
    // emulate postback target control
    $("input[name=__EVENTTARGET]").val(controlUniqueId);
    var f = $("#mainForm");
    var action = f.attr("action");
    var serializedForm = f.serialize() + ((additionalParams) ? '&' + additionalParams : '');
    $.post(action,
           serializedForm + "&ajax=Y&ajaxpanel=" + panelId,
           ProcessSimpleAjaxSuccessResponse);
    return false;
}

function RunAjaxRequestByButton(btn, panelId, afterContentUpdatedHandler) {
    // emulate postback target control
    $("input[name=__EVENTTARGET]").val($(btn).attr('name'));
    var f = $("#mainForm");
    var action = f.attr("action");
    var serializedForm = f.serialize();
    $.post(action,
           serializedForm + "&" + $(btn).attr('name') + "=" + $(btn).val() + "&ajax=Y&ajaxpanel=" + panelId,
           function(response, status) { ProcessSimpleAjaxSuccessResponse(response, status, afterContentUpdatedHandler); })
    /*.error(function() { window.location.href = 'error.htm'; })*/
           .complete(function(jqXHR) {
               if (jqXHR.status != 200) {
                   //$('body').replaceWith(jqXHR.responseText);
                   var input = $("input#curUrl", $(jqXHR.responseText));
                   if (input) {
                       window.location.href = input.val();
                   }
                   //window.location.href = 'error.htm';
               }
           });
    return false;
}

function ProcessSimpleAjaxSuccessResponse(response, status, afterContentUpdatedHandler) {
    if (response.indexOf('<html') == -1 && response.indexOf('AjaxPanelId') != -1)
    {
        var ajaxResponse = $.parseJSON(response);
        if (ajaxResponse) {
            if (ajaxResponse.RedirectUrl) {
                window.location.href = ajaxResponse.RedirectUrl;
            }
            else {
                $("#" + ajaxResponse.AjaxPanelId).html(ajaxResponse.Content);
                if ($.isFunction(afterContentUpdatedHandler))
                    afterContentUpdatedHandler();
            }
        }
    } else {
        var input = $("input#curUrl", $(response));
        if (input) {
            window.location.href = input.val();
        }
        else {
            window.location.href = 'error.htm';
        }
    }
}

function InitializeNotifyPopup(containerId, w, h, cssClass, header,
                               hideButtonPane, hideButtonSet) {
    var css = cssClass ? cssClass : 'notifyPopup';

    var p = $("#" + containerId).dialog(
                {
                    title: header,
                    autoOpen: false,
                    width: w,
                    height: h,
                    modal: false,
                    show: 'slide',
                    hide: 'slide',
                    dialogClass: css,
                    buttons: {
                        "Cancel": function() {
                            $(this).dialog("close");
                        }
                    }
                }).bind('clickoutside', function(e) {
                    $(this).dialog('close');
                });
                
    // remove the title bar
    $("." + css + " > .ui-dialog-titlebar").hide();
    if (hideButtonPane)
        $("." + css + " > .ui-dialog-buttonpane").hide();
    if (hideButtonSet)    
        $("." + css + " > .ui-dialog-buttonpane > .ui-dialog-buttonset").hide();

    if (!isMozilla)
        p.parent().appendTo($("form:first"));

    p.removeClass('ui-widget-content');
    var main = $("." + css);
    main.removeClass('ui-dialog');
    main.removeClass('ui-widget-content');
    main.css('padding', '0px');

    main.css('position', 'absolute');
    if (!isMozilla)
        main.css('overflow', 'hidden');
    p.dialog('open');

    return p;
}


MiscJQueryUtility = function() {

}

MiscJQueryUtility.prototype = {

    // http://stackoverflow.com/questions/4259815/how-do-i-detect-the-inherited-background-color-of-an-element-using-jquery-js
    getParentColorAttribute: function(jqueryElement, attr) {
        // Is current element's background color set?
        var color = jqueryElement.css(attr);
        if ((color !== 'rgba(0, 0, 0, 0)') && (color !== 'transparent')) {
            // if so then return that color
            return color;
        }

        // if not: are you at the body element?
        if (jqueryElement.is("body")) {
            // return known 'false' value
            return false;
        } else {
            // call getBackground with parent item
            return this.getParentColorAttribute(jqueryElement.parent(), attr);
        }
    },

    getParentCssAttribute: function(jqueryElement, attr) {
        var v = jqueryElement.css(attr);
        if (v) return v;

        if (jqueryElement.is("body")) {
            return false;
        } else {
            return this.getParentCssAttribute(jqueryElement.parent(), attr);
        }
    },
    
    UpdateDialogStyles: function() {
        // applied for all dialogs
        $(".ui-dialog").addClass("TableWithBorders").addClass('dialog-bg').removeClass("ui-widget-content");
        $(".ui-dialog-buttonpane").removeClass("ui-widget-content").addClass('TableWithBorders');
        $(".ui-dialog-titlebar").addClass("TableHeader").removeClass('ui-widget-header');
    },

    InsertDialogToFormElement: function(elem, parent) {
        elem.parent().appendTo($("form:first"));
        var bgColor = this.getParentColorAttribute(parent, "background-color");
        if (!bgColor) bgColor = '#ffffff';
        var color = this.getParentColorAttribute(parent, "color");
        if (!color) color = '#000';
        var p1 = elem.parent();
        var p2 = p1.find(".ui-dialog-buttonpane");
        p1.css("background-color", bgColor); p2.css("background-color", bgColor);
        p1.css("color", color); p2.css("color", color);

        var fontName = this.getParentCssAttribute(parent, "font-family");
        var fontSize = this.getParentCssAttribute(parent, "font-size");
        var fontWeight = this.getParentCssAttribute(parent, "font-weight");
        if (fontName) {
            p1.css("font-family", fontName); p2.css("font-family", fontName);
        }
        if (fontSize) {
            p1.css("font-size", fontSize); p2.css("font-size", fontSize);
        }
        if (fontWeight) {
            p1.css("font-weight", fontWeight); p2.css("font-weight", fontWeight);
        }
    },

    bung: function() { }
}
