
// all of these functions depend on the following variables
// HM_IE
// HM_DOM

/*
******************************************************************************
** setStyleProp
** Used to set the property of an HTML object's embedded style object to a
** particular value
******************************************************************************
*/
function setStyleProp(panel,sp,v)
{
		nRules  	= document.styleSheets(0).rules.length;
		dstRules	= document.styleSheets(0).rules;
		
		reDst 		= new RegExp("\\."+panel);
		for(j=0;j<nRules;j++)
		{
			dstRule = dstRules.item(j);
			if(dstRule.selectorText.search(reDst) != -1)
			{
				//alert("dstRule.style."+sp+" = '"+v+"'");
				eval("dstRule.style."+sp+" = '"+v+"'");
				break;
			}
		}				
}



function getRule(classname)
{
	nRules  	= document.styleSheets(0).rules.length;
	dstRules	= document.styleSheets(0).rules;
	reDst 		= new RegExp("\\."+classname);
	for(j=0;j<nRules;j++)
	{
		dstRule = dstRules.item(j);
		if(dstRule.selectorText.search(reDst) != -1)
		{
			return dstRule;
		}
	}	
	return null;
}

function loadBorderProps(panel)
{
	var classname = panel;
	var rule = getRule(classname);
	if(rule)
	{
		document.weblayout.cmBorderLeft.value 		= 
			(rule.style.borderLeftStyle == "none" || rule.style.borderLeftWidth == "0px") ? "none" : "solid";
		document.weblayout.cmBorderLeftStyle.value  = rule.style.borderLeftStyle;
		document.weblayout.cmBorderLeftWidth.value	= rule.style.borderLeftWidth;
		

		document.weblayout.cmBorderTop.value 		= 
			(rule.style.borderTopStyle == "none" || rule.style.borderTopWidth == "0px") ? "none" : "solid";
		document.weblayout.cmBorderTopStyle.value  	= rule.style.borderTopStyle;
		document.weblayout.cmBorderTopWidth.value	= rule.style.borderTopWidth;

		document.weblayout.cmBorderRight.value 		= 
			(rule.style.borderRightStyle == "none" || rule.style.borderRightWidth == "0px") ? "none" : "solid";
		document.weblayout.cmBorderRightStyle.value = rule.style.borderRightStyle;
		document.weblayout.cmBorderRightWidth.value	= rule.style.borderRightWidth;

		document.weblayout.cmBorderBottom.value 		= 
			(rule.style.borderBottomStyle == "none" || rule.style.borderBottomWidth == "0px") ? "none" : "solid";
		document.weblayout.cmBorderBottomStyle.value  	= rule.style.borderBottomStyle;
		document.weblayout.cmBorderBottomWidth.value	= rule.style.borderBottomWidth;
	}
}

/*
******************************************************************************
** setColorTarget
** sets the target of the color picker in the customization interface for 
** a panel. if will be a javascript style object property ie. backgroundColor
******************************************************************************
*/
function setColorTarget(v)
{
	cmcolorprop = v; 
}

/*
******************************************************************************
** setPanelTarget
** sets the focus to the current panel that will be modified
******************************************************************************
*/
function setPanelTarget(v)
{
	cmpanel = v; 
	loadBorderProps(v);
}

/*
******************************************************************************
** getElementXY
** dynamically determines the screen offset of a particular object. Given the
** form and objectid it will use cross browser methods to find the object and
** retrieve its xy location 
******************************************************************************
*/
function getElementXY(form,objid)
{
	var el;
	
	if(form.length == 0)
	{
		if(HM_IE)
		{
			eval("el = document.all."+objid);
		}
		else
		{
			eval("el = document.images."+objid);
			if(!el)	eval("el = document.links."+objid);
		}
	}
	else
	{
		eval('el = document.'+form+'.'+objid);
	}
		
	var rd = {x:0,y:0};
	
	if(!el) return rd;
	
	if(HM_IE || HM_DOM)
	{
		do{
			rd.x += el.offsetLeft;
			rd.y += el.offsetTop;
			el = el.offsetParent;
		} while(el);
	}
	else
	{
		rd.x = el.x;
		rd.y = el.y;
	}
	return rd;
}

/*
******************************************************************************
** cmSetGPanelProp
** this cm function works inside the form where the panels are being displayed
** and helps set global properties --- may be obsoleted by the customization window
******************************************************************************
*/

function cmSetGPanelProp(s,panel,pi,v) 
{
	alert('s'+s+panel+'['+pi+']='+v);
	eval("s"+s+panel+"["+pi+"]='"+v+"'");
}

function cmGetGPanelProp(s,p,i) 
{
	//alert("cmGetGPanelProp");
	var val;
	if(typeof eval('s'+s+p) != 'undefined') 
	{
		eval('val=s'+s+p+'['+i+']');
		
		alert('s'+s+p+'['+i+']='+val);
	}
	return val;
}

function cmGenColorBar(color) 
{
	var css = "background-color:" + color + "; font-size: 5px; color:"+color+";";
	return("<font style='"+css+"'>XXXXXXXXXXXXXXX</font>");
}

