var video_dropdown_manager
var animate_mode=1 //0: THE MENUS JUST APPEAR WHEN THEN OPEN AND CLOSE or 1:THEY SLIDE IN AND OUT OF VIEW
var animate_offset=40 //IF ANIMATING, MENU ITEMS WILL BE POSITIONED A NUMBER OF PIXELS ABOVE THEIR TARGET POSITION BEFORE BEING MOVED INTO VIEW. 
var animate_increment=4 //IF ANIMATING, THE AMOUNT BY WHICH THE MENU ITEM WILL BE MOVED IN OR OUT OF POSITION EACH TIME
var ts_last_moveup_ID="" //IF ANIMATING, ts_last_mover_ID WILL BE SET TO THE ID OF THE LAST NODE IN THE LIST TO BE MOVED. UPON MOVING, IF THE NODE'S ID MATCHES ts_last_mover_ID IT WILL RESIZE THE IFRAME
var ts_last_movedown_ID=""

//IF THIS SCRIPT IS INCLUDED ON A PAGE, EXTEND THE PAGE LOAD ACTION TO SETUP THE DIRECTORY MANAGER
Function.prototype.extendFn = function (fn1,fn2) {
    var c = this;
	
    return function () { fn1();fn2() };
};

page_load_actions=page_load_actions.extendFn(page_load_actions,setup_video_dropdown_manager)

function setup_video_dropdown_manager(){
	video_dropdown_manager=new video_dropdown_manager_class(video_ID)
	video_dropdown_manager.setup_interface()
	
}
function video_dropdown_manager_class(video_ID){
	
	var _me=this
	this.holder=document.getElementById("video_list_holder")
	this.menu=document.getElementById("vid_category_ID")
	this.xml_loader=0
	this.xml_ob=0
	this.selected_id=_me.menu.options[_me.menu.selectedIndex].value
	this.vid_category_ID=_me.selected_id.split("_")[0]
	this.video_ID=video_ID
	this.video_item_arr=[]
	this.animate=0
	this.just_started=1
	
	this.setup_interface=function(){
		
		if(document.addEventListener){
			_me.menu.addEventListener('change',_me.menu_change,false);
		}else{
			if(document.attachEvent) {
				_me.menu.attachEvent('onchange',_me.menu_change,false);
			}
		}
		
		if(is_video_index==0){
			_me.load_video_by_category(_me.vid_category_ID,_me.video_ID)
		}
	}
	
	this.menu_change=function(){
		var new_cat=_me.menu.options[_me.menu.selectedIndex].value
		if(is_video_index==0){
			_me.animate=1
			_me.video_ID=""
			_me.selected_id=_me.menu.options[_me.menu.selectedIndex].value
			_me.vid_category_ID=_me.selected_id.split("_")[0]
			_me.load_video_by_category(new_cat.split("_")[0],_me.video_ID)
		}else{
			document.location=pthstr+"video/index.asp?vid_category_ID="+new_cat
		}
	}
	
	this.load_video_by_category=function(catid,vid){
		
		_me.load_xml("library/get_videos_by_vid_category_xml.asp","vid_category_ID="+catid+"&video_ID="+vid+"&theme="+theme)
	}
	
	this.load_xml=function(xmlpth,params){

		
		xmlpth=pthstr+xmlpth
		
	
		//window.open(xmlpth+"?"+params)
		/*@cc_on @*/
		/*@if (@_jscript_version >= 5)
			// JScript gives us Conditional compilation, we can cope with old IE versions.
			// and security blocked creation of the objects.
	
			try {
				_me.xml_loader = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					_me.xml_loader = new ActiveXObject("Microsoft.XMLHTTP");
				} catch (E) {
					_me.xml_loader = false;
				}
			}
	
		/*@end @*/
		
		if (!_me.xml_loader && typeof XMLHttpRequest!='undefined') {
			_me.xml_loader = new XMLHttpRequest();
		}
		
		if(params!=""){
			_me.xml_loader.open("POST", xmlpth,true);
		}else{
			_me.xml_loader.open("GET", xmlpth,true);
		}
		_me.xml_loader.setRequestHeader( "If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT" );
		_me.xml_loader.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

		_me.xml_loader.onreadystatechange=function(){_me.xml_loaded()}

		if(params!=""){
			_me.xml_loader.send(params)
		}else{
			_me.xml_loader.send(null)
		}

		
	
	}
	
	this.xml_loaded=function(){
		
		if (_me.xml_loader.readyState==4) {

			
			
			if(browser.isIE){
				var xml_ob=new ActiveXObject('Microsoft.XMLDOM')
				xml_ob.loadXML(_me.xml_loader.responseText)
			}else{
				var xml_ob=new DOMParser().parseFromString(_me.xml_loader.responseText, 'text/xml')
			}
			
			var output_nodes=xml_ob.selectNodes("//item")
				
			if(output_nodes.length>0){
				_me.xml_ob=xml_ob
				
				var selected_node=xml_ob.selectSingleNode("//item[@selected=1]")
				
				_me.video_ID=selected_node.getAttribute("video_ID")
			
				_me.draw_list()

			}


		}
	}
	
	this.draw_list=function(){
		
		_me.holder.innerHTML=""
		_me.video_item_arr=[]
		var output_nodes=_me.xml_ob.selectNodes("//item")
		
		max_items=output_nodes[0].parentNode.getAttribute("max_items")
		
		if(output_nodes.length>max_items){
			max_len=max_items
		}else{
			max_len=output_nodes.length
		}
		for(var i=0;i<max_len;i++){
			var id=output_nodes[i].getAttribute("video_ID")
			
			_me.video_item_arr[id]=new video_item_class(output_nodes[i],_me,i)
			_me.video_item_arr[id].setup_interface()
		}
		
		if(output_nodes.length>max_items && _me.selected_id!="recent"){
			var diff=output_nodes.length-max_items
			
			if(diff==1){
				var msg="See more videos"
			}else{
				var msg="See "+(diff)+" more videos"
			}
			var div=document.createElement("div")
			div.id="v_linkmore"
			div.className="vid_category_link"
			
			var a=document.createElement("a")
			a.setAttribute("href",pthstr+"video/index.asp?vid_category_ID="+_me.selected_id)
			a.innerHTML=msg+"..."
			
			div.appendChild(a)
			_me.holder.appendChild(div)
			
		}
		if(_me.just_started==0){
			_me.video_item_arr[_me.video_ID].load_video()
		}
		_me.just_started=0
	}
	
}



