// JS Pass address params


function openStartWindow(URL) {
	window.open(URL, "searchLnk","width=606px, height=420px, status=no, toolbar=no, menubar=no, location=no");
}

function startClient(params)
{
	if (CLIENT_EXISTS)
	{
		params = params.replace(/"/ig,'\\"');
		
		if (firefox)
			alert("This feature is not supported within this browser.");
		else
		{
			URL = CLIENT_HOST + "/search/start.php?" + params + "&close=1";
			openStartWindow(URL);
		}
	}
	else
	{
		alert('Coming soon...');
	}
}

function openRingtoneWindow(id){
	if (firefox)
		alert("This feature is not supported within this browser.");
	else
	{
		window.open(CLIENT_HOST+'/ringtone/ringtone.php?Content='+id, 'ring','height=450,width=540,status=no,toolbar=no,menubar=no,location=no,resizable=no');
	}
}
function imResultsSf()
{
	if (query!= undefined && query!= '')
	{
		query=decodeURIComponent(query);
		if (window.RegExp)
		{
			document.f.q.value = query.replace(/\+/g, " ");
		}
		else
		{
			document.f.q.value = query;
		}
		
		IMMainSearch(document.f.q.value, fileType, searchType);
	}
}
function setMusicSearchOptions()
{
	if (CLIENT_EXISTS)
	{
		document.getElementById("searchOptions").style.display = "";
		if (params["fileType"] != null && params["fileType"] != "")
		{
			for(var i=0;i<document.all["fileType"].options.length;i++)
			{
				if (params["fileType"] == document.all["fileType"].options[i].value)
					document.all["fileType"].options[i].selected = true;
			}
		}
		if (params["searchType"] != null && params["searchType"] != "")
		{
			for(var i=0;i<document.all["searchType"].options.length;i++)
			{
				if (params["searchType"] == document.all["searchType"].options[i].value)
					document.all["searchType"].options[i].selected = true;
			}
		}
	}
	else
	{
		document.getElementById("fileType").options[2].selected = true;
	}
}

function IMMainSearch(query, fileType, searchType) {
	document.body.style.cursor = "wait";
	var queryTag = '<' + searchType + '><![CDATA[' + query + ']]></' + searchType + '>';
	fileType = (!CLIENT_EXISTS) ? 2 : ((fileType == '') ? 0 : fileType);
	var fileTypeTag = '<FileType>' + fileType	+ '</FileType>';
	if (fileType == 2){	//Video Only
		commercialType = "0";
	}	else {
		commercialType = "";
	}

	var XMLDOC = '<SearchRequest><CommercialType>' + commercialType + '</CommercialType>'+ queryTag + fileTypeTag +'</SearchRequest>';
	makeAjaxRequest("http://" + window.location.hostname + "/search/search.php","function=Search&XMLDOC="+encodeURIComponent(XMLDOC),"drawResults(XML_OBJECT);document.body.style.cursor = '';");
}

function setText(pObj, pText)
{
	if (firefox)
		pObj.textContent = pText;
	else
		pObj.innerText = pText;
}

function drawResults(xmlObj) {
	document.getElementById("results").style.display = '';

	var TBODY = document.getElementById("resultsTable");
	var nodes = xmlObj.selectNodes("//Items/Item")
	for (var i=TBODY.rows.length-1;i>0;i--) TBODY.deleteRow(i);

	if (nodes.length == 0) {
		var TR = TBODY.insertRow(TBODY.rows.length);
		var TD = TR.insertCell(TR.cells.length);
		var TD = TR.insertCell(TR.cells.length);
		TD.colSpan = 6;
		TD.innerHTML = '<B><I>No results found</I></B>';
	} else {
		for (var i=0; i<nodes.length; i++) {
			var id = getSingleNode(getNode(nodes,i),"ComponentId");
			var isMN = eval(getSingleNode(getNode(nodes,i),"ContentSource"));
			var contentType = eval(getSingleNode(getNode(nodes,i),"ContentType"));
			var title = getSingleNode(getNode(nodes,i),"Title");
			var artistId = getSingleNode(getNode(nodes,i),"ArtistId");
			var artistName = getSingleNode(getNode(nodes,i),"Artist");
			var albumId = getSingleNode(getNode(nodes,i),"AlbumId");
			var albumName = getSingleNode(getNode(nodes,i),"AlbumTitle");
			var length = getSingleNode(getNode(nodes,i),"SongLength");
			var availability = getSingleNode(getNode(nodes,i),"Availability");
			var download = eval(getSingleNode(getNode(nodes,i),"DownloadIndicator"));
			var pdlLicensed = eval(getSingleNode(getNode(nodes,i),"TrackPurchasable"));
			var mp3Licensed = eval(getSingleNode(getNode(nodes,i),"MP3Purchasable"));
			var ringtone = eval(getSingleNode(getNode(nodes,i),"RingtoneIndicator"));
			var nodeXml = nodes[i].xml;
			var availabilityStr = '';
			
			if (CLIENT_EXISTS)
			{
				var buy = ((supportPdl && pdlLicensed) || (!supportPdl && mp3Licensed));
			}
			else
			{
				var buy = pdlLicensed;
			}
			
			if ( (buy || download || ringtone) || !isMN) {
				if (isMN){
					availabilityStr = '<IMG src="/images/search/starGoldIcon5.gif" border="0">';
				}
				else if (!isNaN(eval(availability)) && eval(availability) > 0) {
					availability = Math.floor(availability/2);
					availability = (availability<1)? 1:availability;
					availability = (availability>5)? 5:availability;
					availabilityStr = '<IMG src="/images/search/starGIcon' + availability + '.gif" border="0">';
				}

				if (!isMN && (title==undefined || title =='')){

					var filename = getSingleNode(getNode(nodes,i),"FileName");
					var filenameParts = filename.split('.');
					title = '';

					for(var j=0;j<(filenameParts.length-1);j++){
							title+=filenameParts[j] + '.';
					}
					title = title.substring(0, title.length-1);
				}

				var TR = TBODY.insertRow(TBODY.rows.length);
				var TD = TR.insertCell(TR.cells.length);
				TD.className = 'cellPadding';
				TD.align = 'center';
				TD.vAlign = 'center';

				var IMG = document.createElement("IMG");
				TD.appendChild(IMG);
				if (contentType){
					 IMG.src = '/images/search/trackVIcon.gif';
				}
				else{
					 if(isMN)
					 		IMG.src = '/images/search/trackIcon.gif';
					 else
							IMG.src = '/images/search/trackGIcon.gif';
				}
				IMG.border = '0';
				TD = null;
				IMG = null;

				var TD = document.createElement("TD");
				TR.appendChild(TD);
				TD.className = 'cellPadding';

				var NOBR = document.createElement("NOBR");
				TD.appendChild(NOBR);




				NOBR.className = "ellipsis";
				var A = document.createElement("A");
				NOBR.appendChild(A);
				A.id = id;
				A.className = 'regText';
				if (isMN)
				{
					A.className = 'regLink';
					A.onclick = function () 
					{
						startClient('command=track&id=' + this.id);
					}
					A.href = "javascript:void(0)";
				}
			
				tdWidth = TD.offsetWidth - 5;
				setText(A, title);
				if (firefox && A.offsetWidth > tdWidth)
					doEllipsis(A, tdWidth);
					
				A.title = title;				
				TD = null;
				NOBR = null;
				A = null;

				var TD = document.createElement("TD");
				TR.appendChild(TD);
				TD.className = 'cellPadding';

				var NOBR = document.createElement("NOBR");
				TD.appendChild(NOBR);


				NOBR.className = 'ellipsis';

				var A = document.createElement("A");
				NOBR.appendChild(A);
				A.id = artistId;
				A.className = 'regText';
				if (isMN)
				{
					A.className = 'regLink';
					A.onclick = function () 
					{
						startClient('command=artist&id=' + this.id);
					}
					A.href = "javascript:void(0)";
				}

				tdWidth = TD.offsetWidth - 5;
				setText(A, artistName);
				if (firefox && A.offsetWidth > tdWidth)
					doEllipsis(A, tdWidth);

				A.title = artistName;
				TD = null;
				NOBR = null;
				A = null;

				var TD = document.createElement("TD");
				TR.appendChild(TD);
				TD.className = 'cellPadding';

				var NOBR = document.createElement("NOBR");
				TD.appendChild(NOBR);
				NOBR.className = 'ellipsis';

				var A = document.createElement("A");
				NOBR.appendChild(A);
				A.id = albumId;
				A.className = 'regText';
				if (isMN)
				{
					A.className = 'regLink';
					A.onclick = function () 
					{
						startClient('command=album&id=' + this.id);
					}
					A.href = "javascript:void(0)";
				}

				tdWidth = TD.offsetWidth - 5;
				setText(A, albumName);
				if (firefox && A.offsetWidth > tdWidth)
					doEllipsis(A, tdWidth);
					
				A.title = albumName;
				TD = null;
				NOBR = null;
				A = null;

				var TD = document.createElement("TD");
				TR.appendChild(TD);
				TD.className = 'cellPadding';
				TD.innerHTML = length;
				TD.title = length;
				TD = null;

				var TD = document.createElement("TD");
				TR.appendChild(TD);
				TD.className = 'cellPadding';
				TD.innerHTML = availabilityStr;
				TD = null;

				var TD = document.createElement("TD");
				TR.appendChild(TD);
				var getTrackTab = document.createElement("TABLE");
				getTrackTab.cellPadding = "0";
				getTrackTab.cellSpacing = "0";
				TD.appendChild(getTrackTab);
				var TR = getTrackTab.insertRow(getTrackTab.rows.length);
				var TD = TR.insertCell(TR.cells.length);
				
				//TD.className = (ringtone) ? 'ringtone cellPadding' : 'cellPadding';
				TD.className = 'cellPadding';

				var NOBR = document.createElement("NOBR");
				TD.appendChild(NOBR);
				TD.style.width = "100%";
				NOBR.className = 'ellipsis';

				if (buy) {
					var A = document.createElement("A");
					NOBR.appendChild(A);
					A.id = id;
					var re = /<DownloadIndicator>(.*)<\/DownloadIndicator>/g;
					if (!firefox)
						A.nodeXml = nodeXml.replace(re, '');
					A.className = 'regLink';
					A.onclick = function () {
						startClient('command=buy&id=' + this.id + '&data=' + encodeURIComponent(this.nodeXml));
					}
					A.href = "javascript:void(0)";
					A.innerHTML = 'Buy';
					A.title = 'Buy';
				}

				if (buy && download) {
					var SPAN = document.createElement("SPAN");
					NOBR.appendChild(SPAN);
					SPAN.innerHTML = '&nbsp;|&nbsp;';
					SPAN.style.fontSize = "11px";
					A = null;
					SPAN = null;
				}

				if (download || !isMN) {
					var A = document.createElement("A");
					NOBR.appendChild(A);
					A.id = id;
					A.nodeXml = nodeXml;
					A.className = 'regLink';
					A.onclick = function () {
						if(isMN)
							urlParams = 'command=download&id=' + this.id + '&data=' + encodeURIComponent(this.nodeXml);
						else
							urlParams = 'command=freedownload&id=' + this.id + '&data=' + encodeURIComponent(this.nodeXml);

						startClient(urlParams);
					}
					A.href = "javascript:void(0)";
					A.innerHTML = 'Download';
					A.title = 'Download';
					A = null;
				}

				if (!ringtone) 
					TD.colSpan="2";
				else{
					var TD = document.createElement("TD");
					TR.appendChild(TD);
					TD.align = "right";

					var A = document.createElement("A");
					TD.appendChild(A);
					A.id = id;
					A.href = '#';
					A.onclick = function () {
						openRingtoneWindow(this.id);
					}

					var IMG = document.createElement("IMG");
					A.appendChild(IMG);
					IMG.src = '/images/search/ringtoneIcon.gif';
					IMG.border = '0';

					A = null;
					IMG = null;
				}
				TD = null;
			}
		}

		TD = null;
		NOBR = null;
	}
	TR = null;

	return false;
}
function doEllipsis(elem, maxlen)
{
	var textHolder = elem.innerHTML;
	elem.innerHTML = "...";
	var ellipsisWidth = elem.offsetWidth;
	elem.innerHTML = textHolder + "...";
	while (elem.offsetWidth >= (maxlen - ellipsisWidth)) 
	{
		elem.textContent = elem.textContent.substring(0,elem.textContent.length-4) + "...";
	}
}
var commercialType = "";

/************************** AJAX FUNCTIONS ***********************/
// mozXPath [http://km0ti0n.blunted.co.uk/mozxpath/] km0ti0n@gmail.com
// Code licensed under Creative Commons Attribution-ShareAlike License
// http://creativecommons.org/licenses/by-sa/2.5/
if( document.implementation.hasFeature("XPath", "3.0") )
{
	XMLDocument.prototype.selectNodes = function(cXPathString, xNode)
	{
		if( !xNode ) { xNode = this; }
		var oNSResolver = this.createNSResolver(this.documentElement)
		var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
		var aResult = [];
		for( var i = 0; i < aItems.snapshotLength; i++)
		{
			aResult[i] =  aItems.snapshotItem(i);
		}

		return aResult;
	}
	XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode)
	{
		if( !xNode ) { xNode = this; }

		var xItems = this.selectNodes(cXPathString, xNode);
		if( xItems.length > 0 )
		{
			return xItems[0];
		}
		else
		{
			return null;
		}
	}

	Element.prototype.selectNodes = function(cXPathString)
	{
		if(this.ownerDocument.selectNodes)
		{
			return this.ownerDocument.selectNodes(cXPathString, this);
		}
		else{throw "For XML Elements Only";}
	}

	Element.prototype.selectSingleNode = function(cXPathString)
	{
		if(this.ownerDocument.selectSingleNode)
		{
			return this.ownerDocument.selectSingleNode(cXPathString, this);
		}
		else{throw "For XML Elements Only";}
	}

}

