
var p;
var ITEMS_PER_LIST_FILE = 50;
var MINIMIZE_TITLE = "";
var MAXIMIZE_TITLE = "";

function order(item_id)
{
	window.open(rootURL + "order.php?item_id=" + escape(item_id), null, "height=400,width=500,top=50,left=50,resizable=yes,directories=no,scrollbars=yes,status=yes,titlebar=yes,toolbar=no");
}

function Recommender(rootURL, voteText, currentVoteText, alreadyVotedText)
{
	this.rootURL = rootURL;
	this.voteText = voteText;
	this.currentVoteText = currentVoteText;
	this.alreadyVotedText = alreadyVotedText;

	this.votes = new Array();

	var str = getCookie("votes");
	if (str === null)
		return;

	str = unescape(str);

	var list = str.split('&');
	for (var i=0; i<list.length; i++) {
    	var list2 = list[i].split('=');
		this.votes[unescape(list2[0])] = unescape(list2[1]);
 	}

}

p = Recommender.prototype;

p.vote = function(itemId, formId, elementId)
{

	var points = 0;
	for (var i=0; i<4; i++)
		if (document.forms[formId].points[i].checked) {
        	points = document.forms[formId].points[i].value * 1;
        	break;
		}

	if (points == 0)
		return;

	loadData(this, this.rootURL + "vote.php?item_id=" + itemId + "&points=" + points, elementId);
}

p.load = function(data, elementId)
{
	var list = data.split(' ');
	var str = "";

	if (list[0] == "OK")
    	str = this.currentVoteText;
	else if (list[0] == "VOTE_EXISTS")
		str = this.alreadyVotedText;
	else {
		alert(list[1]);
		return;
	}

	this.votes[list[1]] = (list[2] * 1 > 0?"+":"") + list[2];

	var element = document.getElementById(elementId);

	if (element === null)
		return;

	element.innerHTML = "<font class=\"smallest\"><i><b>" + str + "</b>: " + this.votes[list[1]] + "</i></font>";
}

p.getHTML = function(listId, itemId)
{

	if (this.votes[itemId])
		return "<div id=\"vote_" + listId + "_" + itemId + "\"><font class=\"smallest\"><i><b>" +
				this.currentVoteText + "</b>: " + this.votes[itemId] + "</i></font></div>";

	return "<div id=\"vote_" + listId + "_" + itemId + "\">" +
			"<form name=\"form_" + listId + "_" + itemId + "\" style=\"padding: 0px; margin: 0px;\">" +
			"<font style=\"white-space: nowrap;\"><input type=radio name=\"points\" value=\"-2\" class=\"smallest\"/><font class=\"smallest\">-2 </font>" +
			"<input type=radio name=\"points\" value=\"-1\" class=\"smallest\"/><font class=\"smallest\">-1 </font>" +
			"<input type=radio name=\"points\" value=\"1\" class=\"smallest\"/><font class=\"smallest\">+1 </font>" +
			"<input type=radio name=\"points\" value=\"2\" class=\"smallest\"/><font class=\"smallest\">+2 </font></font>" +
			"<input type=button value=\"" + this.voteText + "\" onClick=\"voter.vote('" + itemId + "', 'form_" + listId + "_" + itemId + "', 'vote_" + listId + "_" + itemId + "')\" class=\"smallest\">" +
			"</form></div>";

}

/*
 *
 */
function ItemList(id, rootURL, filePrefix, staticList, itemsPerFile, total, loaded, minimizeCount, maximizeCount,
				  styleIndex, backTitle, forwardTitle, orderTitle)
{
	this.id = id;
	this.rootURL = rootURL;
	this.filePrefix = filePrefix;
	this.staticList = staticList;
	this.itemsPerFile = itemsPerFile <= 0?50:itemsPerFile;
	this.styleIndex = styleIndex;
	this.backTitle = backTitle;
	this.forwardTitle = forwardTitle;
	this.orderTitle = orderTitle;
	this.from = 1;
	this.isMinimized = true;
	this.total = total || 0;
	this.loaded = loaded || 0;
	this.minimizeCount = minimizeCount || 0;
	this.maximizeCount = maximizeCount || 0;

	this.previewIndex = -1;

}

