// cougar.anim
// Cougar Animation Framework
// copyright Concept Lab 2007
// (c) Concept Lab 2007

function cougarAnim(framerate) {
	this.fps = (framerate || 25);
}

function cougarAnimTween() {
	
}

cougarAnim.prototype.resize = function(obj_id, target_width, target_height, speed, callback) {
	//var _step = 1;
	var obj = document.getElementById(obj_id);
	
	
	
	this.animate(obj, 'width', target_width, speed);
	this.animate(obj, 'height', target_height, speed, callback);
}

cougarAnim.prototype.relocate = function(obj_id, target_x, target_y, speed, callback) {
	var obj = document.getElementById(obj_id);
	this.animate(obj, 'x', target_x, speed);
	this.animate(obj, 'y', target_y, speed, callback);
}

cougarAnim.prototype.fade = function(obj_id, target_alpha, speed, callback) {
	var obj = document.getElementById(obj_id);
	// alert('attaching fade to '+obj_id);
	this.animate(obj, 'alpha', target_alpha, speed, callback);
}

cougarAnim.prototype.animate = function(obj, attribute, target, speed, callback) {
	var target_attribute;
	var speedMS = speed * 1000;
	var framesToPerform = speed * this.fps;
	
	if (framesToPerform==0) framesToPerform=1
	// make sure the target value is an integer
	target = target*1;
	// make sure the position is absolute
	if (attribute=='x' || attribute=='y') obj.style.position = 'absolute';
	
	// get current attribute
	switch(attribute) {
		case 'width': object_attribute = obj.style.width.replace('px','') || 0;; break;
		case 'height': object_attribute = obj.style.height.replace('px','') || 0; break;
		case 'x': object_attribute = obj.style.left.replace('px','') || 0; break;
		case 'y': object_attribute = obj.style.top.replace('px','') || 0; break;
		case 'alpha' :  object_attribute = (obj._opacity!=null ? obj._opacity : 100); break;
	}
	
	//alert(obj.id+':'+object_attribute);
	
	// already in position - no move required
	if (object_attribute==target) return;
	
	
	if (!obj._tweens) {
		obj._tweens = new Array();
	}
	
	// clear current tween with current attr
	if (obj._tweens[attribute]) {
		clearInterval( obj._tweens[attribute].timer ); obj._tweens[attribute]=null;
	}
	
	if (!obj._tweens[attribute]) {
		// set up tween if it has not been set yet
		obj._tweens[attribute] = new cougarAnimTween();
		obj._tweens[attribute].start_attribute = object_attribute;
		obj._tweens[attribute].current_attribute = object_attribute;
		obj._tweens[attribute].end_attribute = target;
		obj._tweens[attribute].callback = callback;
		obj._tweens[attribute].distance = obj._tweens[attribute].end_attribute - obj._tweens[attribute].start_attribute;
		if (obj._tweens[attribute].distance<0) 	obj._tweens[attribute].distance= 	obj._tweens[attribute].distance * (-1);
		
		if (obj._tweens[attribute].end_attribute > obj._tweens[attribute].start_attribute) {
			obj._tweens[attribute].increase = 1;
		} else {
			obj._tweens[attribute].increase = -1;
		}
		//alert(framesToPerform);
		obj._tweens[attribute].obj = obj;
		obj._tweens[attribute].step =  (obj._tweens[attribute].distance/framesToPerform) * obj._tweens[attribute].increase;
		obj._tweens[attribute].steps = new Array();
		//var framesToRender = obj._tweens[attribute].distance/obj._tweens[attribute].step;
		var delay = speedMS/framesToPerform;
		//alert(delay);
		for (x=1; x<framesToPerform;x++) {
			// any easing should be done here
			obj._tweens[attribute].steps[obj._tweens[attribute].steps.length ] = (1*object_attribute) + (obj._tweens[attribute].step*x);
		}
			// add finish state step
			obj._tweens[attribute].steps[obj._tweens[attribute].steps.length ]= target;
		
		obj._tweens[attribute].timer = setInterval(function() { tweenObj.step(obj, attribute); }, delay);
		
		//}
		//obj._tweens[attribute].interval = setInterval(function() { tweenObj.tween(obj, attribute, target, speed); }, delay);
		//alert(obj._tweens[attribute].step);
	}
	
	var delay = Math.floor(speedMS/obj._tweens[attribute].distance); // calculate

	var tweenObj = this;
	
}

cougarAnim.prototype.step = function(obj, attribute) {
	
	if (!obj._tweens[attribute]) return false;
	
	if (obj._tweens[attribute].steps.length == 0) { 
		clearInterval( obj._tweens[attribute].timer ); 
		if (obj._tweens[attribute].callback) var callback = obj._tweens[attribute].callback;
		obj._tweens[attribute]=null; 
		if (callback) callback();
		return; 
	}
	
	// get step from list and chunk it out
	var current_step = obj._tweens[attribute].steps.splice(0,1);
	
	// transform object to new state
		obj._tweens[attribute].current_attribute = current_step;
			switch(attribute) {
				case 'width': obj.style.width = obj._tweens[attribute].current_attribute+'px'; break;
				case 'x': obj.style.left = obj._tweens[attribute].current_attribute+'px'; break;
				case 'y': obj.style.top = obj._tweens[attribute].current_attribute+'px'; break;
				case 'height': obj.style.height = obj._tweens[attribute].current_attribute+'px'; break;
				case 'alpha':
			
				obj._opacity = obj._tweens[attribute].current_attribute;
				obj.style.opacity = obj._opacity/100;
				obj.style.filter = 'alpha(opacity=' + obj._opacity + ')';
				
				break;
				
			}
	
}

cougarAnim.prototype.set = function(id, attribute, value) {
	obj = document.getElementById(id);
	switch(attribute) {
				case 'width': obj.style.width =value+'px'; break;
				case 'x': obj.style.left = value+'px'; break;
				case 'y': obj.style.top = value+'px'; break;
				case 'height': obj.style.height = value+'px'; break;
				case 'alpha':
		
				//alert('setting'+attribute+' to '+value);
				obj._opacity = value;
				obj.style.opacity = obj._opacity/100;
				obj.style.filter = 'alpha(opacity=' + obj._opacity + ')';
				
				break;
				
			}
	
}

//var cougar = new Object();
cougar.anim = new cougarAnim(25);


