﻿/*
 *@lhgdialog - Dialog Plugin v2.3.2 - Date : 2009-12-10
 *@Copyright lhgcore (c) 2009 By LiHuiGang Reserved
 */

var lhgdialog = (function()
{
    var topWin = window, cover, topDoc;
	while( topWin.parent && topWin.parent != topWin )
	{
	    try{
		    if( topWin.parent.document.domain != document.domain ) break;
			//假如你不想跨frameset只跨iframe就把下面这句的注释去掉
			//if( J('frameset',topWin.parent.document).length > 0 ) break;
		}
		catch(e){ break; }
		topWin = topWin.parent;
	}
	topDoc = topWin.document;
	
	// 取窗口的zIndex值
	var getZIndex = function()
	{
	    if( !lhgdialog.zIndex )
		    lhgdialog.zIndex = 1999;
		return ++lhgdialog.zIndex;
	};
	
	// 取指定的js文件的绝对路径，返回形式为：http://www.xxx.com/xxx/xxx/
	var getMap = function( file )
	{
		var bp, fp;
		
		J('script').each(function(){
			if( this.src.indexOf(file) !== -1 )
			{
				bp = this.src.substr( 0, this.src.toLowerCase().indexOf(file) );
				return false;
			}
		});
		
		if( typeof bp !== 'undefined' )
		{
			if( !J.browser.ie || bp.indexOf('http') === 0 ||
			    (J.browser.i8 && isDTD(document)) )
				return bp;
			else
			{
				fp = window.location.href;
				fp = fp.substr( 0, fp.lastIndexOf('/') );
				
				if( bp.indexOf('../') !== -1 )
				{
					while( bp.indexOf('../') >= 0 )
					{
						bp = bp.substr(3);
						fp = fp.substr( 0, fp.lastIndexOf('/') );
					}
					return fp + '/' + bp;
				}
				else if( bp.indexOf('/') === 0 )
				{
					fp = document.location.protocol + '//' + document.location.host;
					return fp + bp;
				}
				else
					return fp + '/' + bp;
			}
		}
	};
	
	var isDTD = function( doc )
	{
	    return ( 'CSS1Compat' == ( doc.compatMode || 'CSS1Compat' ) );
	};
	
	var now = function()
	{
	    return +new Date;
	};
	
	var xcopy = function( destination, source )
	{
	    for( var property in source )
		    destination[property] = source[property];
	};
	
	// 窗口大小改变时重新计算遮罩层的尺寸
	var reSizeHdl = function()
	{
		if( !cover ) return;
		var rel = isDTD( topDoc ) ? topDoc.documentElement : topDoc.body;
		
		J(cover).css({
		    width: Math.max( rel.scrollWidth, rel.clientWidth, topDoc.scrollWidth || 0 ) - 1,
			height: Math.max( rel.scrollHeight, rel.clientHeight, topDoc.scrollHeight || 0 ) - 1
		});
	};
	
	// 取客户端的可视页面尺寸
	var client = function( win )
	{
		win = win || window;
		
		if( J.browser.ie )
		{
			var oSize, doc = win.document.documentElement;
			if( doc && doc.clientWidth ) oSize = doc; else oSize = win.document.body;
			
			if( oSize )
				return { w : oSize.clientWidth, h : oSize.clientHeight };
			else
				return { w : 0, h : 0 };
		}
		else
			return { w : win.innerWidth, h : win.innerHeight };
	};
	
	// 取客户端的页面滚动尺寸
	var scroll = function( win )
	{
		win = win || window;
		
		if( J.browser.ie )
		{
			var doc = win.document;
			oPos = { x : doc.documentElement.scrollLeft, y : doc.documentElement.scrollTop };
			if( oPos.x > 0 || oPos.y > 0 ) return oPos;
			
			return { x : doc.body.scrollLeft, y : doc.body.scrollTop };
		}
		else
			return { x : win.pageXOffset, y : win.pageYOffset };
	};
	
	return {
	    zIndex : null, indoc : {}, inwin : {}, infrm : {}, inndoc : {}, innwin : {},
		
		get : function( id, r )
		{
			if( !id || J('#'+id)[0] ) return;
			
			r = this.dialogSet( r );
			
			//是否打开遮罩层
			if( r.cover )
			    this.dispCover();
			else
			{
			    if( cover ) cover = null;
			}
			
			//是否从缓存读取内容页
			if( r.page && r.cache === false )
			    r.page = ( /\?/.test(r.page) ? r.page + '&' :
				    r.page + '?' ) + 'uuid=' + now();

			//插件路径，注意lhgdialog.js和lhgdialog.html一定要在同一目录下
			var dpath = getMap( 'lhgdialog.js' ),
			
			iTop = r.top ? r.top + r.scroS.y :
			    Math.max( r.scroS.y + ( r.viewS.h - r.height - 20 ) / 2, 0 ),
			iLeft = r.left ? r.left + r.scroS.x :
			    Math.max( r.scroS.x + ( r.viewS.w - r.width - 20 ) / 2, 0 ),
			
			dialog = J('<iframe frameborder="0"></iframe>',topDoc).attr({
			    scrolling: 'no', allowTransparency: true,
				id: id, src: dpath + 'lhgdialog.html'
			}).css({
			    top: 100, left: iLeft, position: 'absolute',
				width: r.width, height: r.height, zIndex: getZIndex()
			}).appendTo( topDoc.body ).get(0);
			
			dialog._dlgargs = r; dialog = null;
		},
		
		// 窗口默认参数设置
		dialogSet : function( options )
		{
			var _default = {
				width : 730,
				height : 570,
				title : 'ISF (Importer Security Filing) ',
				win : window,  //调用控件页面的window对象
				core : getMap( 'lhgcore.min.js' ),   //取lhgcore.min.js路径，用在lhgdialog.html里加载此文件
				skin : 'default',    //窗口默认皮肤的目录
				drag : true,   //是否充许拖动操作
				foot : true,   //是否显示页脚
				topW : topWin,   //取顶层窗口页面（也就是控件创建的页面）的window对象
				viewS : client( topWin ),
				scroS : scroll( topWin )
			};
			
			xcopy( _default, options || {} );
			
			return _default;
		},
		
		getvoid : function()
		{
			if( J.browser.ie )
				return ( J.browser.i7 ? '' : "javascript:''" );
			else
				return 'javascript:void(0)';
		},
		
		// 关闭窗口函数
		close : function( dlgWin, co )
		{
		    var dlg = dlgWin.frameElement;
			
			if( dlg )
			{
			    dlg.src = this.getvoid();
				J(dlg).remove(); dlg = null;
			}
			
			if( co )
			    this.hideCover( co );
		},
		
		//显示遮罩层
		dispCover : function()
		{
			cover = J('<div/>',topDoc).css({
			    position: 'absolute', zIndex: getZIndex(), top: '0px',
				left: '0px', backgroundColor: '#fff', opacity: 0.5
			}).appendTo( topDoc.body ).get(0);
			
			if( J.browser.ie && !J.browser.i7 )
			{
			    J('<iframe/>',topDoc).attr({
				    hideFocus: true, frameBorder: 0, src: this.getvoid()
				}).css({
				    width: '100%', height: '100%', position: 'absolute', left: '0px',
					top: '0px', filter: 'progid:DXImageTransform.Microsoft.Alpha(opacity=0)'
				}).appendTo( cover );
			}
			
			J( topWin ).bind( 'resize', reSizeHdl ); reSizeHdl();
		},
		
		// 取遮罩层对象
		getcover : function(){ return cover; },
		hideCover : function( co ){ J(co).remove(); cover = null; }
	};
})();

J.dialog = lhgdialog;
