
/* Hey look at the WTF dataobject, why does it exist */
Data = {
 centR : undefined,
 colorH : function calcHueFromTime() {

 		// Yum, let's ensure everyone has colour based of off
 		// something 
		var t = new Date(),
			val = ((t.getHours() * 60) + t.getMinutes()) / 1380 * 360;

		return val;
	},
  tracking : true
}


/* Setup our Character Prototype */

Char = function( points ) {
    
    this.debug = {
    	initPoints : points
    }

	this.outline = new Path();

	// Loop through initial points
	for ( var i = 0; i < points.length; i++ ) {
		this.outline.add(points[i]);
	}
	this.outline.closed = true;
	this.outline.strokeColor = window.g = new HslColor( Data.colorH(), 0.2, 0.67);

	// Find and define center;
	this.centerPoint = new Point(
						this.outline.bounds.x + this.outline.bounds.width/2.3,
						this.outline.bounds.y + this.outline.bounds.height/1.5
					);
	
	// Center
	this.toCenter = function () {
		
		var shape = this.outline, // Just for readability
			bounds = this.outline.bounds,
			offset = new Point( (paper.view.size.width - 250 )/2, (paper.view.size.height - bounds.height*1.6)/2);

		for ( var i = 0; i < shape.segments.length; i++ )  {
		  
		  var p = shape.segments[i].point + offset;
		  shape.segments[i].point = p;
		}

		this.centerPoint += offset;
		//LADEBUG var debug = new Path.Circle( this.centerPoint, 2 ); debug.fillColor = 'blue';

	}

	this.toCenter();

	this.section = [];
	// Segments
    for ( var i = 0, max = this.outline.segments.length; i < max; i++ ) {
    	
      this.section[i] = new Path();
      var last = this.outline.segments[i+1] || this.outline.segments[0];
      this.section[i].add( this.outline.segments[i], this.centerPoint, last );
      this.section[i].strokeColor = new HslColor( Data.colorH(), 0.2, 0.67);
    }

    this.animateSection = function () {


		// 
		var vector = Data.centR - this.centerPoint;
    	this.centerPoint += (vector / 20) > .1 ? .1 : vector / 20;
	
    	for ( var i = 0, max = this.section.length; i < max; i++ )  {
  
    		this.section[i].segments[1].point = this.centerPoint;
    	}

    	/*
    	** If we're nearing the target then let's chuck in 
    	** a new random target to aim for. Keep it moving folks
    	*/
    	if (this.centerPoint.getDistance(Data.centR) < 10 ) {
    		
    		Data.tracking = false;
    		Data.centR = Point.random() * view.size;
    		setTimeout( function () {
			
				Data.tracking = true;	

    		}, 660 );
    	}
    	
    } // animateSection();

}

var LPoints = [
  new Point( 0, 0 ),
  new Point( 126, 0),
  new Point( 126, 150 ),
  new Point( 0, 150 )
];

var APoints = [
  new Point( 183, 0 ),
  new Point( 217, 0),
  new Point( 268, 150 ),
  new Point( 132, 150 )
]

// Background
Data.bg  = new Path.Rectangle( new Point( 0, 0), view.size );
Data.bg.fillColor = new HsbColor( Data.colorH(), 0.2, 1 );

L = new Char( LPoints );
A = new Char( APoints );

function onResize ( event )  {}

function onMouseMove ( event ) {
  
  if (Data.tracking) {
	  Data.centR = Data.finalTarget = event.point;	
  }
}

function onFrame () {
	
	Data.bg.fillColor = new HsbColor(Data.colorH(), 0.2, 1);

	if ( typeof Data.centR != "undefined" ) {
		
		L.animateSection();
		A.animateSection();
	}

}

function onResize() {
	
	console.log("Resize");
	Data.bg.segments[2].point = new Point( paper.view.size.width, 0 );
	Data.bg.segments[3].point = new Point( paper.view.size.width, paper.view.size.height)
	Data.bg.segments[0].point = new Point( 0, paper.view.size.height );
}
