/**
 * @author     Ludek Benedik (www.ludekbenedik.cz)
 * @copyright  Copyright (c) 2010 Ludek Benedik
 * @version    1.0
 */



var NeoMask = {

    defaultId          : '_mask',
    defaultSpeed       : 200, // jQuery 'fast'
    defaultOpacity     : 0.4,
    ajaxLoaderIdSuffix : '-ajax-loader',
    $ajaxLoader        : null,



    show: function(speed, opacity, id)
    {
        speed   = this.getSpeed(speed);
        opacity = this.getOpacity(opacity);
        id      = this.getMaskId(id);

        if (jQuery('#' + id).length > 0) {
            return false;
        }

        var $mask = jQuery('<div/>', {
            id  : id,
            css : {
                display  : 'none',
                position : 'absolute',
                left     : 0,
                top      : 0,
                width    : jQuery(window).width(),
                height   : jQuery(document).height(),
                zIndex   : 9000,
                backgroundColor : '#000'
            }
        })

        $mask.appendTo('body').fadeTo(speed, opacity);

        return true;
    },



    hide: function(speed, id)
    {
        speed = this.getSpeed(speed);
        id    = this.getMaskId(id);
        jQuery('#' + id).fadeOut(speed, function() {jQuery(this).remove()});
    },



    initAjaxLoader: function(imagePath, alt)
    {
        if (typeof alt === 'undefined') {
            alt = 'Processing...';
        }

        this.$ajaxLoader = jQuery('<img/>', {
            src : imagePath,
            alt : alt,
            css : {
                display  : 'none',
                position : 'fixed',
                zIndex   : 9001
            }
        });
    },



    showAjaxLoader: function(speed, opacity, id)
    {
        speed    = this.getSpeed(speed);
        id       = this.getMaskId(id);
        var alid = this.getAjaxLoaderId(id);

        if (this.show(speed, opacity, id)) {
            var $ajaxLoader = null;

            if (this.$ajaxLoader === null) {
                $ajaxLoader = jQuery('<div>', {
                    text : 'Processing...',
                    css  : {
                        display  : 'none',
                        position : 'fixed',
                        zIndex   : 9001,
                        margin   : 0,
                        padding  : 10,
                        border   : '1px solid #000000',
                        color    : '#000000',
                        backgroundColor : '#ffffff',
                        fontSize   : 24,
                        fontWeight : 'bold',
                        fontFamily : 'Arial'
                    }
                });
            }
            else {
                $ajaxLoader = this.$ajaxLoader.clone();
            }

            $ajaxLoader
                .appendTo('body')
                .attr('id', alid)
                .css({
                    left : (jQuery(window).width() / 2) - ($ajaxLoader.width() / 2),
                    top  : (jQuery(window).height() / 2) - ($ajaxLoader.height() / 2)
                })
                .fadeTo(speed, 1);
        }
    },



    hideAjaxLoader: function(speed, id)
    {
        speed    = this.getSpeed(speed);
        id       = this.getMaskId(id);
        var alid = this.getAjaxLoaderId(id);

        jQuery('#' + alid).fadeOut(speed, function() {jQuery(this).remove()});
        this.hide(speed, id);
    },
    
    
    
    getMaskId: function(id)
    {
        if (typeof id === 'undefined' || id === null) {
            id = this.defaultId;
        }

        return id;
    },



    getSpeed: function(speed)
    {
        if (typeof speed === 'undefined' || speed === null) {
            speed = this.defaultSpeed;
        }

        return speed;
    },



    getOpacity: function(opacity)
    {
        if (typeof opacity === 'undefined' || opacity === null) {
            opacity = this.defaultOpacity;
        }

        return opacity;
    },



    getAjaxLoaderId: function(maskId)
    {
        maskId = this.getMaskId(maskId);
        return maskId + this.ajaxLoaderIdSuffix;
    }

}

