// JavaScript Document

function css_slide(element,property,output_prefix,output_suffix,data_type,final_value,update_interval,step_size){
	
	switch(data_type){
		case "hex" :
			re = /([0-9a-fA-F]+)/;
		break;
		case "num" :
			re = /([0-9\.\-]+)/;
		break;
		case "rgb" :
			re = /rgb\s*\(\s*([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\s*\)/;
		break;			
	}
	
	if(typeof document.getElementById(element).style[property] != "undefined"){
		this.current_value = document.getElementById(element).style[property].match(re);  //array
	}	
	
	//Events
	this.oncomplete = function(){};
	this.onstepstart = function(){};
	this.onwait = function(){};
	
	//Vars
	this.stop_slide = false;
	this.re = re;
	this.property = property;
	this.element = element;		
	this.output_prefix = output_prefix;
	this.output_suffix = output_suffix;
	
	if(final_value!=null){ this.final_value = final_value; }
	if(update_interval!=null){ this.update_interval = update_interval; }
	if(step_size!=null){ this.step_size = step_size; }		
	
	this.block = Array();
	this.next = Array();
	
	this.slide = function(_this,final_value,update_interval,step_size){
		
		_this.onstepstart();
		
		if(typeof document.getElementById(_this.element).style[_this.property] != "undefined"){

			if(final_value!=null){ _this.final_value = final_value; }
			if(update_interval!=null){ _this.update_interval = update_interval; }
			if(step_size!=null){ _this.step_size = step_size; }	
				
			_this.block = Array();
			_this.next = Array();
			_this.current_value = document.getElementById(_this.element).style[_this.property].match(_this.re);
			
			for(i=1 ; i < _this.current_value.length ; i++){
				//alert("p "+document.getElementById(element).style[property]+" r "+document.getElementById(element).style[property].match(_this.re)+" len "+_this.current_value.length+" cur "+_this.current_value[i]+" fin "+_this.final_value[i])
				if(_this.current_value[i] > _this.final_value[i]){ // slide down
					//alert("d");
					_this.next[i] = _this.current_value[i]*1 - _this.step_size[i];
					_this.block[i] = 'n';					
					if(_this.next[i] <= _this.final_value[i]){ 
						_this.next[i] = _this.final_value[i]; 
						_this.block[i] = 'y';
					}			
				}else if(_this.current_value[i] < _this.final_value[i]){ // slide up
					//alert("u");
					_this.next[i] = _this.current_value[i]*1 + _this.step_size[i];
					_this.block[i] = 'n';
					if(_this.next[i] >= _this.final_value[i]){ 
						_this.next[i] = _this.final_value[i];
						_this.block[i] = 'y';
					}
				}else{ // No slide
					//alert("n");
					_this.next[i] = _this.final_value[i];
					_this.block[i] = 'y';
				}				
			}
			var n = '';
			for(i=1 ; i <= _this.next.length ; i++){
				if(_this.next[i] || _this.next[i]===0){
					if(n){ n += ","; }
					n += _this.next[i];
				}
			}
			n = _this.output_prefix+n+_this.output_suffix;
			//alert(n)
			document.getElementById(_this.element).style[_this.property] = n;			
			var b = true;
			for(i=1 ; i <= _this.block.length ; i++){
				if(_this.block[i]){
					if(_this.block[i]!='y'){ b = false; }
				}
			}
			if(!b && !_this.stop_slide){
				var t = setTimeout( function(){ _this.s(); } ,_this.update_interval);
				_this.onwait();
			}else if(_this.stop_slide){
				_this.stop_slide = false;
				_this.oncomplete();
			}else{
				_this.oncomplete();
			}
		}
	}
	
	this.curry = function(method){
		var curried = [];
		for (var i = 1; i < arguments.length; i++) {
			curried.push(arguments[i]);
		}
		return function() {
			var args = [];
			for (var i = 0; i < curried.length; i++) {
				args.push(curried[i]);
			}
			for (var i = 0; i < arguments.length; i++) {
				args.push(arguments[i]);
			}
			return method.apply(null, args);
		}
	}
		
	this.s = this.curry(this.slide,this)	
	
}
