/*-----------------------------------------------------------\
|                    EditFrame v2.2.2                        |
|------------------------------------------------------------|
| Author: Dan Sutton                                         |
| Date:   October 7th, 2008                                  |
|------------------------------------------------------------|
| Dependencies: iframeSupport.js                             |
|------------------------------------------------------------|
| Provides pop-up frames (fake windows).                     |
\-----------------------------------------------------------*/

// Control Variable
var efControl=
{
	images: "/images/editFrame/",	// Images for Shading, etc.
	items: [],										// Registered Items
	
	// Return item by ID
	find: function(id)
	{
		return this.items[id];
	},
	
	// Create a new EditFrame
	add: function(id, defaultPage, noShadows)
	{
		var x=new efFrame(id, defaultPage, noShadows);
		this.items[id]=x;
		return x;
	},

	// This returns the inner width of the browser
	innerWidth:	function()
	{
		if (window.innerWidth)
			return window.innerWidth;
		else if (document.body)
			return document.body.clientWidth;
		else if (document.documentElement && document.documentElement.clientWidth)
			return document.documentElement.clientWidth;
		else
			return screen.width;
	},
									
	// This returns the inner height of the browser
	innerHeight: function()
	{
		if (window.innerWidth)
			return window.innerHeight;
		else if (document.body)
			return document.body.clientHeight;
		else if (document.documentElement && document.documentElement.clientWidth)
			return document.documentElement.clientHeight;
		else
			return screen.height;
	}
};

// Edit Frame Class
function efFrame(id,defaultPage, noShadows)
{
	this.id=id;
	this.defaultPage=defaultPage;
	this.noShadows=noShadows;
	this.divid=this.id+"_div";
	this.shadowid=this.id+"_shadow";
	this.frameid=this.id+"_frame";
	this.placerid=this.id+"_placer";
	this.rtableid=this.id+"_rtable"; // for Safari
}

// Determine whether to use DXImageTransform filters for old version of MSIE
efFrame.prototype.is_msie6=(navigator.userAgent.toLowerCase().indexOf("msie 6")>=0)
 || (navigator.userAgent.toLowerCase().indexOf("msie 5")>=0)
 || (navigator.userAgent.toLowerCase().indexOf("msie 4")>=0);

// Open the Frame with the given URL
efFrame.prototype.openFrame=function(url, zIndex, show)
{
	var f=iframeSupport.findFrame(this.frameid);
	if (url!=null && url) // Need first clause for Firefox
	{
		url+=(url.indexOf("?")>=0? "&" : "?")+"ef_rnd="+Math.random();
		try
		{
			f.location=url;
			f.src=url;
		}
		catch(e)
		{
			alert("Unable to open frame: "+e.message);
		}
	}									
	if (show)
		this.showFrame(zIndex);
};

// Render frame
efFrame.prototype.showFrame=function(zIndex)
{
	var x=document.getElementById(this.divid);
	var y=document.getElementById(this.shadowid);
	y.style.visibility="visible";
	x.style.visibility="visible";
	zIndex=(zIndex? parseFloat(zIndex) : 99998);
	y.style.zIndex=zIndex;
	x.style.zIndex=1+zIndex;
};

// Find top of iFrame in page
efFrame.prototype.frameTop=function()
{
	var f=iframeSupport.findFrame(this.frameid);
	if (f.frameElement)
		f=f.frameElement;
	return f.offsetTop;
};

// Find Left of iFrame in page
efFrame.prototype.frameLeft=function()
{
	var f=iframeSupport.findFrame(this.frameid);
	if (f.frameElement)
		f=f.frameElement;
	return f.offsetLeft;
};

// Resize the Frame
efFrame.prototype.sizeFrame=function(width, height)
{
	var f=iframeSupport.findFrame(this.frameid);
	if (f.frameElement)
		f=f.frameElement;
	f.style.width=width;
	f.style.height=height;
	var t=f.offsetTop;
	var l=f.offsetLeft;
	if (is_safari || is_opera) // then it needs help sizing the vertical shadow
	{
		try
		{
			var r=document.getElementById(this.rtableid);
			r.style.height=2+parseFloat(height);
		}
		catch(e){}
	}
	// Now center the frame
	var x=(efControl.innerWidth()-width)/2;
	var y=(efControl.innerHeight()-height)/2;
	var d=document.getElementById(this.divid);
	d.style.left=Math.max(0,x);
	d.style.top=Math.max(0,y);
};

