/* iframe代理控制 v1.0.0版
 * 
 * 1.建立此方法...2010/12/02
 * 2.完成基本功能...2010/12/04
 */

function jquery_iAgent()
{
	this.ID_BoxDiv="";	//盒子區塊ID(父框)
	this.ID_Iframe="";	//頁框區塊ID(父框)
	
	this.ID_Iproxy="";	//代理頁框ID(子框)
	this.LK_Iproxy="";	//代理連結字串
	this.TO_Action="";	//運行活動字串
	this.TP_Iframe="";	//暫存頁框連結
	this.BX_WidthS=90;	//暫存盒子寬度
	this.BX_Height=70;	//暫存合子高度
	this.IF_WidthS=80;	//暫存頁框寬度
	this.IF_Height=60;	//暫存頁框高度
	
	this.JQ_Parent=null;	//jQuery物件(父框)
	this.FLAG_Test=false;	//標示:測試
	
	/* 設定 目標DIV塊ID
	 * @param string ID_BoxDiv notnull	盒子區塊ID(父框)
	 * @param string ID_Iframe notnull	頁框區塊ID(父框)
	 * @param string ID_Iproxy notnull 	代理頁框ID(子框)
	 * @access public (可串接運行)
	 */
	this.set_id=function(ID_BoxDiv, ID_Iframe, ID_Handle)
	{
		this.ID_BoxDiv=ID_BoxDiv;
		this.ID_Iframe=ID_Iframe;
		this.ID_Handle=ID_Handle;
		return this;
	}
	
	/* 設定 代理連結
	 * @param string URL_Agent null=""	代理連結 位置
	 * @param string FIL_Agent notnull	代理連結 程式
	 * @access public (可串接運行)
	 */
	this.set_proxy=function(ID_Iproxy, LK_Iproxy)
	{
		this.ID_Iproxy=ID_Iproxy;
		this.LK_Iproxy=(LK_Iproxy)?LK_Iproxy:"jquery_iAgent.htm";
		return this;
	}
	
	/* 設定 測試標示啟用
	 * @access public (可串接運行)
	 */
	this.set_test=function()
	{
		this.FLAG_Test=true;
		return this;
	}
	
	/* 建置 ()
	 * @access public (可串接運行)
	 */
	this.build=function()
	{
		var oThis=this;
		jQuery(document).ready(function($)
		{
			var id_BoxDiv="#"+oThis.ID_BoxDiv;
			var id_Iframe="#"+oThis.ID_Iframe;
			var id_Iproxy="#"+oThis.ID_Iproxy;
			
			var iframe_select=id_BoxDiv+" "+id_Iframe;
			var jq_Parent=$(id_BoxDiv, window.parent.document);
			oThis.JQ_Parent=(jq_Parent.attr("id"))?jq_Parent:null;
		});
		return this;
	}
	
	/* 連動:開始
	 * @access public (可串接運行)
	 */
	this.to_start=function()
	{
		var oThis=this;
		jQuery(document).ready(function($)
		{
			oThis.TO_Action="";
			if(oThis.FLAG_Test){alert("start");}//TEST
		});
		return this;
	}
	
	/* 連動:重設尺寸(父框)
	 * @param string sWidth  ""=null	重設的寬度值
	 * @param string sHeight ""=null	重設的高度值
	 * @access public (可串接運行)
	 */
	this.to_resize=function(sWidth, sHeight)
	{
		var oThis=this;
		jQuery(document).ready(function($)
		{
			sWidth=sWidth*1;sHeight=sHeight*1;
			var doc_Width=$(document).width();
			var doc_Height=$(document).height();
			var win_Width=$(window).width();
			var win_Height=$(window).height();
			var base_Width=(!isNaN(sWidth)&&sWidth>0)?sWidth:doc_Width;
			var base_Height=(!isNaN(sHeight)&&sHeight>0)?sHeight:doc_Height;
			oThis.TO_Action+="#width_"+base_Width+"#height_"+base_Height;
			if(oThis.FLAG_Test){alert("resize:"+base_Width+"|"+base_Height);}//TEST
		});
		return this;
	}
	
	/* 連動:重設位置(父框)
	 * @param string Place_x ""=null	重設X座標值
	 * @param string Place_y ""=null	重設Y座標值
	 * @access public (可串接運行)
	 */
	this.to_replace=function(Place_x, Place_y)
	{
		var oThis=this;
		jQuery(document).ready(function($)
		{
			var id_BoxDiv="#"+oThis.ID_BoxDiv;
			var bx_PlaceX=Place_x;
			var bx_PlaceY=Place_y;
			if(Place_x=="L"||Place_x=="C"||Place_x=="R"){//字母定位X
				Place_x=oThis._absolute(Place_x);
			}
			if(Place_y=="T"||Place_y=="M"||Place_y=="B"){//字母定位Y
				Place_y=oThis._absolute(Place_y);
			}
			
			Place_x=Place_x*1;Place_y=Place_y*1;
			bx_PlaceX=(!isNaN(Place_x))?Place_x:bx_PlaceX;
			bx_PlaceY=(!isNaN(Place_y))?Place_y:bx_PlaceY;
			
			oThis.TO_Action+="#x_"+bx_PlaceX+"#y_"+bx_PlaceY;
			if(oThis.FLAG_Test){alert("To replace:"+bx_PlaceX+"|"+bx_PlaceY);}//TEST
		});
		return this;
	}//*/
	
	/* 連動:關閉(父框)
	 * @param string _val ""=null	運作類型值
	 * @access public (可串接運行)
	 */
	this.to_close=function(_val)
	{
		var oThis=this;
		jQuery(document).ready(function($)
		{
			oThis.TO_Action+="#close_"+_val;
			if(oThis.FLAG_Test){alert("close");}//TEST
		});
		return this;
	}
	
	/* 連動:重載(父框)
	 * @param string _val ""=null	運作類型值
	 * @access public (可串接運行)
	 */
	this.to_reload=function(_val)
	{
		var oThis=this;
		jQuery(document).ready(function($)
		{
			oThis.TO_Action+="#reload_"+_val;
			if(oThis.FLAG_Test){alert("reload");}//TEST
		});
		return this;
	}
	
	/* 連動:運行
	 * @access public (可串接運行)
	 */
	this.to_work=function()
	{
		var oThis=this;
		jQuery(document).ready(function($)
		{
			var id_Iproxy="#"+oThis.ID_Iproxy;
			var iframe_src=oThis.LK_Iproxy+oThis.TO_Action;

			if(oThis.JQ_Parent==null)
			{	
				if(!$(id_Iproxy).attr("id")){$("body").append('<div id="confige_proxy"></div>');}
				var _iframe=[];
				_iframe.push('<iframe id="iAgent" class="" width="100px" height="100px" ');
				_iframe.push('frameborder="0px" marginwidth="0px" marginheight="0px" scrolling="no" ');
				_iframe.push('src="'+iframe_src+'"></iframe>');
				$(id_Iproxy).html(_iframe.join(''));//重載內容(iframe)
				if(!oThis.FLAG_Test){$(id_Iproxy).hide();}//TEST
			}
			else
			{
				oThis.noProxy();
			}
			if(oThis.FLAG_Test){alert("work");}//TEST
		});
		return this;
	}
	
	/* 運行:不用代理控制(父框)
	 * @param string insert_str ""=null	連動運作字串
	 * @access public
	 */
	this.noProxy=function(insert_str)
	{
		var oThis=this;
		jQuery(document).ready(function($)
		{			
			var $Parent=oThis.JQ_Parent;
			var param_str=(insert_str)?insert_str:oThis.TO_Action;
			var param_arr=param_str.split("#");
			$(param_arr).each(function(i)
			{
				var _arr=this.split("_");
				var _cmd, _val;
				var _cmd=(_arr[0]&&_arr[0]!="")?_arr[0]:"";
				var _val=(isNaN(_arr[1]*1))?0:_arr[1]*1;
				if(_cmd!=""){oThis._action(_cmd, _val);}
			});
			$Parent.find("#loading_images").hide();//*****寫死的	//隱藏底圖區塊
			
			var SHOW_Test=(insert_str)?"onProxy":"noProxy";
			if(oThis.FLAG_Test){alert(SHOW_Test+":"+param_str+" "+$Parent.attr("id"));}//TEST	
		});
	}
	
	/* 運行:使用代理控制(父框)
	 * @access public
	 */
	this.onProxy=function()
	{
		var oThis=this;
		jQuery(document).ready(function($)
		{
			var id_BoxDiv="#"+oThis.ID_BoxDiv;
			var jq_Parent=$(id_BoxDiv, window.parent.parent.document);
			
			oThis.JQ_Parent=jq_Parent;
			oThis.noProxy(location.hash);//
			if(oThis.FLAG_Test){alert("onProxy:"+oThis.TO_Action+" "+jq_Parent.attr("id"));}//TEST
		});
	}
	
	/* 啟動控制(父框)
	 * @access public
	 */
	this._action=function(_cmd, _val)
	{
		var oThis=this;
		jQuery(document).ready(function($)
		{
			var id_Iframe="#"+oThis.ID_Iframe;
			var $Parent=oThis.JQ_Parent;
			switch(_cmd)
			{
				case "width"://設定Iframe寬度
					$Parent.find(id_Iframe).css("width", _val);
					break;
				case "height"://設定Iframe高度
					$Parent.find(id_Iframe).css("height", _val);
					break;
				case "x"://設定Iframe寬度
					$Parent.css("left", _val);
					break;
				case "y"://設定Iframe高度
					$Parent.css("top", _val);
					break;
				case "close"://關閉Iframe框架
					switch(_val){
						case "1"://真正清掉頁面
							$Parent.find(id_Iframe).html("");
							break;
					}
					$Parent.hide("");//隱藏盒子
					break;
				case "reload"://重載Iframe頁面
					var if_WidthS=oThis.IF_WidthS;
					var if_Height=oThis.IF_Height;
					switch(_val){
						case "1":
							$Parent.find(id_Iframe).css("width", if_WidthS+"px");	//還原頁框區塊寬度
							$Parent.find(id_Iframe).css("height", if_Height+"px");	//還原頁框區塊高度
							$Parent.find("#loading_images").show();//*****寫死的	//顯示底圖區塊
							break;
					}
					document.location.reload();
					break;
				default:
					trace("Command:"+_cmd+" Error!!");
			}
		});
	}
	
	this._absolute=function(_type)
	{
		var oThis=this;
		var Return_val=1;//"L"://X  ||  "T"://Y  預設為0
		jQuery(document).ready(function($)
		{
			var doc_Width=$(document).width();
			var doc_Height=$(document).height();
			var win_Width=$(window).width();
			var win_Height=$(window).height();
			var base_Width=(doc_Width>win_Width)?win_Width:doc_Width;
			var base_Height=(doc_Height>win_Height)?win_Height:doc_Height;
			var bx_WidthS=oThis.BX_WidthS;
			var bx_Height=oThis.BX_Height
			var if_WidthS=oThis.IF_WidthS;
			var if_Height=oThis.IF_Height;
			switch(_type)
			{
				case "C"://X
					Return_val=base_Width/2-bx_WidthS-if_WidthS;
					break;
				case "R"://X
					Return_val=base_Width-bx_WidthS-if_WidthS;
					break;
				case "M"://Y
					Return_val=base_Height/2-bx_Height-if_Height;
					break;
				case "B"://Y
					Return_val=base_Height-bx_Height-if_Height;
					break;
			}
			Return_val=(!isNaN(Return_val)&&Return_val>0)?Return_val:1;
			if(oThis.FLAG_Test){
				if(_type=="C"||_type=="R"){alert(base_Width+"|"+bx_WidthS+"|"+if_WidthS+"|"+Return_val);}
				if(_type=="M"||_type=="B"){alert(base_Height+"|"+bx_Height+"|"+if_Height+"|"+Return_val);}
			}
		});
		return Return_val;
	}
}
