/* 
DAPI version 3.0
Created by jonas.bohlin@halogen.com
*/
function browser(){
	var sUA=navigator.userAgent.toLowerCase();
	this.compatible = (sUA.indexOf('compatible')!=-1);	
	this.ns  = (!this.compatible && sUA.indexOf('mozilla')!=-1); 	
	this.ge = (this.ns && sUA.indexOf('gecko')!=-1);
	this.geNs = (sUA.indexOf('gecko')!=-1 && sUA.indexOf('netscape')!=-1);	//eg netscape
	this.ie = (sUA.indexOf("msie") != -1);			
	this.opera = (sUA.indexOf("opera") != -1);
	
	this.safari = (sUA.indexOf('safari') != - 1);

	
	iAdd = 0
	if (this.compatible || this.geNs || this.ge){	//use to read netscape 6 true subversion
			if (this.opera){
				reApp = /opera.([0-9\.]+)/
				this.ie = false
			} else if (this.ie) {
				reApp = /msie.([0-9\.]+)/
			} else if (this.geNs){
				reApp = /gecko\/.+\/([0-9\.]+)/
			} else if (this.ge) {
				reApp = /rv\:([0-9\.]+)/
				iAdd = 6 //add to make it look like netscape 6, 7 etc				
			}
		aRes = reApp.exec(sUA)
		this.appVersion = aRes[1]
	} else {
		this.appVersion = navigator.appVersion
	}
	this.major = parseInt(this.appVersion)+iAdd; 
  this.minor = parseFloat(this.appVersion)+iAdd;
  this.ns5 = this.ns6 = (this.ge && this.major==6);
  this.ns5up = this.ns6up = (this.ge && this.major>=6); 
	this.ns7 = (this.ge && this.major==7);
	this.ns7up = (this.ge && this.major>=7);
//check these last...
  this.ns4 = (this.ns && (this.major == 4)); 
  this.ns4up = (this.ns && (this.ns4 || this.ns6 || this.ns6up)); 
  this.nsOnly  = (this.ns && ((sUA.indexOf(";nav") != -1) || (sUA.indexOf("; nav") != -1)));

	this.ie3down = (this.ie && (this.major < 4)); 
  this.ie4  = (this.ie && (this.major == 4)); 
  this.ie4up  = (this.ie && (this.major >= 4)); 
  this.ie5  = (this.ie && (this.major == 5)); 
  this.ie5up  = (this.ie && (this.major >= 5));
	this.ie5_5 = (this.ie && (this.minor == 5.5));	
	this.ie5_5up = (this.ie && this.minor >= 5.5);
  this.ie6  = (this.ie && (this.major == 6));
  this.ie6up  = (this.ie && (this.major >= 6));

	this.op5 = (this.opera && this.major==5) 
	this.op5up = (this.opera && this.major>=5) 	
	this.op6 = (this.opera && this.major==6) 	
	this.op6up = (this.opera && this.major>=6)
	
	this.dom = (this.ie5up || this.ns5up || this.op5up)
	
	this.win   = ((sUA.indexOf("win")!=-1) || (sUA.indexOf("16bit")!=-1));
	this.mac    = (sUA.indexOf("mac")!=-1);
	window.oClient = this
}

function addPop(){ if (oClient.ie4up && typeof(Array.prototype.pop)=="undefined"){	Array.prototype.pop = dapi_Pop }}
function addContains(){ if ((oClient.ns5up || oClient.ns4) && typeof(Object.prototype.contains)=="undefined"){ Object.prototype.contains = dapi_Contains}}

function dapi_Pop(){
	var oRemoved = this[this.length-1]
	this.length = this.length-1
	return oRemoved
}

function dapi_Contains(oElement,oObj){
		if (oClient.ns5up){
			if (!oElement) return false
			if (this==oElement) return true
			oNodeAt = oElement 
			 do {
				oNodeAt = oNodeAt.parentNode
			} while (this!=oNodeAt && oNodeAt.parentNode)
			retVal = (this==oNodeAt)
			return retVal
		} else if (oClient.ns4){		
			if (!oElement) return false
			if (oObj==oElement) return true
			oNodeAt = oElement 
			do {
				oNodeAt = oNodeAt.oParent
			} while (oObj!=oNodeAt && oNodeAt.oParent)
			retVal = (oObj==oNodeAt)
			return retVal			
		}
}

