//(c)Alexander Barnard 2006/7/8/9
var	__loaded=false
var d=document
var _onLoad=new clsEvents()
var _cache=new clsCache()
var _childWindow=new clsChildWindow
var _DnD=new Object;_DnD.o=0;
var _error=0

var POS_TOGGLE=0
var POS_HIDE=1
var POS_SHOW=2
var POS_COVER=4
var POS_NO_MOUSE_OFFSET=8
var POS_LOAD=16
var POS_CENTRE=32

d._FORM=1
d._FORMcheck=0
d._hasPOPUP=0
/* GLOBAL FUNCTIONS*/
function _getObject($id,$w){
	if(typeof($id)=="object")return $id
	$w=(!$w)?d:$w.document
	if(d.getElementById)
		return $w.getElementById($id)
	else if(d.all)
		return $w.all[$id]
	else if(d.layers)
		return $w.layers[$id]
	return false
}
function _getOffset(me,p){
	var o=me,x=0
	while(o){x=x+o['offset'+p];o=o.offsetParent}
	return x
}		
function _msg(txt,cell){
	var o=_getObject("statusbar")
	if(o){
		o=o.getElementsByTagName("LI")
		if(cell<o.length)o[cell].innerHTML=txt
	}else 
		window.status=txt
	return true
}	
function _keyCode(e){
	return window.event ? event.keyCode : e.which
}
function _hasChanged(o){
	if(!o)return false
//	var o=it,idx=-1
//	if(it.type=="radio"){var a=it.form[it.name];for(var i=0;i<a.length;i++){o=a[i];if(o.checked)break}}
	function _op(){var b=true;for(var i=0;i<o.options.length;i++)if(o.options[i].defaultSelected){b=(i==o.selectedIndex);break}return b}
	return o.value==undefined?0:o.options!=undefined?!_op():/radio|checkbox/i.test(o.type)?o.defaultChecked!=o.checked:!o.value.like(o.defaultValue)
}
function _getStyle($o,$prop,$state){
	if($o.currentStyle)
		return $o.currentStyle[$prop]
	else if(document.defaultView){
		if(document.defaultView.getComputedStyle)
			return d.defaultView.getComputedStyle($o,'')[$prop]
	}
	return $o.style[$prop]
	var s=''
	if ($o.currentStyle)
		s=$o.currentStyle[$prop];
	else if(window.getComputedStyle){
		var o=$o
		if(!$state)$state=''
		while(o.tagName!="HTML"){o=o.parentNode}
		s=o.parentNode.defaultView.getComputedStyle($o, $state)
		if(s)s=s[$prop];else s=''//.getPropertyValue($prop)
	}else
		s=$o.style[$prop]
	return s
}
function _getStyleRule(cssTxt,oD){
	if(oD==undefined)oD=document
	var rExp=new RegExp('^'+cssTxt+'$','i')
	var s=oD.styleSheets
	for (var i=0;i<s.length;i++){
		var r=(s[i].cssRules)?s[i].cssRules:(s[i].rules)?s[i].rules:0
		for(var j=0;j<r.length;j++){
			if(rExp.test(r[j].selectorText))return r[j]
		}
	} 
	return null
}
function __Cookie(pCookie){
	var val=""
	var cookies=" "+document.cookie+";"
	var p=cookies.indexOf(" "+pCookie)+pCookie.length+1
	if(p>pCookie.length){
		val=unescape(cookies.substring(p,cookies.indexOf(";",p)))
	}	
	return val
}
function _setCookie(pCookie,pVer){
	var NAME=pCookie+"="
	if(__Cookie(NAME)!=pVer){
		document.cookie=NAME+escape(pVer)+"; path=/"
		return true
	}
	return false
}	
function _getCookie(pCookie){
	return __Cookie(pCookie+"=")
}
function _formFocus(it,top){
	if(!it)return
	if(typeof(it)=="string")it=d.forms[it]
	if(!it)return
	function _t(){var y=top?0:it._top;if(y!=undefined)setTimeout(function(){window.scrollTo(0,y)},0)}
	for(var i=0;i<it.elements.length;i++){
		var o=0
		var f=it.elements[i]
		if (f.id)
			o=_getObject(f.id)
		else if(_setFocus(f,1)){_t();return}
		if(o){
			var oo=o
			for(;;){
				if((oo=oo.offsetParent)==null){
					if(_setFocus(f,1)){_t();return}
					break
				}			
				if((/xdiv/ig).test(oo.id)){
					if(oo._open||o.offsetParent.offsetTop<oo._divHeight){
						if(_setFocus(f,1)){_t();return}
					}else break
				}
			}
		}
	}
}
function _setFocus(it,ns){
	if(it.type)
		if(it.type!="hidden")
			if(!it.readOnly)
				if(!it.disabled)
					if(_getStyle(it,"display")!="none"){
						if(it.editorIndex!=undefined){
							_cursPos(it.editorIndex)
							return true
						}
						var o=it
						while(o.tagName!="BODY"){
							o=o.parentNode
							if(_getStyle(o,"display")=="none")return false
						}
						it.focus()
						if("textareapassword".indexOf(it.type)>-1)
							if(!ns)it.select()
						return true
					}
	return false
}
function _formClear(it){
	if(!it)return false
	for(var i=0;i<it.elements.length;i++){
		var o=it.elements[i]
		if("textselect-one".indexOf(o.type)>=0){
			if(o.selectedIndex)o.selectedIndex=0;else o.value=""
			if(o.onchange)o.onchange()
		}
	}
	_formFocus(it)
	return false
}
function _formReset(it){
	if(!it)return false
	it.reset()
	_formFocus(it)
	return false
}
function _position(o,flg,toIt){
	//flg !/0=toggle, 1=hide, 2=show,4=pos over toIt,8=don't add on mouse offset
	var space=20
	var x=0,y=0
	if(toIt==undefined)toIt=0
	o=_getObject(o)
	if(!o)return 
	if(flg==undefined)flg=0
	var change=((flg&POS_SHOW)>0)?true:((flg&POS_HIDE)>0)?false:!o._state
	if(change==o._state)return 
//alert("was :"+o._state+" flg="+flg+" hide="+((flg&POS_HIDE)>0)+" show="+((flg&POS_SHOW)>0)+" tog="+!o._state)	
	o._state=change
//alert("is :"+o._state)
	if(o._state){
		o.style.position="absolute"
		o.style.display="block"
		var oc=o.childNodes
		oc=(oc.length>1)?oc[0+(oc[0].nodeType==3)]:o
		if(toIt==0){
			x=_window('Width')/2-oc.offsetWidth/2-x
			y=_window('Height')/2-oc.offsetHeight/2+_pageScroll('Y')-y
		}else{	
			o.style.left="0px";o.style.top="0px"
			x=_getOffset(toIt,'Left')-_getOffset(o,"Left")-1
			y=_getOffset(toIt,'Top')-_getOffset(o,"Top")-(/popup/i.test(o.className)?3:0)+((flg&POS_COVER)?-2:(toIt.offsetHeight+((flg&POS_NO_MOUSE_OFFSET)?2:4)))
		}
		if(_getOffset(toIt,'Left')+oc.offsetWidth+space>_window('Width')){
			x=_window('Width')-_getOffset(toIt,'Left')
			o.style.left=(_window('Width')-oc.offsetWidth-x+space*2)+"px"		
		}	
		else
			o.style.left=x+"px"	
		o.style.top=((!toIt||(flg&POS_NO_MOUSE_OFFSET||flg&POS_COVER)>0)?1:8)+y+"px"
		var bh=o.offsetTop+oc.offsetHeight+space/2
		var wh=_pageScroll('Y')+_window('Height')
		if(bh>wh&&toIt!=0)o.style.top=(y-oc.offsetHeight-toIt.offsetHeight-((flg&POS_COVER)?0:toIt.offsetHeight+2))+"px"
	}else{
		o.style.display="none"		
	}
}		
function _pageScroll(os){
	var p=(os=="X")?"Left":"Top"
	return (d.body && d.body['scroll'+p])?d.body['scroll'+p]:d.documentElement && d.documentElement['scroll'+p] ? d.documentElement['scroll'+p]:(typeof(window['page'+os+'Offset']) == 'number') ? window['page'+os+'Offset']:0;
}
function _window(p){
	return (d.documentElement&&d.documentElement['client'+p])?d.documentElement['client'+p]:(d.body && d.body['client'+p])?d.body['client'+p]:(typeof(window['inner'+p]) == 'number')?window['inner'+p]:0;
}
function _mouseCursor(pType,doc){
	if(!doc)doc=d
	if(!pType)pType="default"
	doc.body.style.cursor=pType
}
//
//
//
//EVENTS
function clsEvents(me){
	var fn
	this._active=true
	this.parent=(!me)?document:me
	this._fn=new Array()
	this.add=function(){var a=this.add.arguments;for(var i=0;i<a.length;i++)this._fn[this._fn.length]=a[i]}
	this.swap=function(){var a=this.swap.arguments;for(var i=0;i<a.length;i+=2)for(var j=0;j<this._fn.length;j++)if(this._fn[j]==a[i]){this._fn[j]==a[i+1];if(!a[i+1]){this._fn.splice(j,1);j--}}}
	this.go=function(e){
		if(!this._active)return false
		var ret=0
		for(var i=0;i<this._fn.length;i++){
			if(typeof(this._fn[i])=="string"){
				var fn=this._fn[i].replace(/\bthis(?=\W)/g,'this.parent')
				//var o=_getObject("test")
				//if(o)o.innerHTML=o.innerHTML+"["+fn+"]"
				if(/return /.test(fn)){
					fn=fn.replace("return","")
					if(!ret)ret=fn;else eval(fn)
				}else	
					eval(fn)
			}else{
//alert(this._fn[i].toString())
				this._fn[i]()
			}	
		}
//if(ret){alert("RET:"+ret);ret=eval(ret);alert(ret);return ret}
		if(ret)return eval(ret)

		return true
	}	
}
function _fireEvent(e){
	if(!e)e=event
	if(e!=undefined){
		var o=this['_'+e.type]
		if(o)return o.go(e)
	}	
	//NOTE:passing e in _fn[i] = current event
}
function _addEvent(me,pM,pF){
	if(me[pM]==undefined){
		me[pM]=new clsEvents(me)
		var f=me['on'+pM.substr(1)]
		if(f){
			f=f.toString()
			me[pM].add(f.substring(f.indexOf("{")+1,f.lastIndexOf("}")-1))
		}	
	}
	me[pM].add(pF)
	me['on'+pM.substr(1)]=_fireEvent
}

