$(document).ready(function()
{
	
	var zzDescId = "#desc";
	var zzTitleId = "#title";
	var zzPlayerId = '#player';
	var zzPlaylist = "#playlist_title";
	
		/*=======================================
			FLOWPLAYER FUNCTIONS
		========================================*/
		
		
		
		/*=======================================
			YOUTUBE AJAX THUMBNAIL
		========================================*/
		
		$('a.youtube').live('click', function(event) {
		//	$('#myytplayer').show();
			
			var zzThis = this;
			$(zzPlayerId).fadeOut('slow');
			var zzYoutubeThumbID = $(zzThis).attr('rel');
			var zzYoutubeVidID = $(zzThis).attr('id');
			vision_loadNewVideo(zzYoutubeVidID, 0);
			
			
			$.ajax({// pull xml feed based on rel tag
				   
				type: 		"GET",
				url: 		"/themes/video/xml/feed.php?id=" + zzYoutubeThumbID,
				dataType: 	"xml",
				success: 	parseYoutubeXml
				
			});//ajax
			
		 });//a.youtube
		
		/*=======================================
			EDGECAST AJAX THUMBNAIL
		========================================*/
		
		$('a.edgecast').live('click', function(event) {
			var ttEdgeCast = this;
			$(zzPlayerId).fadeIn('slow');
			var zzEdgecastID = $(ttEdgeCast).attr('rel');
			var zzSEO			= $(ttEdgeCast).attr('seo');
			
			// get video mode from rel tag
			zzMode = $(ttEdgeCast).attr('mode');
			
			$.ajax({// pull xml feed based on rel tag
				   
				type: 		"GET",
				url: 		"/themes/video/xml/feed.php?id=" + zzEdgecastID,
				dataType: 	"xml",
				success: 	parseEdgecastXml
				
			});//ajax
			
		 });

		/*=======================================
			PLAYLIST FUNCTIONS
		========================================*/
		
		
		
		function parsePlaylistXml(xml)
		{
			$("#related_videos").empty();
			//
			
			//alert('parseplaylist');
			
			var zzSeoArray			= new Array();
			var zzThumbArray 		= new Array();
			var zzThumbYoutubeID 	= new Array();
			var zzThumbYoutubeTitle = new Array();
			var zzModeArray 		= new Array();
			var zzStreaming 		= '';
			var zzVal 				= '';
			var zzType 				= '';
			var zzMode				= '';
			var zzSEO				= '';

			var i = 0;
			
			$(xml).find("video").each(function(){
				
				var ttXML			= this;
										   
				zzVal 				= $(ttXML).attr("id"); 
				zzType 				= $(ttXML).attr("isYoutube");
				zzYoutubeID			= $(ttXML).find("youtube_id").text();
				zzYoutubeTitle 		= $(ttXML).find("title").text();
				zzMode				= '';
				zzSEO 				= $(ttXML).find("title").attr("seo");
				//alert(zzSEO);
				
				if(zzType == 'false'){
					zzStreaming = $(ttXML).find("source").attr('isStreaming');
					if(zzStreaming == 'true'){
						zzMode 		= 'streaming';
					}else{
						zzMode 		= 'progressive';
					}//else
				}//if
				
			
				
				zzThumbArray.push(zzVal);
				zzThumbYoutubeID.push(zzYoutubeID);
				zzThumbYoutubeTitle.push(zzYoutubeTitle);
				zzModeArray.push(zzMode);
				zzSeoArray.push(zzSEO);
			
				$(ttXML).find("low").each(function(){
					
					
					
					if(zzType == 'true'){
						zzThumb = $("#related_videos").append("<li><div class='video_thumb'><a href='#/" + zzSeoArray[i] + "' seo='"+ zzSeoArray[i] +"' id='"+ zzThumbYoutubeID[i] +"' class='youtube' rel='" + zzThumbArray[i] + "'><img src='" + $(this).text() + "' width='130' height='80' /></a><div id='vid_desc'>" + zzThumbYoutubeTitle[i] + "</div></div></li>");
					}else{
						zzThumb = $("#related_videos").append("<li><div class='video_thumb'><a href='#/"+ zzSeoArray[i] +"' seo='"+ zzSeoArray[i] +"' onclick='return myytplayer.pauseVideo()' class='edgecast' rel='" + zzThumbArray[i] + "' mode='" +  zzModeArray[i] + "'><img src='" + $(this).text() + "' alt='' width='135' height='80' /></a><div id='vid_desc'>" + zzThumbYoutubeTitle[i] + "</div></div></li>");
					}//else
					
					$('div.video_thumb').hide();
					$('div.video_thumb').fadeIn('slow');
					
				});// find thumbnail
				
				i++;
			});// $xml
			
		}// parsePlaylistXML
		
		
		var zzMode = '';
		var zzProvider = '';
		
		function parseEdgecastXml(xml)
		{
			var zzHeader = new Array();
			var zzDesc = new Array();
			var zzEdgecastTitle = '';
			var zzEdgecastDesc = '';
			
			$(xml).find("FenderVision").each(function(){
				
				var ttXMLFenderVision = this;
				
				$(ttXMLFenderVision).find("video").each(function(){
													
					$(ttXMLFenderVision).find("title").each(function(){
						zzEdgecastTitle = $(this).text();
						var z = $(this).attr("seo");
						$(zzTitleId).empty();
						zzHeader.push([zzEdgecastTitle, z]);
						$(zzTitleId).append(zzHeader[0][0]);
					});
					
					$(ttXMLFenderVision).find("description").each(function(){
						zzEdgecastDesc = $(this).text();
						$(zzDescId).empty();
						zzDesc.push(zzEdgecastDesc);
						$(zzDescId).append(zzDesc[0]);
					});
					
				
				});
					
						
					
				
				/*==========================================================
					IF STREAMING VIDEO
				==========================================================*/
				
				if(zzProvider == 'streaming'){
					flowplayer("player", "/themes/video/flash/flowplayer.commercial-3.1.5.swf", { 
						
						key: '#@6c7901c1cc25d13e065',
					
						clip: { 
							url: 		$(this).find("source").text(), 
							autoPlay: 	true,
							provider: 	'rtmp' 
						},  
						
						plugins: { 
							rtmp: {
								url: 				'/themes/video/flash/flowplayer.rtmp-3.1.3.swf',
								netConnectionUrl: 	'rtmp://fms.077D.edgecastcdn.net/00077D', 
								durationFunc: 		'getStreamLength' 
							}, 
							controls: { 
								buttonOverColor: 		'#cccccc',
								volumeSliderGradient: 	'none',
								tooltipColor: 			'#000000',
								buttonColor: 			'#333333',
								tooltipTextColor: 		'#ffffff',
								progressColor: 			'#666666',
								bufferGradient: 		'none',
								bufferColor: 			'#000000',
								backgroundColor: 		'#4f4f4f',
								sliderColor: 			'#000000',
								durationColor: 			'#ffffff',
								borderRadius: 			'0',
								timeColor: 				'#ffffff',
								volumeSliderColor: 		'#000000',
								timeBgColor: 			'#555555',
								height: 				25,
								opacity: 				1.0,
								fullscreen: 			true
								
							}// controls
							
						}//plugins
						
					});// flowplayer
				}
				/*==========================================================
					IF PROGRESSIVE VIDEO
				==========================================================*/
				else{
					flowplayer("player", "/themes/video/flash/flowplayer.commercial-3.1.5.swf", { 
						
						key: '#@6c7901c1cc25d13e065',
					
						clip: { 
							url: 		$(this).find("source").text(), 
							autoPlay: 	true
						},  
						
						plugins: { 
							rtmp: {
								url: 				'/themes/video/flash/flowplayer.rtmp-3.1.3.swf',
								netConnectionUrl: 	'rtmp://fms.077D.edgecastcdn.net/00077D', 
								durationFunc: 		'getStreamLength' 
							}, 
							controls: { 
								buttonOverColor: 		'#cccccc',
								volumeSliderGradient: 	'none',
								tooltipColor: 			'#000000',
								buttonColor: 			'#333333',
								tooltipTextColor: 		'#ffffff',
								progressColor: 			'#666666',
								bufferGradient: 		'none',
								bufferColor: 			'#000000',
								backgroundColor: 		'#4f4f4f',
								sliderColor: 			'#000000',
								durationColor: 			'#ffffff',
								borderRadius: 			'0',
								timeColor: 				'#ffffff',
								volumeSliderColor: 		'#000000',
								timeBgColor: 			'#555555',
								height: 				25,
								opacity: 				1.0,
								fullscreen: 			true
								
							}// controls
							
						}//plugins
						
					});// flowplayer
	
				}//else
				
			});// xml find video
			
			(zzHeader[0][1] != null) ? pageTracker._trackPageview('video=/edgecast/'+zzHeader[0][1]) : null;
			
			$(zzTitleId).hide();
			$(zzTitleId).fadeIn('slow');
			
			$(zzDescId).hide();
			$(zzDescId).fadeIn('slow');
			
		}//	parseEdgecastXml
		
		function parseYoutubeXml(xml)
		{
			
			var zzYTHeader = new Array();
			var zzYTDesc = new Array();
			var zzYTEdgecastTitle = '';
			var zzYTEdgecastDesc = '';
			/*==========================================================
				EMPTY FLOW PLAYER
			==========================================================*/
			$f("player", "", ""); 
			
			/*==========================================================
				PARSE XML, EMPTY TAGS, POPULATE TAGS
			==========================================================*/
			
			$(xml).find("video").each(function(){
				
				var ttXMLVideo = this; 
				
				$(ttXMLVideo).find("title").each(function(){
					zzYTEdgecastTitle = $(this).text();
					var z = $(this).attr("seo");
					$(zzTitleId).empty();
					zzYTHeader.push([zzYTEdgecastTitle, z]);
					$(zzTitleId).append(zzYTHeader[0][0]);
					
				});
				
				$(ttXMLVideo).find("description").each(function(){
					zzYTEdgecastDesc = $(this).text();
					$(zzDescId).empty();
					zzYTDesc.push(zzYTEdgecastDesc);
					$(zzDescId).append(zzYTDesc[0]);
				});
				
			
			});// $xml
			
			$(zzTitleId).hide();
			$(zzTitleId).fadeIn('slow');
			
			$(zzDescId).hide();
			$(zzDescId).fadeIn('slow');
			
			(zzYTHeader[0][1] != null) ? pageTracker._trackPageview('video=/youtube/'+zzYTHeader[0][1]) : null;
			
		}//parseYoutubeXml
		
		
		/*==========================================================	
			 PLAYLIST ONLICK
		==========================================================*/
		
		
		$('a.zzPlaylist').click( function(){
			
			var zzThis = this;
			$(zzPlaylist).empty();
			$(zzPlaylist).append($(zzThis).text());
			
			var zzPlaylistID = $(zzThis).attr('rel');
			pageTracker._trackPageview('video=/playlist/'+zzPlaylistID);
			$.ajax({// pull xml feed based on rel tag 
				   
				type: 		"GET",
				url: 		"/themes/video/xml/feed.php?mode=playlist&id=" + zzPlaylistID,
				dataType: 	"xml",
				success: 	parsePlaylistXml
				
			});//ajax
				
		})
		
		
	
	});// document ready