﻿/* =========== Media Tool By Javascript : VNVN_41 =========== *\
\* ===========    Copyright © 2007 www.vnvn.net   =========== */
function __setCookie(c_name, value)
{
	var exp = new Date(11245711156480).toGMTString(), end = ';expires=' + exp + ';path=/'
    document.cookie = c_name+ "=" + escape(value) + end;
}

function __getCookie(c_name)
{
    if (document.cookie.length>0)
    {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start != -1)
        { 
            c_start=c_start + c_name.length+1; 
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) 
                c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}

var MyPlayList =
{
	list:[],
	curPlay: 0,
	ctrl: null,
	
	init: function(idControl)
	{
		var list = __getCookie("MyPlayList");
		if (list != "")	MyPlayList.list = list.split(",");
		MyPlayList.ctrl = idControl;
		if (MyPlayList.list.length > 0) $("#MyPlayListBoxControl").show();
	},

	add: function(div)
	{
		div = $(div).removeAttr("onclick").unbind("click");
		div.removeClass().addClass("AudioList_HasPlayList");
		var id = div.attr("aid");
		$("<div style='padding:2px 0'><div class='playlistRemoveMusicItem' onclick='MyPlayList.remove(this)'></div><span class='playlistMusicItem' onclick='MyPlayList.play(" + id + ")'></span></div>")
		.appendTo("#MyPlayListBox").find(".playlistMusicItem").attr(
		{
			aid: div.attr("aid"),
			alink: div.attr("alink")
		}).text(div.attr("title"));
		MyPlayList.list.push(id);
		$("#MyPlayListBoxControl").show();
		__setCookie("MyPlayList", MyPlayList.list.toString());
	},
	
	remove: function(div)
	{
		div = $(div).parent();
		var id = div.find(".playlistMusicItem").attr("aid");
		MyPlayList.list = jQuery.grep(MyPlayList.list, function(n, i)
		{
			return n != id;
		});
		$("#AudioListItemContainer .AudioList_HasPlayList[aid='" + id + "']").removeClass().addClass("AudioList_AddToPlayList").click(function()
		{
			MyPlayList.add(this);
		});
		if (MyPlayList.list.length <= 0) $("#MyPlayListBoxControl").hide();
		div.remove();
		__setCookie("MyPlayList", MyPlayList.list.toString());
	},

	play: function(id)
	{
		if (id == null) 
			id = MyPlayList.curPlay;
		else
			id = $.inArray(id.toString(), MyPlayList.list); 

		if (MyPlayList.list.length > 0)
		{
			$("#MyPlayListBoxControl .playListPlay").hide();
			$("#MyPlayListBoxControl .playListStop").show();
			MyPlayList.curPlay = id;
			
			if (MyPlayList.curPlay < 0) MyPlayList.curPlay = MyPlayList.list.length - 1;
			if (MyPlayList.curPlay > MyPlayList.list.length - 1) MyPlayList.curPlay = 0;
			
			id = MyPlayList.list[MyPlayList.curPlay];
			$("#MyPlayListBox > div > span.playlistMusicItemCurrent").removeClass("playlistMusicItemCurrent");
			var div = $("#MyPlayListBox > div > span[aid='" + id + "']").addClass("playlistMusicItemCurrent");
			MyPlayList.ctrl.changeUrl(div.attr("alink"), true, function(nst)
			{
				if (nst == "Completed" || nst == "COMPLETED")
				{
					MyPlayList.curPlay++;
					MyPlayList.play(null);
				}
				else if (nst == "Paused")
					MyPlayList.stop();
			});
		}
	},
	
	next: function()
	{
		MyPlayList.curPlay++;
		MyPlayList.play(null);
	},
	
	back: function()
	{
		MyPlayList.curPlay--;
		MyPlayList.play(null);
	},
	
	stop: function()
	{
		$("#MyPlayListBoxControl .playListStop").hide();
		$("#MyPlayListBoxControl .playListPlay").show();
		$("#MyPlayListBox > div.playlistMusicItemCurrent").removeClass("playlistMusicItemCurrent");
		MyPlayList.ctrl.remove();
	}
}

