﻿/*!
 * lhgcore Dialog Plugin v2.3.3
 * Date : 2009-12-31 14:13:11
 * Copyright (c) 2009 By Li Hui Gang
 */

;(function(J){

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值
function getZIndex()
{
	if( !J.dialog.zIndex )
		J.dialog.zIndex = 1999;
	return ++J.dialog.zIndex;
};

function isDTD( doc )
{
    return ( 'CSS1Compat' == ( doc.compatMode || 'CSS1Compat' ) );
};

// 取指定的js文件的绝对路径，返回形式为：http://www.xxx.com/xxx/xxx/
function getMap( 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;
		}
	}
};

// 窗口大小改变时重新计算遮罩层的尺寸
function reSizeHdl()
{
	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
	});
};

// 取客户端的可视页面尺寸
function client( 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 };
};

// 取客户端的页面滚动尺寸
function scroll( 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 };
};

function setDefault( options )
{
	// 窗口的默认值的设置
	var _default = {
		width: 400,
		height: 300,
		title: 'lhgdialog',
		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 )
	};
	
	return J.extend( _default, options || {} );
};

var lhgdialog = function()
{
    this.zIndex = 1999;
	this.indoc = {};
	this.inwin = {};
	this.infrm = {};
	this.inndoc = {};
	this.innwin = {};
};

lhgdialog.prototype = {
    get: function( id, options )
	{
	    if( !id || J('#'+id)[0] ) return;
		
		var r = setDefault( options );
	
		//是否打开遮罩层
		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=' + (new Date).getTime();

		//插件路径，注意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" scrolling="no" allowTransparency="true" id="' + id + '" src="' + dpath +
		    'lhgdialog.html" style="top:' + iTop + 'px;left:' + iLeft + 'px;position:absolute;width:' + r.width +
			'px;height:' + r.height + 'px;z-index:' + getZIndex() + ';"></iframe>',topDoc).appendTo( topDoc.body )[0];
		
		dialog._dlgargs = r; dialog = null;
	},
	
	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 style="position:absolute;z-index:' + getZIndex() +
			';top:0px;left:0px;background-color:#fff;"></div>',topDoc).css( 'opacity', 0.5 ).appendTo( topDoc.body )[0];
		
		if( J.browser.ie && !J.browser.i7 )
		{
			J('<iframe hideFocus="true" frameborder="0" src="' + this.getvoid() + '" style="width:100%;"' +
			  'height:100%;top:0px;left:0px',topDoc).css( 'opacity', 0 ).appendTo( cover );
		}
		
		J( topWin ).bind( 'resize', reSizeHdl ); reSizeHdl();
	},
	
	// 取遮罩层对象
	getcover: function(){ return cover; },
	hideCover: function( co ){ J(co).remove(); cover = null; }
};

J.dialog = new lhgdialog();

})(lhgcore);
