var theAudio;
var time;
var timeOut;

$(document).ready(function() {
	theAudio = genAudioElement("turkeypig");
	time = 3 * 60;
	tick();
	$(".timeAdjust").click(function () {
		var temp = this.href.split("#");
		time = temp[1] * 60;
		clearTimeout(timeOut);
		tick();

	});
});
	
function genAudioElement(name) {
	var tempAudioElement = document.createElement('audio');
	tempAudioElement.src = (tempAudioElement.canPlayType('audio/ogg;') ? name + '.ogg' : name + '.mp3');
	tempAudioElement.preload = "auto";
	tempAudioElement.innerHTML = "Get a better browser... hater";
	return tempAudioElement;
}

function tick() {
	$("#minutes").text(getMinutes());
	$("#seconds").text(getSeconds());
	
	time--;
	
	if(time >= 0)
		timeOut = setTimeout('tick()',1000);
	else
		theAudio.play();
}

function getMinutes() {
	// minutes is seconds divided by 60, rounded down, added leading zero if under 10
	mins = Math.floor(time / 60);
	if(mins <= 9)
		mins = "0" + mins;
		
	return mins;
}
function getSeconds() {
	// take mins remaining (as seconds) away from total seconds remaining, added leading zero if under 10 seconds.
	secs = time % 60;
	if(secs < 10) 
		secs = "0" + secs;
	
	return secs;
}




// Read a page's GET URL variables and return them as an associative array.
function getUrl()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }

    console.log(vars);
    
   // return vars;
}

!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="http://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");
