﻿function $RunOrExecute(funstr){
	if(typeof(funstr)=="string"){eval(funstr);}
	else if(typeof(funstr)=="function"){funstr();}
	else{alert("Not a runnable script");}
}
function $SetTransparency(obj,percent){
   if(obj!=null){
        obj.style.filter = "alpha(opacity=" + (parseFloat(percent)) + ")";
        obj.style.KhtmlOpacity = parseFloat(percent)/100;
        obj.style.MozOpacity = parseFloat(percent)/100;
        obj.style.opacity = parseFloat(percent)/100;
   }
}
function $GetLeft(obj,fromfloating){
    if(fromfloating){
        if(obj){
            if(obj.style){
                if(obj.style.position){
                    if(obj.style.position.toLowerCase()!=""){
                        return 0;
                    }
                }
            }
        }
    }
    return obj.offsetLeft + ((obj.offsetParent != null) ? $GetLeft(obj.offsetParent,fromfloating) : 0);
}
function $GetTop(obj,fromfloating){
    if(fromfloating){
        if(obj){
            if(obj.style){
                if(obj.style.position){
                    if(obj.style.position.toLowerCase()!=""){
                        return 0;
                    }
                }
            }
        }
    }
    return obj.offsetTop + ((obj.offsetParent != null) ? $GetTop(obj.offsetParent,fromfloating) : 0);
}

