$(document).ready( function() {
	$('#slides > li:first').show();
	$('#slides > li:first').addClass('active');
	var slide_count = $('#slides > li').length;
	var slide_speed = 7000;
	var include_slide_navigation_control = true;
	$('#comment-call-to-action').click( function() {
		$(this).hide();
		$('#comment-form').show();
		return false;
	});
	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="' + _g_bp_base_url + '/images/content/home/slide-checkbox-checked.png" width="20" height="20" alt="' + i + '" /></li>';
				} else
				{
					html += '<li><a href="#' + this_index + '"><img src="' +  _g_bp_base_url +'/images/content/home/slide-checkbox-unchecked.png" width="20" height="20" alt="' + i + '" /></a></li>';
				}
			}
			html += '</ul>';
			if ( include_slide_navigation_control)
				$(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_speed);		
		}

		$('#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();
	}
	// &callback=showMyVideos
	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>';
		// console.log(data);
		for ( var i = 0; i < data.feed.entry.length; i++)
		{
			var s = data.feed.entry[i];
			var e = {
				title: s.title.$t,
				img: s.media$group.media$thumbnail[0].url,
				rating: (s.gd$rating) ? s.gd$rating.average : 0,
				views: s.yt$statistics.viewCount,
				uploaded: s.media$group.yt$uploaded.$t,
				description: s.media$group.media$description.$t,
				description_type: 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;
			var link = 'http://www.youtube.com/user/waikrumma#p/c/0/' + e.video_id;
			html += '<li>';
			// html += '<a href="http://www.youtube.com/watch?v=' +
			// 			e.video_id + '" target="_blank">';
			html += '<a href="' +
						link + '" target="_blank">';
			html += '<img src="' + e.img + '" class="thumbnail" />';
 			html += '</a>';
			html += '<h3>';
			html += '<a href="' +
						link + '" 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);
	}
	/* uncomment to turn on videos
	if (( !$.browser.msie ) || ( $.browser.version > 6))
		$.getJSON("http://gdata.youtube.com/feeds/api/playlists/" +
			"525CA8C4782B2AC3" +
			"?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>.');
	*/
});


