//////////////////// *UI ////////////////////
var UI = {
	init: function(){	
		var trees = $G('ul',document,{className:'UITree'});
		for(var i=0; i<trees.length; i++){
			if(trees[i].getAttribute('id') != 'filebrowser')
				new UI.Tree(trees[i]);
		}
	}
}

//////////////////// *UI_SWF ////////////////////
UI.SWF = Class.create();
UI.SWF.prototype = {
	so: false,
	init: function(swf,swfID,width,height,flashVersion,container,flashVars,params){
		this.so = new SWFObject(swf,swfID,width,height,flashVersion);
		if(flashVars){
			for(i in flashVars)
				this.so.addVariable(i,flashVars[i]);
		}
		if(params){
			for(i in params)
				this.so.addParam(i,params[i]);
		}
		this.so.write($(container));
		return this;
	}
}
UI.FullScreenSWF = Class.create();
UI.FullScreenSWF.prototype = {
	init: function(swf,swfID,flashVersion,container,flashVars,params){
		if(!flashVars) flashVars = {};
		if(!params) params = {};
		flashVars.salign = 'lt';
		flashVars.scale = 'noscale';
		new UI.SWF(swf,swfID,'100%','100%',flashVersion,container,flashVars,params);
	}
}

//////////////////// *UI_VIDEO ////////////////////
UI.Video = Class.create();
UI.Video.prototype = {
	init: function(source,data,container,width,height){
		switch(source.toLowerCase()){
			case 'google':
				if(width == undefined) width = '100%';
				if(height == undefined) height = 326;
				new UI.SWF('http://video.google.com/googleplayer.swf?hl=en&docId='+data,container+'_SWF',width,height,8,container);
				break;
			case 'youtube':
				if(width == undefined) width = 425;
				if(height == undefined) height = 350;
				new UI.SWF('http://www.youtube.com/v/'+this.getYouTubeIDFromURL(data),container+'_SWF',width,height,8,container);
				break;
			default:
				Elem.update(container,'<p class="message error">Only google videos are supported so far.</p>');
				break;
		}
	},
	getYouTubeIDFromURL: function(URL){
		var qryStringStart = URL.indexOf('?');
		if(qryStringStart < 0)
			return URL;
		var qryString = URL.substr(qryStringStart+1,URL.length-qryStringStart);
		var argPairs = qryString.split('&');
		var args = {};
		for(var i=0; i<argPairs.length; i++){
			var argPair = argPairs[i].split('=');
			var argName = argPair[0].toLowerCase();
			var argValue = argPair[1];
			args[argName] = argValue;
		}
		if(args.v != undefined)
			return args.v;
		else
			return URL;
	}
}


