function fnGetObject(id) {
	function findLayerObj (layerName, parentLayer) {
		parentLayer = (parentLayer) ? parentLayer : document;
		layerCollection = parentLayer.layers;
		if (layerCollection[layerName])	return layerCollection[layerName];
		for (i=0; i<layerCollection.length; i++) if(layerCollection[i].layers.length>0) return(findLayerObj(layerName, layerCollection[i]));
	}
	if (!id) return false;
	if (typeof(id)=='object') return id;
	if (typeof(id)=='string') {
		if (document.getElementById) return document.getElementById(id);
		if (document.all) return document.all[id];
		if (document.layers) return findLayerObj(id);
	}
}

function fnSetVisible(id,condition) {
	if(!(layerObj=fnGetObject(id))) return false;
	if(typeof(condition)=='boolean'||typeof(condition)=='number') {
	}else if(typeof(condition)=='string'){
		if(condition.toLowerCase()=='show'||condition.toLowerCase()=='visible') condition=true;
		else if(condition.toLowerCase()=='hide'||condition.toLowerCase()=='hidden') condition=false;
		else return false;
	}else return false
	targetObj= (typeof(layerObj.style)!='undefined') ? layerObj.style : layerObj;
	if(typeof(targetObj.visibility)!='undefined') targetObj.visibility= condition ? ((document.layers) ? 'show' : 'visible') : ((document.layers) ? 'hide' : 'hidden') ;
}

function fnSetContent(id,description) {
	if(!(layerObj=fnGetObject(id))) return false;
	if(typeof(description)!='string') return false;
	description=unescape(description);
	if(typeof(layerObj.style)!='undefined') {
		if(typeof(layerObj.innerHTML)!='undefined') {
			layerObj.innerHTML= description;
		}else{
			//do nothing for browsers that supports DOM but not innerHTML
		}
	}else{
		layerObj.document.write(description);
		layerObj.document.close();
	}
}

function fnSetX(id,x) {
	if(!(layerObj=fnGetObject(id))) return false;
	if(typeof(x)!='number') return false;
	if(document.layers) {
		if(typeof(layerObj.left)!='undefined') layerObj.left= x;
	}else if(typeof(layerObj.style)!='undefined') {
		if(typeof(layerObj.style.left)=='string') layerObj.style.left= x+"px";
		else if(typeof(layerObj.style.pixelLeft)!='undefined') layerObj.style.pixelLeft= x;
	}
}

function fnSetY(id,y) {
	if(!(layerObj=fnGetObject(id))) return false;
	if(typeof(y)!='number') return false;
	if(document.layers) {
		if(typeof(layerObj.top)!='undefined') layerObj.top= y;
	}else if(typeof(layerObj.style)!='undefined') {
		if(typeof(layerObj.style.top)=='string') layerObj.style.top= y+"px";
		else if(typeof(layerObj.style.pixelTop)!='undefined') layerObj.style.pixelTop= y;
	}
}

function fnSetZ(id,zIndex) {
	if(!(layerObj=fnGetObject(id))) return false;
	layerObj= (typeof(layerObj.style)!='undefined') ? layerObj.style : layerObj ;
	if(typeof(layerObj.zIndex)!='undefined') layerObj.zIndex= zIndex;
}

function fnGetXYZ(id,whichCoord) {
	if(!(layerObj=fnGetObject(id))) return false;
	layerObj= (typeof(layerObj.style)!='undefined') ? layerObj.style : layerObj ;
	if(whichCoord=='x'||whichCoord=='X') return (typeof(layerObj.left)!='undefined') ? parseInt(layerObj.left) : ( (typeof(layerObj.pixelLeft)!='undefined') ? layerObj.pixelLeft : false) ;
	if(whichCoord=='y'||whichCoord=='Y') return (typeof(layerObj.top)!='undefined') ? parseInt(layerObj.top) : ( (typeof(layerObj.pixelTop)!='undefined') ? layerObj.pixelTop : false) ;
	if(whichCoord=='z'||whichCoord=='Z') return (typeof(layerObj.zIndex)!='undefined') ? layerObj.zIndex : false ;
	return false;
}

