﻿var G_parent=window.parent;
//var G_parent=document.parentWindow;
var F_H;
var moz=!document.all;
/*
在子框架窗口内设置iframe的高度百分比
*/
function setHeight(P_num,P_ms){
	//P_num=0.1～1，高度的百分比，默认100%；如果P_num>1，那么它即是指定的高度.
	//P_ms=延时多少毫秒执行操作，默认110
	P_num=P_num || 1.0;
	P_ms=P_ms || 110;
	if (!!G_parent){
		var F_parentbody=G_parent.document.body;
		var F_selfbody=document.body;
		
		//父页面总高度V_parentH
		var V_parentH=moz?F_parentbody.offsetHeight:F_parentbody.scrollHeight;
		//父页面可见高度V_parentVH
		var V_parentVH=F_parentbody.clientHeight;
		//自己页面总高度V_selfH
		var V_selfH=F_selfbody.offsetHeight;
		//父页面可见高度-父页面其它元素的高度=自己页面可用高度
		var V_selfAH=V_parentVH-(V_parentH-V_selfH);
		if (P_num>1) {
			V_selfAH=P_num;
		}else{
			V_selfAH=Math.round(V_selfAH*P_num);
			V_selfAH=V_selfAH<0?20:V_selfAH;
		}
		for (var i=0;i<G_parent.frames.length;i++){
			if (G_parent.frames[i].location==self.document.location){
				var n=i;
				setTimeout(function(){
					G_parent.document.getElementsByTagName("iframe")[n].setAttribute("height",V_selfAH);
					self.document.body.style.overflow="hidden";
				},P_ms);
				return G_parent.document.getElementsByTagName("iframe")[n];
			}
		}
	}
}

/* 
子框架窗口操作iframe自动适应页面 
注意：嵌入页面不能带DOCTYPE声明
*/
function autoHeight(){
	if (!!G_parent){
		for (var i=0;i<G_parent.frames.length;i++){
			if (G_parent.frames[i].location==self.document.location){
				//alert(document.location);
				//F_H = Math.max(self.document.body.offsetHeight,self.document.body.scrollHeight);
				//F_H = Math.max(F_H,self.document.body.clientHeight);
				F_H=moz?(self.document.body.offsetHeight+20):self.document.body.scrollHeight;
				G_parent.document.getElementsByTagName("iframe")[i].setAttribute("height",F_H);
				self.document.body.style.overflow="hidden";
				window.status="Auto height:"+G_parent.document.getElementsByTagName("iframe")[i].getAttribute("height");
				//alert("offsetHeight=="+self.document.body.offsetHeight+"\nscrollHeight=="+self.document.body.scrollHeight+"\nclientHeight=="+self.document.body.clientHeight);
			}
		}
	}
}


/*父框架窗口中操作iframe自动适应高度,IE5.0中测试通过
body中onLoad="CFAutoHeight();"
iframe中onLoad="CFAutoHeight();"
按钮中onClick="CFAutoHeight('FrameName');"
*/
function CFAutoHeight(P_obj){
	var V_childH,G_obj,G_ID;
	if (typeof P_obj=="undefined"){
		for (var i=0;i<window.frames.length;i++){
			G_obj=window.frames[i];
			G_ID=document.getElementById(G_obj.name);
			if (moz){
				V_childH=G_obj.document.body.offsetHeight;
			}else{
				V_childH=G_obj.document.body.scrollHeight;
			}	
			//alert(V_childH);
			G_ID.setAttribute("height",V_childH);
			G_obj.document.body.style.overflow="hidden";
			window.status=G_obj.name+" height:"+G_ID.getAttribute("height");
				
		}
	}else{
		G_obj=window.frames[P_obj];
		G_ID=document.getElementById(P_obj);
		if (moz){
			V_childH=G_obj.document.body.offsetHeight;
		}else{
			V_childH=G_obj.document.body.scrollHeight;
		}	
		//alert(V_childH);
		G_ID.setAttribute("height",V_childH);
		G_obj.document.body.style.overflow="hidden";
		window.status=P_obj+" height:"+G_ID.getAttribute("height");
	}
}

//对父窗口的父窗口进行自动匹配高度
var G_Grand=window.parent.parent;
function autoHeightGrand(){
	if(!!G_Grand){
		for (var i=0;i<G_Grand.frames.length;i++){
			if (G_Grand.frames[i].location==G_parent.document.location){
				//alert(document.location);
				//F_H = Math.max(self.document.body.offsetHeight,self.document.body.scrollHeight);
				//F_H = Math.max(F_H,self.document.body.clientHeight);
				F_H=moz?(G_parent.document.body.offsetHeight+20):G_parent.document.body.scrollHeight;
				try{
				G_Grand.document.getElementsByTagName("iframe")[i].setAttribute("height",F_H);
				G_parent.document.body.style.overflow="hidden";
				window.status="Auto height:"+G_Grand.document.getElementsByTagName("iframe")[i].getAttribute("height");
				//alert("offsetHeight=="+G_parent.document.body.offsetHeight+"\nscrollHeight=="+G_parent.document.body.scrollHeight+"\nclientHeight=="+G_parent.document.body.clientHeight);
				}catch(e){}
			}
		}
	}
}
