﻿/* =========== Media Tool By Javascript : VNVN_41 =========== *\
\* ===========    Copyright © 2007 www.vnvn.net   =========== */

var VNVN_MultiMediaControl = new Class();
inherit(VNVN_MultiMediaControl, 
{
	MEDIA_TYPE : 
	{
		WINMEDIA	: 0,
		REAL		: 1,
		QUICK		: 2,
		FLASH		: 3,
		YOUTUBE		: 4
	},

	_mediaExt :
	[
		".wma, .wmv, .mpeg, .mpeg4; .mpg, .wav, .mp3, .mp4, .avi, .asx, .asf, .dat, ",	/*For Windows Media Player*/
		".rm, .rmvb, .divx, .smil, ",													/*For Real*/
		".mov, .qt, .mqv, ",															/*For Quick*/
		".swf, "
	],

	/*classID, appType, codebase, plugingpage*/
	_embedData : 
	[
		[	
			"CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6", 
			"application/x-ms-wmp", 
			"http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=7,0,0,0", 
			"http://www.microsoft.com/Windows/MediaPlayer/", 
			"Browser cannot support Window Media Player"
		],	/* WIN 7 */
		[
			"CLSID:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA", 
			"audio/x-pn-realaudio-plugin", 
			"", 
			"",
			"Browser cannot support Real Player"
		],
		[
			"CLSID:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B", 
			"video/quicktime", 
			"http://www.apple.com/qtactivex/qtplugin.cab", 
			"http://www.apple.com/quicktime/download/",
			"Browser cannot support QuickTime"
		],
		[
			"CLSID:D27CDB6E-AE6D-11cf-96B8-444553540000", 
			"application/x-shockwave-flash", 
			"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0", 
			"http://www.macromedia.com/go/getflashplayer",
			"Browser cannot support Flash"
		],
		[
			"CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95",
			"application/x-mplayer", 
			"http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715", 
			"http://www.microsoft.com/Windows/MediaPlayer/", 
			"Browser cannot support Window Media Player"
		] /*WIN 6*/
	]
});