function fnSetClip(id, top, right, bottom, left) {
	if(!(layerObj=fnGetObject(id))) return false;
	layerObj= (typeof(layerObj.style)!='undefined') ? layerObj.style : layerObj ;
	if(typeof(layerObj.clip)!='undefined'){
		if(typeof(layerObj.clip)=='string'){ 
			if(typeof(top)=='number' && typeof(left)=='number' && bottom>0 && right>0) layerObj.clip= "rect("+top+"px "+right+"px "+" "+bottom+"px "+left+"px )";
			else layerObj.clip= "rect(auto auto auto auto)";
		}
		else if(typeof(layerObj.clip.top)!='undefined'){
			if(typeof(top)=='number' && typeof(left)=='number' && bottom>0 && right>0) {
				layerObj.clip.top = top;
				layerObj.clip.right = right;
				layerObj.clip.bottom = bottom;
				layerObj.clip.left = left;
			}
		}
	}
}

function fnSetHeight(id,ht) {
	function isValid(value) { return (typeof(value)=='number') ? (value>=0 ? true : false) : false; }
	if(!(layerObj=fnGetObject(id))) return false;
	if(!isValid(ht)) return false;
	if(document.layers) {
		if(typeof(layerObj.clip)!='undefined') if(typeof(layerObj.clip.height)!='undefined') layerobj.clip.height= ht;
	}else if(typeof(layerObj.style)!='undefined') {
		if(typeof(layerObj.style.height)=='string') layerObj.style.height= ht+"px";
		else if(typeof(layerObj.style.pixelHeight)!='undefined') layerObj.style.pixelHeight= ht;
	}
}

function fnSetWidth(id,wd) {
	function isValid(value) { return (typeof(value)=='number') ? (value>=0 ? true : false) : false; }
	if(!(layerObj=fnGetObject(id))) return false;
	if(!isValid(wd)) return false;
	if(document.layers) {
		if(typeof(layerObj.clip)!='undefined') if(typeof(layerObj.clip.width)!='undefined') layerobj.clip.width= wd;
	}else if(typeof(layerObj.style)!='undefined') {
		if(typeof(layerObj.style.width)=='string') layerObj.style.width= wd+"px";
		else if(typeof(layerObj.style.pixelWidth)!='undefined') layerObj.style.pixelWidth= wd;
	}
}

function fnGetContentHeight(id) {
	if(!(layerObj=fnGetObject(id))) return false;
	return layerObj.clip ? layerObj.clip.height : (layerObj.offsetHeight ? layerObj.offsetHeight : (layerObj.style ? (parseInt(layerObj.style.height) ? parseInt(layerObj.style.height) : (layerObj.style.pixelHeight ? layerObj.style.pixelHeight : false) ) : false));
}

function fnGetContentWidth(id) {
	if(!(layerObj=fnGetObject(id))) return false;
	return layerObj.clip ? layerObj.clip.width : (layerObj.clientWidth ? layerObj.clientWidth : (layerObj.style ? (parseInt(layerObj.style.width) ? parseInt(layerObj.style.width) : (layerObj.style.pixelWidth ? layerObj.style.pixelWidth : false) ) : false));
}

function fnGetWindowHeight() {
	if(window.innerHeight) return window.innerHeight;
	if((document.body) && (document.body.offsetHeight)) return document.body.offsetHeight;
	return false;
}

function fnGetWindowWidth() {
	if(window.innerWidth) return window.innerWidth;
	if((document.body) && (document.body.clientWidth)) return document.body.clientWidth;
	return false;
}

