﻿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;
        /*if (min === undefined)
        min = 0;
        if (max === undefined)
        max = 9999999;*/

        return min + Math.floor((max - min + 1) * (Math.random() % 1));
    },
    trimEnd: function(o) {
    }
    ,
    toBool: function(o) {
        if (o === "true" || o === "True" || o === "1" || o === 1)
            return true;
        else
            return false;
    }
    ,
    isNullOrEmpty: function(o) {
        if (typeof o === "undefined")
            return false;
        else if (o === null)
            return false;
        else if (o === "")
            return false;
        else if ((typeof o.length !== "undefined") && o.length > 0)
            return true;
        return false;
    }
    ,
    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);
        },
        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);
    }
});

