$(document).ready(function()
	{
		
		/*=======================================
			FLOWPLAYER FUNCTIONS
		========================================*/
		
		
		
		/*=======================================
			YOUTUBE AJAX THUMBNAIL
		========================================*/
		
		$('a.youtube').livequery('click', function(event) {
		//	$('#myytplayer').show();
			$('#player').hide();
			
			
			var zzYoutubeThumbID = $(this).attr('rel');
			var zzYoutubeVidID = $(this).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
			
			
			return false;
		 });
		
		/*=======================================
			EDGECAST AJAX THUMBNAIL
		========================================*/
		
		$('a.edgecast').livequery('click', function(event) {
		//	$('#myytplayer').hide();
			$('#player').show();
			
			
			// get video ID from rel tag
			var zzEdgecastID = $(this).attr('rel');
			
			// get video mode from rel tag
			zzMode = $(this).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
			
			return false;
			
		 });

		/*=======================================
			PLAYLIST FUNCTIONS
		========================================*/
		
		
		
		function parsePlaylistXml(xml)
		{
			$("#related_videos").empty();
			//
			
			//alert('parseplaylist');
			
			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 i = 0;
			
			$(xml).find("video").each(function(){
				
				
										   
				zzVal 				= $(this).attr("id"); 
				zzType 				= $(this).attr("isYoutube");
				zzYoutubeID			= $(this).find("youtube_id").text();
				zzYoutubeTitle 		= $(this).find("title").text();
				zzMode				= '';
				
				if(zzType == 'false'){
					zzStreaming = $(this).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);
				
				
				
				
				$(this).find("low").each(function(){
					
					if(zzType == 'true'){
						zzThumb = $("#related_videos").append("<li><div class='video_thumb'><a href='#' 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='#' 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
				});// 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(){
				
				
				$(this).find("video").each(function(){
													
					$(this).find("title").each(function(){
						zzEdgecastTitle = $(this).text();
						$("#title").empty();
						zzHeader.push(zzEdgecastTitle);
						$("#title").append(zzHeader[0]);
					});
					
					$(this).find("description").each(function(){
						zzEdgecastDesc = $(this).text();
						$("#desc").empty();
						zzDesc.push(zzEdgecastDesc);
						$("#desc").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
			
		}//	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(){
				
				$(this).find("title").each(function(){
					zzYTEdgecastTitle = $(this).text();
					$("#title").empty();
					zzYTHeader.push(zzYTEdgecastTitle);
					$("#title").append(zzYTHeader[0]);
				});
				
				$(this).find("description").each(function(){
					zzYTEdgecastDesc = $(this).text();
					$("#desc").empty();
					zzYTDesc.push(zzYTEdgecastDesc);
					$("#desc").append(zzYTDesc[0]);
				});
				
			
			});// $xml
			
		}//parseYoutubeXml
		
		
		/*==========================================================	
			 PLAYLIST ONLICK
		==========================================================*/
		
		
		$('a.zzPlaylist').click( function(){
			
			$("#playlist_title").empty();
			$("#playlist_title").append($(this).text());
			
			var zzPlaylistID = $(this).attr('rel');
			//alert(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
				
		})
		
		return false;
	
	});// document ready