/*
 * @(#)jQuery.CEA.common.js
 *       
 * 系统名称：东航电子商务国内B2C系统
 * 版本号：1.0
 * 
 * Copyright (c)  TravelSky
 * All Rights Reserved.
 * 
 * 作者：bjiang
 * 创建日期：Nov 2, 2009
 * 
 * 功能描述：弹出窗口基础框架
 * 使用注意:
 *      需要定议id=container标签
 * 公用方法描述：
 *            
 *    
 * 修改人：
 * 修改日期：
 * 修改原因：
 * 
 * 
 */
;
(function($)
{
    var $ieframe = $('<iframe class="popupwinieframe" frameborder="0" src="#"></iframe>').css({ position: "absolute", display: "none", zIndex: -1,"overflow":"hidden"});
    jQuery.msgbox = { //弹出窗口
        msgbox: function(id, width, height, $title, $content, rezindex)
        {
            $.msgbox.closemsgbox(id);
            
            var popwinstr = '<div id="' + id + '"  class="popwincontainer">' +
                                '<div class="border">' +
                                    '<div id="title">' +
                                        '<a id="close" href="#" title="关闭">X</a>'+
                                    '</div>'+
                                    '<div id="content">' +
                                        '<table border="0" cellspacing=\"0\" cellpadding="0">' +
                                        '<tr>' +
                                           '<td align="center" valign="top"></td>' +
                                        '</tr>' +
                                        '</table>' +
                                    '</div>' +
                                '</div>' +
                            '</div>'; 
            var $popwin = $(popwinstr);
            $popwin.find('#close:first').after($title);
            $popwin.find('#content td[align="center"]:first').append($content);
            $('#container').before($popwin);
            if($.browser.version === '6.0')
            {
               $("#"+id).prepend($ieframe);
               $ieframe.show();
            }
            $('#' + id + ' > .border > #title > #close').click(function()
            {
                $.msgbox.closemsgbox(id);
                return false;
            });
            if (rezindex)
            {
                $('#' + id).click(function()
                {
                    var maxzindex = 10000;
                    $('.popwincontainer').each(function()
                    {
                        if ($(this).css('z-index') / 1 > maxzindex)
                        {
                           maxzindex = $(this).css('z-index') / 1 + 1;
                        }
                    });
                    $(this).css('z-index', maxzindex / 1 + 1);
                });
            }
            $('#' + id).draggable({
                cursor: "move",
                revert: false,
                ghosting: true,
                opacity: 0.7,
                handle: '#title'
            });
            //取得内容具体的宽高
            innerheight = $('#' + id + ' > .border > #content > table').height() + 34;
            //宽未定义
            innerwidth = $('#' + id + ' > .border > #content > table').width() + 16;
            height = height < innerheight ? innerheight: height;
            $('#' + id + ' > .border > #content > table').css({
                height: (height / 1) + 'px',
                width: (width / 1 - 2) + 'px'
            });
            $('#' + id).css({
                height: (height / 1 + 2) + 'px',
                width: (width / 1 + 2) + 'px'
            });
            $('#' + id + ' > .border').css({
                height: (height / 1) + 'px',
                width: (width / 1) + 'px'
            });
			$ieframe.height(height);
            scrolltop = $(window).scrollTop();
            leftto = $('#' + id).offset().left - (width / 2);
            if (($(window).height() / 2) > (height / 2))
            {
                topto = $('#' + id).offset().top - (height / 2);
            }
            else
            {
                topto = $(window).height() / 2;
            }
            topto = (topto + scrolltop);
            $('#' + id).css({
                'left': leftto + 'px',
                'top': topto + 'px',
                'position': 'absolute'
            });
            return $("#" + id);            
        }, 
        popupwin: function(id, width, height, title, content, rezindex)
        {
            var popwinstr = '<div id="' + id + '"  class="popwincontainer" style="display:none">' +
                                '<div class="border">' +
                                    '<div id="title">' +
                                        '<a id="close" href="#" title="关闭">X</a>' +
                                        '$title' +
                                    '</div>'+
                                    '<div id="content">' +
                                        '<table border="0" cellspacing=\"0\" cellpadding="0">' +
                                        '<tr>' +
                                           '<td align="center" valign="top">$content</td>' +
                                        '</tr>' +
                                        '</table>' +
                                    '</div>' +
                                '</div>' +
                            '</div>'; 
            $.msgbox.closemsgbox(id);
            $("#container").before(popwinstr.replace("$title", title).replace("$content", content));
            $("#"+id).show();
            if($.browser.version === '6.0')
            {
               $("#"+id).prepend($ieframe);
               $ieframe.show();
            }
            $("#" + id + " > .border > #title > #close").click(function()
            {
                $.msgbox.closemsgbox(id);
                return false;
            });
            if (rezindex)
            {
                $("#" + id).click(function()
                {
                    //alert($(this).css("z-index"));
                    var maxzindex = 10000;
                    $(".popwincontainer").each(function()
                    {
                        if ($(this).css("z-index") / 1 > maxzindex) maxzindex = $(this).css("z-index") / 1 + 1;
                    });
                    $(this).css("z-index", maxzindex / 1 + 1);
                });
            }
            $("#" + id).draggable({
                cursor: "move",
                revert: false,
                ghosting: true,
                opacity: 0.7,
                handle: "#title"
            });
            //取得内容具体的宽高
            innerheight = $("#" + id + " > .border > #content > table").height() + 34;
            //宽未定义
            innerwidth = $("#" + id + " > .border > #content > table").width() + 16;
            height = height < innerheight ? innerheight: height;
            $("#" + id + " > .border > #content > table").css({
                height: (height / 1) + "px",
                width: (width / 1 - 2) + "px"
            });
            $("#" + id).css({
                height: (height / 1 + 2) + "px",
                width: (width / 1 + 2) + "px"
            });
            $("#" + id + " > .border").css({
                height: (height / 1) + "px",
                width: (width / 1) + "px"
            });

            scrolltop = $(window).scrollTop();
            leftto = $("#" + id).offset().left - (width / 2);
            if (($(window).height() / 2) > (height / 2))
            {
                topto = $("#" + id).offset().top - (height / 2);
            }
            else
            {
                topto = $(window).height() / 2; //-((height/2) - ((height/2) - (document.body.clientHeight/2)));
            }
            topto = (topto + scrolltop);
            $("#" + id).css({
                "left": leftto + "px",
                "top": topto + "px",
                "position": "absolute"
            });
            
            return $("#" + id);
        },
        closemsgbox: function(id, time, callbackfunction)
        {
            if (time)
            {
                window.setTimeout(function()
                {
                    $("#" + id).hide("fast", callbackfunction).remove();
                },
                time);
            }
            else
            {
                $("#" + id).remove();
            }
        },
        resizemsgbox: function(msgbox)
        {
        }
    }
})(jQuery);