p = ItemList.prototype;

p.forward = function() {

	this.previewIndex = -1;

	if (this.isMinimized)
		this.isMinimized = false;
	else if (this.from + this.maximizeCount < this.total)
		this.from += this.maximizeCount;
	this.showList();
}

p.back = function() {

	this.previewIndex = -1;

	if (this.isMinimized)
		return;
	else if (this.from - this.maximizeCount < 1)
		this.from = 1;
	else
		this.from -= this.maximizeCount;
	this.showList();
}

p.maximize = function() {

	this.isMinimized = false;
	this.showList();
}

p.minimize = function() {
	this.from = 1;
	this.isMinimized = true;
	this.showList();
}

p.loadNextSet = function() {
	var fileIndex = Math.floor((this.loaded + 1) / this.itemsPerFile);
	if (this.staticList)
		loadData(this, this.rootURL + "~lists/" + this.filePrefix + "_" + (fileIndex * this.itemsPerFile + 1) + "_" + (fileIndex * this.itemsPerFile + this.itemsPerFile) + ".lst?", fileIndex * this.itemsPerFile + 1);
	else
		loadData(this, this.rootURL + this.filePrefix + "&from=" + this.loaded);
}

p.getMinMaxPrevNextHTML = function() {

	var contents = "";

	if (this.isMinimized) {
		contents += '<a href="JavaScript: ' + this.id + '.maximize();" class="il_' + this.styleIndex + '_go_link">' + MAXIMIZE_TITLE + ' &gt;&gt;</a>';
 	} else {
		contents += '<a href="JavaScript: ' + this.id + '.minimize();" class="il_' + this.styleIndex + '_go_link">&lt;&lt; ' + MINIMIZE_TITLE + '</a>';
		if (this.from > 1)
			contents += ' | <a href="JavaScript:  ' + this.id + '.back();" class="il_' + this.styleIndex + '_go_link">&lt; ' + this.backTitle + '</a>';
		if (this.from + this.getCount() < this.total)
			contents += ' | <a href="JavaScript:  ' + this.id + '.forward();" class="il_' + this.styleIndex + '_go_link">' + this.forwardTitle + ' &gt;</a>';
	 }
	return contents;
}

p.showList = function()
{

	var count = this.getCount();

	//wheather the "loading..." message should be shown
	if (this.showLoading(this.from, count))
    	document.getElementById(this.id).innerHTML = htmlLoading;

	//generate heading of the top
	var contents = this.getTopHTML(this.from, count);

	//generate contents
	for (var i=0; i<count; i++) {
		var data = this.getHTML(this.from + i);
		//data not yet fully loaded
		if (data == null)
			return;
		//index exceeds the total number of items
		if (data == '')
			break;
		contents += data;
 	}

	//generate bottom of the top
	contents += this.getBottomHTML();
	
	document.getElementById(this.id).innerHTML = contents;

}

p.getCount = function()
{
	return this.isMinimized ? this.minimizeCount : this.maximizeCount;
}

p.showLoading = function() {
	return (this.total > this.loaded) && (this.from + this.getCount() < this.loaded);
}

/*
 * Virsraksti iekļauti dažādu kodēšanas tipu dēļ dēļ.
 */
function MelodyList(id, rootURL, filePrefix, staticList, itemsPerFile, total, minimizeCount, maximizeCount,
					styleIndex, backTitle, forwardTitle, orderTitle, errorTitle, addedByStr, lengthStr, ids, titles, authors)
{

	this.base = ItemList;
	this.base(id, rootURL, filePrefix, staticList, itemsPerFile, total, ids.length, minimizeCount, maximizeCount,
					styleIndex, backTitle, forwardTitle, orderTitle);

	this.ids = ids;
	this.titles = titles;
	this.authors = authors;

	this.errorTitle = errorTitle;
	this.addedByStr = addedByStr;
	this.lengthStr = lengthStr;

	this.previewFile = false;
	this.previewDuration = -1;
	this.adDuration = -1;
	this.mixDuration = -1;
}

MelodyList.prototype = new ItemList;

