HtmlPlayer.fullReset = function() {
	$("a#playLink").unbind('click');
	$("a#soundLink").unbind('click');

	/* Create player */
	if (null == HtmlPlayer.model) {
		/* Use the default HtmlPlayer */
		HtmlPlayer.model = 'HtmlPlayer';
	}
	HtmlPlayer.flash = new FlashObject("/video/"+HtmlPlayer.model+".swf", "htmlPlayer", HtmlPlayer.width, HtmlPlayer.height, "7.0.00", "#000000", true);
	HtmlPlayer.flash.addParam("allowScriptAccess","always");
	HtmlPlayer.flash.write("playa");
	HtmlPlayer.movie = document.getElementById("htmlPlayer");

	/* Init controls */
	HtmlPlayer.duration = 0;
	HtmlPlayer.isMute = false;
	HtmlPlayer.isPlaying = false;
	HtmlPlayer.isTabOpen = false;
	HtmlPlayer.isPreroll = true;
	HtmlPlayer.monitorInterval = setInterval(monitorStream, 500); /* Don't make this faster or AS and JS will clash */
	HtmlPlayer.movie.SetVariable("playStatus","none");
	s.Media.autoTrack = false; /* Manually track video */

	/* Init player */
	HtmlPlayer.movie.SetVariable("streamTime","0");
	HtmlPlayer.movie.SetVariable("userVolume","50");
	HtmlPlayer.setDuration = function(duration) {
		HtmlPlayer.duration = duration;
		HtmlPlayer.movie.SetVariable("nsDuration",duration);
	};

	HtmlPlayer.init = function() {
		HtmlPlayer.movie.SetVariable("autostart",HtmlPlayer.autostart);
		if (null != HtmlPlayer.preroll) {
			HtmlPlayer.setDuration(HtmlPlayer.preroll.duration);
			HtmlPlayer.movie.SetVariable("PREROLL_URL",HtmlPlayer.preroll.flv);
			$("img#timeScrub").draggable('disable');
			if (null != HtmlPlayer.preroll.clickUrl) {
				HtmlPlayer.movie.SetVariable("adURL",HtmlPlayer.preroll.clickUrl);
			}
			if ('4:3' == HtmlPlayer.preroll.aspect) {
				HtmlPlayer.movie.SetVariable("videoWidth",HtmlPlayer.height*4/3); /* Standard */
			} else if ('16:9' == HtmlPlayer.preroll.aspect) {
				HtmlPlayer.movie.SetVariable("videoWidth",HtmlPlayer.height*16/9); /* Wide */
			}
			s.Media.open(HtmlPlayer.preroll.flv,HtmlPlayer.preroll.duration,HtmlPlayer.model);
		}
		if (null != HtmlPlayer.pcwVideo) {
			HtmlPlayer.movie.SetVariable("FLV_URL",HtmlPlayer.pcwVideo.flv);
			if (null == HtmlPlayer.preroll) {
				initPcwVideo();
			}
		}
	};

	function initPcwVideo() {
		HtmlPlayer.setDuration(HtmlPlayer.pcwVideo.duration);
		if ('4:3' == HtmlPlayer.pcwVideo.aspect) {
			HtmlPlayer.movie.SetVariable("videoWidth",HtmlPlayer.height*4/3);
		} else if ('16:9' == HtmlPlayer.pcwVideo.aspect) {
			HtmlPlayer.movie.SetVariable("videoWidth",HtmlPlayer.height*16/9);
		}
		HtmlPlayer.movie.SetVariable("adURL",undefined);
		HtmlPlayer.isPreroll = false;
		s.Media.open(HtmlPlayer.pcwVideo.flv,HtmlPlayer.pcwVideo.duration,HtmlPlayer.model);
	}

	/* Play button */
	$("a#playLink").click(function(event){
		if(HtmlPlayer.isPlaying) {
			stopPlaying();
		} else {
			startPlaying();
		}
		event.preventDefault();
	});
	function startPlaying() {
		HtmlPlayer.movie.SetVariable('JsCommand', 'startPlaying');
		$("img#playButton").css({'display' : 'none'});
		$("img#pauseButton").css({'display' : 'inline'});
		HtmlPlayer.isPlaying = true;
		if (null != HtmlPlayer.startTrackCallback) {
			HtmlPlayer.startTrackCallback();
			HtmlPlayer.startTrackCallback = null;
		}
		if (HtmlPlayer.isPreroll) {
			s.Media.play(HtmlPlayer.preroll.flv,HtmlPlayer.movie.GetVariable('streamTime'));
		} else {
			s.Media.play(HtmlPlayer.pcwVideo.flv,HtmlPlayer.movie.GetVariable('streamTime'));
		}
	}
	function stopPlaying() {
		HtmlPlayer.movie.SetVariable('JsCommand', 'stopPlaying');
		$("img#playButton").css({'display' : 'inline'});
		$("img#pauseButton").css({'display' : 'none'});
		HtmlPlayer.isPlaying = false;
		if (HtmlPlayer.isPreroll) {
			s.Media.stop(HtmlPlayer.preroll.flv,HtmlPlayer.movie.GetVariable('streamTime'));
		} else {
			s.Media.stop(HtmlPlayer.pcwVideo.flv,HtmlPlayer.movie.GetVariable('streamTime'));
		}
	}
	HtmlPlayer.stopPlaying = stopPlaying; /* Make available outside this script */
	HtmlPlayer.startPlaying = startPlaying;
	/* Mute button */
	$("a#soundLink").click(function(event){
		if(HtmlPlayer.isMute) {
			startSound();
		} else {
			stopSound();
		}
		event.preventDefault();
	});
	function startSound() {
		HtmlPlayer.movie.SetVariable('JsCommand', 'startSound');
		$("img#soundOnButton").css({'display' : 'inline'});
		$("img#soundOffButton").css({'display' : 'none'});
		HtmlPlayer.isMute = false;
	}
	function stopSound() {
		HtmlPlayer.movie.SetVariable('JsCommand', 'stopSound');
		$("img#soundOnButton").css({'display' : 'none'});
		$("img#soundOffButton").css({'display' : 'inline'});
		HtmlPlayer.isMute = true;
	}

	/* Scrubbers */
	function getScrubPercent(scrubName) {
		return ($("img#"+scrubName).offset().left-$("div#"+scrubName+"Bg").offset().left)/
			($("div#"+scrubName+"Bg").width() - $("img#"+scrubName).width())*100;
	}

	/* Sound scrubber */
	$("img#soundScrub").draggable({ axis: 'x', containment: 'parent', stop: function(event, ui) {changeSound();}});
	function changeSound() {
		HtmlPlayer.movie.SetVariable("userVolume", getScrubPercent("soundScrub"));
		startSound();
	}

	/* Time scrubber */
	$("img#timeScrub").draggable({ axis: 'x', containment: 'parent',
		stop: function(event, ui) {changeTime();},
		start: function(event, ui) {clearInterval(HtmlPlayer.monitorInterval);}});
	function changeTime() {
		percent = getScrubPercent("timeScrub");
		HtmlPlayer.movie.SetVariable("seekToPercent", percent * 95 / 100); /* Don't go all the way forward or AS chokes */
		updateTimeScrub(percent * HtmlPlayer.duration / 100);
		HtmlPlayer.monitorInterval = setInterval(monitorStream, 500);
	}
	function monitorStream() {
		updateTimeScrub(HtmlPlayer.movie.GetVariable('streamTime'));
		playStatus = HtmlPlayer.movie.GetVariable('playStatus');
		if ('started' == playStatus) {
			HtmlPlayer.movie.SetVariable("playStatus","playing");
			startPlaying();
		} else if ('done' == playStatus) {
			stopPlaying();
			HtmlPlayer.movie.SetVariable("seekToPercent", 0);
			updateTimeScrub(0);
			HtmlPlayer.movie.SetVariable("playStatus","reset");
			if (!HtmlPlayer.isPreroll) {
				s.Media.close(HtmlPlayer.pcwVideo.flv);
			}
			if (null != HtmlPlayer.stopTrackCallback) {
				HtmlPlayer.stopTrackCallback();
				HtmlPlayer.stopTrackCallback = null;
			}
		} else if ('prerollDone' == playStatus) {
			s.Media.close(HtmlPlayer.preroll.flv);
			initPcwVideo();
			startPlaying();
			HtmlPlayer.movie.SetVariable("seekToPercent", 0);
			updateTimeScrub(0);
			$("img#timeScrub").draggable('enable');
			HtmlPlayer.movie.SetVariable("playStatus","reset");
		}
	}
	function updateTimeScrub(streamTime) {
		ns_seconds = HtmlPlayer.duration - streamTime;
		if (ns_seconds < 0) {
			ns_seconds = 0;
		}
		minutes = Math.floor(ns_seconds/60);
		seconds = Math.floor(ns_seconds%60);
		if (seconds < 10) {
			seconds = "0"+seconds;
		}
		$("div#timeText").html(minutes+":"+seconds);
		progress = streamTime / HtmlPlayer.duration * ($("div#timeScrubBg").width() - $("img#timeScrub").width());
		$("img#timeScrub").css({'left':progress});
	}
};