/**
*	LayerFactory
*
*	Returns an extended reference to an existing div, or creates a new div,
*	adds it to the dom, and extends it.
*/

//	Factory that extends and creates divs
var LayerFactoryClass = Class.create();

LayerFactoryClass.prototype = Object.extend(new BaseClass(),{
	classid : "LayerFactory",
	divBaseName:"autoDiv",
	initialize: function(){;},
	id:0,
	getLayer : function(id) {
		var lay=$(id);
		if(!lay){return null;}
		Object.extend(lay, new LayerBaseClass());
		lay.created=true;
		return lay;
		},
	addLayer : function(id,parent,properties){
		parent=$(parent);
		if(!parent){parent=document.body;}
		var lay = document.createElement('div');
		lay.id = this.divBaseName+this.id++;
		//	Extend div with prototype methods
		Element.extend(lay);
		//	Extend div with LayerBaseClass methods
		Object.extend(lay, new LayerBaseClass());
		parent.appendChild(lay);
		//	If passed properties object, set them on layer
		if(properties){lay.setProperties(properties);}
		lay.created=true;
		return lay;
		},
	getUniqueDivName : function(baseName){
		if(!$(baseName)){return baseName;}
		var numb=0;
		var name;
		while (numb < 200){
			name=baseName+this.getTwoDigitString(numb);
			if(!$(name)){break;}
			numb++;
			}
		return name;
		}
});

//	Initialize a copy of the factory class
var LayerFactory=new LayerFactoryClass();



//	Base class that is used to add additional methods to divs
var LayerBaseClass = Class.create();

LayerBaseClass.prototype=Object.extend(new BaseClass(), {
	classid : "LayerClass",
	initialize: function(){;},
	setOffset : function(offset,value){
		if(this.style){
			if((value!=null) && ((typeof value=="number")||(value.indexOf("px")<0))){value=""+value+"px";}
			this.style[offset]=value;
			}
		},
	getOffset : function(offset,obj){
		if(!obj){obj=this;}
		return parseInt(obj["offset"+offset.capitalize()]);
		},
	setTop : function(value){
		this.setOffset("top",value);
		return this;
		},
	getTop : function(obj){
		return this.getOffset("top",obj);
		},
	setLeft : function(value){
		this.setOffset("left",value);
		return this;
		},
	getLeft : function(obj){
		return this.getOffset("left",obj);
		},
	setRight : function(value){
		this.setOffset("right",value);
		return this;
		},
	getRight : function(obj){
		return this.getOffset("right",obj);
		},
	setBottom : function(value){
		this.setOffset("bottom",value);
		return this;
		},
	getBottom : function(obj){
		return this.getOffset("bottom",obj);
		},
	setWidth : function(value){
		if((typeof value=="number")||((typeof value=="string")&&(value.indexOf("px")<0))){
			value=""+value+"px";
			}
		this.setStyle({"width":value});
		},
	setHeight : function(value){
		if((typeof value=="number")||((typeof value=="string")&&(value.indexOf("px")<0))){
			value=""+value+"px";
			}
		this.setStyle({"height":value});
		},
	/**
	*	getAbsPos
	*	Returns an array of the cumulative offset of the current
	*	or passed object.
	*/
	getAbsPos : function(obj){
		if(!obj){obj=this;}
		return Position.cumulativeOffset(obj);
		},
	/**
	*	getPosition
	*	Same as getAbsPos, returns an object of {x:numb, y:numb}
	*/
	getPosition : function(obj){
		if(!obj){obj=this;}
		var pos=this.getAbsPos(obj);
		return {x:pos[0],y:pos[1]};
		},
		
	/**
	*	setOpacity
	*
	*	Sets the opacity (or inversly transparency) of the div and its contentx, 
	*	this function converts the value to correct css property for ie
	*	and moz based browsers.  Pass opacity as value between 0 and 1, with 1 
	*	being fully opaque, or no transaparency.
	*	For opacity to work item must have a placement specified (float:left, or 
	*	position:absolute) 
	*/
	setOpacity: function(value){
		if(this.ie4){
			//	set ie version
			value=value*100;
			this.style.filter="alpha(opacity="+value+")";
			}
		else if(this.dom){
			this.style.opacity=value;
			}
	},
	
	/**
	*	setProperties
	*	Accepts hash and sets key/value pairs as properties of this object, will
	*	try to use setter functions of this object to set properties by calling
	*	setProperty() method.
	*/
	setProperties : function(properties){
		for(var prop in properties){
			try{this.setProperty(prop, properties[prop]);}
			catch(e){
				this.log.warn("LayerBaseClass.setProperties() Error occurred while setting the initial property " + prop + " to " + properties[prop] + ", error=" + e.toString());
				}
			};
		},
	/**
	*	setProperty
	*	Internal function to set a property on this object, will try to call 
	*	setter function first, then try to set style property or attribute.
	*	Used by setProperties().
	*/
	setProperty : function(key, value){
		//	If a setter function exists for the property, call it
		var setterName = "set" + key.charAt(0).toUpperCase() + key.substring(1);
		if(this[setterName]){
			this[setterName](value);
			return;
			}
		//	Try to set as style property
		if(this.style){
			this.style[key]=value;
			}
		else if(this.setAttribute){
			//this.log.debug("Setting dom attribute " + key + " to " + value);
			//	Set value using dom
			this.setAttribute(key, value);
			}
		},
	/**
	*	position
	*	Uses the Position Class and a position definition (passed) to 
	*	position this layer on screen
	*/
	position : function(alignFrom, alignTo, offset){
		var posObj = new Positioner(document, this, alignFrom, alignTo, offset);
		posObj.position();
		},
	insertBefore : function(content){
		new Insertion.Before(this,content);
		return this;
		},
	insertTop : function(content){
		new Insertion.Top(this,content);
		return this;
		},
	insertBottom : function(content){
		new Insertion.Bottom(this,content);
		return this;
		},
	insertAfter : function(content){
		new Insertion.After(this,content);
		return this;
		},
	addInitProp : function(prop,value){
		if(this.initProps==null){
			this.initProps={};
			}
		this.initProps[prop]=value;
		}
});	//	
	

