//--------------------------------------------------------------------------------------------------------------
// FILE: imUtils.js
// Author: Ben Siroshton
//--------------------------------------------------------------------------------------------------------------
function E(id){
	return document.getElementById(id);
}

//--------------------------------------------------------------------------------------------------------------
// imLogger
//--------------------------------------------------------------------------------------------------------------
var imLogger = {};
{
	imLogger.instance = function(){
		return this;
	};

	imLogger.log = function(msg){
		this.onLog(msg);
	};
	
	imLogger.onLog = function(msg){
	};	
}


//--------------------------------------------------------------------------------------------------------------
// imTimer
//--------------------------------------------------------------------------------------------------------------
var imTimer = {};
{
	imTimer.instance = function(){
		return this;
	};
	
	imTimer.setTimeout = function(target, time){
		window.setTimeout(
			function(){
				target.onTimer();
			},
			time);
	};
	
}

//--------------------------------------------------------------------------------------------------------------
// imUtils
//--------------------------------------------------------------------------------------------------------------
var imUtils = {};
{
	imUtils.isDefined = function(variable){
		
		if( typeof(variable)=='undefined' ){
			return false;
		}
	
		return true;
	};
	
	imUtils.toggleVisibility = function(ele){
		if( ele.style.visibility=="hidden" ){
			ele.style.visibility="visible";
		}else{
			ele.style.visibility="hidden";
		}
	};
	
	imUtils.isVisible = function(ele){
		return ele.style.visibility=="visible";
	};
	
	imUtils.toggleVisibilityById = function(ele){
		var E = document.getElementById(ele);
		if( E==null ){
			return;
		}
		imUtils.toggleVisibility( E );
	};
	
	imUtils.setVisibility = function(ele, visible){
		if( visible ){
			ele.style.visibility="visible";
		}else{
			ele.style.visibility="hidden";
		}	
	};
	
	imUtils.hide = function(ele){
		imUtils.setVisibility(ele,false);
	};
	
	imUtils.show = function(ele){
		imUtils.setVisibility(ele,true);
	};
	
	imUtils.showById = function(ele){
		var E = document.getElementById(ele);
		if( E==null ){
			return;
		}
		imUtils.setVisibility(E,true);
	};
	
	imUtils.hideById = function(ele){
		var E = document.getElementById(ele);
		if( E==null ){
			return;
		}
		imUtils.setVisibility(E,false);
	};
	
	imUtils.hideBlock = function(ele){
		ele.style.display = "none";
	};
	
	imUtils.showBlock = function(ele){
		ele.style.display = "";
	};
	
	imUtils.formatFloat = function(num, decimals){
	    var m = Math.pow(10, decimals);
	    return parseInt(num * m, 10) / m;
	};
	
	imUtils.isIE = function(){
		return navigator.appName == 'Microsoft Internet Explorer';
	};
	
	imUtils.isFirefox = function(){
		return navigator.appName == 'Netscape';
	};
	
	
	imUtils.deleteTableRows = function(tbl){
	
		while( tbl.rows.length>0 ){
			tbl.deleteRow(0);
		}
		
	};
	
	imUtils.clearCombo = function(e){	
		imUtils.removeChildNodes(e);		
	};
	
	imUtils.removeChildNodes = function(e){
	
		while( e.childNodes.length>0 ){
			e.removeChild(e.childNodes[0]);
		}
		
	};
	
	imUtils.setTextValue = function(elementId, value){
	
		var Elem = document.getElementById(elementId);
	
		if( Elem==null )
			return;
	
		if( value==null )
			Elem.value = "";
		else
			Elem.value = value;
	
	};
	
	imUtils.setCheckValue = function(elementId, value){
	
		var Elem = document.getElementById(elementId);
	
		if( Elem==null )
			return;
	
		if( value==null )
			Elem.value = "";
		else
			if( value=='y' || value=='Y' || value=='t' || value=='T' || value==1 )
				Elem.checked = true;
			else
				Elem.checked = false;
	
	};
	
	imUtils.setSelectValue = function(elementId, value){
	
		var Elem = document.getElementById(elementId);
	
		if( Elem==null )
			return;
	
		if( value==null )
			Elem.childNodes[0].selected = true;
		else
			for(var i=0;i<Elem.childNodes.length;i++)
				if( Elem.childNodes[i].value==value ){
					Elem.childNodes[i].selected = true;
					return;
				}
	
	};
	
	imUtils.stripUnit = function(val){
			
		alert( val );
		
		var SVal = new String(val);
		var Result = new String();
		
		var Index=0;
	
		while(Index<SVal.length){
			var Char = SVal.charAt(Index);
			if( !isNaN(Char) ){
				Result += new String(Char);
			}else{
				return Result;
			}
			Index++;
		}
		
		return Result;
		
	};
	
	imUtils.trim = function(str){
	
		if( str==null ){
			return null;
		}
	
		str = new String(str);
		
		var Front=0;
		var Back = str.length-1;
	
		while(Front<str.length && str.charAt(Front)==' '){
			Front++;
		}	
	
		while(Back>=0 && str.charAt(Back)==' '){
			Back--;
		}	
	
		if( Front==0 && Back==0 ){
			return str;
		}
	
		if( Back-Front<=0 ){
			return '';
		}
	
		return str.substring(Front,Back+1);
	
	};
	
	
	imUtils.innerClientWidth = function(){
		if ( navigator.appName == 'Netscape' ){	
			return window.innerWidth;
		}else{
			//return document.body.clientWidth;
			return document.documentElement.clientWidth;
		}		
	};
	
	imUtils.innerClientHeight = function(){
		if ( navigator.appName == 'Netscape' ){	
			return window.innerHeight;		
		}else{
			//return document.body.clientHeight;
			return document.documentElement.clientHeight;
		}		
	};
	
	imUtils.clamp = function(val,min,max){
		if( val<min ){
			return min;
		}else if( val>max ){
			return max;
		}
		return val;
	};
	
	imUtils.lerp = function(val1,val2,amount){
	
		amount = imUtils.clamp(amount,0,1);
		return (val1 * (1.0-amount) + val2 * amount);
	
	};
	
	imUtils.isNumeric = function(value){
	
		if( value==null ){
			return false;
		}
		
		if( imUtils.trim(value)=="" ){
			return false;
		}
	
		if( isNaN(value) ){
			return false;
		}
		
		return true;
		
	}
	

}


