var MozillaFixes = {
	isGecko: /Gecko\/\d*/.test(navigator.userAgent)
}

var IEFixes = {
	// Opera handles document.all so we need to exclude it here
	isIE: (document.all && !(/Opera/.test(navigator.userAgent)) ? true : false)
}

var SafariFixes = {
	isMobileSafari: / AppleWebKit\/.+Mobile\//.test(navigator.userAgent),
	isWebKit: /WebKit/.test(navigator.userAgent),
	isTigerSafari: (/WebKit\/\d+/.test(navigator.userAgent) && (parseInt(navigator.userAgent.match(/WebKit\/(\d+)/)[1]) < 420))
}

var OperaFixes = {
	isOpera: /Opera/.test(navigator.userAgent)
}

var PodcastEpisode = Class.create();
PodcastEpisode.prototype = {
	mWidth: 303,
	mHeight: 16,
	mEmbedFrameID: 0,
	initialize: function(inElement) {
		this.handleTitleClick = this.handleTitleClick.bindAsEventListener(this);
		this.mElement = $(inElement);
		this.mElement.observe('click', this.handleTitleClick);
		if (Element.hasClassName(this.mElement, 'selecteditem')) this.addPlayer();
	},
	addPlayer: function() {
		if (SafariFixes.isMobileSafari) return false; // MobileSafari needs a different content encoding...
		var qtplayer = this.mElement.down('.qtplayer');
		if (!qtplayer) return false;
		var fullSrc = qtplayer.getAttribute('longdesc');
		if (!fullSrc) return false;
		// object HTML for non-FF browsers
		var objectHTML = '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" width="'+this.mWidth+'" height="16" codebase="http://www.apple.com/qtactivex/qtplugin.cab"><param name="SRC" value="/collaboration/fake.qti"><param name="QTSRC" value="'+fullSrc+'"><param name="TYPE" value="video/quicktime"><param name="SCALE" value="aspect"><param name="AUTOPLAY" value="false"><param name="CONTROLLER" value="true"><param name="TARGET" value="myself"><param name="BGCOLOR" value="#000000"></object>';
		// Firefox/Opera wants only an embed tag, and it has to be written using innerHTML. Also, have to hide image *first*
		if (MozillaFixes.isGecko || OperaFixes.isOpera) {
			qtplayer.innerHTML = '<embed src="fake.qti" qtsrc="'+fullSrc+'" type="video/quicktime" autoplay="false", controller="true" target="myself" bgcolor="#ffffff" width="'+this.mWidth+'" height="16" pluginspage="http://www.apple.com/quicktime/download/" scale="aspect" />';
		}
		// IE requires that the plugin be written using document.write at page load time, so we must create an iframe
		else if (IEFixes.isIE) {
			var frmID = 'qFrame'+(this.mEmbedFrameID++);
			var qFrame = new Element('iframe', {border:'0', frameborder:'0', id:frmID, name:frmID, style:'width:'+this.mWidth+'px;height:18px;border:0;overflow:hidden'});
			qtplayer.appendChild(qFrame);
			$(frmID).contentWindow.document.write('<html><body style="padding:0;margin:0;overflow:hidden">'+objectHTML.replace(/<object/, '<object id="qtmovie1"')+'</body></html>');
			var attempts = 0;
			var checkForPlayer = function() {
				try {
					$(frmID).contentWindow.document.qtmovie1.Play();
				}
				catch(e) {
					if (++attempts > 10) return false;
					setTimeout(checkForPlayer, 1000);
				}
			}
			setTimeout(checkForPlayer, 1000);
		}
		// Safari, others
		else {
			qtplayer.innerHTML = objectHTML;
		}
	},
	handleTitleClick: function(inEvent) {
		Event.stop(inEvent);
		var thisitemelm = this.mElement; // so we don't have to bind
		if (Element.hasClassName(thisitemelm, 'selecteditem')) return false; // bail if we're already selected
		// remove all existing players
		$$('#podcast iframe').invoke('remove');
		$$('#podcast object').invoke('remove');
		$$('#podcast embed').invoke('remove');
		// iterate through each of the podcast items
		$$('#podcast .item').each(function(pcitemelm) {
			if (pcitemelm == thisitemelm) { // this item
				pcitemelm.addClassName('selecteditem');
			}
			else { // other items
				pcitemelm.removeClassName('selecteditem');
			}
		});
		var timeoutVal = (IEFixes.isIE || MozillaFixes.isGecko ? 10 : 600);
		setTimeout(this.addPlayer.bind(this), timeoutVal);
		return false;
	}
}
PodcastEpisode.collection = $A([]);

window.onload = function() {
	$$('#podcast .item').each(function(pcitemelm) {
		PodcastEpisode.collection.push(new PodcastEpisode(pcitemelm));
	});
}
