var Twist = {
	Init: function(e){
		if(arguments.length==2){
			this.element = document.getElementById(e);
			/* trigger is the link element. When set, is used to
			switch the className of that link element */
			this.trigger = arguments[1];
		}else{
			this.element = document.getElementById(e);
			this.trigger = false;
		}
		//Search classes to find 'show' or 'hide'
		this.sClasses = this.element.className;
		var aClasses = this.sClasses.split(" ");
		for(var i=0; i<aClasses.length; i++){
			if(aClasses[i]=="hide" || aClasses[i]=="show"){
				//store as class name
				this._className = aClasses[i];
			}
		}
	},
	
	Show: function(){
		/* if 'Show' used as public takes 1 argument 'elemnet id' */
		if(arguments.length==1){
				this.Init(arguments[0]);
		}
		this.sClasses = this.sClasses.replace(/(hide|show)/g,'');
		this.element.className = this.sClasses+" show";
	},
	
	Hide: function(){
		/* if 'Hide' used as public takes 1 argument 'elemnet id' */
		if(arguments.length==1){
				this.Init(arguments[0]);
		}
		this.sClasses = this.sClasses.replace(/(hide|show)/g,'');
		this.element.className = this.sClasses+" hide";
	},
	
	Toggle: function(e){
		/* 2nd argument allowed for link/trigger element */
		if(arguments.length==2){
			this.Init(e,arguments[1]);
		}else{
			this.Init(e);
		}
		switch(this._className){
			case "hide":
				this.Show();
				break;
			case "show":
				this.Hide();
				break;
		}
		if(this.trigger){
			this.ToggleTrigger(this.trigger);
		}
	},
	
	ToggleTrigger: function(e){
		switch(e.className){
				case "twistOpen":
					e.className = "twistClosed";
					break;
				case "twistClosed":
					e.className = "twistOpen";
					break;
			}
	},
	
	ExpandAll: function(e){
		e = document.getElementById(e);
		var nodes = e.childNodes;
		var nNodes = nodes.length;
		
		for(var i=0;  i<nNodes; i++){
			if(nodes[i].nodeName == '#text' || nodes[i].nodeName == '#comment'){
				//Do nothing
			}else{
				if(nodes[i].className != undefined){
					var sClasses = nodes[i].className;
				}
				
				var match = sClasses.indexOf('hide');
				if(match != -1){
					sClasses = sClasses.replace('hide','');
					nodes[i].className = sClasses + " show";
				}
				
				var match = sClasses.indexOf('twistClosed');
				if(match != -1){
					sClasses = sClasses.replace('twistClosed','');
					nodes[i].className = sClasses + " twistOpen";
				}
				
			}
		}
	},
	
	CollapseAll: function(e){
		e = document.getElementById(e);
		var nodes = e.childNodes;
		var nNodes = nodes.length;
		
		for(var i=0;  i<nNodes; i++){
			if(nodes[i].nodeName == '#text' || nodes[i].nodeName == '#comment'){
				//Do nothing
			}else{
				if(nodes[i].className != undefined){
					var sClasses = nodes[i].className;
				}
				
				var match = sClasses.indexOf('show');
				if(match != -1){
					sClasses = sClasses.replace('show','');
					nodes[i].className = sClasses + " hide";
				}
				
				var match = sClasses.indexOf('twistOpen');
				if(match != -1){
					sClasses = sClasses.replace('twistOpen','');
					nodes[i].className = sClasses + " twistClosed";
				}
				
			}
		}
	}
};

var Win = {
	New: function(e){
		var sFeatures = '';
		if(e.title == ''){
			var title = 'win';
		}else{
			var title = e.title;
		}
		
		if(arguments.length >= 2){
			var sOtherF = '';
			var aFeatures = arguments[1].split(',');
			var nF = aFeatures.length;
			var width = ''; var height = ''; var top = ''; var left = '';
			for(var i=0; i<nF; i++){
				var aF = aFeatures[i].split('=');
				switch(aF[0]){
					case 'width': width = aF[1];
						break;
					case 'height': height = aF[1];
						break;
					case 'top': top = aF[1];
						break;
					case 'left': left = aF[1];
						break;
					default:
						sOtherF += ','+aF[0]+'='+aF[1];
				}  
			}
			
			if(arguments.length == 3){
			// Window Centering (requires that width/height is supplied )
					switch(arguments[2]){
						case 'center':
							top = this.getCenterY(height);
							left = this.getCenterX(width);
							break;
						case 'centerX':
							left = this.getCenterX(width);
							break;
						case 'centerY':
							top = this.getCenterY(height);
							break;
					}
			}
			
			if(width!=''){ sFeatures += ',width='+width; }
			if(height!=''){ sFeatures += ',height='+height; }
			if(top!=''){ sFeatures += ',top='+top; }
			if(left!=''){ sFeatures += ',left='+left; }
			
			sFeatures = sFeatures.substr(1, sFeatures.length)+sOtherF;
		}
		
		if(sFeatures!=''){
			window.open(e.href,title,sFeatures);
		}else{
			window.open(e.href,title);
		}
	},
	
	getCenterX: function(w){
		x = (screen.availWidth - w) / 2;
		return x;
	},
	
	getCenterY: function(h){
		x = (screen.availHeight - h) / 2;
		return x;
	}
	
}