function move(layer,x,y)  { document.getElementById(layer).style.left=x; document.getElementById(layer).style.top = y; } 

function browserWidth() {
	if (window.innerWidth) return window.innerWidth;
	else if (document.body.clientWidth) return document.body.clientWidth;
	else return 1024;
}

function browserHeight() {

    	if (window.innerHeight) return window.innerHeight;
    	else if (document.body.clientHeight) return document.body.clientHeight;
    	else return 800;
}

function setZ(layer,zNR) { document.getElementById(layer).style.zIndex=zNR; }

function makeDiv(objName,x,y,content,w,h,overfl,parentDiv) {	  
		if (parentDiv==null) parentDiv='body';
		var oDiv = document.createElement ("DIV");
    oDiv.id = objName;
    if (w) oDiv.style.width = w;
		if (h) oDiv.style.height= h;
		if (content) oDiv.innerHTML=content;
		oDiv.style.position = "absolute";
		if (x) oDiv.style.left=x; else oDiv.style.left=-2000;
		if (y) oDiv.style.top=y; else oDiv.style.top=-2000;
		if (overfl) oDiv.style.overflow=overfl; else oDiv.style.overflow="hidden";
    eval('  document.'+parentDiv+'.appendChild (oDiv);  ');
		delete oDiv;
}


var browserW = browserWidth();
var browserW_half = Math.round(browserW/2);
var browserH = browserHeight();
var browserH_half = Math.round(browserH/2);

function cloud(objName) {
	this.name = objName;
	r = Math.random()*100;
	if (r<50) { // utgangspunk plasseres på hoyre side
		r = Math.random() * browserW_half;
		this.x = browserW_half + r;
	} else { // utgangspunk plasseres på venstre side	
		r = Math.random() * browserW_half;
		this.x = r;
	}
	
	this.speed = (Math.random()*2)-1;
	this.y = Math.round(Math.random()*(browserH-200));
	r = Math.random()*100;

	if (r<33) {image = "cloud_small.png";this.w = 100; }
	 else if (r<66) {image = "cloud_medium.png";this.speed=this.speed*2;this.w = 200; }
	  else {image = "cloud_large.png";this.speed=this.speed*3;this.w = 300;}
	
	
	//makeDiv(objName,this.x,this.y,'<img src="'+image+'">',null,null,null,"body");
	makeDiv(objName,this.x,this.y,'<img src="'+image+'">');
	this.drift = drift;
	setZ(this.name,0);
	
}

function drift() {
	this.x+=this.speed;
	move(this.name,this.x+'px',this.y+'px');
	if (this.speed>0 && this.x>browserW) this.x=-this.w;
	 else if (this.x<-this.w-2) this.x=browserWidth()+2;
}


var number_of_clouds = 7;
var cloudsArr = new Array();


function run() {
	for (t=0;t<number_of_clouds;t++) { cloudsArr[t].drift(); }

}



function init() {

	for (t=0;t<number_of_clouds;t++) { 
		cloudsArr[t] = "cloud"+t;
		cloudsArr[t] = new cloud(cloudsArr[t]);
	}
	
	
	
	
	loopID = setInterval("run();",50); 
	
}
init();


