﻿var videos = {
	myYouTubeFeed: new Ajax("/en/experience/feeds/yt_videos.xml", {method: 'get'}).request(),
	youTubeVideo : [],
	nowPlaying: '',
	videoCount: 0,
	firstVideo: '',

	read : function() {
		// This is going to read the videos Array
		var youTubeXMLDoc = this.myYouTubeFeed.response.xml;
		var videoList = youTubeXMLDoc.getElementsByTagName('video_list')[0];
		var videos = videoList.getElementsByTagName('video');

		/*LOOP THRU YOU TUBE FEED AND MAKE ARRAY*/
		for(i=0; videos.length > i; i++){
			var nodeCount = videos[i].childNodes.length;
			this.youTubeVideo["video" + i] = new Array();
			for(n=0; nodeCount > n; n++){
				if(videos[i].childNodes[n].nodeType == 1){
					if(videos[i].childNodes[n].nodeName == "tags"){
						var cur_tags = videos[i].childNodes[n].firstChild.nodeValue.split(" ");
						this.youTubeVideo["video" + i][videos[i].childNodes[n].nodeName] = cur_tags;
					}else{
					   
					    if(videos[i].childNodes[n].firstChild){
						    this.youTubeVideo["video" + i][videos[i].childNodes[n].nodeName] = videos[i].childNodes[n].firstChild.nodeValue;
						 }
					}
				}
			}
		}
	},
	
	find : function() {
		// This will build out thumbs from XML array
		var thumbLayout = '';

		for (vid in this.youTubeVideo) {

			if (typeof(this.youTubeVideo[vid]) == 'object') {
				if (this.videoCount < 12) {
					thumbLayout += this.build(this.youTubeVideo[vid]);
				}
				this.videoCount++;
			}
		}

		$('video_thumbs').setHTML(thumbLayout);
		
		/* Set up Tool Tips For Video Thumbs */
        global.makeTips({x:-276,y:-110},'.video_link','video-tool');

		//Find and play first video.
		this.firstVideo = $('video_thumbs').getFirst().getAttribute('id');
		onload_register('videos.play("'+this.firstVideo+'")');
	},

	build : function(argItem) {
	    /*This code it to filter out " and ' and convert 
	    them to ASCII set so they wont break title tages.*/
	    var catchQuote = '&#34;';
	    var catchSingleQuote = '&#39;';
	    var curTitle = "<strong>" + argItem['title'] + ': <br />Length:' + argItem['length_seconds'] + '&nbsp;&nbsp;&nbsp;&nbsp;Views:' + argItem['view_count'] + '</strong><br /><br />' + argItem['description'];
	    var filterTitle1 = curTitle.replace(/"/g, catchQuote);
	    var filterTitle2 = filterTitle1.replace(/'/g, catchSingleQuote);
	    curTitle = filterTitle2;
	    
		return '<div id="video'+this.videoCount+'" class="video_thumb_link"><a class="video_link" title="' + curTitle + '" href="javascript:void(0);" name="&amp;lid='+argItem['title']+' Video&amp;lpos=Video Thumb Nav" onClick="videos.play(\'video'+this.videoCount+'\');"><img class="main_thumb" src="' + argItem['thumbnail_url'] + '" /></a></div>';
	},

	play : function(argVideo) {
		var FO = {
			movie:'http://www.youtube.com/v/'+ this.youTubeVideo[argVideo].id +'&rel=0&border=0',
			width:        "100%",
			height:       "245px",
			name:	  	   "youtube_video",
			id:		       "youtube_video",
			allowscriptaccess:"always",
			bgcolor:      "#FFFFFF",
			scale:        "noscale",
			wmode:        "transparent",
			play:         "true",
			quality:      "high",
			menu:         "false",
			majorversion: "8",
			build:        "0",
			xi:           "false",
			flashvars:    ""
		};
		UFO.create(FO, 'feature_video');

		$('video_title').setHTML(this.youTubeVideo[argVideo]['title']);
		$('video_length').setHTML("LENGTH: " + this.youTubeVideo[argVideo]['length_seconds']);
		$('video_views').setHTML("VIEWS: " + this.youTubeVideo[argVideo]['view_count']);
		//$('video_copy').setHTML(this.youTubeVideo[argVideo]['description']);
		this.nowPlaying = argVideo;
	}
}
