var VideoListing = function(){
	var ie;
	if (document.all) ie = true;
	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];
			}
			
			var listType="ul";
			//if(p_xml.attributes[1].nodeValue != "num"){
			//	listType="ul";
			//}
			
			//el.createChild({tag:"div"});
			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";
				//console.log(str);
				
				var obj = p_xml.childNodes[i];
				
				if(obj.nodeName == "video"){
					var index = 1;

					if (document.all){
						index = 0;
					}
					
					var htmStr = "";
					htmStr += "<span><b>("+obj.attributes[0].nodeValue+")</b></span>";
					htmStr += "<b>"+obj.childNodes[index].childNodes[0].nodeValue+"</b>";
					
					var itm = holder.createChild({tag:"li", html:htmStr});
					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.currentVideo = videoArray[0];
			this.currentVideoElement = videoArrayElements[0];
			
			if(isLoaded){
				this.startCurrentVideo();
			}
			//}
		},
		
		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(p_fn.data);
			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){
			//console.log(p_obj.childNodes[3]);
			var index = 3;
			if(document.all){
				index = 1;
			}
			
			var el = Ext.get('videoDesc');
			if(p_obj.childNodes[index] != undefined){
				el.update(p_obj.childNodes[index].firstChild.nodeValue);
			}else{
				el.update("");
			}
		},
		
		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);