var derbyvideo = {};

function createCookie(name,value,days) {
	var expires;
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		expires = "; expires="+date.toGMTString();
	}
	else {
		expires = "";
	}
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') {
			c = c.substring(1,c.length);
		}
		if (c.indexOf(nameEQ) == 0) {
			return c.substring(nameEQ.length,c.length);
		}
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function attach_resolution_clicks(){
	$('a#hi-res').click(
		function(){
			this.blur();
			createCookie('derbyvideo_size', 'hi-res', 0);
			$("a#lo-res").removeClass('active');
			$(this).addClass('active');
			$('#single')[0].loadFile(derbyvideo.current);
			return false;
		}
	);			
	$('a#lo-res').click(
		function(){
			this.blur();
			createCookie('derbyvideo_size', 'lo-res', 0);
			$("a#hi-res").removeClass('active');
			$(this).addClass('active');
			$('#single')[0].loadFile(derbyvideo.current);
			return false;
		}
	);			
}

function derbyvideoLoad(obj) {
	// I can't figure out how to play the current file on LOAD. This
	// function fires on LOAD, but when it fires, the old file is still
	// the "current" file, so any events sent to the controller apply
	// to the old file. So, we wait a second...
	setTimeout(function(){
		$('#single')[0].sendEvent("PLAY", true);
	}, 1000);
	
}

$(document).ready(
	function(){
		$('a.derbyvideo-play').click(
			function(){
				this.blur();
				if ($(this).is('.isPlaying')) {
					if ($(this).attr('href').indexOf('2009/videos') > -1) {
						// Toggle PLAY/PAUSE
						$('#single')[0].sendEvent("PLAY");
					}
				}
				else {
					// LOAD the new video
					$('a.derbyvideo-play').removeClass('isPlaying');
					$(this).addClass('isPlaying');
					
					if ($(this).attr('href').indexOf('youtube.com') < 0) {
						// Swap out FLV Player
						var aid = this.href.match(/videos\/([0-9]+)/)[1];
						derbyvideo.aid = aid;
						derbyvideo.current = {
								file: this.href.replace('/videos/', '/asset/') + '/playlist.xml'
						};
						$('#video-sidebar .now-playing:eq(0)').load('/2009/derbyvideos/' + aid + '/nowplaying');
						if ($('#single')[0]) {
							$('#single')[0].addViewListener("LOAD", 'derbyvideoLoad');
							$('#single')[0].sendEvent("LOAD", derbyvideo.current);
						}
						else {
							// FLV player needs to be re-embedded
							$('#video-player-large').html('<div id="player_'+ aid +'"></div>');
							var s1 = new SWFObject("/2009/sites/all/modules/digett/asset/contrib/asset_flv/jw_media_player/mediaplayer.swf","single","640","380","9", true);
						    s1.addParam("allowfullscreen", "true");
						    s1.addParam("wmode", "transparent");
						    s1.addVariable('lightcolor', '0c80cb');
							s1.addVariable('backcolor', '000000');
							s1.addVariable('frontcolor', 'CCCCCC');
							s1.addVariable('autostart', 'true');
							s1.addVariable('file', '/2009/asset/'+ aid +'/playlist.xml');
							//s1.addVariable('title', 'Morning Backside Buzz 4.25.09');
							s1.addVariable('image', 'http://kentuckyderby.atthepost.com/2009/sites/kentuckyderby.com/files/thumbnails/'+ aid +'.1.jpg');
							s1.addVariable('enablejs', 'true');
							s1.addVariable('plugins', 'googlytics-1');
							s1.write("player_"+ aid);
						}
					}
					else {
						// Swap out YouTube player
						var id = $(this).attr('href').split('=');
						id = id[id.length - 1];
						var YouTube = '<object width="640" height="360">';
						YouTube += '<param name="movie" value="http://www.youtube.com/v/'+ id +'"></param>';
						YouTube += '<param name="wmode" value="transparent"></param>';
						YouTube += '<embed src="http://www.youtube.com/v/'+ id +'" type="application/x-shockwave-flash" ';
						YouTube += 'wmode="transparent" width="640" height="360">';
						YouTube += '</embed></object>';
						$('#video-player-large').html(YouTube);
					}
				}
				return false;
			}
		);		
	}
);