p = MelodyList.prototype;

p.getHTML = function(index) {

	if (index > this.total)
		return "";

	if (index > this.loaded) {
		this.loadNextSet();
		return null;
	}

	var oddEven = index % 2 == 0?"odd":"even";

	index--;

    var row = '<tr><td class="il_' + this.styleIndex + '_index_' + oddEven + '_cell">' + (index + 1) +
			'</td><td class="il_' + this.styleIndex + '_' + oddEven + '_cell"><a href="JavaScript: ' + this.id +
			'.preview(' + index + ');" class="il_' + this.styleIndex + '_' + oddEven + '_link">' + this.titles[index] + '</a>' +
			(this.authors[index] != ""?"<br>" + this.authors[index]:"")+ '</td></tr>\n';

	if (this.previewIndex == index) {
		row += '<tr><td colspan=2 align=left class="il_' + this.styleIndex + '_' + oddEven + '_cell">';
		if (this.previewFile === false)
			row += '<center><img src="' + this.rootURL + '~images/loading.gif" width=17 height=18></center>';
		else  
			row += '<center><object data="' + this.rootURL + '~fplayer/player.swf" type="application/x-shockwave-flash" width=200 height=18>' +
				'<param name="movie" value="' + this.rootURL + '~fplayer/player.swf"/>' +
				'<param name="codebase" value="' + this.rootURL + '~fplayer/"/>' +
				'<param name="FlashVars" value="duration=' + this.previewDuration +
					'&file=' + encodeURIComponent(this.rootURL + '~samples/' + this.previewFile) +
					'&bg_color=EFEFEF' +
					'&ad_duration=' + this.adDuration +
					'&mix_duration=' + this.mixDuration +
					'&errorLoadingTXT=' + encodeURIComponent(this.errorTitle)  + '"/></object>' +
					voter.getHTML(this.id, this.ids[index]) + '</center>' +
					'<b>' + this.addedByStr + ':</b> <a href="' + this.rootURL + 'pers.php?id=' + this.addedById + '">' + this.addedByName  + '</a><br>\n' +
					'<b>' + this.lengthStr + ':</b> ' + this.fullLength  + '<br>\n' +
					(this.descr != ""?'<i>' + this.descr + '</i><br>':'') + '<br>\n' +
					'<center><a href="JavaScript: order(\'' + this.ids[index] + '\');" class="il_' + this.styleIndex + '_order_link">' + this.orderTitle + '</a></center>';
			row += '</td></tr>\n';

	}


	return row;

}

p.load = function(data, fileItemIndex) {

	var list = data.split(";");

	//preview loaded
	if (fileItemIndex == -1)
	{
    	if (list[0] != 'OK')
    		return;
    	this.previewFile = list[1];
		this.previewDuration = list[2];
		this.adDuration = list[3];
		this.mixDuration = list[4];
		this.addedById = list[5];
		this.addedByName = decodeURIComponent(list[6]);
		this.fullLength = decodeURIComponent(list[7]);
		this.descr = decodeURIComponent(list[8]);
	}
	else
	{

		var lastIndex = this.ids.length;

		if (data.substring(data.length - 1, data.length) == ";")
			list.pop();

		for (var i=0; i<list.length; i+=3) {
	    	if (lastIndex >= fileItemIndex) {
				fileItemIndex++;
				continue;
	     	}
			this.ids.push(list[i]);
			this.titles.push(decodeURIComponent(list[i + 1]));
			this.authors.push(decodeURIComponent(list[i + 2]));
			this.loaded++;
		}
	}

    this.showList();
}

p.getTopHTML = function() {
	return '<table cellpadding=0 cellspacing=0 width="100%" class="il_' + this.styleIndex + '_sub_table">';
}

p.getBottomHTML = function() {
	return '<tr><td colspan=2 align=right class="il_' + this.styleIndex + '_go_cell">' + this.getMinMaxPrevNextHTML() + '</td></tr></table>';
}

p.preview = function(index)
{
	if (this.previewIndex == index)
		this.previewIndex = -1;
	else {
		this.previewIndex = index;
		loadData(this, this.rootURL + "preview_sound.php?id=" + this.ids[index], -1);
	}

	this.previewFile = false;
	this.showList();
}