function getNode(obj,pos)
{
	if (firefox)
		return obj[pos];
	else
		return obj.item(pos);
}

function getSingleNode(obj,tagName)
{
	if (firefox && getRawNode(obj,tagName) != null)
		if (getRawNode(obj,tagName) != null && getRawNode(obj,tagName).selectSingleNode("text()") != null)
			return getRawNode(obj,tagName).selectSingleNode("text()").nodeValue;
		else
			return "";
	else
		if (getRawNode(obj,tagName) != null)
			return getRawNode(obj,tagName).text;
		else
			return "";
}

function getRawNode(obj,tagName)
{
	return obj.selectSingleNode(tagName);
}

function makeAjaxRequest(url,postData,callback,noXML)
{
	if (firefox)
	{
		var http_request = new XMLHttpRequest();
	}
	else
	{
		if (window.ActiveXObject)
		{
			var http_request = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	http_request.onreadystatechange = function()
	{
		if (http_request.readyState == 4)
		{
			if (noXML)
			{
				eval(callback);
			}
			else
			{
				if (http_request.responseXML != null)
				{
					eval(callback.replace("XML_OBJECT","http_request.responseXML"));
				}
			}
		}
	}
	if (postData == "")
	{
		http_request.open("GET",url,true);
		http_request.send("");
	}
	else
	{
		http_request.open("POST",url,true);
		http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		http_request.send(postData);
	}
}
/*************************************************************************/

