﻿jQuery.jqModweb = {
    /*random: function (o) {
    var defaults = {
    min: 0,
    max: 9999999
    }
    var o = jQuery.fn.extend({}, defaults, o);
    var min = o.min;
    var max = o.max;
    return min + Math.floor((max - min + 1) * (Math.random() % 1));
    },*/
    trimEnd: function (o) {
    }
    ,
    toBool: function (o) {
        if (o===true || o === "true" || o === "True" || o === "1" || o === 1)
            return true;
        else
            return false;
    }
    ,
    toInt: function (o) {
        if (typeof (o) === "undefined" || o == null)
            return null;
        var i = parseInt(o);
        if (i == NaN)
            return null;
        return i;
    }
    ,
    isNullOrEmpty: function (o) {
        if (typeof o === "undefined")
            return true;
        else if (o === null)
            return true;
        else if (o === "")
            return true;
        else if ((typeof o.length !== "undefined") && o.length > 0)
            return false;
        return true;
    }
    ,
    debug:
    {
        write: function (o) {
            if (!jQuery.jqModweb.debug._consoleLog('DEBUG: ' + o)) {
                jQuery.jqModweb.debug._messageBox('DEBUG: ' + o);
            }
        },
        log: function (o) {
            jQuery.jqModweb.debug._consoleLog('LOG: ' + o);
        },
        warning: function (o) {
            jQuery.jqModweb.debug._consoleLog('WARNING: ' + o);
        },
        validation: function (o) {
            jQuery.jqModweb.debug._consoleLog(o);
            jQuery.jqModweb.debug._messageBox(o);
        },
        error: function (o) {
            jQuery.jqModweb.debug._consoleLog('ERROR: ' + o);
            jQuery.jqModweb.debug._messageBox('ERROR: ' + o);
        },
        _consoleLog: function (o) {
            var hasConsole = typeof console !== "undefined";
            if (hasConsole == true) {
                // write to firebug.
                console.log(o);
            }
            return hasConsole;
        },
        _messageBox: function (o) {
            alert(o);
        }
    }
}
jQuery.fn.extend({
    modwebFadeInOut: function (o) {
        //alert('bb');
        //jQuery(this).each(function() {
        //alert('b');
        if (o === "stop") {
            //alert('stopping');
            jQuery(this).attr({ 'x-jquery-modwebFadeInOut-stop': 'yes' });
            jQuery.jqModweb.debug.log("jQuery.fn.modwebFadeInOut cycle stopped.");
        }
        else {
            jQuery.jqModweb.debug.log("jQuery.fn.modwebFadeInOut cycle started.");

            jQuery(this).attr({ 'x-jquery-modwebFadeInOut-stop': 'no' });
            var cycle = function (me, speed) {
                me.fadeTo(speed, 0.0, function () {
                    me.fadeTo(speed, 0.5, function () {
                        if (me.attr('x-jquery-modwebFadeInOut-stop') == 'no') {
                            // cycle again.
                            cycle(me, speed);

                            jQuery.jqModweb.debug.log("jQuery.fn.modwebFadeInOut cycle repeated.");
                        }
                        else {
                            // stopped.
                            me.fadeTo(speed, 1.0);
                        }
                    });
                });
            };

            // initial start.
            var speed = o;
            if (typeof speed === 'undefined') {
                speed = 'normal';
                //alert('here');
            }
            cycle(jQuery(this), speed);
        }
        //});
        return jQuery(this);
    }
});



/* JQUERY EXTENSIONS */
jQuery.fn.extend({
    modwebGetFixedPositionLeft: function (o) {
        var containerOffsetLeft = jQuery(this).get(0).offsetLeft;
        // and the ancestors.
        var a = jQuery(this).get(0).offsetParent;
        while (a != null) {
            containerOffsetLeft += a.offsetLeft;
            a = a.offsetParent;
        }
        return containerOffsetLeft;
    },
    modwebGetFixedPositionTop: function (o) {
        var containerOffsetTop = jQuery(this).get(0).offsetTop;
        // and the ancestors.
        var a = jQuery(this).get(0).offsetParent;
        while (a != null) {
            containerOffsetTop += a.offsetTop;
            a = a.offsetParent;
        }
        return containerOffsetTop;
    },
    modwebLoader: function (o) {
        var defaults = {
            text: 'Loading, please wait...'
            , html: null
            , coverBorder: false
            , position: 'absolute' // warning: only set to 'fixed' or 'absolute' to match the container you are applying to.
            , overlayColor: '#888888' // the background color of the overlay.
            , overlayOpacity: 0.4 // the background color of the overlay.
            //, icon: 'bar' // see /_framework/images/loaders/ for a list of icons. no extensions.
        };
        o = jQuery.fn.extend({}, defaults, o);
        var overlay = jQuery('<div></div>');


        var containerOffsetLeft = jQuery(this).modwebGetFixedPositionLeft();
        if (!o.coverBorder)
            containerOffsetLeft += parseInt(jQuery(this).css('border-left-width'));

        var containerOffsetTop = jQuery(this).modwebGetFixedPositionTop();
        if (!o.coverBorder)
            containerOffsetTop += parseInt(jQuery(this).css('border-top-width'));


        var overlayWidth = jQuery(this).width();
        if (o.coverBorder)
            overlayWidth += parseInt(jQuery(this).css('border-left-width')) + parseInt(jQuery(this).css('border-right-width'));
        overlay.width(overlayWidth);


        var overlayHeight = jQuery(this).height();
        if (o.coverBorder)
            overlayHeight += parseInt(jQuery(this).css('border-top-width')) + parseInt(jQuery(this).css('border-bottom-width'));
        overlay.height(overlayHeight);


        overlay.css({
            'border': 'none'
            , 'position': o.position
            , 'left': containerOffsetLeft
            , 'top': containerOffsetTop
            , 'opacity': o.overlayOpacity
            , 'background-color': o.overlayColor
            , 'z-index': '3001'
        });
        jQuery('body').append(overlay);



        // the payload container.
        height = jQuery(this).height();
        var payload = jQuery('<div></div>');
        payload.html('<div style="width:220px;height:19px;margin-bottom:6px;"><img src="/_framework/images/loaders/bar.gif" /></div>');
        if (o.html)
            payload.html(o.html);
        else
            payload.append(o.text);
        payload.css({
            'border': '3px solid #333333'
            , 'padding': '12px'
            , '-moz-border-radius': '10px'
            , '-webkit-border-radius': '10px'
            , 'border-radius': '10px'
            , 'position': o.position
            , 'left': containerOffsetLeft
            , 'top': containerOffsetTop
            , 'text-align': 'center'
            , 'background-color': '#bbbbbb'
            , 'color': '#333333'
            , 'z-index': '3001'
            , 'font-weight': 'bold'
            , 'font-size': '14px'
            , 'font-family': 'sans-serif'
        });
        payload.hide();
        jQuery('body').append(payload);
        payload.ready(function () {
            payload.show();
            payload.css({ 'margin-top': overlay.height() / 2 - payload.get(0).offsetHeight / 2 }); // center the height.
            payload.css({ 'margin-left': overlay.width() / 2 - payload.get(0).offsetWidth / 2 }); // center the width.
        });



        // remove the loader if the container gets removed.
        jQuery(this).bind('remove', function () {
            payload.remove();
            overlay.remove();
        });


        return this;
    }
});