function video_item_class(nd,par,pos){
	var _me=this
	this.node=nd
	this.parent=par
	this.listpos=pos
	this.id=_me.node.getAttribute("video_ID")
	this.selected=parseInt(_me.node.getAttribute("selected"))
	this.setup_interface=function(){
	
		var div=document.createElement("div")
		div.id="v_link"+_me.id
		if(_me.selected==1){
			div.className="vid_category_link v_selected"
		
		}else{
			div.className="vid_category_link"
		}
		
		var title_nodes=_me.node.selectNodes("./title")
		var title=""
		if(title_nodes.length>0){
			
			title=title_nodes[0].firstChild.nodeValue
		
		}
		
		var strap_nodes=_me.node.selectNodes("./strapline")
		var strap=""
		if(strap_nodes.length>0){
			strap=strap_nodes[0].firstChild.nodeValue
		}
		
		var a=document.createElement("a")
		a.setAttribute("href","javascript:;")
		a.innerHTML=title
		
		if(document.addEventListener){
			a.addEventListener('click',_me.select_video,false);
		}else{
			if(document.attachEvent) {
				a.attachEvent('onclick',_me.select_video,false);
			}
		}
		
		div.appendChild(a)
		
		if(strap!=""){
			var strap_div=document.createElement("div")
			strap_div.className="title_strap"
			strap_div.innerHTML=strap
			div.appendChild(strap_div)
		}
		_me.parent.holder.appendChild(div)
		
		_me.element=document.getElementById("v_link"+_me.id)
		
		
		if(_me.parent.animate==1){
			_me.element.style.marginTop="-80px"
	
			
			
			
			 var attributes = {   
				marginTop: { to: 0 }  
				
			};   
			_me.move_down_anim = new YAHOO.util.Anim(_me.element, attributes, 1, YAHOO.util.Easing.easeOut);  
			
			_me.move_down_anim.animate()
		}
		
	}
	
	this.select_video=function(){
		if(_me.parent.video_ID!=_me.id){
			
			if(_me.parent.video_ID!=""){
				_me.parent.video_item_arr[_me.parent.video_ID].deselect_video()
			}
			_me.parent.video_ID=_me.id
			_me.selected=1
			_me.node.setAttribute("selected",_me.selected)
			if(_me.element.className.indexOf(" v_selected")==-1){
				_me.element.className=_me.element.className+" v_selected"
			}
		
			
			_me.load_video()
		}
	}
	
	this.deselect_video=function(){

		_me.element.className="vid_category_link"
		_me.selected=0
		_me.node.setAttribute("selected",_me.selected)
		
	}
	
	this.load_video=function(){
		//DESELECT OTHERS
		
		
		change_video(_me.id)
	}
	
}

