jQuery.fn.youtubeplayer = function(data) {
	var config = {
		playlist_id: 'F7FCAE37E0DCFDD9',
		videos_per_page: 3,
		width: 425,
		height: 344,
		max_teaser_length: 70,
		thumb_height: 65,
		elem: this.get(0),
		color_scheme: 'green'
	};
	if (data) $.extend(config, data);
	if (this.length > 1) {
		$(this).eq(0).html('<div id="player">&nbsp;</div><div id="playlist"><div id="loading"><img src="/assets/img/ajax-loader.gif" width="66" height="66" alt="Loading video playlist" /><br/><br/>Loading video playlist&hellip;</div></div>');
	} else {
		$(this).html('<div id="player">&nbsp;</div><div id="playlist"><div id="loading"><img src="/assets/img/ajax-loader.gif" width="66" height="66" alt="Loading video playlist" /><br/><br/>Loading video playlist&hellip;</div></div>');
	}
	$.youtubeplayer.init(config);
};
$.youtubeplayer = {
	config: {},
	playlist: new Array(),
	num_pages: 0,
	video_id: null,
	thumbs: new Array(),
	init: function(config) {
		this.config = config;
		var script  = document.createElement('script');
		script.type = 'text/javascript';
		script.src  = 'http://gdata.youtube.com/feeds/api/playlists/'+config.playlist_id+'?alt=json-in-script&v=2&callback=jQuery.youtubeplayer.build_player&start-index=1';;
		document.documentElement.firstChild.appendChild(script);
	},
	get_video_id: function(url) {
	   var a = url.split("=");
	   var video_id = (a) ? a[1].replace(/\&feature/, '') : null;
	   return video_id;
	},
	build_player: function(json_data) {
		if (json_data.feed.entry) {
			var plyr = this;
			$.each(json_data.feed.entry, function(i, item) {
				for (var k=0; k<item.link.length; k++) {
					if (item.link[k].rel == 'alternate') {
						url = item.link[k].href;
						break;
					}
				}
				var video_id = plyr.get_video_id(url);
				var title = item.title.$t;
				var tmb = new Image();
				if (typeof(item.media$group.media$thumbnail) != 'undefined') {
					var thumb = item.media$group.media$thumbnail[1].url;
					var width = parseInt(item.media$group.media$thumbnail[1].width);
					var height = parseInt(item.media$group.media$thumbnail[1].height);
					tmb.src = thumb;
				} else {
					tmb.src = '/assets/img/spacer.gif';
					var width = 0;
					var height = 0;
				}
				plyr.playlist.push({video_id: video_id, title: title, thumb: thumb, thmb_width: width, thmb_height: height});
				plyr.thumbs.push(tmb)
			});
			this.num_pages = Math.ceil(this.playlist.length / this.config.videos_per_page);
			$('#playlist').html('');
			this.build_playlist(1);
		}
	},
	build_playlist: function(page_num) {
		if (this.playlist.length > 0) {
			$('#playlist .item, #playlist #controls').hide().remove();
			var start_row = (page_num - 1) * this.config.videos_per_page;
			var cnt = 1;
			var playlist_html = '';
			for(var p=start_row; p<this.playlist.length; p++) {
				if (cnt > this.config.videos_per_page) break;
				var title = this.playlist[p].title.replace(/'/g,"&#39;");
				var link_title = this.playlist[p].title.replace(/'/g,"\\'");
				var item = '<div class="item" id="'+this.playlist[p].video_id+'">';
				item += '		<a href="javascript:$.youtubeplayer.play(\''+this.playlist[p].video_id+'\', 1, \''+link_title+'\');" title="'+title+'">';
				item += '			<img onload="$(this).show()" src="'+this.thumbs[p].src+'" alt="'+this.playlist[p].title.replace(/"/g,'&quot;')+'" border="0"';
				if (this.config.thumb_height != null) {
					item += ' height="'+this.config.thumb_height+'"';
					var width = Math.round(parseInt(this.config.thumb_height)*this.playlist[p].thmb_width/this.playlist[p].thmb_height);
					item += ' width="'+width+'"';
				} else {
					item += ' height="'+this.playlist[p].thmb_height+'"';
					item += ' width="'+this.playlist[p].thmb_width+'"';
				}
				item += ' /></a>';
				item += '	<div class="title"><a href="javascript:$.youtubeplayer.play(\''+this.playlist[p].video_id+'\', 1, \''+link_title+'\');" title="'+title+'">'+title+'</a></div>';
				item += '	<div class="watch"><a href="javascript:$.youtubeplayer.play(\''+this.playlist[p].video_id+'\', 1, \''+link_title+'\');" title="Watch the Video">Watch the video</a></div>';
				item += '	<div class="clear">&nbsp;</div>';
				item += '</div>';
				playlist_html += item;
				cnt++;
			}
			$('#playlist').hide().html(playlist_html).fadeIn('fast');
		}
		var prev_page_num = (page_num > 1  && this.num_pages > 0) ? parseInt(page_num)-1 : 0;
		var next_page_num = (page_num < this.num_pages && this.num_pages > 0) ? parseInt(page_num)+1 : 0;
		var controls = '<table id="controls" width="100%" cellpadding="0" cellspacing="0" border="0"><tr valign="middle"><td class="prev_page" nowrap="1">';
		if (prev_page_num) {
			controls += '<a href="javascript:$.youtubeplayer.build_playlist(\''+prev_page_num+'\')" title="Previous Page">&laquo;</a>';
		} else {
			controls += '&nbsp;';
		}
		controls += '</td>';
		controls += '<td width="5%" class="pages" nowrap="1">';
		if (this.num_pages > 1) {
			controls += '<ul>';
			for (var pg = 1; pg <= this.num_pages; pg++) {
				var pg_class = (page_num == pg) ? 'class="sel"' : '';
				controls += '<li><a href="javascript:$.youtubeplayer.build_playlist(\''+pg+'\')" '+pg_class+' title="Page '+pg+'">'+pg+'</a></li>';
			}
			controls += '</ul>';
		} else {
			controls += '&nbsp;';
		}
		controls += '</td>';
		controls += '<td class="next_page" nowrap="1">';
		if (next_page_num) {
			controls += '<a href="javascript:$.youtubeplayer.build_playlist(\''+next_page_num+'\')" title="Next Page">&raquo;</a>';
		} else {
			controls += '&nbsp;';
		}
		controls += '</td></tr></table>';
		$('#playlist').append(controls);
		$('#playlist .item').hover(
			function(){$(this).addClass('item_hover')},
			function(){$(this).removeClass('item_hover')}
		);
		$('#playlist .item, #playlist #controls').show();
	},
	play: function(id, autoplay, title) {
		var html  = '';
		html += '<object width="'+this.config.width+'" height="'+this.config.height+'">';
		html += '<param name="movie" value="http://www.youtube.com/v/'+id+'&hl=en&fs=1&rel=0'+this.color()+'"></param>';
		html += '<param name="autoplay" value="'+autoplay+'">';
		html += '<param name="allowFullScreen" value="true"></param>';
		html += '<param name="wmode" value="transparent"></param>';
		html += '<embed src="http://www.youtube.com/v/'+id+'&hl=en&fs=1&rel=0'+this.color()+'&autoplay='+autoplay+'" wmode="transparent" type="application/x-shockwave-flash" allowfullscreen="true" width="'+this.config.width+'" height="'+this.config.height+'"></embed>';
		html += '</object>';
		this.video_id = id;
		$('#playlist .item').removeClass('item_sel');
		$('#playlist #'+id).addClass('item_sel');
		$('#player').html(html);
		tb_show(this.truncate(title, 9), '#TB_inline?height=350&width=450&inlineId=player');
		_gaq.push(['_trackPageview', '/video/'+id]);
	},
	truncate: function(str, max_length) {
		if (typeof(str) == 'string') {
			var words = str.split(' ');
			if (words.length > max_length) {
				var trunc_str = '';
				for (var w = 0; w < max_length; w++) {
					trunc_str += ' ' + words[w];
				}
				return trunc_str.substring(1) + '&hellip;';
			}
			else {
				return str;
			}
		} else {
			return '';
		}
	},
	color: function(){
		var color_str = '';
	    switch (this.config.color_scheme) {
			case 'darkgrey':  color_str = '&color1=0x3a3a3a&color2=0x999999'; break;
			case 'darkblue':  color_str = '&color1=0x2b405b&color2=0x6b8ab6'; break; 
			case 'lightblue': color_str = '&color1=0x006699&color2=0x54abd6'; break;
			case 'green':     color_str = '&color1=0x234900&color2=0x4e9e00'; break; 
			case 'orange':    color_str = '&color1=0xe1600f&color2=0xfebd01'; break;
			case 'pink':      color_str = '&color1=0xcc2550&color2=0xe87a9f'; break; 
			case 'purple':    color_str = '&color1=0x402061&color2=0x9461ca'; break;
			case 'red':       color_str = '&color1=0x5d1719&color2=0xcd311b'; break;
	    }
	    return color_str;
	}
};

