var netscape = ((navigator.appName.indexOf("Netscape") !=-1)&&(navigator.appVersion.indexOf("5.0") !=-1));
var microsoft = (navigator.appName.indexOf("Microsoft") !=-1);
mousePos=new Position(0,0)
// function xxxxxxxxxxxxxxxx
function mouseCatch(e){
	if(microsoft){
		mousePos = new Position(event.clientX, event.clientY)
	}
	if(netscape){
		mousePos = new Position(e.pageX, e.pageY)
	}
	//window.status="mousepositionX= "+ mousePos.x+ " mousepsoitionY= "+mousePos.y
}
function initMouseCatch(){
	if(netscape){
		document.captureEvents(Event.MOUSEMOVE);
	}	
	document.onmousemove= mouseCatch;
}		
//xxxxxxxxxxxxxxxxxxxxxxxxxx
function resetMovingButton(){
	document.getElementById("buttonHolder").innerHTML=""
	document.onmousemove =mouseCatch
	
}	
//xxxxxxxxxxxxxxxxxxxxxxxxxx
function initMovingButton(){
	document.getElementById("buttonHolder").style.left=(docWidth-150)+"px"
	document.getElementById("buttonHolder").style.top=150+"px"
	newPos()
	document.getElementById("buttonHolder").innerHTML="\<button\>  Vinn gratis\<br\>Clownf&ouml;rest&auml;llning!\<br\>   KLICKA H&Auml;R!\<\/button\>"
	document.onmousemove = mouseAndNewPos
}
//function xxxxxxxxxxxxxxxxxxxxxxxxxxx
function mouseAndNewPos(e){
	mouseCatch(e)
	newPos(e)
}	
function newPos(e) {
	X=mousePos.x
	Y=mousePos.y
	Xo = parseInt(document.getElementById("buttonHolder").style.left);
	Yo = parseInt(document.getElementById("buttonHolder").style.top);
	//alert("X= " +X +"  Y= "+ Y + "  X0= "+ Xo + "  Yo= "+ Yo);
	if( (Xo-40< X) && (X < Xo +220)  && (Yo-40 < Y) && (Y < Yo + 190) ){
		document.getElementById("buttonHolder").style.left = docWidth-400+Math.floor(Math.random()*300)+"px";
		document.getElementById("buttonHolder").style.top =  Math.floor(Math.random()*300)+"px";
		newPos(e)
	}
}

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx   
function setImage(index){
document.getElementById("imageHolder").innerHTML="\<img src='"+image[index]+"'\>"
//alert("hello"+index+"\<img src='"+image[index]+"\>")
setCookie("imageIndex", index)
}
//objectsXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXxx
function Position(x,y){
this.x=x
this.y=y
}
//xxxxxxxxxxxxxxxxxx
function Citat(text, navn){
this.text=text
this.navn=navn
}
//********************
function initBases(){
	basePos[0]=new Position(-imageWidth,20)
	basePos[1]=new Position(docWidth/2+textWidth/2 , 20)
	basePos[2]=new Position(docWidth-tentWidth-imageWidth, 20)
	basePos[3]=new Position(docWidth-tentWidth-imageWidth, 140)
	basePos[4]=new Position(docWidth-tentWidth,140)
	basePos[5]=basePos[3]
	basePos[6]=basePos[2]
	basePos[7]=new Position(docWidth/2-textWidth/2-imageWidth+50, 20)
	basePos[8]=new Position(-imageWidth-textWidth, 20)
}
//xxxxxxxxxxxxxxxxxxxxxxxx
function initDist(){
	for (var i=0; i<8; i++){
		deltaX=basePos[i].x-basePos[i+1].x
		deltaY=basePos[i].y-basePos[i+1].y
		distances[i]=Math.sqrt(deltaX*deltaX + deltaY*deltaY)
	}
	distances[8]=1
}
//xxxxxxxxxxxxxxxxxxxxxxxxx
function setText(index){
document.getElementById("textHolder").firstChild.innerHTML=text[index].text
document.getElementById("textHolder").childNodes[1].innerHTML=text[index].navn
textIndex=index
setCookie("textIndex", index)
fadetext(-2)
}
function fadetext(delta){
	if(hex>=0 && hex<=255) { //If color is not black yet
		hex+=delta; // increase color darkness
		//document.getElementById("textHolder").style.color="rgb("+hex+","+hex+","+hex+")";
		document.getElementById("textHolder").style.color="rgb("+hex+", "+hex+", "+hex+" )";
		document.getElementById("citatText").style.color="rgb("+hex+", "+hex+", "+hex+" )";
		setTimeout("fadetext("+delta+")",40); 
		}
	else if(hex<0){
		hex=0
		}
	else if(hex>255){
		hex=255
		changeText()
		}	
}
//*************************
function changeText(){
newIndex=(textIndex+1)%6
setText(newIndex)
}

