/*
	Dynamic Logo for CIID / ciid.dk
	shiftcontrol studios copenhagen, 2008 / shiftcontrol.dk 
*/
var mouseX = 0;
var mouseY = 0;
var deltaX = 0.0;
var deltaY = 0.0;
var lastX = 0;
var lastY = 0;
var dist = 0.0;
var angl = 0.0;
var resL = [0.9,0.95];
var fkL = [0.02,0.01];
var sLine;
var ctx;
var offx = 0;
var offy = 0;
var fps = 20;
var doinit = true;
var mask;
var lineCount = 2;

var Logo = {
	
	start: function( _lineCount){
		lineCount = _lineCount;		
		window.setInterval(Logo.timing, 1000.0/fps);
		mask = $('logo_mask');

		// Setup Canvas
		canvas = $('logo_canvas');
		ctx = canvas.getContext("2d");
		ctx.globalAlpha = 1;//0.2;
		ctx.lineWidth = 1;
		ctx.strokeStyle = "rgba(96,199,255,0.2)";
		
		sx = mouseX = lastX = Math.random()*600;
		sy = mouseY = lastY = Math.random()*600;
		
		canvas.addEvent("click", function(){
			document.location.reload();
		});

		// Create
		sLine = new Array();
		for(ln=0;ln<lineCount;ln++){
			var pct = ln/(lineCount-1.0);
	     	var fKN = fkL[0]*pct + fkL[1]*(1-pct);
	     	var resN = resL[0]*pct + resL[1]*(1-pct);
			sLine.push( new SmoothLine( {"x":sx, "y":sy, "fk":fKN, "res":resN, "ctx":ctx} ) );
			//sLine[ln].Init(mouseX,mouseY);
	   	}
		Logo.resizeMask();

	},
	
	draw: function(){
		if(doinit){ //move to start?
			
			angl = 0;
			dist = 0;
			lastX = mouseX;
			lastY = mouseY;
			deltaX = 0;
			deltaY = 0;
			for(ln=0;ln<lineCount;ln++){
				sLine[ln].Init(mouseX,mouseY);
			}
			doinit = false;
			
		} else {

			var cosAngl = Math.cos(angl);
			var sinAngl = Math.sin(angl);
			//var l = 2 + dist;
			var l = 1 + dist*0.8;
			var xMax = mouseX-cosAngl*l;
		  	var yMax = mouseY+sinAngl*l;
		  	var xMin = mouseX+cosAngl*l;
   			var yMin = mouseY-sinAngl*l;
   		
			for(ln=0;ln<lineCount;ln++){
				//sLine[ln].Update(mouseX, mouseY);
				var t = ln/(lineCount-1.0);
    	 		sLine[ln].Update((xMax*t+xMin*(1-t)),(yMax*t+yMin*(1-t)));
	
			}
		}
	},
	
	resizeMask: function(){
		var ds = window.getCoordinates();
		var h = ds.height + window.getScrollTop();
		mask.setStyles({"width": ds.width-2, "height":h-2});
	},
	
	getMousePos: function(e) {
		if (!e) var e = window.event;
		if (e.pageX || e.pageY) 	{
			mouseX = e.pageX;
			mouseY = e.pageY;
		}
		else if (e.clientX || e.clientY) 	{
			mouseX = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			mouseY = e.clientY + document.body.scrollTop  + document.documentElement.scrollTop;
		}
	},
	
	timing: function(){
		deltaX = mouseX-lastX;
		deltaY = mouseY-lastY;
		dist = dist*0.6 + Math.sqrt(Math.pow(deltaX,2)+Math.pow(deltaY,2))*0.4;
		angl = Math.atan2(deltaX,deltaY);
		
		//console.log( mouseX +", "+ mouseY +" | "+ lastX +", "+ lastY);
		//if( lastX != 0 && lastY != 0 ){
			Logo.draw();
		//}

		lastX = lastX*0.5 + mouseX*0.5;
		lastY = lastY*0.5 + mouseY*0.5;
	},
	
	fade: function(){
		ctx.fillStyle = "rgba(255,255,255,0.08)";
		ctx.fillRect(0,0,3000,3000); //dynamic size for performance?
	}
};



SmoothLine = new Class({

	initialize: function( options ){
		this.x0 = options.x;
		this.y0 = options.y;
		this.x1 = options.x;
		this.y1 = options.y;
		this.fk = options.fk;
		this.res = options.res;
		this.vX = 0;
		this.vY = 0;
		this.fX = 0;
		this.fY = 0;
		this.ok = 0;
		this.segCount = 10;
		
		this.ctx = options.ctx;
		//this.ctx = $('logo_canvas');
	},
  
	Update: function(_xT, _yT){
		
		//console.log( "Update called with "+ _xT +", this.e:"+ this.e);
		
		if( this.ok==0 ) return;
		
		this.x0 = this.x1;
		this.y0 = this.y1;
		
		for(i=0;i<this.segCount;i++){
			this.fX = (this.x0 - _xT) * this.fk;
			this.fY = (this.y0 - _yT) * this.fk;

			this.vX -= this.fX;
			this.vX *= this.res;
			this.vY -= this.fY;
			this.vY *= this.res;

			this.x0 += this.vX;
			this.y0 += this.vY;

			//line(x0,y0,x1,y1);
			/*
			this.e = new Element('div', {'class': 'sprite'});
			$("logo_area").adopt( this.e );
			this.e.setStyles({"top": this.x0, "left": this.y0});
			*/
			this.ctx.beginPath();
			
			this.ctx.moveTo( this.x0, this.y0);
			this.ctx.lineTo( this.x1, this.y1);
			
			this.ctx.closePath();
			this.ctx.stroke();
			
			this.x1 = this.x0;
			this.y1 = this.y0;
		}
	},
	
	Init: function(_x, _y){
    	this.x0 = _x;
    	this.y0 = _y;
    	this.x1 = _x;
    	this.y1 = _y;
    	this.vX = 0;
    	this.vY = 0;
		this.ok = 1;
  	}
});