function loadStyles()
{
	transferLayoutStyles(window.opener,window.name,window,'','');
}


/*
******************************************************************************
** transferLayoutStyles
** this function transfers layout styles from one window to another, using 
** prefixes found on one or the other
******************************************************************************
*/
function transferLayoutStyles(srcWnd,srcPrefix,dstWnd,dstPrefix,formfield)
{
	// alert("transferLayoutStyles: from "+srcWnd.document.title+":"+srcPrefix+" to "+dstWnd.document.title+":"+dstPrefix);
	
	var debugon = 0; //srcWnd.document.title.search("Blank") != -1;
	
	if(HM_IE)
	{	
		nSrcRules  	= srcWnd.document.styleSheets(0).rules.length;
		nDstRules  	= dstWnd.document.styleSheets(0).rules.length;
		srcRules 	= srcWnd.document.styleSheets(0).rules;
		dstRules 	= dstWnd.document.styleSheets(0).rules;
	
	//	reSearch = /^.*\d$/;
	//	if(srcPrefix.search(reSearch) != -1)
	//		re	= new RegExp("\\."+srcPrefix+"\\D");
	//	else
			re	= new RegExp("\\."+srcPrefix);
			
		if(debugon) alert("secPrefixRegExp: "+re.source);
		for(i=0;i<nSrcRules;i++)
		{	
			srcRule = srcRules.item(i);
			//if(debugon) alert("checking: "+srcRule.selectorText);
					
			if(srcRule.selectorText.search(re) != -1)
			{
			// make the corresponding destination prefix
				dstSelector = srcRule.selectorText.replace(srcPrefix,dstPrefix);
				reDst = new RegExp(dstSelector.replace(".","\\.")); // literalize the period
				
				//alert("lookingFor: "+reDst.source);
				
				for(j=0;j<nDstRules;j++)
				{
					dstRule = dstRules.item(j);
					//alert("matching: "+dstRule.selectorText+" with: "+dstSelector);
					
					if(dstRule.selectorText.search(reDst) != -1)
					{
						dstRule.style.backgroundColor 	= srcRule.style.backgroundColor;
						dstRule.style.borderLeft		= srcRule.style.borderLeft;
						dstRule.style.borderRight		= srcRule.style.borderRight;
						dstRule.style.borderTop			= srcRule.style.borderTop;
						dstRule.style.borderBottom		= srcRule.style.borderBottom;
						// alert("transferring: ("+srcRule.style.cssText+")"+srcRule.selectorText+" styles to "+dstRule.selectorText);	
					// only one dest rule matches
					
						if(formfield.length != 0)
						{
						// transfer the cssText of the source rule to the formfield in the target
						// window
							field = eval("dstWnd.document.all."+formfield);
							if(field)
							{
								field.value = srcRule.style.cssText;
							}
						}
						break;
					}
				}
			}
		}
	}	
}

/*
******************************************************************************
** cmCustomizeLayout
** this function works from the panel as well and will open the customization
** panel, and based on the current layout should update the panel properties
** in the customize window to the panel settings
******************************************************************************
*/
function cmCustomizeLayout(form,snum,pnum,propname)
{
// find the element that we're customizing
	var loc = getElementXY(form,propname);
	
	var left = loc.x; // - 400;
	var top  = loc.y; // - 200;
	
// the height will certainly be dependent on the type of panel ... maybe we can do a resize
// inside of the created window -- the file that is opened will also be particular to the
// type of file they
	var winX, winY;
	/*
	if(HM_IE)
	{
		winX = (window.screen.width-400)/2 - window.screenLeft;
		winY = (window.screen.height-230)/2 + window.screenTop;	
	}
	else
	{
		winX = window.screenX;
		winY = window.screenY;		
	}
	left = left - winX; top = top-winY;
	*/
	//alert("left: "+left+" top: "+top+"sleft: "+winX+" stop: "+winY);
	
	var features = 'toolbar=0,location=0,directories=0,status=0,menubar=0,resizable=1,copyhistory=0,scrollbars=0,width=400,height=210'; //,left='+left+',top='+top;
	
	layout = eval('s'+snum+'layout');
	layoutTitle = eval('s'+snum+'layoutTitle');
	
	//alert("layout = "+layout+" layoutTitle "+layoutTitle);
	if(cmCustomizeWnd != null && cmCustomizeWnd.closed == false)
		cmCustomizeWnd.close();
		
	//	cmCustomizeWnd.location = "/weblayout/weblayout_"+layout+".html";
	//	cmCustomizeWnd.moveTo(left,top);
	//	cmCustomizeWnd.focus();
	//}
	//else
	//{
	
	cmCustomizeWnd=window.open("/weblayout/weblayout_"+layout+".html",'s'+snum,features);
	

	//}
// we want to push the properties of the current panel into the customize window 
// we're modifying the styles and we'll do it at the array level rather than the item level
// we'll just copy from our local styles of the same name

	if(cmCustomizeWnd != null && cmCustomizeWnd.closed == false)
	{
		
	
	}
} 