/*
 *
 */
function ImageList(id, rootURL, filePrefix, staticList, itemsPerFile, total, minimizeCount, maximizeCount,
					styleIndex, backTitle, forwardTitle, orderTitle,
					titleStr, addedByStr, img_width, img_height, images_per_row, showTitles, infoScript, ids, titles)
{

	this.base = ItemList;
	this.base(id, rootURL, filePrefix, staticList, itemsPerFile, total, ids.length, minimizeCount, maximizeCount,
					styleIndex, backTitle, forwardTitle, orderTitle);

	this.ids = ids;
	this.titles = titles;
	this.titleStr = titleStr;
	this.addedByStr = addedByStr;
	this.img_width = img_width;
	this.img_height = img_height;
	this.images_per_row = images_per_row;
	this.showTitles = showTitles;
	this.infoScript = infoScript;
}

ImageList.prototype = new ItemList;

p = ImageList.prototype;

p.getHTML = function(index) {

	var result = "";

	index--;

	var oddEven;
	
	if (this.images_per_row % 2 == 0)
		oddEven = (index + Math.floor(index / this.images_per_row)) % 2 == 0?"odd":"even";
	else
		oddEven = index % 2 == 0?"odd":"even";

	if (index >= this.total) {

		if (index % this.images_per_row != 0 && index > this.images_per_row)
		{
			result = '<td class="il_' + this.styleIndex + '_' + oddEven + '_cell"><img src="' + this.rootURL + 'zero.gif" width=1 height=1></td>';
			if ((index  + 1) % this.images_per_row == 0)
				result += "</tr>";
		}

		return result;
	}

	if (index >= this.loaded) {
		this.loadNextSet();
		return null;
	}

    if (index % this.images_per_row == 0)
    	result += "<tr>";

	if (this.showTitles) {
	    result += '<td align=left valign=top width=140 class="il_' + this.styleIndex + '_' + oddEven + '_cell"><center><a href="JavaScript: ' + this.id + '.preview(' + index + ');" class="il_' + this.styleIndex + '_' + oddEven + '_link"><img src="' + this.rootURL + "~samples/" + this.ids[index] + '_preview.png" width=' + this.img_width + ' height=' + this.img_height + ' border=0></a><br>';
		if (this.previewIndex != index)
			result += (index + 1) + ". <a href=\"JavaScript: " + this.id + ".preview(" + index + ");\" class=\"il_" + this.styleIndex + "_" + oddEven + "_link\">" + this.titles[index] + '</a>';
		result += "</center>";
	}
	else
	    result += '<td align=left valign=top width=140 class="il_' + this.styleIndex + '_' + oddEven + '_cell"><center>' + (index + 1) + '. <a href="JavaScript: ' + this.id + '.preview(' + index + ');" class="il_' + this.styleIndex + '_' + oddEven + '_link"><img src="' + this.rootURL + "~samples/" + this.ids[index] + '_preview.png" width=' + this.img_width + ' height=' + this.img_height + ' border=0></a></center>';

	if (this.previewIndex == index) {
		if (this.title === false)
			result += '<center><img src="' + this.rootURL + '~images/loading.gif" width=17 height=18></center>';
		else
			result += '<center>' + voter.getHTML(this.id, this.ids[index]) + '</center>' +
						'<b>' + this.titleStr + ':</b> ' + this.title  + '<br>\n' +
						'<b>' + this.addedByStr + ':</b> <a href="' + this.rootURL + 'pers.php?id=' + this.addedById + '">' + this.addedByName  + '</a><br>\n' +
						(this.descr != ""?'<i>' + this.descr + '</i><br>':'') + '<br>\n' +
						'<center><a href="JavaScript: order(\'' + this.ids[index] + '\');" class="il_' + this.styleIndex + '_order_link">' + this.orderTitle  + '</a></center>\n';

	}

	result += "</td>";

	if ((index  + 1) % this.images_per_row == 0)
		result += "</tr>";

	return result;
}