function wrapLayer(sDiv,sDivObj,sNest){
	//Test if sDiv or sDivObj are objects...
	bODiv =  (typeof(sDiv)=="object")
	bObjNest =  (typeof(sNest)=="object")
	if (bODiv) {
		this.layer = sDiv
	} else if (oClient.dom){
		this.layer = document.getElementById(sDiv)		
	} else if (oClient.ie4) {
		this.layer = document.all[sDiv];
	} else if (oClient.ns4) {
		if (sNest){
			this.layer = (bObjNest)?sNest.layer.document.layers[sDiv]:eval(sNest).document[sDiv];
		} else {
			this.layer = document[sDiv];
		}
	}
	if (!this.layer){ alert("Layer "+sDiv+"\nDoes not exist")	}
	if (oClient.ns4){ this.layer.style = this.layer }	//add "style" to netscape.
	this.obj = sDivObj
	var bReadProps = true
	if (oClient.ie4){ if (!this.layer.tagName){ alert("can't read those props"); bReadProps = false } } //control only, shouldn't happen.
	if (oClient.dom && bReadProps){
		this.id = this.layer.id;
		this.w = (!oClient.opera)?this.layer.offsetWidth:this.layer.style.pixelWidth;
		this.h = (!oClient.opera)?this.layer.offsetHeight:this.layer.style.pixelHeight;
    this.xpos = (!oClient.opera)?this.layer.offsetLeft:this.layer.style.pixelLeft;
		this.ypos =  (!oClient.opera)?this.layer.offsetTop:this.layer.style.pixelTop;
		this.z = this.layer.style.zIndex;
	} else if (bReadProps) {
		this.id = (oClient.ns4)?this.layer.id:this.layer.id;
		this.w = (oClient.ns4)?this.layer.clip.width:this.layer.style.pixelWidth; // use offset with IFRAME
		this.h = (oClient.ns4)?this.layer.clip.height:this.layer.style.pixelHeight;
    this.xpos = (oClient.ns4)?this.layer.left:this.layer.style.pixelLeft;
		this.ypos = (oClient.ns4)?this.layer.top:this.layer.style.pixelTop;		
		this.z = this.layer.style.zIndex;
	}
}
{
p = wrapLayer.prototype
p.writeIntoLayer = writeIntoLayer
p.layerVis = layerVis
p.setLayerPixel	= wrapLayer_setPixel // remove
p.setPixel = wrapLayer_setPixel
p.nudge	= setRelativePixel
p.setClip = setLayerClip
p.getClip = wrapLayer_getClip
p.setZ = setLayerZindex
p.addCover = addCover
p.addEvent = wrapLayer_addEvent
p.clearEvent = wrapLayer_clearEvent
p.getOffsetWidth = wrapLayer_getOffsetWidth
p.getOffsetHeight	= wrapLayer_getOffsetHeight
p.getDim = wrapLayer_getDim
p.getOffsetLeft = wrapLayer_getOffsetLeft
p.getOffsetTop = wrapLayer_getOffsetTop		
p.setWidth = wrapLayer_setWidth
p.setHeight = wrapLayer_setHeight
}

function makeLayer(zindex,left,top,width,height,bgcolor,show,content,nestref,sId,bForceClip){
	if (oClient.opera){ return false } //opera can't create DOM elements like this
	sId = sId || getUniqueID("MakeLayer") 	//ID is optional, but preferred.
	if (oClient.ie5up) { // extra safety
		a = document.getElementById(sId)
		if (a) alert("suggested id already exists!\n"+sId+"\n"+a.id)
	}
	if (!content) content = ""
	if (show && show==true) sVis = "visible";
	else if (show) sVis = show;
	else sVis = "hidden"
	if (oClient.ns4){
		if (nestref) oDiv = new Layer(zindex, nestref.layer);
		else oDiv = new Layer(zindex);
		oDiv.left = left;
		if (isNaN(top)) alert(top+","+left+","+width+"\n"+makeLayer.caller+"\n"+oClient.ns4up)
		oDiv.top = top;
		oDiv.clip.width = width;
		oDiv.clip.height = height;
		oDiv.zIndex = zindex;
		if (bgcolor  && bgcolor!="transparent") oDiv.bgColor = bgcolor
		if (content){
			oDiv.document.open("text/html");
			oDiv.document.write(content);
			oDiv.document.close();
		}
		oDiv.visibility = sVis
		return oDiv
	} else {
		if (!content) content = ""
		if (show && show==true) vis = "visible";
		else if (show) vis = show;
		else vis = "hidden"
		if (oClient.ie4 || (oClient.mac && oClient.ie5up)){
			var createStr = '<DIV ID="'+sId+'" STYLE="position: absolute;">'+content+'</DIV>'
			if (nestref) nestref.layer.insertAdjacentHTML("BeforeEnd", createStr) //Changed so that every item is added as lastchild, hence first item added remains first item...
			else document.body.insertAdjacentHTML("BeforeEnd", createStr)
			oDiv = document.all[sId]
		} else {
			var oDiv=document.createElement("DIV");
			if (nestref) nestref.layer.appendChild(oDiv);			
			else document.body.appendChild(oDiv)
			oDiv.id = sId
			oDiv.style.position = "absolute"			
			oDiv.innerHTML = content			
		}
		oDiv.style.visibility = sVis;
		oDiv.style.zIndex = zindex;
		if (bgcolor) oDiv.style.backgroundColor = bgcolor
		oDiv.style.left = left+"px";
		oDiv.style.top = top+"px";
		oDiv.style.width = width+"px";
		oDiv.style.height = height+"px";
		if (!bForceClip) oDiv.style.clip =  "rect(auto auto auto auto)"	
		else oDiv.style.clip =  "rect("+0+"px "+width+"px "+height+"px "+0+"px)"
		return oDiv
	}
}

