/**
 * This array is used to remember mark status of rows in browse mode
 */
var marked_row = new Array;
var maxSong = 20;
var baseUrl = "http://mp3.xalo.vn/";
var currentForumEmbed = "";

function setHomePage() {
	if (document.all)
	{
		document.body.style.behavior='url(#default#homepage)';
		document.body.setHomePage('http://mp3.xalo.vn/');
	}
	else if (window.sidebar) {
		if(window.netscape) {
			try
			{
				netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
			}
			catch(e)
			{
				alert("Trình duyệt của bạn chặn tính năng này. Vào Tools > Options > Main (Trong linux: Edit > Preferences) và thiết lập http://mp3.xalo.vn làm trang chủ.");
			}
		}
		var prefs =
		Components.classes['@mozilla.org/preferences-service;1'].getService(Components.
		interfaces.nsIPrefBranch);
		prefs.setCharPref('browser.startup.homepage','http://mp3.xalo.vn/');
	}
}

function refreshFrame() {
	fr = document.getElementById('fb_frame');
	fr.src = 'http://www.facebook.com/connect/connect.php?id=116400274623&connections=10&stream=0';
}

// Open popup
function openWindow(filename, winname, width, height, feature) {
	var features, top, left;
	var reOpera = /opera/i;
	var winnameRequired = ((navigator.appName == "Netscape" && parseInt(navigator.appVersion) == 4) || reOpera
			.test(navigator.userAgent));

	left = (window.screen.width - width) / 2;
	top = (window.screen.height - height) / 2;
		features = "width=" + width + ",height=" + height + ",top=" + top
				+ ",left=" + left + ",status=no,location=no";
	// if(! winnameRequired) winname = "";
	newwindow = window.open(baseUrl+filename, winname, features);
	newwindow.focus();
}

function PopUpSong(songInfo, obj) {
	if (!songInfo) {
		return true;
	}
	if (window.ActiveXObject) {
		openWindow('player.xalo?id=' + songInfo+'&rand='+Math.random()*1234567, 'popupsong', 700, 562, '');
	} else {
		openWindow('player.xalo?id=' + songInfo+'&rand='+Math.random()*1234567, 'popupsong', 700, 562, '');
	}
	if (obj) {
		obj.name = 'xalo';
		obj.href = location.href.replace('#' + obj.name, '') + '#' + obj.name;
	} else {
		return false;
	}
	return false;
}

function markAllRows(container_id) {
	var rows = document.getElementById(container_id).getElementsByTagName('tr');
	var unique_id;
	var checkbox;

	for ( var i = 0; i < rows.length; i++) {

		checkbox = rows[i].getElementsByTagName('input')[0];

		if (checkbox && checkbox.type == 'checkbox') {
			unique_id = checkbox.name + checkbox.value;
			if (checkbox.disabled == false) {
				checkbox.checked = true;
				if (typeof (marked_row[unique_id]) == 'undefined'
						|| !marked_row[unique_id]) {
					rows[i].className += ' marked';
					marked_row[unique_id] = true;
				}
			}
		}
	}

	return true;
}

/**
 * marks all rows and selects its first checkbox inside the given element the
 * given element is usaly a table or a div containing the table or tables
 * 
 * @param container
 *            DOM element
 */
function unMarkAllRows(container_id) {
	var rows = document.getElementById(container_id).getElementsByTagName('tr');
	var unique_id;
	var checkbox;

	for ( var i = 0; i < rows.length; i++) {

		checkbox = rows[i].getElementsByTagName('input')[0];

		if (checkbox && checkbox.type == 'checkbox') {
			unique_id = checkbox.name + checkbox.value;
			checkbox.checked = false;
			rows[i].className = rows[i].className.replace(' marked', '');
			marked_row[unique_id] = false;
		}
	}

	return true;
}