//CLASSES
//
function clsChildWindow(){
	this.win=new Object	
	this.open=function(url,n,w,h,p,force){
		p=(p)?p:""
		p+=((w)?",width="+w:"")
		p+=((h)?",height="+h:"")
		var o=this.win[n]
		if(o&&force)o.close()
		var ok=(o)?o.closed:1
		if(ok)
			this.win[n]=window.open(url,n,p)
		else
			o.location.replace(url)
		if(this.win[n])this.win[n].focus()
	}	
}
//
function clsCache(){
	this.load=function(it){
		if(!it)it=d
		var o=(d.getElementsByTagName)?it.getElementsByTagName("IMG"):d.images
		for(var i=0;i<o.length;i++)
				this.add(o[i])
		o=(d.getElementsByTagName)?it.getElementsByTagName("INPUT"):0
		for(var i=0;i<o.length;i++)
			if(o[i].src)
				this.add(o[i])
	}
	this.swap=function(me,state){
	me.src=(state==0)?me._off:(state==1)?((me._on.complete&&me._on.width>0)?me._on.src:me._off):(me._click.complete&&me._click.width>0)?me._click.src:me.src}
	this.change=function(me,from,to){var r=new RegExp(from,"ig");me._on.src=me._on.src.replace(r,to);me._off=me._off.replace(r,to)}
	this.add=function(me){
		if(me._on!=undefined)return 0
		if(!/(btn[^:]*0)(.[^\d]{3})$/.test(me.src))return 0
		me._off=me.src
		me._on=new Image();me._on.src=me.src.replace(/(0)(.[^\d]{3})$/,function(a,b,c){return 1+c})
		me._click=new Image();me._click.src=me.src.replace(/(0)(.[^\d]{3})$/,function(a,b,c){return 2+c})
//alert(me.src+"\n:me"+me.complete+" on:"+me._on.complete+" clk:"+me._click.complete)
		_addEvent(me,'_mouseover','_cache.swap(this,1)')
		_addEvent(me,'_mouseout','_cache.swap(this,0)')
		_addEvent(me,'_mousedown','_cache.swap(this,2)')
		_addEvent(me,'_mouseup','_cache.swap(this,1)')
		var o=(me.tagName=="INPUT")?me:(me.parentNode.tagName=="A")?me.parentNode:0
		if(o)	
			if(o.title){
				_addEvent(o,"_mouseover","return _msg('Button : "+o.title+"',1)")
				_addEvent(o,"_mouseout","_msg('',1)")
				_addEvent(o,"_mouseup","_msg('Button : "+o.title+" - {clicked}',1)")
			}
	}
}

//prototypes
String.prototype.like=function($o){
	var r=new RegExp("^"+this.replace(/[\$\|\^\+\*\(\)\[\]\\?]/g,function(a){return '\\'+a})+"$")
	return r.test($o)
}	


//LOAD
function onLoad(){
	__loaded=true
	_cache.load()
	_onLoad.go()
}
window.onload=onLoad