//--------------------------------------------------------------------------------------------------------------
// imRect
//--------------------------------------------------------------------------------------------------------------
/**
* @class
* @constructor
*/
function imRect(left,top,right,bottom){

	this.top = top;
	this.left = left;
	this.right = right;
	this.bottom = bottom;
	
	imRect.clone = function(rect){
		return new imRect(rect.left, rect.top, rect.right, rect.bottom);
	};

	this.setStyle = function(htmlElement, unit){
		htmlElement.style.left = this.left + unit;
		htmlElement.style.top = this.top + unit;
		htmlElement.style.width = this.getWidth() + unit;
		htmlElement.style.height = this.getHeight() + unit;
	};
	
	this.getWidth = function(){
		return this.right - this.left;
	};

	this.getHeight = function(){
		return this.bottom - this.top;
	};

	this.move = function(x,y){
		this.left += x;
		this.top += y;
		this.right += x;
		this.bottom += y;
	};
	
	this.snapTL = function(rect){
		var w = this.getWidth();
		var h = this.getHeight();
		this.left = rect.left;
		this.top = rect.top;
		this.right = this.left + w;
		this.bottom = this.top + h;
	};
	
	this.size = function(amount){
		this.left -= amount;
		this.top -= amount;
		this.right += amount;
		this.bottom += amount;
	};

	this.extend = function(r){
		this.left = Math.min(this.left,r.left);
		this.top = Math.min(this.top,r.top);
		this.right = Math.max(this.right,r.right);
		this.bottom = Math.max(this.bottom,r.bottom);
	};
	
	this.getScaledLeft = function(scale){
		return new imRect(this.left,this.top,this.left+this.getWidth()*scale,this.bottom);
	};
	
	this.getScaledRight = function(scale){
		return new imRect(this.right-this.getWidth()*scale,this.top,this.right,this.bottom);
	};

	this.getScaledTop = function(scale){
		return new imRect(this.left,this.top,this.right,this.top+this.getHeight()*scale);
	};

	this.getScaledBottom = function(scale){
		return new imRect(this.left,this.bottom-this.getHeight()*scale,this.right,this.bottom);
	};
	
	this.getIntersection = function(rect){
		if( rect.left>=this.right ) return null;
		if( rect.bottom<=this.top ) return null;
		if( rect.right<=this.left ) return null;
		if( rect.top>=this.bottom ) return null;
		
		return new imRect(Math.max(this.left,rect.left),
							Math.max(this.top,rect.top),
							Math.min(this.right,rect.right),
							Math.min(this.bottom,rect.bottom));
	};
}