function getUniqueID(sPrefix){
	sId = sPrefix || ""
	if (oClient.ie4 || (oClient.mac && oClient.ie5up)){
		do { a = (sId + Math.round(Math.random()*99999))} while (document.all[a])
		sId = a
	} else if (oClient.ie5up && !oClient.opera){
		sId += document.uniqueID
	} else if (oClient.ns5up || oClient.opera){
		do { a = (sId + Math.round(Math.random()*99999))} while (document.getElementById(a))
		sId = a
	} 
	return sId
}

function wrapLayer_addEvent(sEvent,oFunction,bUseCapture,bIgnoreCoverNS){
	if (oClient.ns5up){
		this.layer.addEventListener(sEvent.substring(2),oFunction,bUseCapture)
	} else if (oClient.pc && oClient.ie5up){	
		this.layer.attachEvent(sEvent,oFunction)
	} else {
		var addCover = (oClient.ns4 && !bIgnoreCoverNS && this.cover)?".cover":"";
		oSetEventTo = (oClient.ns4)?this.obj+addCover+".layer":this.obj+".layer";
		eval(oSetEventTo+"."+ sEvent +"="+ oFunction)
		if (oClient.ns4){
			sCapture = sEvent.substring(2).toUpperCase()
	 		eval(oSetEventTo+".captureEvents(Event."+sCapture+")")
		}
	}
}

function wrapLayer_clearEvent(nEvent){
	argLen = wrapLayer_clearEvent.arguments.length
	args = wrapLayer_clearEvent.arguments
	igCoverPassed = (typeof(args[argLen-1])=="boolean")?true:false;
	ignoreCover = (igCoverPassed)?args[argLen-1]:false;
	var addCover = (oClient.ns4up && !ignoreCover && this.cover)?".cover":"";
	iRunTo = (igCoverPassed)?argLen-1:argLen;
	for (e = 0;e<iRunTo;e++){
		oSetEventTo = (oClient.ns4)?this.obj+addCover+".layer":this.obj+".layer.access";
		sEvent = args[e]
		eval(oSetEventTo+"."+ sEvent +"= null")
		if (oClient.ns4){
			sCapture = sEvent.substring(2).toUpperCase()
 			eval(oSetEventTo+".releaseEvents(Event."+sCapture+")")
		}
	 }
}

function openWindow(id,url,w,h,x,y,bNormalWindow,nManual,bNoFocus) {
		if (!window.OfficialMap){
			window.OfficialMap = (!window.oWindow)?new mapWindow():window.oWindow;
		}
		var sArgs = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0"
		if ((typeof(w)=="boolean" && w==true)){ //create a replica of the current window
				sArgs = "toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1"
				var	winW = OfficialMap.w
				var	winH = OfficialMap.h
				var	Xwin = OfficialMap.xpos
				var	Ywin = OfficialMap.ypos	
		} else {
			/*create a new window but use default arg-string, with possibility for manual additions (can't disable)*/
			if (bNormalWindow) sArgs = "toolbar=1,location=1,directories=1,status=1,menubar=1,scrollbars=1,resizable=1"	
			var winW = w || 0.5;
			var	winH = h || 0.5;
			var Xwin = x || 0.5;
			var Ywin = y || 0.5;
			if (isFloat(winW))	winW = parseInt(OfficialMap.screenW*winW)
			if (isFloat(winH))	winH = parseInt(OfficialMap.screenH*winH)
			if (isFloat(Xwin)) Xwin = parseInt((OfficialMap.screenW - winW)*Xwin);
			if (isFloat(Ywin)) Ywin = parseInt((OfficialMap.screenH - winH)*Ywin);
		}
		if (nManual){
			aProps = nManual.split(",")
			for (i=0;i<aProps.length;i++){
				aRe = eval("/("+aProps[i]+")=./i")
				if (aRe.test(sArgs)){
					sArgs = sArgs.replace(aRe,"$1=1")
				} else {
					sArgs += ","+aProps[i]+"=1"
		}	}	}		
    if (winW || winH || Xwin || Ywin) sArgs += ',width='+winW+',height='+winH+',left='+Xwin+',top='+Ywin
		this.win = window.open(url,id,sArgs)
		if (!bNoFocus) this.win.focus()
		return this.win
}

