﻿var _theForm;
var _theForm_oldSubmit;
var _theForm_submit;
var _theForm_oldOnSubmit;
var _theForm_onsubmit;
var ___doPostBack;
var jqSettings;

$(document).ready(function() {
    ___doPostBack = __doPostBack;

    _theForm = theForm;
    _theForm_oldSubmit = theForm.oldSubmit;
    _theForm_submit = theForm.submit;
    _theForm_oldOnSubmit = theForm.oldOnSubmit;
    _theForm_onsubmit = theForm.onsubmit;

    installSafetecDoPostback();
    jqSettings = eval("(" + $("#ctl00_divJQSettings").text() + ")");

    $(".submit a").click(function(event) {
        if ($(this).hasClass("submitted")) {
            event.preventDefault();
            return false;
        } else {
            $(this).addClass("submitted");
            return true;
        }
    });

    $(".confirm").click(function(event) {
        if (!confirm(jqSettings.session.confirmMessage)) {
            event.stopPropagation();
            event.preventDefault();
            return false;
        }
    });

    if ($.browser.msie) {
        $(".expanding-select").mousedown(function() {
            $(this).css('position', 'absolute').css('width', 'auto');
        });
        $(".expanding-select").blur(function() {
            $(this).css('position', '').css('width', '');
        });
        $(".expanding-select").change(function() {
            $(this).css('position', '').css('width', '');
        });
    }
});

jQuery.fn.ajaxLoading = function(msg) {
    $(this).empty().html('<span class="ajax-loader">'+msg+'</span>');
};

jQuery.fn.highlight = function(str, className) {
    var regex = new RegExp("(" + str + ")", "gi");
    return $(this).each(function() {
        this.innerHTML = this.innerHTML.replace(regex, "<span class='" + className + "'>\$1</span>");
    });
};

$.fn.serializeObject = function() {
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name]) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

function LoadScript(url) {
    var e = document.createElement("script");
    e.src = url;
    e.type = "text/javascript";
    document.getElementsByTagName("head")[0].appendChild(e);
}

function installSafetecDoPostback() {
    __doPostBack = function(eventTarget, eventArgument) { // Overload DoPostBack Function    
        cleanInputs();
        ___doPostBack(eventTarget, eventArgument);
        //PostBack(eventTarget, eventArgument);
    };
}

function RestoreOriginalForm() {
    theForm = _theForm;
    theForm.oldSubmit = _theForm_oldSubmit;
    theForm.submit = _theForm_submit;
    theForm.oldOnSubmit = _theForm_oldOnSubmit;
    theForm.onsubmit = _theForm_onsubmit;
}

/*function PostBack(eventTarget, eventArgument, postBackForm) {
    cleanInputs();

    if (postBackForm) {
        theForm = postBackForm;
    }

    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }

    if (postBackForm) { // Reset Original Form
        RestoreOriginalForm();
    } 
}*/

function cleanInputs() {
    $('input[type=text], textarea').each(function() { 
        var $textbox = $(this);
        var value = $textbox.val();
        value = value.replace(/<([^ ])/g, '< $1');
        value = value.replace(/([^ ])>/g, '$1 >');
        $textbox.val(value);
    });
}

function htmlEncode(text) {
    var textneu = text.replace(/&/g, "&amp;");
    textneu = textneu.replace(/</g, "&lt;");
    textneu = textneu.replace(/>/g, "&gt;"); 
    return textneu;
}

function prependErrorMessageIfExists(text) {
    if ($("div[id*=fsetMessageError]").length > 0) {
        $("div[id*=fsetMessageError] span").prepend(text + "<br />");
    }
}

function appendErrorMessageIfExists(text) {
    if ($("div[id*=fsetMessageError]").length > 0) {
        $("div[id*=fsetMessageError] span").append("<br />" + text);
    }
}

var current = 0;
$(document).ready(function() {

    /* Drop Down Buttons */
    $(".dropdown-button ul").bgiframe();
    $(".dropdown-button").hover(function() { $("ul", this).addClass("hover"); }, function() { $("ul", this).removeClass("hover"); });
    $(".dropdown-button").click(function() { $("ul", this).toggleClass("hover"); });

    /* Auto Docking */
    $('.auto-dock').each(function() {
        var $float_this = $(this);
        var $place_holder = $(document.createElement("div")).hide();
        var is_floating = false;

        var this_top = Math.round($float_this.offset().top);
        var this_height = $float_this.outerHeight(true);
        var this_inner_height = $float_this.outerHeight();
        var this_width = $float_this.outerWidth(true);

        $float_this.after($place_holder);

        $place_holder.css({ 'height': this_height, 'width': this_width });

        var fadeSpeed = 200, fadeTo = 0.90, topDistance = 30;
        var topbarME = function() { $float_this.fadeTo(fadeSpeed, 1); };
        var topbarML = function() { $float_this.fadeTo(fadeSpeed, fadeTo); };
        var park = 0;

        $(window).scroll(function() {
            if ($(window).scrollTop() + park > this_top) {
                if (is_floating == false) {
                    park = current;
                    $float_this.addClass("floated-menu");
                    $place_holder.show();
                    $float_this.css('top', park + 'px');
                    $float_this.css('width', this_width);                    

                    topbarML();
                    $float_this.bind('mouseenter', topbarME);
                    $float_this.bind('mouseleave', topbarML);
                    current += this_inner_height;
                    is_floating = true;

                    if ($.browser.msie && $.browser.version.substr(0, 1) < 7) { /* Hack for IE6 for fixed positioning */
                        $float_this.css('position', 'absolute');
                    }                   
                }

                if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {  /* Hack for IE6 for fixed positioning */
                    $float_this.css('top', $(this).scrollTop() + "px");
                }
            } else {
                if (is_floating == true) {
                    $place_holder.hide();
                    $float_this.removeClass("floated-menu");
                    $float_this.css('top', '');
                    $float_this.css('width', '');
                    $float_this.css('position', '');

                    this_top = Math.round($float_this.offset().top);
                    this_height = $float_this.outerHeight(true);

                    topbarME();
                    $float_this.unbind('mouseenter', topbarME);
                    $float_this.unbind('mouseleave', topbarML);
                    current -= this_inner_height;
                    is_floating = false;
                }
            }
        });

    });
});