p.load = function(data, fileItemIndex) {

	var list = data.split(";");

	//preview loaded
	if (fileItemIndex == -1)
	{
    	if (list[0] != 'OK')
    		return;
    	this.title = decodeURIComponent(list[1]);
    	this.addedById = list[2];
    	this.addedByName = decodeURIComponent(list[3]);
		this.descr = decodeURIComponent(list[4]);
	}
	else
	{

		var lastIndex = this.ids.length;

		if (data.substring(data.length - 1, data.length) == ";")
			list.pop();

		for (var i=0; i<list.length; i+=3) {
	    	if (lastIndex >= fileItemIndex) {
				fileItemIndex++;
				continue;
	     	}
			this.ids.push(list[i]);
			this.titles.push(decodeURIComponent(list[i + 1]));
			this.loaded++;
		}

	}

    this.showList();
}

p.getTopHTML = function() {
	return '<center><table border=0 cellpadding=0 cellspacing=0 class="il_' + this.styleIndex + '_sub_table">';
}

p.getBottomHTML = function() {
	return '<tr><td colspan=' + this.images_per_row + ' align=right class="il_' + this.styleIndex + '_go_cell">' + this.getMinMaxPrevNextHTML() + '</td></tr></table>';
}

p.preview = function(index)
{
	if (this.previewIndex == index)
		this.previewIndex = -1;
	else {
		this.previewIndex = index;
		loadData(this, this.rootURL + this.infoScript + "?id=" + this.ids[index], -1);
	}

	this.title = false;
	this.showList();
}

/*
 *
 */
function VideoList(id, rootURL, filePrefix, staticList, itemsPerFile, total, minimizeCount, maximizeCount,
					styleIndex, backTitle, forwardTitle, orderTitle,
					titleStr, addedByStr, lengthStr, dimensionsStr, fileSizeStr,
					ids, titles)
{
	this.base = ItemList;
	this.base(id, rootURL, filePrefix, staticList, itemsPerFile, total, ids.length, minimizeCount, maximizeCount,
					styleIndex, backTitle, forwardTitle, orderTitle, false);
	this.ids = ids;
	this.titles = titles;

	this.titleStr = titleStr;
	this.addedByStr = addedByStr;
	this.lengthStr = lengthStr;
	this.dimensionsStr = dimensionsStr;
	this.fileSizeStr = fileSizeStr;

	this.previewFile = false;
}

VideoList.prototype = new ItemList;

p = VideoList.prototype;

p.getHTML = function(index) {

	var result = "";

	index--;

	var oddEven = (index + Math.floor(index / 2)) % 2 == 0?"odd":"even";

	if (index >= this.total) {
		if (index % 2 != 0 && index > 2)
		{
			result = '<td class="il_' + this.styleIndex + '_' + oddEven + '_cell"><img src="' + this.rootURL + 'zero.gif" width=1 height=1></td>';
			if ((index  + 1) % 2 == 0)
				result += "</tr>";
		}

		return result;
	}

	if (index >= this.loaded) {
		this.loadNextSet();
		return null;
	}

    if (index % 2 == 0)
    	result += "<tr>";

	if (this.previewIndex == index) {
		if (this.previewFile === false)
			result += '<td align=center valign=middle class="il_' + this.styleIndex + '_' + oddEven + '_cell" width=180><img src="' + this.rootURL + '~images/loading.gif" width=17 height=18></td>';
		else
			result += '<td align=left valign=top width=180 class="il_' + this.styleIndex + '_' + oddEven + '_cell">\n' +
						'<center><div id="' + this.id + '_player"><a href="http://www.macromedia.com/go/getflashplayer">Get the Flash Player</a> to see this player.</div>\n' +
						voter.getHTML(this.id, this.ids[index]) +
						'</center>' +
						'<b>' + this.titleStr + ':</b> ' + this.title  + '<br>\n' +
						'<b>' + this.addedByStr + ':</b> <a href="' + this.rootURL + 'pers.php?id=' + this.addedById + '">' + this.addedByName  + '</a><br>\n' +
						'<b>' + this.lengthStr + ':</b> ' + this.fullLength  + '<br>\n' +
						'<b>' + this.dimensionsStr + ':</b> ' + this.dimensions  + '<br>\n' +
						'<b>' + this.fileSizeStr + ':</b> ' + this.fileSize  + '<br>\n' +
						(this.descr != ""?'<i>' + this.descr + '</i><br>':'') + '<br>\n' +
						'<center><a href="JavaScript: order(\'' + this.ids[index] + '\');" class="il_' + this.styleIndex + '_order_link">' + this.orderTitle  + '</a></center><br>\n' +
						'</td>\n';

	}
	else
		result += '<td align=center valign=top class="il_' + this.styleIndex + '_' + oddEven + '_cell" width=180><a href="JavaScript: ' + this.id +
					'.preview(' + index + ');" class="il_' + this.styleIndex + '_' + oddEven + '_link"><img src="' + this.rootURL + "~samples/" + this.ids[index] + '_preview.jpg" width174 height=144 border=0></a><br>' +
				 	+ (index + 1) + '. <a href="JavaScript: ' + this.id +
					'.preview(' + index + ');" class="il_' + this.styleIndex + '_' + oddEven + '_link">' + this.titles[index] + '</a></td>';

	if ((index  + 1) % 2 == 0)
		result += "</tr>";

	return result;
}

