/**
 * Created by toplan on 15/7/11.
 */

(function($){
    var config = {};

    $.loading_default = {
        ajax       : true,
        id         : 'ajaxLoading',
        zIndex     : '1000',
        background : 'rgb(0, 0, 0)',        
        minTime    : 200,
        radius     : '4px',
        width      : '85px',
        height     : '85px',
        imgWidth   : '45px',
        imgHeight  : '45px',
        tip        : 'loading...',
        fontSize   : '14px',
        fontColor  : '#fff'
    };

    $.loading = function (options) {

        var opts = $.extend(
            $.loading_default,
            options
        );

        config = opts;
        init(opts);

        var selector = '#' + opts.id;

        $(document).on('ajaxStart', function(){
            if (config.ajax) {
                $(selector).show();
            }
        });

        $(document).on('ajaxComplete', function(){
            setTimeout(function(){
                $(selector).hide();
            }, opts.minTime);
        });

        return $.loading;
    };

    $.loading.open = function (time) {
        var selector = '#' + config.id;
        $(selector).show();
        if (time) {
            setTimeout(function(){
                $(selector).hide();
            }, parseInt(time));
        }
    };

    $.loading.close = function () {
        var selector = '#' + config.id;
        $(selector).hide();
    };

    $.loading.ajax = function (isListen) {
        config.ajax = isListen;
    };

    

    function init (opts) {
        //wrap div style
        var wrapCss = 'display: none;position: fixed;top: 0;bottom: 0;left: 0;right: 0;margin: 0 auto;padding: 8px;vertical-align: middle;';
        
        // IE8 or lower tweek that does not support rgba
        //if (document.all && !document.addEventListener) {
        //    opts.background = "rgb(0,0,0)";
        //}
        
        var cssArray = [
            'width:' + opts.width,
            'height:' + opts.height,
            'z-index:' + opts.zIndex,
            'background:' + opts.background,
            'border-radius:' + opts.radius,
            "-ms-filter: 'progid:DXImageTransform.Microsoft.Alpha(Opacity=70)'",
            'filter: alpha(opacity=70)',    
            '-moz-opacity: 0.7',
            '-khtml-opacity: 0.7',
            'opacity: 0.7'
        ];
        wrapCss += cssArray.join(';');

        //img style
        var imgCss = 'margin:0;margin-bottom:8px;margin-left:48%;display:block;';        
        cssArray = [
            'width:' + opts.imgWidth,
            'height:' + opts.imgWidth            
        ];
        imgCss += cssArray.join(';');
        
        //text style
        var textCss = 'margin:0;margin-left:48%;display:block;';
        cssArray = [
            'font-size:' + opts.fontSize,
            'color:'     + opts.fontColor
        ];
        textCss += cssArray.join(';');

        var html = '<div id="' + opts.id + '" style="' + wrapCss + '">'
                  +'<img src="' + opts.imgPath + '" style="' + imgCss + '">'
                  +'<p style="' + textCss + '">' + opts.tip + '</p></div>';

        $(document).find('body').append(html);
    }

})(window.jQuery||window.Zepto);
