if(typeof(Delicious) == 'undefined') Delicious = {}
Delicious.Mp3 = {
	playimg: null,
	player: null,
	go: function() {
		var all = document.getElementsByTagName('a')
		for (var i = 0, o; o = all[i]; i++) {
			if(o.href.match(/\.audio$/i)) {
				var img = document.createElement('img')
				if(o.name.match(/\http/i)) {
					var credtemp = o.name.split("^");
					var cred = '<a class="audiosource" href="' + credtemp[0] + '" target="_blank">' + credtemp[1] + '</a>';
				}
				else var cred = o.name				
				if(cred == 'preview') {
					img.src = '/images/v8/miniplay.gif'
					img.width = 75
					img.height = 13
				}
				else if(cred == 'front') {
					img.title = 'Audio Clip (' + (o.href.replace(".audio","")).replace("http://www.dubyaspeak.com/","") + ')'
					img.src = '/images/v8/newplay.gif'
					img.width = 104
					img.height = 17
					document.getElementById('afterbit').style.width='2px';
				}
				else {
					img.title = 'Audio Clip (' + (o.href.replace(".audio","")).replace("http://www.dubyaspeak.com/","") + ')'
					img.src = '/images/v8/newplay.gif'
					img.width = 104
					img.height = 17
				}
				img.style.border = 'none'
				img.style.verticalAlign = 'text-bottom'
				img.style.cursor = 'pointer'
				img.onclick = Delicious.Mp3.makeToggle(img, o.href.replace(".com",".com/mp3"), cred)
				o.parentNode.insertBefore(img, o)
	}}},
	toggle: function(img, url, cred) {
		if (Delicious.Mp3.playimg == img) Delicious.Mp3.destroy(cred)
		else {
			if (Delicious.Mp3.playimg) Delicious.Mp3.destroy(cred)
			var a = img.nextSibling, c=''
			if (document.defaultView && document.defaultView.getComputedStyle) c = document.defaultView.getComputedStyle(a,null).getPropertyValue('color')
			else if (a.currentStyle) c = a.currentStyle['color']
			c = c.replace(/^rgb|[ ()]/g, '')
			var hex = c.match(/^#?([a-f0-9]{6})$/), hexSm = c.match(/^#?([a-f0-9]{3})$/), rgb = c.match(/^([0-9]+),([0-9]+),([0-9]+)$/)
			if (rgb) { var c16 = "0123456789abcdef", c=''; for(var i=1,d;d=rgb[i];i++) { var j=d%16; c=c+c16.charAt((d-j)/16)+c16.charAt(j) } }
			else if (hex) c = hex[1]
			else if (hexSm) c = hexSm[1].charAt(0) + hexSm[1].charAt(0) + hexSm[1].charAt(1) + hexSm[1].charAt(1) + hexSm[1].charAt(2) + hexSm[1].charAt(2)
			else c = "000000"
			if(cred == 'preview') img.src = '/images/v8/ministop.gif'
			else if(cred == 'front') {
				img.src = '/images/v8/newstop.gif'
				document.getElementById('afterbit').style.width='1px';
			}
			else img.src = '/images/v8/newstop.gif'
			Delicious.Mp3.playimg = img
			Delicious.Mp3.player = document.createElement('span')
			Delicious.Mp3.player.innerHTML = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"' +
			'codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0"' +
			'width="1" height="1" id="player">' +
			'<param name="wmode" value="transparent" />' +
			'<param name="allowScriptAccess" value="sameDomain" />' +
			'<param name="flashVars" value="theLink='+url.replace(".audio",".mp3")+'&amp;fontColor='+c+'" />' +
			'<param name="movie" value="/images/v8/playtagger.swf" /><param name="quality" value="high" />' +
			'<embed src="/images/v8/playtagger.swf" flashVars="theLink='+url.replace(".audio",".mp3")+'&amp;fontColor='+c+'"'+
			'quality="high" wmode="transparent" width="1" height="1" name="player"' +
			' allowScriptAccess="sameDomain" type="application/x-shockwave-flash"' +
			' pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>' + 
			(cred!='preview' && cred!='front' ? '<small class="audiosource" style="position:relative;left:2px;">[Source: ' + cred + ']</small>' : '')
			img.parentNode.insertBefore(Delicious.Mp3.player, img.nextSibling)
	}},
	destroy: function(cred) {
		if(cred=='preview') Delicious.Mp3.playimg.src = '/images/v8/miniplay.gif';
		else if(cred == 'front') {
			Delicious.Mp3.playimg.src = '/images/v8/newplay.gif';
			document.getElementById('afterbit').style.width='2px';
		}
		else Delicious.Mp3.playimg.src = '/images/v8/newplay.gif'; 
		Delicious.Mp3.playimg = null
		Delicious.Mp3.player.removeChild(Delicious.Mp3.player.firstChild); Delicious.Mp3.player.parentNode.removeChild(Delicious.Mp3.player); Delicious.Mp3.player = null
	},
	makeToggle: function(img, url, cred) { return function(){ Delicious.Mp3.toggle(img, url, cred) }}
}

Delicious.addLoadEvent = function(f) { 
	var old = window.onload
	if (typeof old != 'function') window.onload = f
	else { 
		window.onload = function() { old(); f() }
	}
}

// Push user to #1 in the page when a highlighted item is present,
// without borking the audio clip links
var hiidRegex1 = /^\/([a-z]{3,})\/(200[1-8])\/goto\/([0-9]{1,4})$/
var hiidRegex2 = /^\/([a-z]{3,})\/goto\/([0-9]{1,4})$/
if(window.location.pathname.match(hiidRegex1) || window.location.pathname.match(hiidRegex2)) {
	Delicious.addLoadEvent(setTimeout("window.location.hash='jump'",500))
}
Delicious.addLoadEvent(Delicious.Mp3.go)


