function $(e){if(typeof e=='string')e=document.getElementById(e);return e};
function collect(a,f){var n=[];for(var i=0;i<a.length;i++){var v=f(a[i]);if(v!=null)n.push(v)}return n};

ajax={};
ajax.x=function(){try{return new ActiveXObject('Msxml2.XMLHTTP')}catch(e){try{return new ActiveXObject('Microsoft.XMLHTTP')}catch(e){return new XMLHttpRequest()}}};
ajax.serialize=function(f){var g=function(n){return f.getElementsByTagName(n)};var nv=function(e){if(e.name)return encodeURIComponent(e.name)+'='+encodeURIComponent(e.value);else return ''};var i=collect(g('input'),function(i){if((i.type!='radio'&&i.type!='checkbox')||i.checked)return nv(i)});var s=collect(g('select'),nv);var t=collect(g('textarea'),nv);return i.concat(s).concat(t).join('&');};
ajax.send=function(u,f,m,a){ var x=ajax.x();x.open(m,u,true);x.onreadystatechange=function(){if(x.readyState==4)f(x.responseText)};if(m=='POST')x.setRequestHeader('Content-type','application/x-www-form-urlencoded');x.send(a)};
ajax.get=function(url,func){ajax.send(url,func,'GET')};
ajax.gets=function(url){var x=ajax.x();x.open('GET',url,false);x.send(null);return x.responseText};
ajax.post=function(url,func,args){ajax.send(url,func,'POST',args)};
ajax.update=function(url,elm){var e=$(elm);var f=function(r){e.innerHTML=r};ajax.get(url,f)};
ajax.submit=function(url,elm,frm){var e=$(elm);var f=function(r){e.innerHTML=r};ajax.post(url,f,ajax.serialize(frm))};

var pos = 0;
var count = 0;
var sdivid = "";
var addInputID="";
var theDivHeight = 500;

function trim(str) { 
  str.replace(/^\s*/, '').replace(/\s*$/, '');
  return str;
} 

function noenter(key) {
	suggcont = document.getElementById("suggcontainer");
	if (suggcont.style.display == "block") {
		if (key == 13) {
			choiceclick(document.getElementById(pos));
			return false;
		} else {
			return true;
		}
	} else {
		return true;
	}
}

function suggest(key,query,ftype) {
	
  query = trim(query);
  query = query.replace(/[&\\?]/g, "");
  theDivHeight = '500';	
  
  if (key == 38) {
		goPrev();
	} else if (key == 40) {
		goNext();
	} else if (key != 13) {
		if (query.length > 2) {
		  t = new Date();
      if(ftype=="tracklist"){
        ajax.post('dynasearch.php',update,'vtype=tracklist&q='+query);
      } else if(ftype=="download"){
			  ajax.post('dynasearch.php',update,'vtype=download&q='+query);
      } else if(ftype=="userse"){ 
        ajax.post('dynasearch.php',update,'vtype=userse&q='+query);
      } else if(ftype=="userfu"){ 
        ajax.post('dynasearch.php',ultrauserupdate,'vtype=userfu&q='+query);
			}
		} else {
			update('');
		}
	}
}

function update(result) {
	arr = new Array();
	arr = result.split('\r\n');

	if (arr.length > 20) {
		count = 20;
	} else {
		count = arr.length;
	}
	suggdiv = document.getElementById("suggestions");
	suggcont = document.getElementById("suggcontainer");
	if (arr[0].length > 0) {
		suggcont.style.display = "block";
		suggdiv.innerHTML = '';
		suggdiv.style.height = count * 20;
	
		for (i = 1; i <= count; i++) {
			novo = document.createElement("div");
			suggdiv.appendChild(novo);
			novo.id = i;
			novo.style.height = "14px";
			novo.style.padding = "3px";
			novo.onmouseover = function() { select(this,true); }
			novo.onmouseout = function() { unselect(this,true); }
			novo.onclick = function() { choiceclick(this); }
			novo.innerHTML = arr[i-1];
		}
	} else {
		suggcont.style.display = "none";
		count = 0;
	}
}

