// date parsing - 2008-04-07T05:11:29.000-07:00
function parseDate(vidDate){
	splitDate = vidDate.split('T',2)[0];
	splitTime = vidDate.split('T',2)[1];
	t_date = splitDate.split('-')[2]
	t_mon = splitDate.split('-')[1]
	t_year = splitDate.split('-')[0]
	pubDate = t_mon + '/' + t_date + '/' + t_year;
	pubDate = new Date(pubDate);
		p_date = pubDate.getDate();	
		p_mon_name = new Array ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');		
		p_mon = p_mon_name[pubDate.getMonth()];
		p_year = pubDate.getFullYear();
	pubDate = p_mon + ' ' + p_date + ', ' + p_year;
}

//LIST ALL VIDEOS
	function listVideos(root) {
	 feed = root.feed;
	 entries = feed.entry || [];
	 
	 //counts		 
	 total = feed.openSearch$totalResults.$t;
	 results = feed.openSearch$startIndex.$t;
	 results = parseInt(results)
	 max_results = 29;
	 next_results = results + max_results;
	 if (next_results > total){next_results = total;}
	 paginate = 'Displaying ' + results + '-' + next_results + ' of ' + total;  

	 var html = '';
	 if (total == 0) {html += '<h3>There are no videos to display</h3>';}
	
	 for (i = 0; i < entries.length; ++i) {
			entry = entries[i];
			title = entry.title.$t;
			published = entry.published.$t;			
			lenDesc = 32;
			if (title.length > lenDesc) {
				title = title.substring(0, lenDesc);
				title = title.replace(/\w+$/, '');
				title += '...';
			}
			
			link = (entry['link'][0].href);
			id = link.replace('http://www.youtube.com/watch?v=','');
			thumb = (entry.media$group['media$thumbnail'][0].url);
			duration = (entry.media$group['yt$duration'].seconds);
			
		//get duration
			seconds = duration / 60;
			duration = Math.round(seconds*100)/100; duration += '';
			d_min = Math.round(duration -.5);
			d_sec = duration.substring(2);
			d_sec = Math.round((d_sec/100)*60);
			if (d_sec < 10){d_sec = '0' + d_sec;}
			
			html += '<div class="float_l border yt_gallery m5r m5b p5 module_content">';
				html += '<a href="' +domain + id+ '"><img class="border m5b" src="' +thumb+ '"></img></a>';
				html += '<h3 class="m5b"><a href="' +domain + id+ '">'  + title + ' (' + d_min + ':' + d_sec + ')</a>' + '</h3>';
				html += '<div class="timestamp">'; 
				parseDate(published);
				html += pubDate;
				html += '</div>';

			html += '</div>';
	 }
	 
	 if(next_results >= total){
	 document.getElementById("next_link").style.display = 'none';
	 document.getElementById("next_link_foot").style.display = 'none';
	 }
	 
	 document.getElementById("pages").innerHTML += paginate;
	 document.getElementById("pages_foot").innerHTML += paginate;
	 document.getElementById("videos").innerHTML += html;
	}	
	
//SECTION FRONT MODULE
	function listVideos_module(root) {
	 feed = root.feed;
	 entries = feed.entry || [];	 
	 total = feed.openSearch$totalResults.$t; 

	 var html = '';
	 html += '<div class="clearfix">';
	 if (total == 0) {html += '<h3>There are no videos to display</h3>';}
	 for (i = 0; i < entries.length; ++i) {
			entry = entries[i];
			title = entry.title.$t;
			published = entry.published.$t;	
			lenDesc = 30;
			if (title.length > lenDesc) {
				title = title.substring(0, lenDesc);
				title = title.replace(/\w+$/, '');
				title += '...';
			}					
			
			link = (entry['link'][0].href);
			id = link.replace('http://www.youtube.com/watch?v=','');
			thumb = (entry.media$group['media$thumbnail'][0].url);
			duration = (entry.media$group['yt$duration'].seconds);
			
		//get duration
			seconds = duration / 60;
			duration = Math.round(seconds*100)/100; duration += '';
			d_min = Math.round(duration -.5);
			d_sec = duration.substring(2);
			d_sec = Math.round((d_sec/100)*60);
			if (d_sec < 10){d_sec = '0' + d_sec;}
			
			parseDate(published);
			
			
			html += '<div class="float_l border yt_gallery_mod m5l m5b p5 module_content">';
				html += '<div><a href="' +domain + id+ '"><img width="77px" height="60px" class="border" src="' +thumb+ '"></img></a></div>';
				html += '<div><b><a href="' +domain + id+ '">'  + title + ' (' + d_min + ':' + d_sec + ')</a>' + '</b></div>';
				html += '<div class="timestamp p5v">' +pubDate+ '</div>'; 

			html += '</div>';
	 }
	 html += '</div>';
	 document.getElementById("vid_module").innerHTML += html;
	}		
 

//DEFAULT VIDEO	  
	function defaultVideo(feature) {
	 feed = feature.feed;
	 entries = feed.entry || [];	 	
	 entry = entries[0];		 
		title = entry.title.$t;
		published = entry.published.$t;
		summary = entry.content.$t;
		duration = (entry.media$group['yt$duration'].seconds);
		embedLink = (entry.media$group['media$content'][0].url);
		embedLink += '&enablejsapi=1&playerapiid=ghsvid';

		head = '<h1>'  + title + '</h1>';
		parseDate(published);
		params = { allowScriptAccess: "always", wmode: "transparent" };
		atts = { id: "ghsplayer" };
		swfobject.embedSWF(embedLink, "embed", "320", "267", "8", null, null, params, atts);		

		document.title=title;
		document.getElementById("feature_vid_title").innerHTML += head;	
		document.getElementById("timestamp").innerHTML += pubDate;
		document.getElementById("content").innerHTML += summary;
	}

//FEATURED VIDEO	  
	function featureVideo(feature) {
	 entry = feature.entry;		 
		title = entry.title.$t;
		published = entry.published.$t;
		summary = entry.content.$t;
		duration = (entry.media$group['yt$duration'].seconds);
		embedLink = (entry.media$group['media$content'][0].url);
		embedLink += '&enablejsapi=1&playerapiid=ghsvid';
		
	/*	if(entry.gd$rating.average){
			rating = entry.gd$rating.average;
			rating = Math.round(rating);
			document.getElementById("rated").innerHTML += rating;	
			}
			
		if(entry.yt$statistics.viewCount){
			views = entry.yt$statistics.viewCount + ' views ';
			document.getElementById("views").innerHTML += views;	
			}
	*/		

		head = '<h1>'  + title + '</h1>';
		parseDate(published);
		params = { allowScriptAccess: "always", wmode: "transparent" };
		atts = { id: "ghsplayer" };
		swfobject.embedSWF(embedLink, "embed", "320", "267", "8", null, null, params, atts);		

		document.title=title;
		document.getElementById("feature_vid_title").innerHTML += head;	
		document.getElementById("timestamp").innerHTML += pubDate;
		document.getElementById("content").innerHTML += summary;
		
	}
	
// FOR API VIDEO CONTROLS	
	function onYouTubePlayerReady(playerId) {
		ghsvid = document.getElementById("ghsplayer");
	}			

	
