var VideoListing = function(){
	
	var currentVideo;
	var currentVideoElement;
	var videoArray;
	var videoArrayElements;
	var isLoaded = false;
	
	return {	
			
        init : function(){
			videoArray = [];
			videoArrayElements = [];
		},
		
		registerData: function(p_xml, p_xml2){
			var el = Ext.get('flashVideoListing');
			//el.update("");
			//if(el){
			if(p_xml.childNodes[0] == undefined || p_xml.childNodes[0] == null){
				p_xml = p_xml2.childNodes[1];
			}
			
			//alert(p_xml.attributes[1].nodeValue);
			
			var listType="ol";
			if(p_xml.attributes[1].nodeValue != "num"){
				listType="ul";
			}
			
			var mp4Path = "";
			if(p_xml.attributes[2] != undefined){
				mp4Path = p_xml.attributes[2].nodeValue;
			}
			
			el.createChild({tag:"div",html:"<b>"+p_xml.attributes[0].nodeValue+"</b><br><br>"});
			var holder = el.createChild({tag:listType, id:"holder"});
			
			
			var l=p_xml.childNodes.length;
			var str="";
			for(var i=0;i<l;i++){
				//str += i+" :: "+p_xml.childNodes[i].nodeName+" ::: "+p_xml.childNodes[i].nodeType+" :: "+p_xml.childNodes[i].nodeValue+"\n";
				var obj = p_xml.childNodes[i];
				
				if(obj.nodeName == "section"){
					var isVid = false;
					var index = 1;
					
					if(obj.childNodes != undefined){
						if(obj.childNodes[index] != undefined){
							if(obj.childNodes[index].firstChild != undefined){
							if(obj.childNodes[index].firstChild.nodeValue == null || obj.childNodes[index].nodeName == "transcript"){
								index = 0;
							}
							}else{
								index = 0;
							}
						}else{
							index = 0;
						}
					}
					
					var htmStr = "";
					if(obj.attributes[0] != undefined){//time
						htmStr += "<span><b>("+obj.attributes[0].nodeValue+")</b></span>";
						isVid = true;
					}
					
					htmStr += "<b>"+obj.childNodes[index].firstChild.nodeValue+"</b>";
					var itm = holder.createChild({tag:"li", html:htmStr});
					
					if(isVid){
						itm.on({'click':{ fn: this.onSelectItem,scope: this, data:obj},
							   'mouseover':{ fn: this.onRollItem,scope: this, data:obj},
							   'mouseout':{ fn: this.onRollOutItem,scope: this, data:obj}
							   });
						videoArray.push(obj);
						videoArrayElements.push(itm.dom);
						if(i==0){
							this.currentVideo = obj;
							this.currentVideoElement = itm.dom;
						}
					}
					this.displayChildren(obj.childNodes, holder);
				}
			}
			//alert(videoArray.length);
			this.currentVideo = videoArray[0];
			this.currentVideoElement = videoArrayElements[0];
			//alert(this.currentVideo);
			
			//console.log(str);
			if(mp4Path != ""){
				el.createChild({tag:"a", href:mp4Path, html:"Download Entire Video", target:"_blank"});
			}
			
			if(isLoaded){
				this.startCurrentVideo();
			}
			//}
		},
		
		displayChildren: function(nodeList, p_holder){
			var l = nodeList.length;
			
			if(l > 0){
				var myHolder = p_holder.createChild({tag:"ul"});
			}
			
			for(var i=0;i<l;i++){
				var obj = nodeList[i];
				
				if(obj.nodeName == "video"){
					var index = 0;
					if(obj.childNodes[0].firstChild == null){
						index = 1;
					}
					var itm = myHolder.createChild({tag:"li", html:"<span>("+obj.attributes[0].nodeValue+")</span>"+obj.childNodes[index].firstChild.nodeValue});
					
					itm.on({'click':{ fn: this.onSelectItem,scope: this, data:obj},
							'mouseover':{ fn: this.onRollItem,scope: this, data:obj},
							'mouseout':{ fn: this.onRollOutItem,scope: this, data:obj}
						   });
					videoArray.push(obj);
					videoArrayElements.push(itm.dom);
				}
			}
		},
		
		onSelectItem: function(p_evt, p_node, p_fn){
			this.currentVideoElement.className = "";
			p_node.className = "playing";
			this.currentVideoElement = p_node;
			this.currentVideo = p_fn.data;
			this.configForVideo(this.currentVideo);
			sendVideoToFlash(p_fn.data.attributes[1].nodeValue);
		},
		
		onRollItem: function(p_this, p_node, p_fn){
			if(p_fn.data != this.currentVideo){
				p_node.className = "hover";
			}
		},
		
		onRollOutItem: function(p_this, p_node, p_fn){
			if(p_fn.data != this.currentVideo){
				p_node.className = "";
			}
		},
		
		navigate: function(p_dir){
			var l = videoArray.length;
			var index = null;
			for(var i=0;i<l;i++){
				if(videoArray[i] == this.currentVideo){
					index = i;
					i = l;
				}
			}
			
			if(index != null){
				index += p_dir;
				
				if(index >= videoArray.length || index < 0){
					if(p_dir < 0){
						index = videoArray.length - 1;
					}else{
						index = 0;
					}
				}
				
				this.currentVideoElement.className = "";
				videoArrayElements[index].className = "playing";
				this.currentVideoElement = videoArrayElements[index];
				
				
				this.currentVideo = videoArray[index];
				sendVideoToFlash(this.currentVideo.attributes[1].nodeValue);
				
				this.configForVideo(this.currentVideo);
			}
		},
		
		startCurrentVideo: function(){
			//alert("CURRENT VIDEO :: "+this.currentVIdeo+" ::: "+videoArray);
			if(this.currentVideo != undefined){
				this.configForVideo(this.currentVideo);
				this.currentVideoElement.className = "playing";
				sendVideoToFlash(this.currentVideo.attributes[1].nodeValue);
			}else{
				isLoaded = true;
			}
		},
		
		configForVideo: function(p_obj){
			var el = Ext.get('flashTranscript');
			
			
			var el2 = Ext.get('flashCurrentVideo');
			
			
			var index = 1;
			var dif = 1;
			if(p_obj != undefined){
				if(p_obj.childNodes[1] != undefined){
				if(p_obj.childNodes[1].nodeName == "title"){
					index = 3;
					dif = 2;
				}
				}
			}
			
			
			//if(this.currentVideo != undefined){
			if(this.currentVideo.childNodes[index] != undefined){
				if(this.currentVideo.childNodes[index].attributes != undefined){
				var html="";
				if(this.currentVideo.childNodes[index].attributes[0] != undefined){
					if(this.currentVideo.childNodes[index].attributes[0].nodeValue != ""){
						html+="<span><a href='"+this.currentVideo.childNodes[index].attributes[0].nodeValue+"'>Download PDF</a></span>";
					}
				}
			
				var html2 = "";
				if(p_obj.childNodes[index] != undefined){
					if(p_obj.childNodes[index].firstChild != undefined){
						html2 = p_obj.childNodes[index].firstChild.nodeValue;
						el2.update(html+="<b>"+p_obj.childNodes[index - dif].firstChild.nodeValue+"</b>");
					}
				}
			
				el.update(html2);
			
				if(html == "" && html2 ==""){
					el.setVisible(false);
					el2.setVisible(false);
				}else{
					el.setVisible(true);
					el2.setVisible(true);
				}
				}
			}else{
				el.setVisible(false);
				el2.setVisible(false);
			}
			//}
		},
		
		showProps: function(p_obj){
			var str = "";
			for(var n in p_obj){
				str += "N :: "+n+" ::: "+p_obj[n]+"\n ";//
			}
			alert(str);
		}
	};
	
}();
Ext.EventManager.onDocumentReady(VideoListing.init, VideoListing, true);