function makeImage(src,width,height){	
		if (oClient.ie4up && !oClient.opera){
			this.img = document.createElement("IMG")
			if (width && height){
				this.img.width = width
				this.img.height = height
			}
			this.img.src = src
			this.img.border = 0
			return this.img
		} else {
			if (width && height) this.img = new Image(width, height)
			else this.img = new Image()
			this.img.src = src
			return this.img
		}
}

function wrapImage(sImageName,vDefaultSwap,oNest){
		ns4 = oClient.ns4
		var modX = (oNest)?oNest.xpos:0;
		var modY = (oNest)?oNest.ypos:0;
		if (oNest && ns4) this.image = (isObject(oNest))?oNest.layer.document.images[sImageName]:eval(oNest).document.images[sImageName];
		else this.image = document.images[sImageName]
		this.parentLayer = oNest
		this.imagename 	= sImageName
		this.dSrc		= this.image.src
		this.dActive 	= (vDefaultSwap)?(!isObject(vDefaultSwap))?new makeImage(vDefaultSwap):vDefaultSwap:null;
		this.w 			= (ns4)?this.image.width:this.image.offsetWidth;
		this.h 			= (ns4)?this.image.height:this.image.offsetHeight;
		this.isActive 	= 0
}
{
 p 				= wrapImage.prototype
 p.swap 		= wrapImage_swap
}

function wrapImage_swap(oImg,forceActive){
	oUseImage = oImg || this.dActive
	if (this.isActive || oUseImage){
		if (forceActive || forceActive==0) this.isActive = (forceActive==1)?0:1;
		this.image.src		= (this.isActive)?this.dSrc:oUseImage.src;	
		this.isActive 		= (this.isActive)?0:1;
	}
}

function addCover(){
	var oCoverTemp = new makeLayer(this.z+1,0,0,this.w,this.h,null,"inherit",null,this,this.obj+"cover")
	this.cover	= new wrapLayer(oCoverTemp,this.obj+".cover");
}

function wrapLayer_setWidth(iWidth) {
        if (oClient.ns4) this.layer.clip.right = iWidth
        else this.layer.style.width = iWidth
}

function wrapLayer_setHeight(iHeight) {
        if (oClient.ns4) this.layer.clip.bottom = iHeight
        else this.layer.style.height = iHeight
}

function setLayerClip(clipTop,clipRight,clipBottom,clipLeft){
		clipTop 	= (clipTop!=null)?clipTop:this.getClip(0);
		clipRight 	= (clipRight!=null)?clipRight:this.getClip(1);
		clipBottom 	= (clipBottom!=null)?clipBottom:this.getClip(2);
		clipLeft	= (clipLeft!=null)?clipLeft:this.getClip(3);
	if (oClient.ns4){
		this.layer.clip.top		= clipTop 	
		this.layer.clip.right 	= clipRight	
		this.layer.clip.bottom 	= clipBottom
		this.layer.clip.left 	= clipLeft
	 } else {
		this.layer.style.clip = "rect("+clipTop+"px "+clipRight+"px "+clipBottom+"px "+clipLeft+"px)"
	 }
}

function wrapLayer_getOffsetTop() {
  if (oClient.ns4) rVal = this.layer.top
  else rVal = this.layer.offsetTop
	return rVal
}

function wrapLayer_getOffsetLeft() {
	if (oClient.ns4) rVal = this.layer.left
  else rVal = this.layer.offsetLeft
	return rVal
}

function wrapLayer_getOffsetHeight(bContentHeight) {
	if (oClient.ns4) rVal = (bContentHeight)?this.layer.document.height:this.layer.clip.height;
  else if (oClient.opera) rVal = this.layer.style.pixelHeight
	else rVal = this.layer.offsetHeight
	return rVal
}

