function onYouTubePlayerReady(playerId) {
	rugby_ytplayer = document.getElementById("myrugby_ytplayer");
	techno_ytplayer = document.getElementById("mytechno_ytplayer");
//	rugby_ytplayer.addEventListener("onError", "onrugby_ytplayerError");
//	techno_ytplayer.addEventListener("onError", "ontechno_ytplayerError");
	if( jQuery("#loading").text() == 'false' ) {
		jQuery("#loading").text("true");
	}
	else { // 2 players means this gets loaded twice
		load_videos();
	}
}

//function onrugby_ytplayerError(errorCode) {
//}

//function ontechno_ytplayerError(errorCode) {
//}

function load_videos() {
	if (rugby_ytplayer && techno_ytplayer) {
		var data = new Object(); 
		var query = location.hash.split('#');
		if( query[1] ) {
			var video_ids = query[1].split('x');
			data.rugby_id = video_ids[0];
			data.techno_id = video_ids[1];
		}
		var dataString = $.toJSON(data); 
		jQuery.post(
			'/load/',
			{ data: dataString },
			function( responseText ) { 
				var json_a = $.evalJSON( responseText );
				if ( json_a.reply == "yep" ) {
					rugby_ytplayer.loadVideoById(json_a.rugby.youtube_id,0);
					rugby_ytplayer.mute();
					techno_ytplayer.loadVideoById(json_a.techno.youtube_id,0);
					techno_ytplayer.setVolume(100);
					var rugby_id = json_a.rugby.video_id;
					jQuery("#rugby_id").text(rugby_id);
					jQuery("#rugby_youtube_id").text(json_a.rugby.youtube_id);
					jQuery("#rugby_title").text(json_a.rugby.title);
					jQuery("#rugby_status").text(json_a.rugby.status);
					var techno_id = json_a.techno.video_id;
					jQuery("#techno_id").text(techno_id);
					jQuery("#techno_youtube_id").text(json_a.techno.youtube_id);
					jQuery("#techno_title").text(json_a.techno.title);
					jQuery("#techno_status").text(json_a.techno.status);
					jQuery("#rugby_views").text(json_a.rugby.views);
					jQuery("#rugby_rads").text(json_a.rugby.rad);
					jQuery("#rugby_not_rads").text(json_a.rugby.not_rad);
					jQuery("#techno_views").text(json_a.techno.views);
					jQuery("#techno_rads").text(json_a.techno.rad);
					jQuery("#techno_not_rads").text(json_a.techno.not_rad);
					jQuery("#share_link").val("http://rugbytechno.com#" + rugby_id + 'x' + techno_id);
					window.location.hash = '#' + rugby_id + 'x' + techno_id;
					document.title = "Rugby Techno = Rugby Highlights (" + jQuery("#rugby_title").text() + ') + Techno Music (' + jQuery("#techno_title").text() + ')';
				}
			}
		);
		reset_buttons();
	}
}

function next() {
	window.location.hash = '';
	load_videos();
	jQuery("#try_it").text("Try It!");
}

function try_it() {
	if (rugby_ytplayer && techno_ytplayer) {
		var rugby_youtube = jQuery('#rugby_youtube').val();
		var techno_youtube = jQuery('#techno_youtube').val();

		jQuery("#try_it").text("Try It!");

		// FIXME
		// this is silly
		// should really do both at once

		if( rugby_youtube != '' && rugby_youtube != "Rugby Youtube Link" ) { 
			if( jQuery("#try_it").text() == "Try It!" )
				jQuery("#try_it").text("Trying...");
			var data = new Object(); 
			data.youtube = rugby_youtube;
			data.type = 'rugby';
			var dataString = $.toJSON(data); 
			jQuery.post(
				'/try/',
				{ data: dataString },
				function( responseText ) { 
					var json_a = $.evalJSON( responseText );
					if ( json_a.validation == "OK" ) { // play
						rugby_ytplayer.loadVideoById(json_a.rugby.youtube_id,0);
						rugby_ytplayer.mute();
						techno_ytplayer.seekTo(0, true);
						var techno_id = jQuery("#techno_id").text();
						var rugby_id = json_a.rugby.video_id;
						jQuery("#rugby_id").text(rugby_id);
						jQuery("#rugby_youtube_id").text(json_a.rugby.youtube_id);
						jQuery("#rugby_title").text(json_a.rugby.title);
						jQuery("#rugby_views").text(json_a.rugby.views);
						jQuery("#rugby_rads").text(json_a.rugby.rad);
						jQuery("#rugby_not_rads").text(json_a.rugby.not_rad);
						jQuery("#share_link").val("http://rugbytechno.com#" + rugby_id + 'x' + techno_id);
						window.location.hash = '#' + rugby_id + 'x' + techno_id;
						document.title = "Rugby Techno = Rugby Highlights (" + jQuery("#rugby_title").text() + ') + Techno Music (' + jQuery("#techno_title").text() + ')';
					}
					else {
						jQuery("#try_it").text( json_a.validation );
					}
				}
			);
		}
		
		if( techno_youtube != '' && techno_youtube != "Techno Youtube Link" ) {
			if( jQuery("#try_it").text() == "Try It!" )
				jQuery("#try_it").text("Trying...");
			var data = new Object(); 
			data.youtube = techno_youtube;
			data.type = 'techno';
			var dataString = $.toJSON(data); 
			jQuery.post(
				'/try/',
				{ data: dataString },
				function( responseText ) { 
					var json_a = $.evalJSON( responseText );
					if ( json_a.validation == "OK" ) { // play
						techno_ytplayer.loadVideoById(json_a.techno.youtube_id,0);
						techno_ytplayer.setVolume(100);
						rugby_ytplayer.seekTo(0, true);
						var rugby_id = jQuery("#rugby_id").text();
						var techno_id = json_a.techno.video_id;
						jQuery("#techno_id").text(techno_id);
						jQuery("#techno_youtube_id").text(json_a.techno.youtube_id);
						jQuery("#techno_title").text(json_a.techno.title);
						jQuery("#techno_views").text(json_a.techno.views);
						jQuery("#techno_rads").text(json_a.techno.rad);
						jQuery("#techno_not_rads").text(json_a.techno.not_rad);
						jQuery("#share_link").val("http://rugbytechno.com#" + rugby_id + 'x' + techno_id);
						window.location.hash = '#' + rugby_id + 'x' + techno_id;
						document.title = "Rugby Techno = Rugby Highlights (" + jQuery("#rugby_title").text() + ') + Techno Music (' + jQuery("#techno_title").text() + ')';
					}
					else {
						jQuery("#try_it").text( json_a.validation );
					}
				}
			);
		}
		reset_buttons();
	}
}