VNVN_MultiMediaControl.prototype =
{
	_id: "",
	_autoType: true,
	_mediaType: VNVN_MultiMediaControl.MEDIA_TYPE.WINMEDIA,
	_path: "",
	_width: 320,
	_height: 268,
	_autoPlay: true,
	_loop: false,
	_autoSize: false,
	_smallSize: false,

	_properties: function(arrStr)
	{
		var result = "";
		for (var i = 0; i < arrStr.length; i++)
			result += arrStr[i];
		return result;
	},

	_property: function(name, value)
	{
		if (Browser.IE)
			return " <param name='" + name + "' value='" + value + "' /> ";
		else
			return " " + name + "='" + value + "' ";
	},

	load: function()
	{
		if (!window.__isLoaded)
			window.onload = this._load.eventHandle(this, window.onload)
		else
			this._load();
	},

	_load: function(e, win)
	{
		if (win) win(e);
		var div = document.getElementById(this._id);
		if (div != null)
			div.innerHTML = this.createHTML();	
	},
	
	createHTML: function()
	{
		this._path = this._path.trim();
		if (this._autoType)
		{
			if (this._path.indexOf("http://www.youtube.com/") == 0 || this._path.indexOf("http://youtube.com/") == 0)
			{
				if (this._path.indexOf("watch?v") != -1)
					this._path = this._path.replace("watch?v=", "v/");
				this._mediaType = VNVN_MultiMediaControl.MEDIA_TYPE.YOUTUBE;
			}
			else
			{
				var b_find = false;
				var i_dotIndex = this._path.lastIndexOf(".");
				if (i_dotIndex != -1)
				{
					var s_ext = this._path.substr(i_dotIndex) + ", "; /* ".ext " */
					for (var i = 0; i < VNVN_MultiMediaControl._mediaExt.length && !b_find; i++)
					{
						if (VNVN_MultiMediaControl._mediaExt[i].indexOf(s_ext) != -1)
						{
							this._mediaType = i;
							b_find = true;
						}
					}
				}
				if (!b_find)
					this._mediaType = VNVN_MultiMediaControl.MEDIA_TYPE.WINMEDIA;
			}
		}
		return this._makeMediaObject();
	},

	_makeMediaObject: function()
	{
		var html = "",
				s_pro = "",
				s_width = "",
				s_height = "";

		var embedSrc = [];

		if (this._mediaType == VNVN_MultiMediaControl.MEDIA_TYPE.YOUTUBE)
			embedSrc = VNVN_MultiMediaControl._embedData[VNVN_MultiMediaControl.MEDIA_TYPE.FLASH];
		else
			embedSrc = VNVN_MultiMediaControl._embedData[this._mediaType];

		if (this._mediaType == VNVN_MultiMediaControl.MEDIA_TYPE.WINMEDIA)
		{
			/*Checking WMP version 6 or 7*/
			var wmp7 = null;
			try
			{
				if (Browser.IE) wmp7 = new ActiveXObject('WMPlayer.OCX');
			} catch (err) { }

			if (wmp7 == null)
				embedSrc = VNVN_MultiMediaControl._embedData[4]; /*WIN 6*/
		}

		if (!this._autoSize)
		{
			s_width = "width='" + this._width + "px'";
			s_height = "height='" + this._height + "px'";
		}
		else
		{
			if (this._mediaType == VNVN_MultiMediaControl.MEDIA_TYPE.REAL)
			{
				s_width = "width='240px'";
				s_height = "height='200px'";
			}
		}

		/*Make Property*/
		switch (this._mediaType)
		{
			case VNVN_MultiMediaControl.MEDIA_TYPE.WINMEDIA:
				s_pro = this._properties
				([
					this._property("FileName", this._path),
					this._property("Url", this._path),
					this._property("src", this._path),
					this._property("AutoStart", this._autoPlay),
					this._property("AutoSize", this._autoSize),
					this._property("playCount", (this._loop ? 999 : 1)),

				/*Default Property*/
					this._property("TransparentAtStart", false),
					this._property("AnimationatStart", true),
					this._property("ShowControls", true),
					this._property("ShowStatusBar", true),
					this._property("Volume", 100)
				]);
				break;

			case VNVN_MultiMediaControl.MEDIA_TYPE.REAL:
				s_pro = this._properties
				([
					this._property("src", this._path),
					this._property("autostart", this._autoPlay),
					this._property("loop", this._loop),

				/*Default Property*/
					this._property("controls", "all"),
					this._property("console", "_master")
				]);
				break;

			case VNVN_MultiMediaControl.MEDIA_TYPE.QUICK:
				s_pro = this._properties
				([
					this._property("src", this._path),
					this._property("autostart", this._autoPlay),
					this._property("loop", this._loop),

				/*Default Property*/
					this._property("controller", true)
				]);
				break;

			case VNVN_MultiMediaControl.MEDIA_TYPE.FLASH:
			case VNVN_MultiMediaControl.MEDIA_TYPE.YOUTUBE:
				s_pro = this._properties
				([
					this._property("movie", this._path),
					this._property("src", this._path),
					this._property("play", this._autoPlay ? -1 : 0),
					this._property("loop", this._loop ? -1 : 1),

				/*Default Property*/
					this._property("wmode", "transparent")
				]);
				break;
		}

		if (Browser.IE)
		{
			/*using <object>*/
			html = "<object classid='" + embedSrc[0] + "' codebase='" + embedSrc[2]
					+ "' " + s_width + " " + s_height + " id='" + this._id + "_OBJ'>" + s_pro + "</object>";
		}
		else
		{
			/*using <embed>*/
			html = "<embed type='" + embedSrc[1] + "' pluginspage='" + embedSrc[3]
					+ "' " + s_width + " " + s_height + " name='" + this._id + "_OBJ' " + s_pro + " /><noembed>" + embedSrc[4] + "</noembed>";
		}

		return html;
	},

	changeType: function(type, _autoType)
	{
		if (_autoType == null) _autoType = true;
		this._autoType = _autoType;
		this._mediaType = type;
	},

	changeUrl: function(url, _autoType)
	{
		if (_autoType == null) _autoType = true;
		this._autoType = _autoType;
		this._path = url;
		this.load();
	},

	remove: function()
	{
		var div = get(this._id);
		if (div != null) div.innerHTML = "";
	},

	play: function()
	{
		switch (this._mediaType)
		{
			case VNVN_MultiMediaControl.MEDIA_TYPE.WINMEDIA:
				break;

			case VNVN_MultiMediaControl.MEDIA_TYPE.REAL:
				break;

			case VNVN_MultiMediaControl.MEDIA_TYPE.QUICK:
				break;

			case VNVN_MultiMediaControl.MEDIA_TYPE.FLASH:
			case VNVN_MultiMediaControl.MEDIA_TYPE.YOUTUBE:
				break;
		}
	},

	stop: function()
	{
		switch (this._mediaType)
		{
			case VNVN_MultiMediaControl.MEDIA_TYPE.WINMEDIA:
				break;

			case VNVN_MultiMediaControl.MEDIA_TYPE.REAL:
				break;

			case VNVN_MultiMediaControl.MEDIA_TYPE.QUICK:
				break;

			case VNVN_MultiMediaControl.MEDIA_TYPE.FLASH:
			case VNVN_MultiMediaControl.MEDIA_TYPE.YOUTUBE:
				break;
		}
	}
}