//frame loading, multiple frames, actions inside the frames

var alayer = {
	minRes : 12,
	animationtime : 350,
	contentwidth : 746,

	showload:false,aFrameObj:null,openclose:false,starttime:0,minRestime:5
}



alayer.init = function() {
	with (alayer){
		this.div = document.getElementById('aDiv')	
		this.minRestime = (animationtime/minRes)/3
	}
}

alayer.setClosePosition = function(cLeft,cTop,cWidth,cHeight) {
	if(!alayer.div)alayer.init()
	with(alayer){
		this.closewidth = cWidth
		this.closeheight = cHeight
		this.closetop = cTop
		this.closeleft = cLeft
	}
}

alayer.setOpenPosition = function(oLeft,oTop,oWidth,oHeight) {
	if(!alayer.div)alayer.init()
	with(alayer){
		this.openwidth = oWidth
		this.openheight = oHeight
		this.opentop = oTop
		this.openleft = oLeft
	}
}


alayer.openScroll = function(fo) {
	with (alayer){
		aFrameObj=fo
		var osRes
		if(showload){
			this.div.innerHTML="&nbsp;"
			this.showload=false
			osRes=minRes*2/3
		}else{
			position(closeleft,closetop,closewidth,closeheight)
			osRes=minRes
			this.starttime = new Date()
			resTime = animationtime/minRes
		}
		this.wsteps		= (openwidth-width)/osRes
		this.hsteps		= (openheight-height)/osRes
		this.tsteps		= (opentop-top)/osRes
		this.lsteps		= (openleft-left)/osRes
		this.stepstogo	= osRes
		this.openclose = true
		visible(true)
		this.aniTimer	= window.setTimeout("alayer.scroll()",resTime)
	}
}

alayer.closeScroll = function(fo){ //position to close from
	with (alayer){
		aFrameObj=fo
		this.starttime = new Date()
		position(aFrameObj.left+aFrameObj.midx,aFrameObj.top,aFrameObj.width,aFrameObj.height)
		this.wsteps		= (closewidth-width)/minRes
		this.hsteps		= (closeheight-height)/minRes
		this.tsteps		= (closetop-top)/minRes
		this.lsteps		= (closeleft-left)/minRes
		this.stepstogo	= minRes
		this.openclose = false
		visible(true)
		resTime = animationtime/minRes
		this.aniTimer	= window.setTimeout("alayer.scroll()",resTime)
	}
}

alayer.scroll = function(){
	with (alayer){
		position(left+lsteps, top+tsteps, width+wsteps, height+hsteps)
		stepstogo = --stepstogo
		if(this.aniTimer) window.clearTimeout(this.aniTimer)
		if(stepstogo>0){
			resTime = (animationtime - ((new Date()).getTime() - this.starttime.getTime()))/stepstogo
			if(resTime<minRestime)resTime=minRestime
			if(openclose)resTime=resTime + stepstogo //this is to slow opening at start
			this.aniTimer	= window.setTimeout("alayer.scroll()",resTime)
		}else{
			if(showload){
				this.div.innerHTML="Loading.."
			}else{
				visible(false)
				aFrameObj.scrollDone(openclose)
			}
		}
	}
}

alayer.showLoading = function(){
	with (alayer){
		var slRes = minRes/2
		position(closeleft,closetop,closewidth,closeheight)
		this.wsteps		= ((openwidth-width)/2)/slRes
		this.hsteps		= ((openheight-height)/2)/slRes
		this.tsteps		= ((opentop-top)/2)/slRes
		this.lsteps		= ((openleft-left)/2)/slRes
		this.stepstogo	= slRes
		this.openclose = false
		this.showload=true
		this.starttime = new Date()
		visible(true)
		this.scroll()
	}
}

alayer.position = function(pLeft,pTop,pWidth,pHeight){
	with(alayer){
		this.width		= pWidth //div.offsetWidth
		this.height		= pHeight
		this.top		= pTop //div.offsetTop
		this.left		= pLeft
		div.style.width = width + "px"
		div.style.height = height + "px"
		div.style.top = top + "px"
		div.style.left = (left) + "px"	
		div.style.lineHeight = (height) + "px"
	}
}


alayer.visible = function(show){
	with (alayer){
		div.style.visibility = (show)?"visible":"hidden"
	}
}

alayer.getBrowserMidX = function(){
	with(alayer){
		var mx
		if (window.innerWidth){ //firefox
			mx = (window.innerWidth/2)-(contentwidth/2)-6 //+window.pageXOffset;
		}else if (document.body){ //ie
			mx = (document.body.clientWidth/2)-(contentwidth/2) //+document.body.scrollLeft;
		}else{
			mx = window.innerWidth/2-(contentwidth/2)
		}
		if(mx<0)mx=12
		return mx
	}
}



function frameobj(){
	this.obj = null
	this.documentObj = null
	this.loadedState = "not"
	this.contentVisibleFlag=false
	this.frameId = ""
	this.frameURL
	this.frameState="closed"
	this.closeURL = null
	this.closeRefresh =null
	this.resizeCmd,this.midx;this.left;this.top;this.width;this.height
}

