var Audio = Class.create();
// Define global vars for Audio class
Audio.wmc = 45; // height of Windows Media Controls
Audio.ieStatus = 19;
Audio.ffStatus = 24;

Audio.prototype = {
	initialize: function(options) {
		// These are the defaults. Don't change them.
		this.options = Object.extend({
			id: 'dpMedia',
			element: null,
			append: true,
			width: 300,
			height: Audio.wmc,
			url: '',
			params: {},
			playClass: 'a.AudioLink'
		}, options);
		this.options.params = Object.extend({
			autoPlay: true,
			uiMode: 'full',
			statusBar: false
		});
		// End defaults
		
		// bind to any anchor tags in the page
		$$(this.options.playClass).invoke('observe', 'click', 
			this.changeURL.bindAsEventListener(this));
		
		
		this.render();
	},
	
	getHTML: function() {
    //alert(this.options.id);
		var html = '\n' +
			'<div id="' + this.options.id + '">\n' +
			this.getCoreHTML() + '\n' +
			'</div>\n';
		return html;

	},
	
	getCoreHTML: function() {
		var blnAStart = (this.options.params.autoPlay) ? 'true' : 'false';
		var intAStart = (this.options.params.autoPlay) ? '1' : '0';
		var statusBar = (this.options.params.statusBar) ? '1' : '0';
		var w = this.options.width; 
		var h = this.options.height + (this.options.params.statusBar ? 
			(Prototype.Browser.IE ? Audio.ieStatus : Audio.ffStatus) : 0);
		var ts = (new Date()).getTime();
		
		var html = '\n' +
			'	<object classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6"\n' +
			'		width="' + w + '" height="' + h + '" id="' + this.options.id + 'Obj">\n' + //
			'		<param name="URL" value="' + this.options.url + '?ts='+ ts + '"/>\n' +
			'		<param name="AutoStart" value="' + blnAStart + '"/>\n' +
			'		<param name="uiMode" value="' + this.options.params.uiMode + '"/>\n' +
			'		<embed type="application/x-mplayer2"\n' +
			'			id="' + this.options.id + 'Emb"\n' +
			'			pluginspage="http://www.microsoft.com/Windows/Downloads/Contents/MediaPlayer/"\n' +
			'			src="' + this.options.url + '?ts='+ ts + '"\n' +
			'			autostart="' + intAStart + '"\n' +
			'			showstatusbar="' + statusBar + '"\n' +
			'			showcontrols="1"\n' +
			'			showdisplay="1"\n' +
			'			stretchToFit="1"\n' +	
			'			width="' + w + '" height="' + h + '" />\n' +  
			'	</object>\n';
		return html;
	},

	render: function(elmnt, append) {
		//alert(elmnt);
        //alert(append);
		if (this.options.element != null) { 
			// An element was passed in. We'll insert our code in that element
			// If append mode is true, add the html code to the element specified. Otherwise,
			// just replace the contents with our code.
			if (this.options.append) new Insertion.Bottom($(this.options.element), this.getHTML());
			else $(this.options.element).update(this.getHTML());
		}
		
		else { 
			// No element was specified, so just document.write the code in the current spot
			document.write(this.getHTML());
		}
		
	},

	changeURL: function(event) {
		Event.stop(event);
		var a = Event.element(event);
		if ('A' != a.tagName) a = a.up('a');
		this.options.url = a.href;
		$(this.options.id).update(this.getCoreHTML());
	}

}