function ultrauserupdate(result) {

	suggdiv = document.getElementById("suggestions");
	suggcont = document.getElementById("suggcontainer");
	if (result.length > 0) {
		suggcont.style.display = "block";
		suggdiv.innerHTML = '';
		suggdiv.style.height = '350';
		novo = document.createElement("div");
		suggdiv.appendChild(novo);
		novo.id = "resultssearch";
		novo.style.height = "14px";
		novo.style.padding = "3px";
		novo.innerHTML = result;
	} else {
		suggcont.style.display = "none";
		count = 0;
	}
}

function select(obj,mouse) {
	//obj.style.backgroundColor = '#C2D1DC';
	obj.style.color = '#000000';
	if (mouse) {
		pos = obj.id;
		unselectAllOther(pos);
	}
}

function unselect(obj,mouse) {
	//obj.style.backgroundColor = '#C2D1DC';
	obj.style.color = '#000000';
	if (mouse) {
		pos = 0;
	}
}

function goNext() {
	if (pos <= count && count > 0) {
		if (document.getElementById(pos)) {
			unselect(document.getElementById(pos));
		}
		pos++;
		if (document.getElementById(pos)) {
			select(document.getElementById(pos));
		} else {
			pos = 0;
		}
	}
}

function goPrev() {
	if (count > 0) {
		if (document.getElementById(pos)) {
			unselect(document.getElementById(pos));
			pos--;
			if (document.getElementById(pos)) {
				select(document.getElementById(pos));
			} else {
				pos = 0;
			}
		} else {
			pos = count;
			select(document.getElementById(count));
		}
	}
}

function choiceclick(obj) {
	document.getElementById("searchbox").value = obj.innerHTML;
	count = 0;
	pos = 0;
	suggcont = document.getElementById("suggcontainer");
	suggcont.style.display = "none";
	document.getElementById("searchbox").focus();
}

function closechoices() {
	suggcont = document.getElementById("suggcontainer");
	if (suggcont.style.display == "block") {
		count = 0;
		pos = 0;
		suggcont.style.display = "none";
	}
}

function unselectAllOther(id) {
	for (i = 1; i <= count; i++) {
		if (i != id) {
			//document.getElementById(i).style.backgroundColor = '#C2D1DC';
			document.getElementById(i).style.color = '#000000';
		}
	}
}

function downloadratereason(vote, reason, djid, vidid) {
	t = new Date();
	sdivid = vidid;
	if (reason.length > 5) {
	ajax.post('ajaxrate.php',myupdate,'vtype=download&vote='+vote+'&reason='+reason+'&dl='+djid+'&hash='+vidid);
	}
}

function downloadrate(vote, djid, vidid) {
	t = new Date();
	sdivid = vidid;
	ajax.post('ajaxrate.php',myupdate,'vtype=download&vote='+vote+'&dl='+djid+'&hash='+vidid);
	
}

function galleryrate(vote, djid, vidid) {
	t = new Date();
	sdivid = vidid;
	ajax.post('ajaxrate.php',myupdate,'vtype=gallery&vote='+vote+'&image='+djid+'&hash='+vidid);
	
}

function myupdate(result) {

	suggdiv = document.getElementById(sdivid);
	suggdiv.innerHTML = result;

}


function addIntoResult(result) {
	arr = new Array();
	arr = result.split('\r\n');

	if (arr.length > 10) {
		count = 10;
	} else {
		count = arr.length;
	}

	suggdiv = document.getElementById("suggestions");
	suggcont = document.getElementById("suggcontainer");
	if (arr[0].length > 0) {
		suggcont.style.display = "block";
		suggdiv.innerHTML = '';
		//suggdiv.style.height = count * 10;
		suggdiv.style.height = theDivHeight;
		for (i = 1; i <= count; i++) {
			novo = document.createElement("div");
			suggdiv.appendChild(novo);
			novo.id = i;
			novo.style.height = "35px";
			novo.style.padding = "3px";
			novo.innerHTML = arr[i-1];
		}
	} else {
		suggcont.style.display = "none";
		count = 0;
	}
}

function addIntoInput(obj, field) {
	addInputID = document.getElementById(field);
	addInputID.value = obj;
	count = 0;
	pos = 0;
	suggcont = document.getElementById("suggcontainer");
	addInputID.focus();
}

