$(document).ready( function() {
	var slide_advance_time = 4000;
	$('#slides > li:first').show();
	$('#slides > li:first').addClass('active');
	var slide_count = $('#slides > li').length;
	if ( slide_count > 1) {
		var automatic_advance = true;
		$('#slides > li').each( function() {
			var html = '<ul>';
			var this_index = $('#slides > li').index($(this));
			for ( i = 0; i < slide_count; i++)
			{
				if ( i == this_index)
				{
					html += '<li><img src="images/content/global/white-checkbox-checked.png" width="20" height="20" alt="' + i + '" /></li>';
				} else
				{
					html = html + '<li><a href="#' + this_index + '"><img src="images/content/global/white-checkbox-unchecked.png" width="20" height="20" alt="' + i + '" /></a></li>';
				}
			}
			html = html + '</ul>';
			$(this).append( html);
		});
		var slide_advance_timer;
		function show_next_slide( timeout_id, next_slide_index)
		{
			var active_slide = $('#slides > li:animated');
			if ( active_slide.length == 0)
				active_slide = $('#slides > li.active');
			else
				active_slide.stop( true, true);

			var next_slide = null;
			if ( typeof next_slide_index == 'number') {
				automatic_advance = false;
				next_slide = $('#slides > li').eq( next_slide_index);
				if ( next_slide.length == 0) next_slide = null;
			}
			if ( next_slide != active_slide) {
				if ( next_slide == null) next_slide = $(active_slide).next();
				if ( next_slide.length == 0)
					next_slide = $('#slides > li:first');
				next_slide.css({opacity:0.0,zIndex:2,display:'block'});
				active_slide.css({zIndex: 1});
				next_slide.animate({opacity: 1.0}, 1500, null, function() {
					active_slide.removeClass('active');
					active_slide.css({display:'none'});
					next_slide.addClass('active');
				});
			}
			set_next_slide_timeout();
		}

		function set_next_slide_timeout() {
			clearTimeout(slide_advance_timer);
			if ( automatic_advance)
				slide_advance_timer = setTimeout( show_next_slide, slide_advance_time);		
		}

		$('#slides > li > ul > li > a').click( function(e)
		{
			var slide_li = $(this).parent().parent().parent();
			var slide_index = $('#slides > li').index( slide_li);
			show_next_slide(null, $('#slides > li:eq(' + slide_index + ') > ul > li').index($(this).parent()));
			return false;
		});
		set_next_slide_timeout();
	}
	function add_commas(nStr) {
		nStr += '';
			x = nStr.split('.');
			x1 = x[0];
			x2 = x.length > 1 ? '.' + x[1] : '';
			var rgx = /(\d+)(\d{3})/;
			while (rgx.test(x1)) {
				x1 = x1.replace(rgx, '$1' + ',' + '$2');
			}
			return x1 + x2;		
	}
	var youtube_callback = function( data, status) {
		var html = '<ul>';
		for ( var i = 0; i < data.feed.entry.length; i++)
		{
			var s = data.feed.entry[i];
			if ( s.app$control)
			{
				/* http://code.google.com/apis/youtube/2.0/reference.html#youtube_data_api_tag_app:control
				The tag's presence indicates that one of the following statements applies to a video:

				    * The video has not yet been published.
				    * The video is no longer publicly available.
				    * Access to view the video is restricted.
				    * The video is not available in the region where the user trying to watch the video is located.
				    * YouTube rejected the video during the video upload process.
				    * The request to upload the video failed.
				*/
			} else
			{
				var e = {
					title: s.title.$t,
					img:
						(s.media$group.media$thumbnail) ?
						 	s.media$group.media$thumbnail[0].url : '',
					rating:
						(s.gd$rating) ?
							s.gd$rating.average : 0,
					views:
						(s.yt$statistics) ?
							s.yt$statistics.viewCount : 0,
					uploaded:
						(s.media$group.yt$uploaded)?
							s.media$group.yt$uploaded.$t : false,
					description:
						(s.media$group.media$description) ?
							s.media$group.media$description.$t : '',
					description_type:
						(s.media$group.media$description) ?
							s.media$group.media$description.type : '',
					video_id: s.media$group.yt$videoid.$t
				};
				if (e.description.length > 50)
				{
					e.description = e.description.substr(0,49) + '…';
				}
				var rating = Math.floor( (e.rating + .25) * 2);
				var	star_top = 0;
				if ((rating % 2) == 1)
				{
					rating = rating + 1;
					star_top = -16;
				}
				rating = Math.floor( rating / 2);
				var star_left = (-75) + rating * 15;
				html += '<li>';
				html += '<a href="http://www.youtube.com/watch?v=' +
							e.video_id + '" target="_blank">';
				html += '<img src="' + e.img + '" class="thumbnail" />';
	 			html += '</a>';
				html += '<h3>';
				html += '<a href="http://www.youtube.com/watch?v=' +
							e.video_id + '" target="_blank">';
				html += e.title;
	 			html += '</a>';
				html += '</h3>';
				html += '<p>' + e.description + '</p>';
				html += '<p class="stats">';
				// var r = e.rating;
				// while ( r > 0) { html += '*'; r -= 1; }
				html += '<span class="stars" ' +
					'style="background-position: ' + star_left + 'px ' +
					star_top + 'px;' +
					'"></span>'
				html += ' ' + add_commas(e.views) + ' views';
				html += '</p>';
				html += '</li>';				
			}
		}
		html += '</ul>';
		$('#videos').html( html);
	}
	if (( !$.browser.msie ) || ( $.browser.version > 6))
		$.getJSON("http://gdata.youtube.com/feeds/api/playlists/" +
			"533684BEE0A9FFD5" +
			"?v=2&alt=json-in-script&callback=?&max-results=3",
			youtube_callback);
	else
		$('#videos').html( 'YouTube does not support Internet Explorer 6; <a href="http://code.google.com/chrome/chromeframe/">Get Google Chrome Frame</a>.');
});

