﻿/*DuyMinh Software www.phamduyminh.com*/

// Create player object from a list of channel
// channelList: channel list
// containerID: id of element where player will be displayed
// width, height: player's size
function player(channelList, containerID, width, height) {
	this.container = (typeof(containerID) == 'undefined' || containerID == '') ? 'player' : containerID;
	this.width = (typeof(width) == 'undefined' || width == '') ? 640 : width;
	this.height = (typeof(height) == 'undefined' || height == '') ? 480 : height;
	this.channels = new Array();
	for (var i = 0; i < channelList.length; i++) {
		this.channels[i] = new Object();
		this.channels[i].name = channelList[i].name;
		this.channels[i].url = channelList[i].url;
	}
}

// Return channel's url by its name
player.prototype.getChannelUrl = function(channelName) {
	for (var i = 0; i < this.channels.length; i++) {
		if (this.channels[i].name == channelName) {
			return this.channels[i].url;
		}
	}
	return this.channels[0].url;
}

// Show and play a channel
player.prototype.play = function(channelName) {
	setHtml(this.container, '<p>Bạn đang xem kênh: <b>' + channelName + '</b></p><object classid="clsid:22D6F312-B0F6-11D0-94AB-0080C74C7E95" width="' + this.width + '" height="' + this.height + '" type="application/x-oleobject"><param name="FileName" value="' + this.getChannelUrl(channelName) + '"><param name="uimode" value="none"><param name="EnableContextMenu" value="1"><embed showdisplay="0" showstatusbar="1" showcontrols="1" type="application/x-mplayer2" pluginspage="http://microsoft.com/windows/mediaplayer/" width="' + this.width + '" height="' + this.height + '" src="' + this.getChannelUrl(channelName) + '" autostart="true" scale="aspect" kioskmode="False" align="middle" hspace="0"></embed></object><p>Copyright &copy; 2006, DuyMinh Software  <a href="http://www.phamduyminh.com" title="Welcome to DuyMinh Software">www.phamduyminh.com</a></p>');
}

// Show channel list in a place specified by id
player.prototype.showChannelList = function(id) {
	var container = $(id);
	var ol = document.createElement('ol');
	for (var i = 0; i < this.channels.length; i++) {
		var li = document.createElement('li');
		var a = document.createElement('a');
		a.me = this;
		a.channel = this.channels[i].name;
		a.onclick = clickHandler;
		a.setAttribute('href', 'javascript:void(0)');
		a.setAttribute('title', 'Xem kênh ' + a.channel);
		a.appendChild(document.createTextNode(a.channel));
		li.appendChild(a);
		ol.appendChild(li);
	}
	container.appendChild(ol);
}

// Handler for clicking on one channel in channel list
function clickHandler() {
	var oPlayer = this.me;
	var sChannel = this.channel;
	oPlayer.play(sChannel);
}

// Reference to DOM node
function $(id) {
	return document.getElementById(id);
}

// Set HTML code to element's content
function setHtml(id, html) {
	$(id).innerHTML = html;
}