function wrapLayer_getDim(vWhich){ //0 || width, 1 || height
	bH = (vWhich == 1 || vWhich == "height")
	if (oClient.ns4) rVal = (bH)?this.layer.document.height:this.layer.document.width;
    else if (oClient.opera) rVal = (bH)?this.layer.style.pixelHeight:this.layer.style.pixelWidth;
	else rVal = (bH)?this.layer.offsetHeight:this.layer.offsetWidth;
	rVal = parseInt(rVal)
	return rVal
}

function wrapLayer_getClip(vWhich,bForceRead) { //nothing = all as array, top || 0 , 1 || right, 2 || bottom, 3 || left, 4 || width, 5 || height
	if (!oClient.ns4 && (!this.clip || bForceRead)){
		sClip = this.layer.style.clip
		aClip = sClip.match(/-?[0-9]+/g)
		if (!aClip){ aClip = [0,this.getDim("width"),this.getDim("height"),0] }
		this.clip = new Object()
		this.clip.top = parseInt(aClip[0])
		this.clip.right = parseInt(aClip[1])
		this.clip.bottom = parseInt(aClip[2])
		this.clip.left = parseInt(aClip[3])
		this.clip.width =this.clip.right - this.clip.left
		this.clip.height =  this.clip.bottom - this.clip.top
	} else if (oClient.ns4 && !this.clip){
		this.clip = this.layer.clip
	}
	oClip = this.clip
	vType = typeof(vWhich)
	if (vType=="string"){
		iRet = this.clip[vWhich]		
	} else {
		aClip = [this.clip.top,this.clip.right,this.clip.bottom,this.clip.left,this.clip.width,this.clip.height]
		if (vType == "undefined") iRet = aClip
		else iRet = aClip[vWhich]
	}
	return iRet
	if (oClient.ns4) aClip = this.layer.clip.height
	else {
		aRe = /([0-9]+)/
		hasClip = aRe.test(this.layer.style.clip)
		if (hasClip){
			aClip = this.layer.style.clip.split("rect(")[1].split(")")[0].split("px")//aRe.exec(this.layer.style.clip)//this.layer.style.clip.replace(aRe,"$1")
			rVal = aClip[2]
		} else {
			rVal = this.getDim(0)
		}
	}
	rVal = parseInt(rVal)
	return rVal
}

function wrapLayer_getOffsetWidth(bContentWidth) {
        if (oClient.ns4) rVal = (bContentWidth)?this.layer.document.width:this.layer.clip.width;
        else rVal = this.layer.offsetWidth
		return rVal
}

function setLayerZindex(z){ this.layer.zIndex = this.z = z }

function setRelativePixel(pLeft,pTop){
	if (oClient.ie4 || oClient.ns4){
		if (pLeft!=null) this.layer.left = this.xpos += pLeft
		if (pTop!=null) this.layer.top = this.ypos += pTop
	}
}

function wrapLayer_setPixel(pLeft,pTop){
	if (pLeft!=null) this.layer.style.left = this.xpos = pLeft
	if (pTop!=null) this.layer.style.top = this.ypos = pTop
}

function layerVis(visible){
	if (visible && visible==true) vis = "visible";
	else if (visible) vis = visible;
	else vis = "hidden";
	this.layer.style.visibility = vis
}

function writeIntoLayer(htmlContent){
	if (oClient.ns4) {
		this.layer.document.open("text/html")
		this.layer.document.write(htmlContent)
		this.layer.document.close()
	} else {
		this.layer.innerHTML = htmlContent
	}
}

function mapWindow() {
		this.w = (oClient.ns || oClient.opera)?window.innerWidth:document.body.clientWidth;
		this.h = (oClient.ns || oClient.opera)?window.innerHeight:document.body.clientHeight;
		this.outerW = (oClient.ns || oClient.opera)?window.outerWidth:document.body.offsetWidth;
		this.outerH = (oClient.ns || oClient.opera)?window.outerHeight:document.body.offsetHeight;
		this.xpos = (oClient.ns || oClient.opera)?window.screenX:window.screenLeft;
		this.ypos =  (oClient.ns || oClient.opera)?window.screenY:window.screenTop;

		this.screenW = screen.availWidth
		this.screenH = screen.availHeight
		this.center_x = this.w*.5
		this.center_y = this.h*.5
		
		this.px_right = this.w - this.center_x
		this.px_left = this.w - this.center_x
		this.px_top = -this.h - this.center_y
		this.px_bottom = this.h - this.center_y
		window.oWindow = this
}

