﻿
//////////////////////////////////////
var mediaPlayer = new Object();
mediaPlayer.init = function()
{
	this.productName = "Rasekhoon Media Player";
	this.productVersion = "1.1";
	this.actionURL = "act.aspx";
	
	if(document.all)
		this.player = document.getElementById("player");
	else
		this.player = window.parent.document.embeds[0];
		
	this.playList = testList;
	
	this.playList_conf();

	this.xhr = createXHR();
	
	this.play(this.playList[0]);
};
mediaPlayer.play = function(media) {
	if (media.type == "mp3") {
		if (mediaPlayer.player.height != 70)
			id = setInterval(function() {
				mediaPlayer.player.height = parseInt(mediaPlayer.player.height) - 14;
				if (parseInt(mediaPlayer.player.height) <= 70) clearInterval(id);
			}, 10);
	}
	else {
		if (mediaPlayer.player.height != 350)
			id = setInterval(function() {
				mediaPlayer.player.height = parseInt(mediaPlayer.player.height) + 14;
				if (parseInt(mediaPlayer.player.height) >= 350) clearInterval(id);
			}, 10);
	}
	for (var i in this.playList) this.playList[i].element.style.backgroundColor = "";
	this.playing = media;
	media.element.style.backgroundColor = "#EFEFEF";
	this.player.SetVariable("mediaPlayer.src", media.id + "." + media.type);
	mediaPlayer.hitMedia(media);
};
mediaPlayer.next = function()
{
	if(this.playList[this.playing.position+1])
	{
		this.play(this.playList[this.playing.position+1]);
	}
	else
	{
		this.play(this.playList[0]);
	}
};
mediaPlayer.manualPlay = function(mediaID)
{
	for(var i in this.playList)
		if(this.playList[i].id == mediaID)
		{
			this.play(this.playList[i]);
		}
};
mediaPlayer.playList_conf = function()
{
	for(var i in this.playList)
	{
		this.playList[i].element = this.playList_add(this.playList[i]);
	}

};
mediaPlayer.playList_over = function(element)
{
	if(this.playing.id != element.id)
		element.style.backgroundColor='#FAFAFA';
};
mediaPlayer.playList_out = function(element)
{
	if(this.playing.id != element.id)
		element.style.backgroundColor='';
};
mediaPlayer.playList_add = function(media)
{
	var _mainTable = document.getElementById('mediaPlayer_recordContainer');
	
	// insert top separator
	var _sepRow = _mainTable.insertRow(_mainTable.rows.length);
	var _firstTD = _sepRow.insertCell(0);
	_firstTD.colSpan = "5";
	_firstTD.style.width = "412px";
	_firstTD.style.height = "1px";
	_firstTD.style.backgroundColor = "#F5F5F5";
	var _secondTD = _sepRow.insertCell(1);
	_secondTD.style.width = "22px";
	
	//insert main record
	var _mainRow = _mainTable.insertRow(_mainTable.rows.length);
	_mainRow.id = media.id
	_mainRow.style.cursor = "pointer";
	_mainRow.style.height = "22px";
	_mainRow.onmouseover = function(){mediaPlayer.playList_over(this);};
	_mainRow.onmouseout = function(){mediaPlayer.playList_out(this);};
	_mainRow.onclick = function(){mediaPlayer.manualPlay(this.id)};
	
	var _td_0 = _mainRow.insertCell(0);
	_td_0.style.width = "101px";
	var _td_1 = _mainRow.insertCell(1);
	_td_1.style.width = "40px";
	_td_1.align = "center";
	_td_1.className = "media_title";
	_td_1.innerHTML = media.duration;
	var _td_2 = _mainRow.insertCell(2);
	_td_2.style.width = "40px";
	_td_2.align = "center";
	_td_2.className = "media_title";
	_td_2.innerHTML = media.hit;
	var _td_3 = _mainRow.insertCell(3);
	_td_3.style.width = "230px";
	_td_3.align = "right";
	_td_3.className = "media_title";
	_td_3.innerHTML = "<p style='margin-right:10px' title='"+media.description+"' >"+media.title+"</p>";
	var _td_4 = _mainRow.insertCell(4);
	_td_4.style.width = "1px";
	_td_4.style.backgroundColor = "#F5F5F5";
	_td_4.align = "right";
	_td_4.className = "media_title";
	_td_4.nowrap = "nowrap";
	var _td_5 = _mainRow.insertCell(5);
	_td_5.style.width = "22px";
	_td_5.style.backgroundColor = "#FFFFFF";
	_td_5.align = "center";
	_td_5.className = "media_title";
	if(media.type == "mp3")
		_td_5.innerHTML = "<img src='/imgs/mda_audio_icon.gif' />";
	else
		_td_5.innerHTML = "<img src='/imgs/mda_video_icon.gif' />";
	// insert bottom separator
	var _sepRow = _mainTable.insertRow(_mainTable.rows.length);
	var _firstTD = _sepRow.insertCell(0);
	_firstTD.colSpan = "5";
	_firstTD.style.width = "412px";
	_firstTD.style.height = "1px";
	_firstTD.style.backgroundColor = "#F5F5F5";
	var _secondTD = _sepRow.insertCell(1);
	_secondTD.style.width = "22px";

	// insert blank separator
	var _sepRow = _mainTable.insertRow(_mainTable.rows.length);
	var _firstTD = _sepRow.insertCell(0);
	_firstTD.colSpan = "6";
	_firstTD.style.width = "434px";
	_firstTD.style.height = "1px";
	_firstTD.style.backgroundColor = "#FFFFFF";
	return _mainRow;
};
mediaPlayer.hitMedia = function(media)
{
	var _url = this.actionURL+"?act=hitMedia&MediaID="+media.id + "." +  media.type;
	this.xhr.open("GET", _url, true);
	this.xhr.send(null);
};
mediaPlayer.getMedia = function(mediaID)
{
	var _url = this.actionURL+"?act=getMedia&MediaID="+mediaID;
	this.xhr.open("GET", _url, true);
	this.xhr.onreadystatechange = handleGetMedia;
	this.xhr.send(null);
};
function handleGetMedia()
{
	if(mediaPlayer.xhr.readyState == 4)
	{
		if(mediaPlayer.xhr.status == 200)
		{
			eval(mediaPlayer.xhr.responseText);
		}
		else
		{
			alert(mediaPlayer.xhr.readyState);
		}
	}
};
function replaceAll(_str, _oldChar, _newChar)
{
	return _str.split(_oldChar).join(_newChar);
};
function createXHR() {
	var xhr;
	try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
					xhr = new ActiveXObject("Microsoft.XMLHTTP");
				} 
				catch (_e) {
					xhr = false;
				}

			}
			if (!xhr && typeof XMLHttpRequest != 'undefined')
				xhr = new XMLHttpRequest();
			
	      return xhr;
 }
;
