/* 
A simple click and clear script
for the search box.
 */

function clickclear(thisfield, defaulttext) {
if (thisfield.value == defaulttext) {
thisfield.value = "";
}
}

function clickrecall(thisfield, defaulttext) {
if (thisfield.value == "") {
thisfield.value = defaulttext;
}
}
function enableTooltips(id){
var links,i,h;
if(!document.getElementById || !document.getElementsByTagName) return;
AddCss();
h=document.createElement("span");
h.id="btc";
h.setAttribute("id","btc");
h.style.position="absolute";
document.getElementsByTagName("body")[0].appendChild(h);
if(id==null) links=document.getElementsByTagName("a");
else links=document.getElementById(id).getElementsByTagName("a");
for(i=0;i<links.length;i++){
    Prepare(links[i]);
    }
}

function Prepare(el){
var tooltip,t,b,s,l;
t=el.getAttribute("title");
if(t==null || t.length==0) t="link:";
el.removeAttribute("title");
tooltip=CreateEl("span","tooltip");
s=CreateEl("span","top");
s.appendChild(document.createTextNode(t));
tooltip.appendChild(s);
b=CreateEl("b","bottom");
l=el.getAttribute("href");
if(l.length>28) l=l.substr(0,25)+"...";
b.appendChild(document.createTextNode(l));
tooltip.appendChild(b);
setOpacity(tooltip);
el.tooltip=tooltip;
el.onmouseover=showTooltip;
el.onmouseout=hideTooltip;
el.onmousemove=Locate;
}

function showTooltip(e){
document.getElementById("btc").appendChild(this.tooltip);
Locate(e);
}

function hideTooltip(e){
var d=document.getElementById("btc");
if(d.childNodes.length>0) d.removeChild(d.firstChild);
}

function setOpacity(el){
el.style.filter="alpha(opacity:95)";
el.style.KHTMLOpacity="0.95";
el.style.MozOpacity="0.95";
el.style.opacity="0.95";
}

function CreateEl(t,c){
var x=document.createElement(t);
x.className=c;
x.style.display="block";
return(x);
}

function AddCss(){
var l=CreateEl("link");
l.setAttribute("type","text/css");
l.setAttribute("rel","stylesheet");
l.setAttribute("href","bt.css");
l.setAttribute("media","screen");
document.getElementsByTagName("head")[0].appendChild(l);
}

function Locate(e){
var posx=0,posy=0;
if(e==null) e=window.event;
if(e.pageX || e.pageY){
    posx=e.pageX; posy=e.pageY;
    }
else if(e.clientX || e.clientY){
    if(document.documentElement.scrollTop){
        posx=e.clientX+document.documentElement.scrollLeft;
        posy=e.clientY+document.documentElement.scrollTop;
        }
    else{
        posx=e.clientX+document.body.scrollLeft;
        posy=e.clientY+document.body.scrollTop;
        }
    }
document.getElementById("btc").style.top=(posy+10)+"px";
document.getElementById("btc").style.left=(posx-20)+"px";
}



// Determine browser and version.

function Browser() {

  var ua, s, i;

  this.isIE    = false;
  this.isNS    = false;
  this.version = null;

  ua = navigator.userAgent;

  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }

  // Treat any other "Gecko" browser as NS 6.1.

  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}

var browser = new Browser();
var dragbrowser = new Browser();

// Global object to hold drag information.

var dragObj = new Object();
dragObj.zIndex = 0;


function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}


function stripHTML(sHTML) {
	var sOutText = "";
	var iTagStart;
	var iTagEnd = 0;
	

	iTagStart = sHTML.indexOf("<");
	
	while (iTagStart != -1) {
		sOutText += sHTML.substring(iTagEnd, iTagStart);
		iTagEnd = sHTML.indexOf(">", iTagStart) + 1;
		iTagStart = sHTML.indexOf("<", iTagEnd);
	}
	
	if (iTagEnd != 0) {
		sOutText += sHTML.substring(iTagEnd, sHTML.length);
	} else {
		sOutText = sHTML;
	}
	
	return sOutText;
}

function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}







