/*
	Windows Media Player JS Engine
	Copyright (C) 200, 2007. Chonla.

	Version: 2.2.6
	Author: Chonla
	Update: 17 apr. 2007

	More infomation, visit: http://www.siamexperts.net/
*/
function $wmplayer (wmpid, propbag)
{
	var w = 
	{
		mute_flag: false,
		wm_player: null,
		cur_stream: "",
		original_width: 0,
		original_height: 0,
		constructor: function(wmpid)
		{
			var o = document.getElementById(wmpid);
			if ((o == null) || (typeof(o)=="undefined"))
			{
				o = document.createElement("OBJECT");
				o.setAttribute("classid","CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6");
				o.setAttribute("id",wmpid);
				o.setAttribute("uiMode","none");
				o.settings.autoStart = "false";
				document.appendChild(o);
			}
			this.mute_flag = o.settings.mute;
			this.wm_player = o;
			this.original_width = w.wm_player.clientWidth;
			this.original_height = w.wm_player.clientHeight;
		},
		loadurl: function(furl)
		{
			this.cur_stream = furl;
			this.wm_player.url = this.cur_stream;
		},
		play: function(furl)
		{
			if (this.wm_player!=1 && this.wm_player!=10)	// stop playing back before playing
			{
				this.wm_player.controls.stop();
				this.release();
			}
			if (furl) this.loadurl(furl);
			this.wm_player.controls.play();
		},
		next: function()
		{
			if (this.wm_player.controls.isAvailable('Next'))
	            this.wm_player.controls.next();
		},
		previous: function()
		{
			if (this.wm_player.controls.isAvailable('Previous'))
	            this.wm_player.controls.previous();
		},
		stop: function()
		{
			if (this.wm_player.controls.isAvailable('Stop'))
				this.wm_player.controls.stop();
		},
		pause: function()
		{
			if ((!this.ispause()) && this.wm_player.controls.isAvailable('Pause'))
				this.wm_player.controls.pause();
		},
		unpause: function()
		{
			if (this.ispause())
				this.wm_player.controls.play();
		},
		mute: function()
		{
			this.mute_flag = true;
			this.wm_player.settings.mute = this.mute_flag;
		},
		unmute: function()
		{
			this.mute_flag = false;
			this.wm_player.settings.mute = this.mute_flag;
		},
		bindevent: function(eventname, callbackname)
		{
			eval("this.wm_player.attachEvent(eventname, " + callbackname + ");");
		},
		ismute: function()
		{
			return this.mute_flag;
		},
		ispause: function()
		{
			return (this.wm_player.playState == 2) ;
		},
		volumeup: function(offset)
		{
			var _offset = 5;
			if (offset) _offset = offset;
			this.volumeto(this.wm_player.settings.volume + _offset);
		},
		volumedown: function(offset)
		{
			var _offset = 5;
			if (offset) _offset = offset;
			this.volumeto(this.wm_player.settings.volume - _offset);
		},
		volumeto: function(level)
		{
			var _level = 50;
			if (level < 0)
				_level = 0;
			else if (level > 100)
				_level = 100;
			else
				_level = level;
			this.wm_player.settings.volume = _level;
		},
		volume: function()
		{
			return (this.wm_player.settings.volume);
		},
		getduration: function()
		{
			return (this.wm_player.currentMedia.duration);
		},
		getcurrentposition: function()
		{
			return (parseInt(Math.ceil(this.wm_player.controls.currentPosition)));
		},
		setcurrentposition: function(newpos)
		{
			this.wm_player.controls.currentPosition = newpos;
		},
		getcurrentpositionstring: function()
		{
			//currentPositionString is buggy
			var t = this.getcurrentposition();
			var s = t % 60;
			var m = parseInt((t - s) / 60) % 60;
			var h = parseInt((t - ((m * 60) + s)) / 360);
			var x = "";
			if (h>0) x = h + ":";
			if (m<10) x += "0";
			x += (m + ":");
			if (s<10) x+="0";
			x += s;
			return x;
		},
		getdurationstring: function()
		{
			return (this.wm_player.currentMedia.durationString);
		},
		getmediaattribute: function(name)
		{
			return (this.wm_player.currentMedia.getItemInfo(name));
		},
		status: function()
		{
			return (this.wm_player.playState);
		},
		isidle: function()
		{
			return (this.wm_player.playState == 10||this.wm_player.playState == 0||this.wm_player.playState == 1);
		},
		release: function()
		{
			try { if (!this.isidle()) this.wm_player.stop(); }
			catch (e) {	}
			try { this.wm_player.close(); }
			catch (e) { }
		},
		zoom: function(percent)
		{
			var controlHeight = 0;
			var playHeight = this.original_height;
			var playWidth = this.original_width;
			switch (w.wm_player.uiMode)
			{
				case "full":
					controlHeight = 64;
					break;
				case "mini":
					controlHeight = 45;
					break;
				default:
					controlHeight = 0;
			}
			playHeight -= controlHeight;
			playWidth = parseInt(playWidth * percent / 100);
			playHeight = parseInt(playHeight * percent / 100) + controlHeight;
			this.zoomTo(playWidth, playHeight);
		},
		zoomTo: function(newWidth, newHeight)
		{
			w.wm_player.width = newWidth;
			w.wm_player.height = newHeight;
			w.wm_player.stretchToFit = true;
		},
		fullScreen: function()
		{
			if(w.wm_player.uiMode!="none" && w.wm_player.playState == 3) 
				w.wm_player.fullScreen = 'true';
		},
		getobject: function()
		{
			return w.wm_player;
		}
	};
	w.constructor(wmpid);
	return w;
}
