/******************************************************************************/
/*  CLASS: 		LayersBase									*/
/*  DESCRIPTION:	Populates basic methods for cross-browser layer manipulation*/
/*  PROPERTIES:	browser - user browser identification (object)			*/
/*  METHODS:	CreateLayerObj								*/
/*			LayerStyleProps								*/
/*			ShowLayer									*/
/*			HideLayer									*/
/*			WriteLayerHTML								*/
/*  REQUIRES:	browser_class.js								*/
/*  NOTE:		NN4.04+, IE4+ Implementation (JavaScript 1.2)			*/
/******************************************************************************/

////////////////////////////////////////////////////////////////////////////////
//  CONSTRUCTOR: 	LayersBase									//
//  USES:		Browser (class)								//
//  DESCRIPTION: 	Instantiates the object							//
////////////////////////////////////////////////////////////////////////////////

function LayersBase() {

	// Object properties
	this.browser = new Browser();	// browser identification

	// Object methods
	this.CreateLayerObj  = CreateLayerObj;
	this.LayerStyleProps = LayerStyleProps;
	this.ShowLayer	   = ShowLayer;
	this.HideLayer 	   = HideLayer;
	this.WriteLayerHTML  = WriteLayerHTML;
}

////////////////////////////////////////////////////////////////////////////////
//  METHOD: 	CreateLayerObj								//
//  PARAMETERS:	sLayerID - layer unique identifier (string)			//
//			bStyle   - create layer's style object (boolean)		//
//  USES:		this.browser.sDoc, this.browser.sSty				//
//  RETURNS:	Specified layer object							//
//  DESCRIPTION: 	Creates a layer object or its style object			//
////////////////////////////////////////////////////////////////////////////////

function CreateLayerObj(sLayerID, bStyle) {

	// Local variables
	// form cross-browser object strings
	var sLayerObj 	 = this.browser.sDoc + '["' + sLayerID + '"]';
	var sLayerStyleObj = sLayerObj + this.browser.sSty;
	
	// create layer object
	var oLayerObj = eval(sLayerObj);

	// Create layer style object if required
	if(oLayerObj && bStyle) {
		oLayerObj = eval(sLayerStyleObj);
	}

	// Return the object - might be null if non-existent
	return (oLayerObj);
}

////////////////////////////////////////////////////////////////////////////////
//  FUNCTION: 	LayerStyleProps								//
//  PARAMETERS:	sLayerID 	     - layer unique identifier (string)		//
//			sLayerStyleProps - comma delimited layer style properties. 	//
//						 Set property if in pair of 'name=value', //
//						 else	retrieve property name. (string)	//
//  USES:		this.browser.sDoc, this.browser.sSty				//
//  RETURNS:	Associative array of layer style properties requested; empty//
//			if only setting properties.						//
//  DESCRIPTION: 	Sets and/or retrieves layer style properties.			//
////////////////////////////////////////////////////////////////////////////////

function LayerStyleProps(sLayerID, sLayerStyleProps) {

	// Local variables
	var sProp   = "";
	var iEqPos  = 0;
	var sWHProp = "";

	// form cross-browser layer object strings
	var sLayerObj		   = this.browser.sDoc + '["' + sLayerID + '"]';	  
	var sLayerStyleObj	   = sLayerObj + this.browser.sSty;

	// layer style properties arrays
	var aLayerStyleProps 	   = new Array();
	var aLayerStylePropsValues = new Array();

	// Trim any spaces found in the property list string
	sLayerStyleProps = sLayerStyleProps.replace(/\s*/g, "");

	// Validate passed parameters
	if (eval(sLayerObj) && sLayerStyleProps) {

		// Split properties string into individual properties
		aLayerStyleProps = sLayerStyleProps.split(",");

		// Iterate through style properties array elements
		for(var iIndex = 0; iIndex < aLayerStyleProps.length; iIndex++) {
			
			// Retrieve style property
			sProp = aLayerStyleProps[iIndex];

			// Set layer style properties, if property element contains
			// the assignment operator ("="), otherwise retrieve property
			iEqPos = sProp.indexOf("="); 
			if (iEqPos != -1) {

				// For the style properties "width" and "height", ensure
				// no negative value is assigned; set a default of 10px
				sWHProp = sProp.substr(0, iEqPos);
				if ( ((sWHProp == "width") || (sWHProp == "height")) &&
				    (parseInt(sProp.substr(iEqPos + 1)) < 0) ) {
				    	    sProp = sWHProp + "=10";
				}	
				eval(sLayerStyleObj + "." + sProp);
			} else {
				aLayerStylePropsValues[sProp] = 
					eval(sLayerStyleObj + "." + sProp);
			}
		}
	}

	// Return requested layer style properties
	return (aLayerStylePropsValues);
}

////////////////////////////////////////////////////////////////////////////////
//  FUNCTION: 	ShowLayer									//
//  PARAMETERS:	sLayerID - layer unique identifier (string)			//
//  USES:		this.LayerStyleProps()							//
//  DESCRIPTION: 	Makes the passed layer visible					//
////////////////////////////////////////////////////////////////////////////////

function ShowLayer(sLayerID) {

	// Show the layer by setting its visibility to "visible"
	this.LayerStyleProps(sLayerID, "visibility='visible'");
}

////////////////////////////////////////////////////////////////////////////////
//  FUNCTION: 	HideLayer									//
//  PARAMETERS:	sLayerID - layer unique identifier (string)			//
//  USES:		this.LayerStyleProps()							//
//  DESCRIPTION: 	Makes the passed layer hidden.					//
////////////////////////////////////////////////////////////////////////////////

function HideLayer(sLayerID) {

	// Hide the layer by setting its visibility to "hidden"
	this.LayerStyleProps(sLayerID, "visibility='hidden'");
}

////////////////////////////////////////////////////////////////////////////////
//  FUNCTION: 	WriteLayerHTML								//
//  PARAMETERS:	sLayerID - layer unique identifier (string)			//
//			sHTML    - HTML code (string)						//
//  USES:		this.browser.sDoc, this.browser.sHtm				//
//  DESCRIPTION: 	Writes passed HTML code within the specified layer.		//
////////////////////////////////////////////////////////////////////////////////

function WriteLayerHTML(sLayerID, sHTML) {

	// Local variables
	// layer object vars
	var sLayerObj = this.browser.sDoc + '["' + sLayerID + '"]';
	var oLayerObj = null;

	// Write the passed HTML code within the layer object
	if (eval(sLayerObj) && sHTML) {

		// Create layer object referencing document methods
		oLayerObj = eval(sLayerObj + this.browser.sHtm);

		// Cross-browsing HTML writting
		if (this.browser.ns4) {
			oLayerObj.write(sHTML);
			oLayerObj.close();
		} else if (this.browser.ie4) {
			oLayerObj.innerHTML = sHTML;
		}
	}
}