// ----------------------------------------------------------------------
function fnHiddenArea(shutDownObjID){ this.shutDownObj=shutDownObjID; }
/* exposed functions:
	.fnShowItem(itemID,depth)
	.fnFadeInOrSetVisible= (itemID, isFadeIn)
	.fnCloseUntilItemDepth(depth)
	.fnShutDown()
   note:
	must be init with a div for shutting down the hidden area
	useful for controlling popUp Divs
	depth starts from 1, NOT 0
*/
fnHiddenArea.prototype.currentContent= ['','','','',''];
fnHiddenArea.prototype.currentDepth= 0;
fnHiddenArea.prototype.fnShowItem= function (id,itemDepth) {
	if(this.currentContent[this.currentDepth]=='') {
		if(typeof(this.shutDownObj)=='string') this.shutDownObj= fnGetObject(this.shutDownObj);
		fnSetVisible(this.shutDownObj,true);
	}
	if(this.currentContent[this.currentDepth]!=id || this.currentContent[this.currentDepth]=='') {
		if(this.currentDepth<itemDepth-1) this.currentDepth= itemDepth-1;
		else if(this.currentDepth>itemDepth-1) this.currentDepth= this.fnCloseUntilItemDepth(itemDepth-1);
		else {
			if(this.currentContent[this.currentDepth]!='') this.fnFadeInOrSetVisible(this.currentContent[this.currentDepth],false);
		}
		this.fnFadeInOrSetVisible(id,true);
		this.currentContent[this.currentDepth]= id;
	}
}
fnHiddenArea.prototype.fnCloseUntilItemDepth= function (depth) {
	for(i=this.currentDepth;i>=depth;i--) {
		this.fnFadeInOrSetVisible(this.currentContent[i],false);
		this.currentContent[i]= '';
	}
	return depth;
}
fnHiddenArea.prototype.fnShutDown= function () {
	this.currentDepth= this.fnCloseUntilItemDepth(0);
	fnSetVisible(this.shutDownObj,false);
}
fnHiddenArea.prototype.fnFadeInOrSetVisible= function (id, isFadeIn) {
	if(!(layerObj=fnGetObject(id))) return false;
	if(typeof(layerObj.filters)!='undefined' ? (layerObj.filters.length>0 ? true : false) : false) {
		layerObj.filters[0].Apply();
		if(isFadeIn) fnSetVisible(layerObj,true);
		else fnSetVisible(layerObj,false);
		layerObj.filters[0].Play();
	}else{
		if(isFadeIn) fnSetVisible(layerObj,true);
		else fnSetVisible(layerObj,false);
	}
}