//--------------------------------------------------------------------------------------------------------------
// imRectTransition
//--------------------------------------------------------------------------------------------------------------
/**
* @class
* @constructor
*/
function imRectTransition(element){

	imRectTransition.STATE_UNKNOWN = 0;
	imRectTransition.STATE_READY = 1;
	imRectTransition.STATE_TRANSITIONING = 2;

	this.element = element;
	this.rectArray = new Array();
	this.duration = 1000; // milliseconds
	this.interval = 50;
	this.pos = 0;
	this.state = imRectTransition.STATE_UNKNOWN;
	this.hideWhenDone = false;
	this.sourceIndex = null;
	this.destIndex = null;
	this.curRect = null;
	
	this.getState = function(){
		return this.state;
	};
	
	this.addRect = function(rect){
		this.rectArray[this.rectArray.length] = rect;
	};

	this.setRect = function(index, rect){
		this.rectArray[index] = rect;
	};
	
	this.getCurrentRect = function(){
		return this.curRect;
	};
	
	this.transitionBetween = function(startRectIndex, endRectIndex){
		this.transitionBetween(startRectIndex, endRectIndex, false);
	};

	this.transitionBetween = function(startRectIndex, endRectIndex, hideWhenDone){
		this.sourceIndex = startRectIndex;
		this.destIndex = endRectIndex;
		this.pos = 0;
		this.state = imRectTransition.STATE_TRANSITIONING;
		this.hideWhenDone = hideWhenDone;

		this.update();
	};
	
	this.update = function(){
		this.pos += 1.0 / (this.duration / this.interval);
		if( this.pos>1.0 ){
			this.pos = 1.0;
			this.state = imRectTransition.STATE_READY;
		}
		var D = this.pos/1.0;
		var S = 1.0 - D;

		var SourceRect = this.rectArray[this.sourceIndex];
		var DestRect = this.rectArray[this.destIndex];

		this.curRect = new imRect(SourceRect.left*S+DestRect.left*D,
									SourceRect.top*S+DestRect.top*D,
									SourceRect.right*S+DestRect.right*D,
									SourceRect.bottom*S+DestRect.bottom*D);
		this.curRect.setStyle(this.element, "px");
		if( this.state == imRectTransition.STATE_TRANSITIONING ){

			var thisObj = this;
			window.setTimeout(function(){ thisObj.update(); }, this.interval);
		
		}else if( this.hideWhenDone ){
			hide(this.element);
			this.onFinished(this.sourceIndex, this.destIndex);
		}
	};
	
	// user events
	this.onFinished = function(startIndex,endIndex){
	};
	
}