function selectTitle(key,query,tt,field) {

	if (key == 38) {
		goPrev();
	} else if (key == 40) {
		goNext();
	} else if (key != 13) {
		if (query.length > 2) {
			t = new Date();
			addInputID = field;
			// Remove Bad Chars (Script Side)
			query = query.replace(/[&\\?]/g, "");
			theDivHeight = '500';			
			ajax.post('cchashahadmin.php',addIntoResult,'q='+query+'&t='+tt);
		} else {
			addIntoResult('');
		}
	}
}

function gallerySearch(key,query,tt,field) {

	if (key == 38) {
		goPrev();
	} else if (key == 40) {
		goNext();
	} else if (key != 13) {
		if (query.length > 2) {
			t = new Date();
			addInputID = field;
			// Remove Bad Chars (Script Side)
			query = query.replace(/[&\\?]/g, "");
			theDivHeight = '500';
			ajax.post('dynasearch.php',addIntoResult,'q='+query+'&vtype='+tt);
		} else {
			addIntoResult('');
		}
	}
}

  var ttcont = 0;
  var ttbelow = 0;
  var plcont = 0;
  var plbelow = 0;
    
function stationUpdate(key,query,cont,below) {

  ttcont = cont;
  ttbelow = below;
  
	if (key == 38) {
		goPrev();
	} else if (key == 40) {
		goNext();
	} else if (key != 13) {
		if (query.length < 3) {
			t = new Date();
			query = query.replace(/[&\\?]/g, "");
			theDivHeight = '10px';
			ajax.post('stream.php',addIntoStationResult,'q='+query+'&c='+ttcont);
		} else {
			addIntoResult('');
		}
	}
}

function stationLoad(key,query,cont,below) {

  ttcont = cont;
  ttbelow = below;
  
	if (key == 38) {
		goPrev();
	} else if (key == 40) {
		goNext();
	} else if (key != 13) {
		if (query.length < 3) {
			t = new Date();
			query = query.replace(/[&\\?]/g, "");
			theDivHeight = '10px';
			ajax.post('stream.php',playerLoad,'q='+query+'&c='+ttcont+'&fo=loadRadio');
		} else {
			addIntoResult('');
		}
	}
}

function playerLoad(result) {
  
	suggdiv = document.getElementById('radioPlayer');
	suggcont = document.getElementById('radioCont');  

  suggcont.style.display = "inline";
	suggdiv.innerHTML = '';
	suggdiv.style.height = theDivHeight;

	novo = document.createElement("div");
	suggdiv.appendChild(novo);
	novo.id = 'div'+result;
	novo.style.height = "30px";
	novo.style.padding = "3px";
	novo.innerHTML = '<embed src="player.swf" width="400" height="20" bgcolor="#ffffff" allowscriptaccess="always" allowfullscreen="false" flashvars="file=http://www.crystalclouds.com/forum/playlist/radio'+result+'.xml"/>';

  ttcont = 0;
  ttbelow = 0;	
  
}

function addIntoStationResult(result) {


	arr = new Array();
	arr = result.split('\r\n');

	if (arr.length > 10) {
		count = 10;
	} else {
		count = arr.length;
	}

	suggdiv = document.getElementById(ttbelow);
	suggcont = document.getElementById(ttcont);
	if (arr[0].length > 0) {
		suggcont.style.display = "inline";
		suggdiv.innerHTML = '';
		suggdiv.style.height = theDivHeight;
		for (i = 1; i <= count; i++) {
			novo = document.createElement("div");
			suggdiv.appendChild(novo);
			novo.id = i;
			novo.style.height = "15px";
			novo.style.padding = "3px";
			novo.innerHTML = arr[i-1];
		}
	} else {
		suggcont.style.display = "none";
		count = 0;
	}
	
  ttcont = 0;
  ttbelow = 0;	
	
}


function aboutusUpdateView(field1, field2) {
	firstID = document.getElementById(field1); 
	secondID = document.getElementById(field2); 
	firstID.value = "";
	firstID.style.display = "none";
	secondID.style.display = "block";	
}