// ----------------------------------------------------------------------
function fnScrollingContent(variableName,id,direction,step,framespeed) {
	this.variableName=variableName;
	this.id= (typeof(id)=='string') ? id : variableName+'Content';
	this.fnSetDirection(direction);
	this.fnSetPxPerFrame(step);
	this.fnSetFramesPerSecond(framespeed);
	this.clipProperty= new Object();
	this.fnSetClipArea();
}
/* exposed functions:
	.fnSetDirection(direction)
	.fnSetPxPerFrame(step)
	.fnSetFramesPerSecond(framespeed)
	.fnMove()
	.fnSetClipArea(width,height)
   note:
    ???
*/
fnScrollingContent.prototype.fnSetClipArea= function(width,height,top,left) {
	if(typeof(top)=='number' && typeof(left)=='number'){
		this.clipProperty.t= top;
		this.clipProperty.l= left;
	}else{
		this.clipProperty.t= 0;
		this.clipProperty.l= 0;
	}
	if(typeof(width)=='number' && typeof(height)=='number' && width>0 && height>0) {
		this.clipProperty.isClip=true;
		this.clipProperty.h=height;
		this.clipProperty.w=width;
		this.clipProperty.scrollSize= (this.isUp|| this.isDown) ? height : width ;
		this.clipProperty.staticSize= (this.isUp|| this.isDown) ? width : height ;
	}else{
		this.clipProperty.isClip=false;
		fnSetClip(this.id);
	}
}
fnScrollingContent.prototype.fnSetDirection= function(direction) {
	if(typeof(direction)=='string'){
		this.isDown= (direction.toLowerCase()=='down') ? true : false ;
		this.isLeft= (direction.toLowerCase()=='left') ? true : false ;
		this.isRight= (direction.toLowerCase()=='right') ? true : false ;
	}else{
		this.isDown=false;
		this.isLeft=false;
		this.isRight=false;
	}
	this.isUp= (this.isDown||this.isLeft||this.isRight) ? false : true ;
}
fnScrollingContent.prototype.fnSetPxPerFrame= function(step) { this.step= (typeof(step)=='number') ? step : 1 ; }
fnScrollingContent.prototype.fnSetFramesPerSecond= function(framespeed) { this.framespeed= (typeof(framespeed)=='number'&&framespeed>0&&framespeed<=50) ? framespeed : 25; }
fnScrollingContent.prototype.fnBuild= function(content,top,left,width,height,zIndex,paddingSize,bgcolor,borderSize,bordercolor) {
	if(fnGetObject(this.id)) { alert('object "'+this.id+'" present!'); return false; }
	if(fnGetObject(this.id+'Holder')) { alert('object "'+this.id+'Holder" present!'); return false; }

	if(typeof(top)!='number') top=0;
	if(typeof(left)!='number') left=0;
	if(typeof(width)!='number'||width<=0) width=200;
	if(typeof(height)!='number'||height<=0) height=150;
	if(typeof(zIndex)!='number'||zIndex<0) zIndex=0;
	if(typeof(content)!='string') return false;
	if(typeof(borderSize)!='number'||borderSize<0) borderSize=0;
	if(typeof(paddingSize)!='number') paddingSize=5;

	this.isBuild= true;

	lowercaseagent = navigator.userAgent.toLowerCase();
	isOpera= lowercaseagent.indexOf("opera")!=-1 ? true : false;
	isLayer= document.layers ? true : false ;
	
	parentDivCode= '<div' ;
	parentDivCode+= ' id="'+this.id+'"';
	parentDivCode+= ' style=" position:absolute;';
	parentDivCode+= ' top:'+top+'px; left:'+left+'px; width:'+(width)+'px; height:'+(height)+'px;';
	parentDivCode+= ' clip: rect(0px '+width+'px '+height+'px 0px);';
	parentDivCode+= ' z-index:'+zIndex+';';
	parentDivCode+= ' padding: '+paddingSize+'px;';
	if(bgcolor) parentDivCode+= ' background-color: '+bgcolor+'; layer-background-color: '+bgcolor+';';
	if(borderSize) parentDivCode+= ' border: '+borderSize+'px solid '+((bordercolor)?(bordercolor):'black')+';';
	parentDivCode+= '">';
/*
	childDivCode= '<div' ;
	childDivCode+= ' id="'+this.id+'"';
	childDivCode+= ' style="position:absolute; z-index:1;';
	childDivCode+= ' padding: '+paddingSize+'px;';
	childDivCode+= ' top: 0px; left: 0px; width: '+width+'px; height: '+height+'px;">';
*/
	mouseOverStr=this.variableName+".fnStop()";
	mouseOutStr=this.variableName+".fnMove()";
	while(content.match(/<a\shref/i)) content=content.replace(/<a\shref/i, '<a onMouseOver="'+mouseOverStr+'" onMouseOut="'+mouseOutStr+'" href');

	if(isLayer){
		tableCode= '<table';
		tableCode+= borderSize>=0 ? ' border="'+borderSize+'"' : '' ;
		tableCode+= ' cellspacing="0" cellpadding="0" width="'+width+'" height="'+height+'"';
		tableCode+= bgcolor ? ' bgcolor="'+bgcolor+'"' : '';
		tableCode+= bordercolor ? ' bordercolor="'+bordercolor+'"' : '' ;
		tableCode+= '>';

//		setTimeout(this.variableName+'.fnCreateLayer("'+this.id+'Holder'+'")',(1000/this.framespeed));

	}else if(isOpera){
		document.write(parentDivCode+content+'</div>');
	}else{
		document.write(parentDivCode+content+'</div>');
		this.fnSetClipArea(width,height,top,left);
	}
}/*
fnScrollingContent.prototype.fnCreateLayer= function (id,left,top,width,height,content,bgColor,visibility,zIndex,nestref) {
	if (document.layers) {
		if (nestref) layerObj= eval("document."+nestref+".document."+id+" = new Layer(width, document."+nestref+")");
		else layerObj= document.layers[id]= new Layer(width);
		layerObj.name= id;
		layerObj.left= left;
		layerObj.top= top;
		layerObj.clip.height= (typeof(height)=='number') ? height : 150 ;
		layerObj.bgColor= (typeof(bgColor)=='string')  bgColor : '#CCCCCC' ;
		layerObj.visibility= (visibility=='hidden')? 'hide' : 'show' ;
		layerObj.zIndex= (typeof(zIndex)=='number'&&zIndex>=0) zIndex : 0 ;
		if (typeof(content)=='string') {
			layerObj.document.open();
			layerObj.document.write(content);
			layerObj.document.close();
		}
	}
}*/
fnScrollingContent.prototype.fnMove= function() {
	if(typeof(this.id)=='string') this.id=fnGetObject(this.id);
	
	isUpOrLeft= (this.isUp||this.isLeft) ? true : false ;
	isUpOrDown= (this.isUp||this.isDown) ? true : false ;

	opPosNeg= isUpOrLeft ? '-' : '+' ;
	opGreaterLesser= isUpOrLeft ? '>' : '<' ;
	opXY= isUpOrDown ? 'y' : 'x' ;
	opHeightWidth= isUpOrDown ? 'Height' : 'Width' ;
	
	currentPos= fnGetXYZ(this.id,opXY);
	step= eval(opPosNeg+'this.step');
	realScrollSize= eval('fnGetContent'+opHeightWidth+'(this.id)'); 
	clipScrollSize= (this.clipProperty.isClip) ? this.clipProperty.scrollSize : realScrollSize ;
	positionLimit= isUpOrLeft ? 'realScrollSize' : 'clipScrollSize' ;
	positionOffset= isUpOrDown ? this.clipProperty.t : this.clipProperty.l ;

	if (eval('(currentPos+=step)'+opGreaterLesser+opPosNeg+positionLimit+'+positionOffset'))
		eval('fnSet'+opXY.toUpperCase()+'(this.id,currentPos)');
	else{
		if(this.isBuild) resetPos= -eval(opPosNeg+'clipScrollSize-positionOffset')-1;
		else resetPos= -eval(opPosNeg+(isUpOrLeft ? 'clipScrollSize' : 'realScrollSize')+'-positionOffset')-1;

		eval('fnSet'+opXY.toUpperCase()+'(this.id,'+resetPos+')');
//		alert(this.variableName+' positionLimit:'+positionLimit+' resetPos:'+resetPos+' clipScrollSize:'+clipScrollSize+' realScrollSize:'+realScrollSize+' top:'+this.clipProperty.t+' left:'+this.clipProperty.l);
		currentPos= resetPos;
	}

	if(this.clipProperty.isClip){
		cTp=this.clipProperty.t;
		cLt=this.clipProperty.l;
		cWd=this.clipProperty.w;
		cHt=this.clipProperty.h;
		if(isUpOrDown) fnSetClip(this.id,cTp-currentPos,cWd,cHt-currentPos+cTp,0);
		else fnSetClip(this.id,cTp,cWd-currentPos,cHt,-currentPos);
	}

	this.moveObj=setTimeout(this.variableName+'.fnMove()',(1000/this.framespeed));
}
fnScrollingContent.prototype.fnStop = function () { if(this.moveObj) clearTimeout(eval(this.variableName+'.moveObj')); }


/*
// can use mouse.x and mouse.y to return mouse cursor's absolute position on the page
var mouse = new Object;
if(typeof(Event) != 'undefined') document.captureEvents(Event.MOUSEMOVE);
document.onmousemove= function (mouseObj){ typeof(event) != 'undefined' ? (mouse.x=event.clientX, mouse.y=event.clientY) : (mouse.x=mouseObj.pageX, mouse.y=mouseObj.pageY); }

*/