/*
 * jmodal
 * version: 2.0 (05/13/2009)
 * @ jQuery v1.3.*
 *
 * Licensed under the GPL:
 *   http://gplv3.fsf.org
 *
 * Copyright 2008, 2009 Jericho [ thisnamemeansnothing[at]gmail.com ]
 *
 * modified by g.airwin[at]gmail.com @ 2009.05
*/

jQuery(function($) {
    var userAgent = navigator.userAgent.toLowerCase();
	var browserVersion = (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [0,'0'])[1];
	var isIE6 = (/msie/.test(userAgent) && !/opera/.test(userAgent) && parseInt(browserVersion) < 7);
	var doc = $(document);

    $.hideJmodal = $.fn.hideJmodal = function(callback) {
        if(isIE6 && $('#jquery-jmodal').css('position') == 'absolute') {
            $(window).unbind('scroll');
        }
        $(window).unbind('resize');
        $('#jmodal-overlay').fadeOut(200);
        var call = $.isFunction(callback) ? callback : function(){};
        $('#jquery-jmodal').fadeOut(200, call);
    };
    
    $.jmodal = $.fn.jmodal = function(setting) {
    	setting = setting || {};
        var ps = $.extend({
            data: {},
            center: false,
            drag: false,
            marginTop: 150,
            okEvent: function(data,args){args.complete();},
            initWidth: 360,
            fixed: true,
            title: '\u63d0\u793a\u4fe1\u606f',
            zIndex: 100,
            content: 'This is a jquery plugin!',
            autoClose: 0,
            noOverlay: false,
            onlyOverlay: false,
            afterSetup: function(){},
            afterClose: function(){}
        }, setting);
        var overLayConfig = $.extend({opacity: 0.5,backgroundColor: '#888',zIndex: ps.zIndex - 1}, setting.overlay||{});
        var buttonText = $.extend({ok: '', cancel: ''}, setting.buttonText||{});

        //private functions
        var resizeJmode = function(e) {
            $('#jquery-jmodal').css({
                left: ($(document).width() - ps.initWidth) / 2,
                top: ps.marginTop + document.documentElement.scrollTop
            });
            $('#jmodal-overlay').css({height: $(document).height()});
        };
        
        var showOverlay = function() {
        	if($('#jmodal-overlay').length == 0) {
        		$('<div id="jmodal-overlay" class="jmodal-overlay"/>').appendTo($('body', doc));
        	}
        	var overlayCss = {};
        	if(isIE6) {
        		if($('#jModalIframeHideIe').length == 0) {
        			$('<iframe id="jModalIframeHideIe" style="position:absolute;top:0;left:0;width:100%;height:100%;filter:alpha(opacity=0);"></iframe>')
        			.appendTo($('#jmodal-overlay'));
        		}
        		overlayCss = {
        			position: 'absolute',
        			width: Math.max(document.documentElement.scrollWidth, document.documentElement.clientWidth),
        			height: doc.height()
        		};
        	}

        	$('#jmodal-overlay').css($.extend({
        		display: 'block',
        		left: 0,
        		top: 0,
        		width: '100%',
        		height: '100%',
        		position: 'fixed'
        	}, overLayConfig, overlayCss)).fadeTo(250, overLayConfig.opacity||0.5);
        };
        
        $('#jquery-jmodal').remove();

        ps.docWidth = doc.width();
        ps.docHeight = doc.height();

        if(ps.autoClose > 0) {
        	window.setTimeout(function(){$.hideJmodal.call(null, ps.afterClose);}, ps.autoClose*1000);
        }
        if(!ps.noOverlay) {
        	showOverlay();
        }
        if(ps.onlyOverlay == true) return;
        
        if ($('#jquery-jmodal').length == 0) {
            $('<div class="jmodal-main" id="jquery-jmodal" >' +
                    '<table cellpadding="0" cellspacing="0" width="100%">' + 
                        '<tr>' +
                            '<td class="jmodal-top-left jmodal-png-fiexed"></td>' +
                            '<td class="jmodal-border-top jmodal-png-fiexed"></td>' +
                            '<td class="jmodal-top-right jmodal-png-fiexed"></td>' +
                        '</tr>' +
                   '<tr>' +
                        '<td class="jmodal-border-left jmodal-png-fiexed"></td>' +
                        '<td valign="top">' +
                           '<div class="jmodal-title" id="jmodal-title"></div><div id="tc_close"><input type="button" id="jmodal-button-close" class="tc_close"/></div>' +
                           '<div class="jmodal-content" id="jmodal-container-content"></div>' +
                           '<div class="jmodal-bottom">' +
                               '<input type="button" id="jmodal-button-ok" class="tc_bt1"/><input type="button" id="jmodal-button-cancel" class="tc_bt2"/>' +
                          '</div>' +
                        '</td>' +
                        '<td class="jmodal-border-right jmodal-png-fiexed"></td>' +
                   ' </tr>' +
                   '<tr>' +
                       '<td class="jmodal-bottom-left jmodal-png-fiexed"></td>' +
                       '<td class="jmodal-border-bottom jmodal-png-fiexed"></td>' +
                       '<td class="jmodal-bottom-right jmodal-png-fiexed"></td>' +
                   '</tr>' +
                  '</table>' +
               ' </div>').hide().appendTo($('body', doc));
        }
        var jmodal = $('#jquery-jmodal');

        // events
        if(ps.autoClose > 0) {
        	$('#jmodal-button-ok,#jmodal-button-cancel').hide();
        } else {
            $('#jmodal-button-ok,#jmodal-button-cancel').css('cursor', 'pointer');
        	$('#jmodal-button-close').one('click', $.hideJmodal).show();
        	if(buttonText.ok && buttonText.ok.length > 0) {
                $('#jmodal-button-ok')
                    .attr('value', buttonText.ok)
                    .unbind('click')
                    .one('click', function(e) {
                        var args = {
                            complete: $.hideJmodal
                        };
                        ps.okEvent(ps.data, args);
                    }).show();
            } else {
            	$('#jmodal-button-ok').hide();
            }
            if(buttonText.cancel && buttonText.cancel.length > 0) {
                $('#jmodal-button-cancel')
                    .attr('value', buttonText.cancel)
                    .one('click', $.hideJmodal).show();
            } else {
            	$('#jmodal-button-cancel').hide();
            }
        }

        jmodal.css({
            position: ((ps.fixed && !isIE6) ? 'fixed' : 'absolute'),
            width: ps.initWidth,
            left: (ps.docWidth - ps.initWidth) / 2,
            top: (ps.fixed && !isIE6) ? ps.marginTop : (ps.marginTop + document.documentElement.scrollTop),
            zIndex: ps.zIndex,
            height: 'auto'
        }).fadeIn(200);

        if(!!ps.fixed && isIE6) {
            $(window).scroll(function(){resizeJmode();});
        }

        // contents
        jmodal.find('.jmodal-title').html(ps.title);
        
        if (typeof ps.content == 'string') {
            $('#jmodal-container-content').html(ps.content);
        }
        if (typeof ps.content == 'function') {
            var e = $('#jmodal-container-content');
            e.holder = jmodal;
            ps.content(e);
        }
        if($.isFunction(ps.afterSetup)) {
            ps.afterSetup();
        }

        $(window).resize(function(){resizeJmode();});

        if(ps.drag && $.isFunction($.fn.easydrag) && !(ps.fixed && isIE6)) {
            jmodal.easydrag('jmodal-title');
        }
    };
});