function toggle() {
	if (rugby_ytplayer && techno_ytplayer) {
		if (rugby_ytplayer.getPlayerState() == 1) {
			rugby_ytplayer.pauseVideo();
			techno_ytplayer.pauseVideo();
			jQuery("#toggle").text("Play");
		}
		else {
			rugby_ytplayer.playVideo();
			techno_ytplayer.playVideo();
			jQuery("#toggle").text("Pause");
		}
	}
}

function save_rad(rugby_id, techno_id) {
	if( !jQuery('#rad').is('.selected') && !jQuery('#rad').is('.disabled') )
	{
		horn('airhorn-stereo');
		jQuery("#rugby_rads").text(parseInt(jQuery("#rugby_rads").text())+1);
		jQuery("#techno_rads").text(parseInt(jQuery("#techno_rads").text())+1);
		var data = new Object(); 
		data.rugby_id = rugby_id;
		data.techno_id = techno_id;
		var dataString = $.toJSON(data); 
		jQuery.post(
			'/rad/',
			{ data: dataString },
			function( responseText ) { 
				var json_a = $.evalJSON( responseText );
				if ( json_a.reply == "yep" ) {
//					jQuery('#rad').text( 'Super Rad!' );
					jQuery('#rad').addClass('selected');
					jQuery('#not_rad_rugby').addClass('disabled');
					jQuery('#not_rad_techno').addClass('disabled');
				}
			}
		);
	}
}

function save_not_rad(video_id, type) {
	if( !jQuery('#rad').is('.selected') && !jQuery('#rad').is('.disabled') )
	{
		if( type == 'rugby' ) {
			horn('foghorn');
			jQuery("#rugby_not_rads").text(parseInt(jQuery("#rugby_not_rads").text())+1);
		}
		else {
			horn('foghorn2');
			jQuery("#techno_not_rads").text(parseInt(jQuery("#techno_not_rads").text())+1);
		}
		var data = new Object(); 
		data.video_id = video_id;
		var dataString = $.toJSON(data); 
		jQuery.post(
			'/not_rad/',
			{ data: dataString },
			function( responseText ) { 
				var json_a = $.evalJSON( responseText );
				if ( json_a.reply == "yep" ) {
					jQuery('#rad').addClass('disabled');
					if( type == 'rugby' ) {
						jQuery('#not_rad_rugby').addClass('selected');
						jQuery('#not_rad_techno').addClass('disabled');
					}
					else {
						jQuery('#not_rad_rugby').addClass('disabled');
						jQuery('#not_rad_techno').addClass('selected');
					}
				}
			}
		);
	}
}

function reset_buttons() {
	jQuery("#toggle").text("Pause");
	jQuery('#rad').text('Rad!');
	jQuery('#not_rad_rugby').text('Not Enough Rugby');
	jQuery('#not_rad_techno').text('Not Enough Techno');
	jQuery('#rad').removeClass('disabled').removeClass('selected');
	jQuery('#not_rad_rugby').removeClass('disabled').removeClass('selected');
	jQuery('#not_rad_techno').removeClass('disabled').removeClass('selected');
	document.getElementById("horns").innerHTML="";
}

function view(youtube_id) {
	window.location = "http://youtube.com/watch?v=" + youtube_id;
}

function twitter_dat() {
	window.location = "http://twitter.com/?status=I+saw+Rugby+and+Techno+collide%3A+http%3A%2F%2Frugbytechno.com%23" + jQuery("#rugby_id").text() + "x" + jQuery("#techno_id").text() + "+%40rugbytechno";
}

function facebook_dat() {
	window.location = "http://www.facebook.com/sharer.php?u=http%3A%2F%2Frugbytechno.com%23" + jQuery("#rugby_id").text() + "x" + jQuery("#techno_id").text() + "&t=I+saw+Rugby+and+Techno+collide";
}

function horn(sound) {
   document.getElementById("horns").innerHTML='<embed src="/wav/' + sound + '.wav" autostart="true" loop="false" volume="100" hidden="true" />';
   return true;
}