p.load = function(data, fileItemIndex) {

	var list = data.split(";");

	//preview loaded
	if (fileItemIndex == -1)
	{
    	if (list[0] != 'OK')
    		return;
    	this.previewFile = list[1];
    	this.title = decodeURIComponent(list[2]);
    	this.addedById = list[3];
    	this.addedByName = decodeURIComponent(list[4]);
		this.fullLength = decodeURIComponent(list[5]);
		this.dimensions = decodeURIComponent(list[6]);
		this.fileSize = decodeURIComponent(list[7]);
		this.descr = decodeURIComponent(list[8]);
	}
	else
	{

		var lastIndex = this.ids.length;

		if (data.substring(data.length - 1, data.length) == ";")
			list.pop();

		for (var i=0; i<list.length; i+=3) {
	    	if (lastIndex >= fileItemIndex) {
				fileItemIndex++;
				continue;
	     	}
			this.ids.push(list[i]);
			this.titles.push(decodeURIComponent(list[i + 1]));
			this.loaded++;
		}
	}
    this.showList();
}

p.getTopHTML = function() {
	return '<center><table border=0 cellpadding=0 cellspacing=0 class="il_' + this.styleIndex + '_sub_table">';
}

p.getBottomHTML = function() {
	return '<tr><td colspan=2 align=right class="il_' + this.styleIndex + '_go_cell">' + this.getMinMaxPrevNextHTML() + '</td></tr></table>';
}

p.preview = function(index)
{
	if (this.previewIndex == index)
		this.previewIndex = -1;
	else {
		this.previewIndex = index;
		loadData(this, this.rootURL + "preview_video.php?id=" + this.ids[index], -1);
	}

	this.previewFile = false;
	this.showList();
}

p.showList = function()
{

	var count = this.getCount();

	//wheather the "loading..." message should be shown
	if (this.showLoading(this.from, count))
    	document.getElementById(this.id).innerHTML = htmlLoading;

	//generate heading of the top
	var contents = this.getTopHTML(this.from, count);

	//generate contents
	for (var i=0; i<count; i++) {
		var data = this.getHTML(this.from + i);
		//data not yet fully loaded
		if (data == null)
			return;
		//index exceeds the total number of items
		if (data == '')
			break;
		contents += data;
 	}

	//generate bottom of the top
	contents += this.getBottomHTML();

	document.getElementById(this.id).innerHTML = contents;

	if (this.previewIndex > -1 && this.previewFile !== false)
	{
		var FO = {	movie: this.rootURL + "~flvplayer/flvplayer.swf",width:"176",height:"164",majorversion:"7",build:"0",bgcolor:"#FFFFFF",allowfullscreen:"true",
					flashvars:"file=" + escape("../~samples/" + this.previewFile) +
					"&image=" + escape(this.rootURL + "~samples/" + this.ids[this.previewIndex] + "_preview.jpg") };
		UFO.create(FO, this.id + '_player');
	}

}

