function parseDecimal(d, zeros, trunc){
	d=d.replace(/[a-zA-Z\!\@\#\$\%\^\&\*\(\)\_\+\-\=\{\}\|\[\]\\\:\"\;'\<\>\?\,\/\~\`]/g,"");
	while (d.indexOf(".") != d.lastIndexOf(".")){
		d=d.replace(/\./,"");
	}
	if (typeof zeros == 'undefined' || zeros == "") {
		return parseFloat(d);
		}
	else {
		var mult = Math.pow(10,zeros);
		if (typeof trunc === 'undefined' || (trunc) === false){
			return parseFloat(Math.round(d*mult)/mult);
		}
		else {
			return parseFloat(Math.floor(d*mult)/mult);
		}
	}
}
	
EGG = {
	init: function(){
	},
	
	show_video: function(video_url, seconds){
		video_url = video_url.replace("__", "=");
		video_url = video_url.replace("__", "=");
		video_url = video_url.replace("__", "=");
		video_url = video_url.replace("__", "=");
		video_url = video_url.replace("__", "=");
		video_url = video_url.replace("__", "=");
		
		video_url = video_url.replace("~", "&");
		video_url = video_url.replace("~", "&");
		video_url = video_url.replace("~", "&");
		video_url = video_url.replace("~", "&");
		video_url = video_url.replace("~", "&");

		width = 601;
		height = 338;
		var embed = '<object width="' + width + '" height="' + height + '"><param name="wmode" value="transparent"></param><param name="movie" value="http://swf.tubechop.com/tubechop.swf?' + video_url + '&autoplay=true"></param><embed src="http://swf.tubechop.com/tubechop.swf?' + video_url + '&autoplay=true" type="application/x-shockwave-flash" allowfullscreen="true" width="' + width + '" height="' + height + '" wmode="transparent"></embed></object>';
		$("#video").html(embed);
		$("#playhead").show();
		$("#video").fadeIn();
		$("#video object").fadeIn();
		$("#video embed").fadeIn();
		
		EGG.timer.start(seconds);
	},
	
	show_final_screen: function(){
		$("#flash-content").hide();
		$("#video").hide();
		$("#playhead").hide();
		$("#final").fadeIn();
	},
	
	timer: {
		start: function(seconds){
			EGG.timer.target_seconds = seconds;
			EGG.timer.current_seconds = 0;
			setTimeout(EGG.timer.tick, 1000);
			playhead_width = parseDecimal($("#playhead").css("width").split("px")[0]);
			// console.log("playhead_width: " + playhead_width);
			// console.log("seconds: " + seconds);
			EGG.timer.tick_pixels = playhead_width / seconds;
			// console.log("tick_pixels: " + EGG.timer.tick_pixels);
		},
		
		tick: function(){
			playhead_marker =  $("#marker");
			var left = parseDecimal(playhead_marker.css("left").split("px")[0]);
			left = left + EGG.timer.tick_pixels;
			playhead_marker.css("left", left + "px");
			// console.log("playhead_marker.css.left: " + left);
			if(EGG.timer.current_seconds < (EGG.timer.target_seconds - 1)){
				setTimeout(EGG.timer.tick, 1000);
				EGG.timer.current_seconds++;				
			}
			else {
				EGG.show_final_screen();
			}
		}
	}
};

$(document).ready(function(){
	EGG.init();
});