function getSongsCount() {
	if (document.getElementById('playlistContainer') == null)
		return 0;
	var rows = document.getElementById('playlistContainer')
			.getElementsByTagName('tr');
	return rows.length;
}

function addMany() {
	var i;
	var count = 0;
	var s = "";
	if (document.mainForm.checksong.length == null) {
		s = document.mainForm.checksong.value + "$";
		count++;
	} else {
		for (i = 0; i < document.mainForm.checksong.length; i++) {
			if (document.mainForm.checksong[i].checked) {
				s += document.mainForm.checksong[i].value + "$";
				count++;
			}
		}
	}
	if (count == 0) {
		alert("Bạn phải chọn ít nhất một bài để thêm.");
		return "";
	} else if ((count + getSongsCount()) <= maxSong) {
		return s;
	} else {
		alert("Bạn không thể add quá "
				+ maxSong
				+ " bài hát vào playlist. Hãy xóa bớt một số bài trước khi muốn add thêm.");
		return "";
	}
}

function playMany() {
	var i;
	var count = 0;
	var s = "";
	if (document.mainForm.checksong.length == null) {
		s = document.mainForm.checksong.value + "$";
		count++;
	} else {
		for (i = 0; i < document.mainForm.checksong.length; i++) {
			if (document.mainForm.checksong[i].checked) {
				s += document.mainForm.checksong[i].value + "$";
				count++;
			}
		}
	}
	if (count == 0) {
		alert("Bạn phải chọn ít nhất một bài để nghe.");
		return "";
	}
	if ((count) <= maxSong) {
		return s;
	} else {
		alert("Bạn không thể thêm quá "
				+ maxSong
				+ " bài hát vào playlist. Hãy xóa bớt một số bài trước khi muốn thêm.");
		return "";
	}
}

function firstSong() {
	var s = "";
	if (document.mainForm.checksong.length == null) {
		s = document.mainForm.checksong.value.split("|");
		return s[0];
	} else {
		for (i = 0; i < document.mainForm.checksong.length; i++) {
			if (document.mainForm.checksong[i].checked) {
				s = document.mainForm.checksong[i].value.split("|");
				return s[0];
			}
		}
	}
	return "0";
}

var timeout = 1000;
var closetimer = 0;
var ddmenuitem = 0;

// open hidden layer
function mopen(id) {
	// cancel close timer
	mcancelclosetime();

	// close old layer
	if (ddmenuitem)
		ddmenuitem.style.visibility = 'hidden';

	// get new layer and show it
	ddmenuitem = document.getElementById(id);
	ddmenuitem.style.visibility = 'visible';
	mclosetime();
}
// close showed layer
function mclose() {
	if (ddmenuitem)
		ddmenuitem.style.visibility = 'hidden';
}

// go close timer
function mclosetime() {
	closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime() {
	if (closetimer) {
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

function chooseOption(i) {
	document.getElementById("m1").style.visibility = "hidden";
	sElement = document.getElementById("searchin");
	iElement = document.getElementById("hq");
	switch (i) {
	case 0:
		sElement.innerHTML = "Tìm kiếm chung";
		break;
	case 1:
		sElement.innerHTML = "Tìm theo tên";
		break;
	case 2:
		sElement.innerHTML = "Tìm theo ca sĩ";
		break;
	case 3:
		sElement.innerHTML = "Tìm theo lời";
		break;
	}
	iElement.value = i;
}

function PLFull() {
	if (getSongsCount() == maxSong) 		
		alert("Bạn không thể thêm quá "
			+ maxSong
			+ " bài hát vào playlist. Hãy xóa bớt một số bài trước khi muốn thêm.");;
	return (getSongsCount() == maxSong);
}

function displayForumEmbed(id){
	if (id == "") return;
	if (currentForumEmbed!= ""){
		document.getElementById(currentForumEmbed).style.display = "none";
	}
	document.getElementById(id).style.display = "block";
	currentForumEmbed = id;
}