function imageSlide(imageIndex, positionIndex){
 moveCount=1
timer=setTimeout("imageMove("+imageIndex+","+ positionIndex+")" , 20) 
}
//****************************
function imageMove(imIndex, posIndex){
	if(posIndex<8){
		xPos=basePos[posIndex].x+moveCount/distances[posIndex]*(basePos[posIndex+1].x-basePos[posIndex].x)
		yPos=basePos[posIndex].y+moveCount/distances[posIndex]*(basePos[posIndex+1].y-basePos[posIndex].y)
		
		document.getElementById("imageHolder").style.top=yPos+"px"
		document.getElementById("imageHolder").style.left=xPos+"px"
		
/*		if(posIndex<1 ) {
			document.getElementById("textHolder").style.top=25+"px"
			document.getElementById("textHolder").style.left=(xPos-textWidth)+"px"
		}
		if(posIndex>6){
			document.getElementById("textHolder").style.top=25+"px"
			document.getElementById("textHolder").style.left=(xPos+imageWidth-50)+"px"
		}
*/		
		moveCount =moveCount+1
		if(moveCount<distances[posIndex]){
			timer=setTimeout("imageMove("+imIndex+","+posIndex+")" , 40)
		}
		else if(posIndex==3){
			imIndex ++
			//alert("imIndex= "+imIndex)
			setImage(imIndex)
			initMovingButton()
			timer=setTimeout("imageSlide("+imIndex+",4)", 7000)
			timer2=setTimeout("resetMovingButton()",  7000)
			timer3=setTimeout("fadetext(4)",  7000)
			setCookie("posIndex", 4)
		}
		else{
			timer=setTimeout("imageSlide("+imIndex+","+(posIndex+1)+")", 40)
			setCookie("posIndex", (posIndex+1))
		}
	}
	else{
		imIndex=(imIndex+1)%4
		setImage(imIndex)
		fadetext(4)
		timer=setTimeout("imageSlide("+imIndex+", 0)", 10)
		setCookie("posIndex", "0")
	}
}
//**************************************
function initPage(){
	if(microsoft){
			mouseCatch()
			/*document.getElementById("movieButton").innerHTML="\<button	onClick=\"\
			window.open('\/kropssprog\/index.html','mywindow','width=400,height=380')\"\>\
         Klik her for en kropssprogs-demo!\<\/button\>"*/
         	}	
	 
	if(microsoft || netscape){
		initMouseCatch()
		document.getElementById("update").innerHTML=getUpdatedString()
		posIndex=getNumberFromCookie("posIndex")
		textIndex=getNumberFromCookie("textIndex")
		imageIndex=getNumberFromCookie("imageIndex")
		moveCount=getNumberFromCookie("moveCount")
		myPosition=new Position(basePos[posIndex].x, basePos[posIndex].y)
		document.getElementById("textHolder").style.top=25+"px"
		document.getElementById("textHolder").style.left=(docWidth/2-textWidth/2)+"px"
		setImage(imageIndex)
		setText(textIndex)
		imageMove(imageIndex, posIndex)
		
	}
}
function twoFigures(n){
	if(n>=10) return n
	return "0"+n
}
function getNumberFromCookie(name){
str=getCookie(name)
if(str!=null){
	return parseInt(str)
	}
return 0
}
function getUpdatedString(){
updatedString="Sidan &auml;r uppdaterad: "
var lastMod = document.lastModified
var date = new Date(lastMod)
updatedString += date.getFullYear()+"-"+twoFigures((date.getMonth()+1))+"-"+twoFigures(date.getDate())	
return updatedString
} 
function saveCookies(){
	if(microsoft || netscape){
		setCookie("moveCount", moveCount)
	}
}
function setCookie(szName, szValue, szExpires, szPath, szDomain, bSecure)
{
 	var szCookieText = 	   escape(szName) + '=' + escape(szValue);
	szCookieText +=	 	   (szExpires ? '; EXPIRES=' + szExpires.toGMTString() : '');
	szCookieText += 	   (szPath ? '; PATH=' + szPath : '');
	szCookieText += 	   (szDomain ? '; DOMAIN=' + szDomain : '');
	szCookieText += 	   (bSecure ? '; SECURE' : '');
	
	document.cookie = szCookieText;
}

//******************************************************************************************
// This functions reads & returns the cookie value of the specified cookie (by cookie name) 
//
// Prototype : getCookie(szName)
//******************************************************************************************

function getCookie(szName)
{
 	var szValue =	  null;
	if(document.cookie)	   //only if exists
	{
       	var arr = 		  document.cookie.split((escape(szName) + '=')); 
       	if(2 <= arr.length)
       	{
           	var arr2 = 	   arr[1].split(';');
       		szValue  = 	   unescape(arr2[0]);
       	}
	}
	return szValue;
}