﻿// Media Tool By Javascript : VNVN_41

/*
	id			: ''
	file		: ''
	width		: 0
	height		: 0
	properties	: 0
	container	: ''
	autoplay	: true
*/

var True = true;
var False = false;

var MEDIA_TYPE = 
{
	WINMEDIA	: 0,
	REAL		: 1,
	QUICK		: 2,
	FLASH		: 3,
	YOUTUBE		: 4
};

var _mediaExt =
[
	".wma, .wmv, .mpeg, .mpg, .wav, .mp3, .mp4, .avi, .asx, .asf, ",	//For Windows Media Player
	".rm, .rmvb, .divx, .smil, ",										//For Real
	".mov, .qt, .mqv, ",												//For Quick
	".swf, "
];

//classID, appType, codebase, plugingpage
var _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/", 
		"Bạn cần Window Media Player"
	],	//WIN 7
	[
		"CLSID:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA", 
		"audio/x-pn-realaudio-plugin", 
		"", 
		"",
		"Bạn cần real Player"
	],
	[
		"CLSID:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B", 
		"video/quicktime", 
		"http://www.apple.com/qtactivex/qtplugin.cab", 
		"http://www.apple.com/quicktime/download/",
		"Bạn cần 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",
		"Bạn cần Flash"
	],
	[
		"CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95", 
		"application/x-ms-wmp", 
		"http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715", 
		"http://www.microsoft.com/Windows/MediaPlayer/", 
		"Bạn cần Window Media Player"
	] //WIN 6
];

var VNVN_MultiMediaControl_41 = function(pro)
{
	//Init - dong bo giua server control va client control
	this.isIE	= (navigator.appName == 'Microsoft Internet Explorer');
	this.isMac	= false;
	
	for (var p in pro) this[p] = pro[p];
};

VNVN_MultiMediaControl_41.prototype =
{
	_id			: "",
	_autoType	: true,
	_mediaType	: MEDIA_TYPE.WINMEDIA,
	_path		: "",
	_width		: 320,
	_height		: 268,
	_autoPlay	: true,
	_loop		: false,
	_autoSize	: false,
	_smallSize	: false,
	
	_trim : function(str)
	{
		if (str == null || str == "")
			return str;
			
		while (str.substring(0, 1) == ' ')
		{
			str = str.substring(1, str.length);
		}
		while (str.substring(str.length - 1, str.length) == ' ')
		{
			str = str.substring(0, str.length - 1);
		}
		return str;
	},
	
	_properties : function(arrStr)
	{
		var result = "";
		for (var i = 0; i < arrStr.length; i++)
			result += arrStr[i];
		return result;

	},
	
	_property : function(name, value)
	{
		if (this.isIE)
			return " <param name='" + name + "' value='" + value + "' /> ";
		else
			return " " + name + "='" + value + "' ";
	},
	
	load : function()
	{
		this._path = this._trim(this._path);
		if (this._autoType)
		{
			//
			//Check youtube video link : http://www.youtube.com/watch?v=BFZFj4b5RzE or http://www.youtube.com/v/BFZFj4b5RzE
			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 = 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 < _mediaExt.length && !b_find; i++)
					{
						if (_mediaExt[i].indexOf(s_ext) != -1)
						{
							this._mediaType = i;
							b_find = true;
						}
					}
				}
				if (!b_find)
					this._mediaType = MEDIA_TYPE.WINMEDIA;
			}
		}
		var html	= this._makeMediaObject();
		var div		= document.getElementById(this._id);
		if (div != null)
			div.innerHTML = html;
	},
	
	_makeMediaObject : function()
	{
		var		html		= "",
				s_pro		= "",
				s_width		= "",
				s_height	= "";

		var embedSrc = [];

		if (this._mediaType == MEDIA_TYPE.YOUTUBE)
			embedSrc = _embedData[MEDIA_TYPE.FLASH];
		else
			embedSrc = _embedData[this._mediaType];
			
		if (this._mediaType == MEDIA_TYPE.WINMEDIA)
		{
			//Checking WMP version 6 or 7
			var wmp7 = null;
			try
			{
				wmp7 = new ActiveXObject('WMPlayer.OCX');
			}catch(err){}
			
			if (wmp7 == null)
				embedSrc = _embedData[4];	//WIN 6
		}

		if (!this._autoSize)
		{
			s_width = "width='" + this._width + "px'";
			s_height = "height='" + this._height + "px'";
		}
		else
		{
			if (this._mediaType == MEDIA_TYPE.REAL)
			{
				s_width = "width='240px'";
				s_height = "height='200px'";
			}
		}
		
		//Make Property
		switch (this._mediaType)
		{
			case 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 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 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 MEDIA_TYPE.FLASH:
			case 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 (this.isIE)
		{
			//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();
	},
	
	play : function()
	{
		switch (this._mediaType)
		{
			case MEDIA_TYPE.WINMEDIA:
				break;

			case MEDIA_TYPE.REAL:
				break;

			case MEDIA_TYPE.QUICK:
				break;

			case MEDIA_TYPE.FLASH:
			case MEDIA_TYPE.YOUTUBE:
				break;
		}
	},
	
	stop : function()
	{
		switch (this._mediaType)
		{
			case MEDIA_TYPE.WINMEDIA:
				break;

			case MEDIA_TYPE.REAL:
				break;

			case MEDIA_TYPE.QUICK:
				break;

			case MEDIA_TYPE.FLASH:
			case MEDIA_TYPE.YOUTUBE:
				break;
		}
	}
}