/*
Engine script 0.01
By Ola Persson
Copyright Ktectwo Ab 2008
*/

var posList = [];
var animList = [];
var sliceWidth = 10;
var slices = 225 / sliceWidth;
var animFrames = 150;
var frameSpeed = 10;
var section = 0;
var animation = null;
var busy = 0;


var ktec = {
	init : function(){
		this.placeSymbols();
		for (var i=0;i<slices;i++){
			/*var top=Math.random();*/
			this.addPos();
			this.addSlice(i,top);
		}
		//console.log(posList[1]);
	},
	placeSymbols : function(){
		for (var i=0;i<4;i++){
			this.placeSymbol(i+1);
		}
	},
	placeSymbol : function(symbolNumber){
		var tempX = -200 + (400*Math.random());
		var tempY = -200 + (400*Math.random());
		$('symbol'+(symbolNumber)).style.marginLeft = tempX+"px";
		$('symbol'+(symbolNumber)).style.marginTop = tempY+"px";
	},
	addPos : function(){
		var tempList = [];
		var tempValue = 0;
		for (var i=0;i<4;i++){
			tempValue = parseInt(tempValue+10+((Math.random()*50)+(50*i)));
			tempList.push(tempValue);
		};
		
		tempList = this.mixList(tempList);
		tempList.push(parseInt(tempValue+60+Math.random()*50));
		posList.push(tempList);
	},
	mixList : function(tempList){
		
		var tempLength = tempList.length; 

		for (var i=0; i<tempLength;i++){
			var tempNumber = tempList.shift();
			tempList.splice(parseInt(Math.random()*tempLength),0,tempNumber);
		};
		
		//console.log(tempList[4]);
		return tempList;
	},
	addSlice : function(number){
			Element.insert($('container'), ''+
	'<div class="slice" id="slice'+number+'" style="left:'+(sliceWidth*number)+'px;top:'+(-posList[number][section])+'px;height:'+posList[number][4]+'px;width:'+sliceWidth+'px;">'+
		'<div class="company" style="background-position:'+(-sliceWidth*number)+'px 0px;top:'+posList[number][0]+'px;"></div>'+
		'<div class="visit" style="background-position:'+(-sliceWidth*number)+'px -42px;top:'+posList[number][1]+'px;"></div>'+
		'<a href="mailto:ola@kongotec.com?subject=~~Ktec Mail~~">'+
		'<div class="email" style="background-position:'+(-sliceWidth*number)+'px -102px;top:'+posList[number][2]+'px;" _click="1"></div>'+
		'</a>'+
		'<div class="phone" style="background-position:'+(-sliceWidth*number)+'px -143px;top:'+posList[number][3]+'px;"></div>'+
	'</div>'); 	
	},
	drawSlices : function(tempPosList){
		
		for (var i=0;i<slices;i++){
			$('slice'+i).style.top = -tempPosList[i]+'px';
		}
	},
	anim : function(tempTo){
		animList = this.makeAnim(tempTo);
		this.playAnim(animList, tempTo);
	},
	makeAnim : function(tempTo){
		tempFrom = section;
		var tempAnimList = [];
		var tempIncrease = 1 / animFrames;
		for (var i=0;i<animFrames;i++){
			var tempList = [];
			for (var j=0;j<slices;j++){
				var tempStartPos = posList[j][tempFrom];
				var tempDistance = posList[j][tempTo] - posList[j][tempFrom];
				var tempFrameSlicePos = ease.InOut(tempStartPos,tempDistance,i,animFrames);
				tempList.push(tempFrameSlicePos);
			}
			tempAnimList.push(tempList);
		}
		return tempAnimList;
	},
	playAnim : function(animList,tempTo){
		
		if (animation){
			window.clearInterval(animation);
		}
		
		var tempFrame = 0;
		
		animation = window.setInterval(
		function(){
			
			ktec.drawSlices(animList[tempFrame]);	
			
			tempFrame++;

			if (tempFrame > animFrames-1) {
				window.clearInterval(animation);
				ktec.animDone(tempTo);
			}

		}
		,1);
	},
	animDone : function(tempTo){
		section = tempTo;
		busy = 0;
		
		var tempList = [];
		for (var i=0;i<posList.length;i++){
			tempList.push(posList[i][section]);
		}

		ktec.drawSlices(tempList);	

	}
};

var ease = {
	Lin: function(fromV,distV,frame,frames){
		return distV*frame/frames + fromV;
	},
	In: function(fromV,distV,frame,frames){
		frame /= frames;
    	return distV*frame*frame + fromV;
	},
	Out: function(fromV,distV,frame,frames){
		frame /= frames;
    	return -distV * frame*(frame-2) + fromV;
	},
	InOut: function(fromV,distV,frame,frames){
		frame /= frames/2;
		if (frame < 1) return distV/2*frame*frame + fromV;
		frame--;
		return -distV/2 * (frame*(frame-2) - 1) + fromV;
	}
};

var score=0;

document.observe('mousemove', function(event){

	var tempSymbolNumber = parseInt(event.target.getAttribute('_symbol'));
	
	if (tempSymbolNumber){

		ktec.placeSymbol(tempSymbolNumber);
		score++;
		if (score==200){
			alert("Almost hypnotic, isn't It?");
		}
		if (score==400){
			alert("You must be kidding!");
		}
		if (score==1000){
			alert("Ehh?");
		}

		if (section != tempSymbolNumber-1 && busy == 0){
			ktec.anim(tempSymbolNumber-1);
			busy = 1;
		}

	}
});

function clickety(tempSymbolNumber){

	
	if (tempSymbolNumber){

		ktec.placeSymbol(tempSymbolNumber);
		score++;
		if (score==200){
			alert("Almost hypnotic, isn't It?");
		}
		if (score==400){
			alert("You must be kidding!");
		}
		if (score==1000){
			alert("Ehh?");
		}

		if (section != tempSymbolNumber-1 && busy == 0){
			ktec.anim(tempSymbolNumber-1);
			busy = 1;
		}

	}
}


ktec.init();