var VNVN_MultiMediaControl = new Class();
inherit(VNVN_MultiMediaControl, 
{
	MEDIA_TYPE : 
	{
		WINMEDIA	: 0,
		REAL		: 1,
		QUICK		: 2,
		FLASH		: 3,
		YOUTUBE		: 4,
		FLV         : 5
	},

	_mediaExt :
	[
		".wma, .wmv, .mpeg, .mpeg4; .mpg, .wav, .avi, .asx, .asf, .dat, ",	/*For Windows Media Player*/
		".rm, .rmvb, .divx, .smil, ",										/*For Real*/
		".qt, .mqv, ",														/*For Quick*/
		".swf, ",
		"",
		".flv, .mp3, .mp4, .mov, .h264"
	],

	/*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,
	_completeFunc: null,
	_freemedia: 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)
		{
		    var html = this.createHTML();
			if (html) div.innerHTML = html;
		}
	},
	
	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();
	},
	
	_isSilverlight: function()
	{
		var isSilverlightInstalled = false;
		try 
		{
			try 
			{
				var slc = new ActiveXObject('AgControl.AgControl');
				isSilverlightInstalled = true;
			} 
			catch (e)
			{
				if ( navigator.plugins["Silverlight Plug-In"] ) 
				{
					isSilverlightInstalled = true;
				}
			}
		}
		catch (e){}
		return isSilverlightInstalled;
	},

	_makeMediaObject: function()
	{
		var html = "",
			s_pro = "",
			s_width = "",
			s_height = "",
			html_bonus = "";
			
		
		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'";
			}
		}

		var embedSrc = [];
        if (this._mediaType == VNVN_MultiMediaControl.MEDIA_TYPE.FLV)
        {
			if (!this._freemedia)
			{
				document.getElementById(this._id).innerHTML = '<embed id="' + this._id + '_obj" name="' + this._id + '_obj"'
				+ ' width="' + this._width + '" height="' + this._height + '" '
				+ ' src="images/upload/site/jw-flv-player.swf" allowscriptaccess="true" allowfullscreen="true" wmode="transparent" flashvars="' + $.param
				({
					'file':this._path,
					'width':this._width,
					'height':this._height,
					'autostart':this._autoPlay
				}) + '" />';
				
				if (this._completeFunc)
				{
					var me = this;
					var addListener = function()
					{
						var flvplayer = document.getElementById(me._id + "_obj");
						if(flvplayer.getConfig != null) 
						{
							var name = "__func_" + me._id + "_obj";
							window[name] = function(obj)
							{
								if (me._completeFunc) me._completeFunc(obj.newstate);
							};
							flvplayer.addModelListener("STATE", name);
						}
						else
						{
							setTimeout(addListener, 100);
						}
					};
					addListener();
				}
			}
			else
			{
				document.getElementById(this._id).innerHTML = '<embed id="' + this._id + '_obj" name="' + this._id + '_obj"'
				+ ' width="' + this._width + '" height="' + this._height + '" '
				+ ' src="images/upload/site/mediaplayer.swf" allowscriptaccess="true" allowfullscreen="true" wmode="transparent" flashvars="' + $.param
				({
					'file':this._path,
					'width':this._width,
					'height':this._height,
					'autostart':this._autoPlay
				}) + '" />';
			}
            return null;
        }
        else if (!this._freemedia && this._mediaType == VNVN_MultiMediaControl.MEDIA_TYPE.WINMEDIA)
        {
			if (this._isSilverlight())
			{
				document.getElementById(this._id).innerHTML = "<span id='" + this._id + "_obj'></span>";
				var wmvPlayer = new jeroenwijering.Player
				(
					document.getElementById(this._id + "_obj"), 'images/upload/site/jw-wmv-player.xaml',
					{
						file: this._path.toString(),
						autostart: this._autoPlay.toString(),
						height:this._height.toString(),
						width:this._width.toString(),
						windowless: 'fasle'
					}
				);
				if (this._completeFunc)
				{
					var me = this;
					var addListener = function()
					{
						if(wmvPlayer.view) 
						{
							wmvPlayer.addListener('STATE', function(ost,nst)
							{
								if (me._completeFunc) me._completeFunc(nst);
							});
						}
						else 
						{
							setTimeout(addListener, 100);
						}
					};
					addListener();
				}
				return null;
			}
			else
			{
				html_bonus = "<a href='http://go.microsoft.com/fwlink/?LinkId=149156'><img style='padding-top:5px;' src='images/upload/site/download_sl.png' " + s_width + " alt='Download Silverlight' border='0'/></a>";
			}
        }
        else 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*/
		}

		/*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>" + html_bonus;
		}
		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>" + html_bonus;
		}

		return html;
	},

	changeType: function(type, _autoType)
	{
		if (_autoType == null) _autoType = true;
		this._autoType = _autoType;
		this._mediaType = type;
	},

	changeUrl: function(url, _autoType, _completefunc)
	{
		if (_autoType == null) _autoType = true;
		this._autoType = _autoType;
		this._path = url;
		this._completeFunc = _completefunc;
		this.load();
	},

	remove: function()
	{
		var div = get(this._id);
		if (div != null) div.innerHTML = "";
	}
}