// Hide the Frame; optionally display a message
efFrame.prototype.hideFrame=function(errm, refreshMe)
{
	var x=document.getElementById(this.divid);
	var y=document.getElementById(this.shadowid);
	x.style.visibility="hidden";
	y.style.visibility="hidden";
	
	if (this.defaultPage && refreshMe)
	{
		var f=iframeSupport.findFrame(this.frameid);
		try
		{
			f.location=this.defaultPage;
			f.src=this.defaultPage;
		}
		catch(e){}
	}
	if (errm)
		if (errm.length>0)
			alert(errm);
	this.onHidden();
};

// Generate HTML for frame contents. Special case for older MSIE versions.
efFrame.prototype.innerHTML=function()
{
	if (this.noShadows)
		return "<table cellpadding=0 cellspacing=0 border=0 id='"+this.placerid+"' width=1 height=1"
		 +" class='editNoShadow'>"
		 +"<tr valign='top'><td><iframe src='"+this.defaultPage+"' name='"+this.frameid+"' id='"+this.frameid+"'"
		 +" marginWidth=0 marginHeight=0 frameBorder=0 scrolling=no class='editFrame'></iframe>"
		 +"</td></tr></table>";
	else if (this.is_msie6) // then semi-transparent shadows won't work
		return "<table cellpadding=0 cellspacing=0 border=0 id='"+this.placerid+"' width=1 height=1>"
		 +"<tr valign='top'><td><iframe src='"+this.defaultPage+"' name='"+this.frameid+"' id='"+this.frameid+"'"
		 +" marginWidth=0 marginHeight=0 frameBorder=0 scrolling=no class='editFrame'></iframe>"
		 +"</td><td height='100%'><table cellspacing=0 cellpadding=0 border=0 width='14px' height='100%'>"
		 +"<tr><td class='editShadowTR_msie'>&nbsp;</td></tr><tr>"
		 +"<td class='editShadowR_msie'>&nbsp;"
		 +"</td></tr></table></td></tr><tr>"
		 +"<td colspan=2><table cellpadding=0 cellspacing=0 border=0 width='100%' height='18px'>"
		 +"<tr valign='top'><td class='editShadowBL_msie'>&nbsp;</td>"
		 +"<td class='editShadowB_msie'>&nbsp;</td>"
		 +"<td class='editShadowBR_msie'>&nbsp;<td>"
		 +"</tr></table></td></tr></table>";
	else
		return "<table cellpadding=0 cellspacing=0 border=0 id='"+this.placerid+"' width=1 height=1>"
		 +"<tr valign='top'><td><iframe src='"+this.defaultPage+"' name='"+this.frameid+"' id='"+this.frameid+"'"
		 +" marginWidth=0 marginHeight=0 frameBorder=0 scrolling=no class='editFrame'></iframe>"
		 +"</td><td height='100%'><table cellspacing=0 cellpadding=0 border=0 width='14px' height='100%'>"
		 +"<tr><td height='1%'><img src='"+efControl.images+"sh_tr.png' class='editShadowTR'></td></tr><tr height='100%'>"
		 +"<td height='100%' class='editShadowR'>&nbsp;"
		 +"</td></tr></table></td></tr><tr>"
		 +"<td colspan=2><table cellpadding=0 cellspacing=0 border=0 width='100%' height='18px'>"
		 +"<tr height='18px' valign='top'><td width='1%'><img src='"+efControl.images+"sh_bl.png' class='editShadowBL'>"
		 +"</td><td width='100%' class='editShadowB'>&nbsp;</td>"
		 +"<td width='1%'><img src='"+efControl.images+"sh_br.png' class='editShadowBR'><td>"
		 +"</tr></table></td></tr></table>";
}

// Outer HTML for Frame
efFrame.prototype.toString=function()
{
	return "<div class='editShadowDiv"+(this.is_msie6? "_msie" : "")+"'"
	 +" id='"+this.shadowid+"'></div>"
	 +"<div class='editDiv' id='"+this.divid+"'>"
	 +this.innerHTML()
	 +"</div>";
};

// Trappable event: occurs after the frame is hidden
efFrame.prototype.onHidden=function(){};