// IE XMLHTTP
// http://www.scss.com.au/scripts/xmlhttprequest.js
if (window.ActiveXObject && !window.XMLHttpRequest) {
  window.XMLHttpRequest = function() {
    var msxmls = new Array('Msxml2.XMLHTTP.5.0','Msxml2.XMLHTTP.4.0',
                           'Msxml2.XMLHTTP.3.0','Msxml2.XMLHTTP','Microsoft.XMLHTTP');
    for (var i = 0; i < msxmls.length; i++) {
      try {
        return new ActiveXObject(msxmls[i]);
      } catch (e) {
      }
    }
    return null;
  };
}
function addp(csurl) {
try {
window.external.AddSearchProvider(csurl);
} catch (e) {
alert("You need to use Internet Explorer (7.0 or later) or Firefox (2.0 or later) to install the OpenSearch plug-in.");
}}
function quickSearch(hash) {
	this.active_result = -1;
	this.results = false;
	this.count_results = false;
	this.hash = hash;
	this.keydown = false;
	this.results_displayed = false;
	
	this.checkArrows = function(event) {
		
		// to keep Safari from doing this twice
		if (this.keydown == event.keyCode) {
			return;
		}
		
		this.keydown = event.keyCode;
		
		if (event.keyCode == 38) {
			this.active_result--;
			
			if (this.active_result < 0)
				this.active_result = this.count_results;
			
			this.displayResults();
			return false;		
		}
		else if (event.keyCode == 40) {
			this.active_result++;
			
			if (this.count_results && (this.active_result > this.count_results))
				this.active_result = 0;
				
			this.displayResults();
			return false;
		}
		else if (event.keyCode == 13 || event.keyCode == 9) {

		
				location.href = site_address+'r.php' + document.getElementById('searchbox').value;
		}
		else
			return true;
	
		return false;
	}
	
	this.displayResults = function() {

		if (this.results) {
			var results_done = new Array();
			var counter = 0;
			var html = '';
			var previous_entity = '';
			var entities_displayed = 0;
			
			for (var i in this.results) {

				if (this.results[i][0] != previous_entity) {
					if (this.results[i][1] != undefined) {
						entities_displayed++;
					
						if (counter == 0) {
							html += '<div class="searchresult_header_close" ';
							html += 'onmouseover="this.style.color = \'#000\';" ';
							html += 'onmouseout="this.style.color = \'#444\';" ';									
							html += 'onclick="doquickSearch.results_displayed = false; document.getElementById(\'searchresults\').style.visibility = \'hidden\';">X</div>';
						}
					}
					
					html += '<div class="searchresult_header">';
					
					if (counter > 0) {
						html += '<br />';
					}
					
					// TODO language vars
					switch (this.results[i][0]) {
						case 'artist':
							html += 'Did you mean this artist: ';
							break;
					}
					
					html += '</div>';					
				}
				
				previous_entity = this.results[i][0];
				
				if (this.results[i][1] != undefined && !results_done[this.results[i][0] + '_' + this.results[i][1]]) {
					html += '<div class="searchresult" ';

					html += (this.active_result == counter ? 'style="background: #c2daff; border: 1px solid #9ac1ff" ' : '');
					html += 'onmouseover="this.style.background = \'#c2daff\';this.style.border = \'1px solid #9ac1ff\';" ';
					html += 'onmouseout="this.style.background = \'#fff\';this.style.border = \'1px solid #fff\';" ';						
					html += 'onclick="location.href = \'http://www.albumsindex.com/search/?i=2&s=';
					
		
					html += this.results[i][1] + '&search=Search+artist\';" ';
					html += 'id="result_' + i + '">' + unescape(this.results[i][2]) + '</div>';
	
					results_done[this.results[i][0] + '_' + this.results[i][1]] = true;
					counter++;
				}				
			}
			
			this.count_results = (counter-1);
		}

		document.getElementById('searchresults').innerHTML = html;

		if (counter > 0) {
			//if (counter+((entities_displayed-1)*2) < 8)
			//	document.getElementById('searchresults').style.height = 24+(17*(counter+((entities_displayed-1)*2))) + 'px';
			//else
				document.getElementById('searchresults').style.height = '152px';


			if (this.active_result > 7)
				document.getElementById('searchresults').scrollTop = (17*(this.active_result-7));				
			else
				document.getElementById('searchresults').scrollTop = 0;
				
			var displayResultsEffect = new fx.Opacity(document.getElementById('searchresults'));
			
			if (this.results_displayed) {
				displayResultsEffect.setOpacity(0.9);
				document.getElementById('searchresults').style.visibility = 'visible';						
			}
			else {
				displayResultsEffect.setOpacity(0);

				displayResultsEffect.custom(0, 0.9);
				this.results_displayed = true;
			}
		}
		else {
			document.getElementById('searchresults').style.visibility = 'hidden';
		}		
	}

	this.startSearch = function(event) {
		this.keydown = false;
		
		if (event.keyCode != 38 && event.keyCode != 40 && event.keyCode != 13 && event.keyCode != 9) {
			this.results = new Array();	
			this.search = document.getElementById('searchbox').value;
			setTimeout("doquickSearch.doSearch('" + this.search.replace(/'/gi, "\\'") + "')", 400);
		}
	}
	
	this.doSearch = function(old_search) {

		// has the search been changed in the meantime?

		if (old_search != this.search)
			return false;

		doquickSearch.active_result = -1;
		
		req = new XMLHttpRequest(); 
		req.onreadystatechange = function() { 
			if (req.readyState == 4 && req.status == 200) { 
				var results = req.responseText; 

				var lines = new Array();
				lines = results.split("\n");
	
				if (lines.length > 1) {
					for (var i = 0 ; i < (lines.length-1) ; i++) {			
						var fields = new Array();
								
						fields  = lines[i].split(';;');
	
						if (i!=0 || fields[0] != '') {
							doquickSearch.results[doquickSearch.results.length] = fields;
						}
					}
				}
				
				doquickSearch.displayResults();
			} 
		}; 
	
		
		req.open('GET', site_address+'r.php?hash='+this.hash+'&search='+escape(this.search), true); 
		req.send(null);
	}
} /* end quickSearch() */