function $WindowInnerWidth(){
	var WinInnerWid = 630;
	var WinInnerWid1 = 0;
	if( typeof( window.innerWidth ) == 'number' ) {    
		WinInnerWid1 = window.innerWidth;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {    
		WinInnerWid1 = document.documentElement.clientWidth;    
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {    
		WinInnerWid1 = document.body.clientWidth;    
	}
	WinInnerWid = WinInnerWid < WinInnerWid1 ? WinInnerWid1 : WinInnerWid;
	return WinInnerWid;
}
function $WindowInnerHeight(){
	var WinInnerHei = 460;
	var WinInnerHei1 = 0;
	if( typeof( window.innerHeight ) == 'number' ) {    
		WinInnerHei1 = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {    
		WinInnerHei1 = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {    
		WinInnerHei1 = document.body.clientHeight;
	}
	WinInnerHei = WinInnerHei < WinInnerHei1 ? WinInnerHei1 : WinInnerHei;
	return WinInnerHei;
}
Array.prototype.add = function(item){
	var currItemIndex = this.length;
	this[currItemIndex] = item;
	return currItemIndex;
}
var PageTimers = new Array();
function Timer(delay){
	this.intervalId = null;
	this.delay = (delay) ? delay : 1000;
	this.id = PageTimers.add(this);
	this.timerroutine = function(){
		clearTimeout(this.intervalId);
		if(this.enabled==true){
			$RunOrExecute(this.onTick);
		}
		this.intervalId = setTimeout("PageTimers[" + this.id + "].timerroutine();", this.delay);
	}
	this.timerroutine();				
	return this;
}
Timer.prototype = {
	enabled : new Boolean(false),
	delay : new Number(1000),
	onTick : function(){}
}
var ExpandItems = new Array();
function ExpandItem(inner,outer){
	this.getElement = function(comp){
		return (typeof(comp)=="string") ? document.getElementById(comp) : comp;
	}
	this.innerId = inner;
	this.innerDiv = function(){
		return this.getElement(inner);
	}
	this.outerId = outer;
	this.outerDiv = function(){
		return this.getElement(outer);
	}
	//this.init();
	this.isActionComplete = true;
	this.timer = new Timer();
	this.timer.delay = 20;
	this.id = ExpandItems.add(this);				
	this.startWidth = 0;
	this.startHeight = 0;
	this.startX = 0;
	this.startY = 0;
	this.endWidth = 0;
	this.endHeight = 0;
	this.minWidth = 1;
	this.minHeight = 1;
	this.maxWidth = 1;
	this.maxHeight = 1;
	this.animationSpeed = 15; // percentage per timer tick
	this.moveX = 0;
	this.moveY = 0;
	this.innerWidth = 0;
	this.innerHeight = 0;
	this.triggerExpand = function(){
		if(this.hideWhenCollapsed==true){
			this.outerDiv().style.display = "";
		}
		this.startWidth = this.outerDiv().offsetWidth;
		this.startHeight = this.outerDiv().offsetHeight;
		this.innerWidth = Math.max(this.innerDiv().offsetWidth,this.innerWidth);
		this.endWidth = this.innerWidth;
		this.endHeight = this.innerDiv().offsetHeight;
		this.maxWidth = this.innerWidth;
		this.maxHeight = this.innerDiv().offsetHeight;
		this.comp = this.getElement(this.linkedComp);
		if(this.comp!=null){
		    this.outerDiv().style.position = "absolute";
		    if(this.position.toString().indexOf("l")>-1){
		        this.outerDiv().style.left = $GetLeft(this.comp) + "px";
		    }
		    if(this.position.toString().indexOf("r")>-1){
		        this.outerDiv().style.left = ($GetLeft(this.comp) + this.comp.offsetWidth) + "px";
		    }
		    if(this.position.toString().indexOf("t")>-1){
		        this.outerDiv().style.top = $GetTop(this.comp) + "px";
		    }
		    if(this.position.toString().indexOf("b")>-1){
		        this.outerDiv().style.top = ($GetTop(this.comp) + this.comp.offsetHeight) + "px";
		    }
		}
		this.startX = $GetLeft(this.outerDiv());
		this.startY = $GetTop(this.outerDiv());
		this.timer.onTick = "ExpandItems[" + this.id + "].Expand();";
		this.timer.enabled = true;		
	}
	this.Expand = function(from){
		if(this.startWidth < this.endWidth){
			this.startWidth += ((this.maxWidth / 100) * this.animationSpeed);
		}
		if(this.startHeight < this.endHeight){
			this.startHeight += ((this.maxHeight / 100) * this.animationSpeed);
		}
		if(this.startWidth>=this.endWidth && this.startHeight >= this.endHeight){
			this.timer.enabled = false;
			this.startWidth = this.endWidth;
			this.startHeight = this.endHeight;
		}
		this.outerDiv().style.width = this.startWidth + "px";
		this.outerDiv().style.height = this.startHeight + "px";
		if(this.dropDirection.indexOf("b")>-1){
		    this.outerDiv().style.top = this.startY - this.startHeight;
		}
		if(this.dropDirection.indexOf("r")>-1){
		    this.outerDiv().style.left = this.startX - this.startWidth;
		}
		var alpha = Math.ceil((this.startHeight / this.maxHeight) * 100);
		$SetTransparency(this.innerDiv(),alpha);					
	}
	this.triggerCollapse = function(){
		this.startWidth = this.outerDiv().offsetWidth;
		this.startHeight = this.outerDiv().offsetHeight;
		this.endWidth = this.minWidth;
		this.endHeight = this.minHeight;
		this.innerWidth = Math.max(this.innerDiv().offsetWidth,this.innerWidth);
		this.maxWidth = this.innerWidth;
		this.maxHeight = this.innerDiv().offsetHeight;
		this.timer.onTick = "ExpandItems[" + this.id + "].Collapse();";
		this.timer.enabled = true;
	}
	this.Collapse = function(to){
		if(this.startWidth > this.endWidth){
			this.startWidth -= ((this.maxWidth / 100) * this.animationSpeed);
		}
		if(this.startHeight > this.endHeight){
			this.startHeight -= ((this.maxHeight / 100) * this.animationSpeed);
		}
		if(this.startWidth<=this.endWidth && this.startHeight <= this.endHeight){
			this.timer.enabled = false;
			this.startWidth = this.endWidth;
			this.startHeight = this.endHeight;
			if(this.hideWhenCollapsed==true){
				this.outerDiv().style.display = "none";
			}
		}
		this.outerDiv().style.width = this.startWidth + "px";
		this.outerDiv().style.height = this.startHeight + "px";
		if(this.dropDirection.indexOf("b")>-1){
		    this.outerDiv().style.top = this.startY - this.startHeight;
		}
		if(this.dropDirection.indexOf("r")>-1){
		    this.outerDiv().style.left = this.startX - this.startWidth;
		}
		var alpha = Math.ceil(( this.startHeight / this.maxHeight) * 100);
		$SetTransparency(this.innerDiv(),alpha);
	}
	return this;
}
ExpandItem.prototype = {
	isExpanded : new Boolean(false),
	hideWhenCollapsed : new Boolean(true),
	dropDirection : new String("t"),
	position : new String("lt"),
	linkedComp : null,
	init : function(){
		if(this.outerDiv()!=null){
			this.outerDiv().style.overflow = "hidden";
		}
		if(this.hideWhenCollapsed == true && this.isExpanded != true){
			this.outerDiv().style.display = "none";
		}else{
			this.outerDiv().style.display = "";
		}
	},
	expand : function(){
	    if(this.isExpanded!=true){
	        this.isExpanded = true;
		    this.triggerExpand();
	    }		
	},
	collapse : function(){
	    if(this.isExpanded!=false){
		    this.isExpanded = false;
		    this.triggerCollapse();	
		}					
	},	
	toggle : function(){
		if(this.isExpanded == true){
			this.collapse();
		}else{
			this.expand();
		}
	}
}