//////////////////// *UI_TREE ////////////////////
UI.Tree = Class.create();
UI.Tree.instances = [];
UI.Tree.prototype = {
	init: function(elem,singleClickExpand,listeners){
		UI.Tree.instances.push(this);
		elem = $(elem);
		this.rootNode = elem;
		this.singleClickExpand = singleClickExpand;
		this.listeners = listeners || {main:{},child:{}};
		this.current = {head:{}};
		this.branches = [];
		this.initChildren(elem);
	},
	initChildren: function(parentnode){
		parentnode.isBranch = true;
		var children = Elem.getChildren(parentnode);
		for(var i=0; i<children.length; i++){
			var node = children[i];
			if(node.nodeName.toLowerCase() == 'li'){
				var isLastChild = i == children.length-1 ? true : false;
				this.initChild(node,isLastChild);
				if(node.childNodes.length > 1){
					var ul = $G('ul',node,{parentNode:node});
					if(ul.length) this.initChildren(ul[0]);
				}
			}
		}
	},
	initChild: function(elem,isLastChild){
		new UI.Tree.Child(this,elem,isLastChild,this.listeners.child,this.singleClickExpand);
	},
	expandAll: function(){
		for(var i=0; i<this.branches.length; i++)
			this.branches[i].expand();
	},
	collapseAll: function(){
		for(var i=0; i<this.branches.length; i++)
			this.branches[i].collapse();
	},
	setCurrent: function(child){
		if(this.current) Elem.removeClassName(this.current.head,'current');
		this.current = child;
		Elem.addClassName(this.current.head,'current');
	},
	getCurrent: function(){
		return this.current;
	}
}
UI.Tree.Child = Class.create();
UI.Tree.Child.prototype = {
	callListener: function(method){
		var L = this.listeners[method];
		if(L) L(this);
	},
	init: function(tree,elem,isLastChild,listeners,singleClickExpand){
		var me = this;
		this.tree = tree;
		this.container = elem;
		
		if(this.container.initialized) return;
		this.container.initialized = true;
		
		this.listeners = listeners || {};
		
		this.singleClickExpand = singleClickExpand;
		
		this.head = $G('div',elem)[0];
		this.ul = $G('ul',elem);
		this.ul.length ? this.ul = this.ul[0] : this.ul = {};
		Elem.addClassName(this.head,'leaf');
		this.head.onmouseover = function(e){
			e = e || window.event;
			me.mouseover(e);
		}
		this.head.onmouseout = function(e){
			e = e || window.event;
			me.mouseout(e);
		}
		if(isLastChild)
			Elem.addClassName(this.container,'lastchild');
		if(this.ul.style){
			Elem.addClassName(this.head,'branch');
			this.tree.branches.push(this);
			this.collapse();
		}
		this.head.onclick = function(e){
			e = e || window.event;
			me.click(e);
		}
		this.head.ondblclick = function(e){
			e = e || window.event;
			me.dblclick(e);
		}
	},
	click: function(e){
		this.tree.setCurrent(this);
		
		if(this.ul.style && this.singleClickExpand){
			this.expandcollapse();
		}
		
		this.callListener('click');
		//Event.stop(e);
	},
	dblclick: function(e){
		if(this.ul.style && !this.singleClickExpand){
			this.expandcollapse();
		}
		this.callListener('dblclick');
		Event.stop(e);
	},
	expandcollapse: function(){
		if(this.expanded){
			this.collapse();
		} else {
			this.expand();
		}
	},
	collapse: function(){
		this.expanded = false;
		Elem.hide(this.ul);
		Elem.removeClassName(this.head,'expanded');
	},
	expand: function(){
		this.expanded = true;
		Elem.show(this.ul);
		Elem.addClassName(this.head,'expanded');
	},
	mouseover: function(e){
		try {
			Elem.addClassName(this.head,'over');
			
			this.callListener('mouseover');
			Event.stop(e);
		}catch(er){}
	},
	mouseout: function(e){
		try {
			Elem.removeClassName(this.head,'over');
			
			this.callListener('mouseout');
			Event.stop(e);
		}catch(er){}
	}
}

//////////////////// *UI_COLLAPSEEXPANDBOX ////////////////////
UI.collapseExpandBox = Class.create();
UI.collapseExpandBox.prototype = {
	container: false,
	open: true,
	init: function(id,charCount,collapsed){
		var me = this;
		
		this.container = $(id);
		this.img = $(id+'_img');
		this.label = $(id+'_label');
		
		if (this.img != undefined) {
			this.img.onclick = this.label.onclick = function(){
				me.toggle();
			}
		}
		
		this.content = this.container.innerHTML;
		this.shortcontent = this.content.trim().left(charCount);
		if (charCount != 0 && this.content.length > charCount) {
			this.shortcontent = this.shortcontent+'...';
		}
		if(collapsed)
			this.toggle();
	},
	toggle: function(){
		if(this.open){
			this.img.src = '/base2/global/images/icons/plus.gif';
			this.container.innerHTML = this.shortcontent;
			this.open = false;
		} else {
			this.img.src = '/base2/global/images/icons/minus.gif';
			this.container.innerHTML = this.content;
			this.open = true;
		}
	}
}

//////////////////// *UI_SHOWHIDEBOX ////////////////////
UI.ShowHideBox = {
	toggle: function(name,inputname,bool){
		Elem[bool ? 'show' : 'hide'](name);
		Elem[bool ? 'hide' : 'show'](name+'_showBtn');
		Elem[bool ? 'show' : 'hide'](name+'_hideBtn');
		$(inputname).value = bool;
	}
}