frameobj.prototype.setFrame = function (idname){
	this.frameId = idname
	if(document.all){
		this.obj = document.all(this.frameId)
	}else{
		this.obj = document.getElementById(this.frameId)
	}
	this.documentObj = window.frames[idname]
}

frameobj.prototype.openURL = function (fURL) {
	if(this.loadedState != "loaded" || this.documentObj.location.href.indexOf(fURL)==-1){
		if(this.loadedState == "loaded")this.loadedState=""
		if(this.frameState == "open")this.closeFrame()
		this.frameURL=fURL
		this.documentObj.location.replace(fURL)
	}
}

frameobj.prototype.visible = function (show){
	with(this){
		if(show){
			contentVisibleFlag=true
			obj.style.visibility = "visible"
			eval("onresize = function(){" + resizeCmd +"}")
		}else{
			contentVisibleFlag=false
			obj.style.visibility = "hidden"
			onresize = null
			obj.style.width = "0px"
			obj.style.height = "0px"
		}	
	}
}

frameobj.prototype.position = function (pLeft,pTop,pWidth,pHeight){
	with(this){
		this.left=pLeft;this.top=pTop;this.width=pWidth;this.height=pHeight
		obj.style.width = pWidth+"px"
		obj.style.height = pHeight+"px"
		obj.style.top = pTop+"px"
		obj.style.left = (pLeft+midx)+"px"
	}
}

frameobj.prototype.doDrag = function(x,y) {
	with(this){
		position(left+x,top+y,width,height)
	}
}

frameobj.prototype.frameLoaded = function() {
	if(this.loadedState=="open" && this.documentObj.location.href.indexOf(this.frameURL)!=-1){
		this.loadedState="loaded"
		this.openFrame()
	}else{
		if(this.frameURL==null || this.documentObj.location.href.indexOf(this.frameURL)!=-1)this.loadedState="loaded"
	}
}

frameobj.prototype.setResizeCmd = function(rc){
	this.resizeCmd = rc
}

frameobj.prototype.setClosePosition = function(cLeft,cTop,cWidth,cHeight) {
	this.closewidth = cWidth
	this.closeheight = cHeight
	this.closetop = cTop
	this.closeleft = cLeft
}

frameobj.prototype.setOpenPosition = function(oLeft,oTop,oWidth,oHeight) {
	this.openwidth = oWidth
	this.openheight = oHeight
	this.opentop = oTop
	this.openleft = oLeft
}

frameobj.prototype.openFrame = function() {
	with(this){
		this.midx = alayer.getBrowserMidX()
		alayer.setClosePosition(closeleft+midx,closetop,closewidth,closeheight)
		alayer.setOpenPosition(openleft+midx,opentop,openwidth,openheight)
		if(loadedState=="loaded"){
			this.frameState = "open"
			alayer.openScroll(this)
		}else{
			alayer.showLoading()
			this.loadedState="open"
		}
	}
}

frameobj.prototype.closeFrame = function(val) {
	with(this){
		if(frameState == "open"){
			visible(false)
			closeURL = (val && val!=true && val.indexOf("html")!=-1 )? val:null
			closeRefresh = (val && val==true)? val:null
			//alayer.setClosePosition(closeleft+midx,closetop,closewidth,closeheight)
			//alayer.setOpenPosition(openleft+midx,opentop,openwidth,openheight)
			alayer.closeScroll(this)
		}
	}
}

frameobj.prototype.scrollDone = function(openclose) {
	with(this){
		if(openclose){
			position(openleft,opentop,openwidth,openheight)
			visible(true)
		}else{
			this.frameState = "closed"
			if(closeURL){
				location=closeURL
				return
			}
			if(closeRefresh && closeRefresh==true) {
				try {
					location=refreshURL
					return
				} catch(er) {
					alert("refreshURL not set");	
				}
			}
			try{
				framemain.openpart2()
			}catch(er){}
		}
	}
}


frameobj.prototype.browserResize = function () {
	with(this){
		this.midx = alayer.getBrowserMidX()
		position(left, top, width, height)
	}
}




var framemain = {
	openeval:"",frameobjs:null
}
framemain.setFrameobjs = function (obj){
	with(this){
		if(frameobjs == null)this.frameobjs = new Array()
		frameobjs[frameobjs.length] = obj
	}
}
framemain.closewin = function(){
	with(this){
		var l=false
		for(n in frameobjs){
			if(frameobjs[n].pframe && frameobjs[n].pframe.frameState=="open"){
				l=true
				frameobjs[n].closeAndUpdate()
			}
		}
		if(!l)openpart2()
	}
}
framemain.openpart1 = function(ope){
	framemain.openeval = ope
	framemain.closewin()
}
framemain.openpart2 = function(){
	with(framemain){
		if(openeval!=null)openeval.pframe.openFrame()
		openeval=null
	}
}