var AjaxSearchReq = false;
var t = null;
var AjaxSearchLast = "";



var KEY_PRESS_DOWN = 40;
var KEY_PRESS_UP = 38;
var KEY_PRESS_ESC = 27;
var KEY_PRESS_ENTER = 13;

var list = null;
var prevVal = "";
var sele = -1;
var f = null;
Element.addMethods({  
  getInnerText: function(element) {
    element = $(element);
    return element.innerText && !window.opera ? element.innerText
      : element.innerHTML.stripScripts().unescapeHTML().replace(/[\n\r\s]+/g, ' ');
  }
});

function ajax_search(_f){
	f = $(_f);
	ajaxf = f.form;
	if(f.value.length <= 2) return;
	if(f.value == prevVal) return;
	prevVal = f.value;
	if(list){
		list.remove(); list = null;
	}
	sele = -1;
	
	f.stopObserving('keydown');
	f.observe('keydown', kd);
	new Ajax.Request("/ajax_bandlist.php?query=" + f.value, {
			method: 'get',
			onSuccess: function(transport) {
				if(list){
					list.remove(); list = null;
				}
				
				list = new Element("div");
				list.setStyle({
					position: "absolute",
					top: (f.positionedOffset()[1] + f.getHeight()) + "px", 				
					left: f.positionedOffset()[0] + "px",
					padding: "2px",
					backgroundColor: "white"
				});
				list.update(transport.responseText);
				f.insert({ after: list });
				
			}
		}
	);


}	

kd = function(eve){
	if(
		eve.keyCode != KEY_PRESS_DOWN &&
		eve.keyCode != KEY_PRESS_UP &&
		eve.keyCode != KEY_PRESS_ENTER &&
		eve.keyCode != KEY_PRESS_ESC
	) return;
	
	if(!list) return;
	var elems = list.getElementsBySelector("dd");
	
	if(eve.keyCode == KEY_PRESS_DOWN){
		sele++;
		if(sele >= elems.length) sele = 0;
	}
	if(eve.keyCode == KEY_PRESS_UP){
		sele--;
		if(sele < 0) sele = elems.length - 1;
	}
	if(eve.keyCode == KEY_PRESS_ENTER){
		var aitm = elems[sele].getElementsBySelector("a");
		if(aitm.length > 0){
			aitm = aitm[0];
			location.href = aitm.getAttribute("href");
			eve.stop();
		}
	}

	if(eve.keyCode == KEY_PRESS_ESC){
		f.value = '';
		list.remove();
		list = null;
		sele = -1;
	}
		
	
	
	for(var i = 0; i < elems.length; i++){
		if(i == sele){
			elems[i].addClassName('AjaxHighlight');
		}else{
			elems[i].removeClassName('AjaxHighlight');
		}
	}
	return false;
};

function AjaxSbmt(el){
		f.value = $(el).getInnerText();
		f.form.submit();
}

















var isIE = false;
// on !IE we only have to initialize it once
if (window.XMLHttpRequest) {
	AjaxSearchReq = new XMLHttpRequest();
}

function AjaxSearchInit() {
	if (navigator.userAgent.indexOf("Safari") > 0) {
		$('ajaxsearch').addEventListener("keydown",AjaxSearchKeyPress,false);
	} else if (navigator.product == "Gecko") {
		$('ajaxsearch').addEventListener("keypress",AjaxSearchKeyPress,false);
		$('ajaxsearch').addEventListener("blur",AjaxSearchHideDelayed,false);
	} else {
		$('ajaxsearch').attachEvent('onkeydown',AjaxSearchKeyPress);
		isIE = true;
	}
	$('ajaxsearch').setAttribute("autocomplete","off");
}

function AjaxSearchHideDelayed() {
	window.setTimeout("AjaxSearchHide()",500);
}

function AjaxSearchKeyPress(event) {
	if (event.keyCode == KEY_PRESS_DOWN ){
		highlight = $("AjaxHighlight");
		if (!highlight) {
			highlight = $("AjaxShadow").firstChild.firstChild;
		} else {
			highlight.removeAttribute("id");
			highlight = highlight.nextSibling;
		}
		if (highlight) {
			highlight.setAttribute("id","AjaxHighlight");
		} 
		if (!isIE) { event.preventDefault(); }
	} else if (event.keyCode == KEY_PRESS_UP ) {
		highlight = $("AjaxHighlight");
		if (!highlight) {
			highlight = $("AjaxResult").firstChild.firstChild.lastChild;
		} 
		else {
			highlight.removeAttribute("id");
			highlight = highlight.previousSibling;
		}
		if (highlight) {
				highlight.setAttribute("id","AjaxHighlight");
		}
		if (!isIE) { event.preventDefault(); }
	}else if (event.keyCode == KEY_PRESS_ESC) {
		highlight = $("AjaxHighlight");
		if (highlight) {
			highlight.removeAttribute("id");
		}
		$("AjaxResult").style.display = "none";
	}else if (event.keyCode == KEY_PRESS_ENTER) {
		highlight = $("AjaxHighlight");
		if (highlight){
			AjaxSearchSubmit();
		}else return false;
	} 
}

function AjaxSearchHide() {
	$("AjaxResult").style.display = "none";
	var highlight = $("AjaxHighlight");
	if (highlight) {
		highlight.removeAttribute("id");
	}
}


function StartAjaxSearch() {
	if (t) {
		window.clearTimeout(t);
	}
	t = window.setTimeout("DoAjaxSearch()",200);
}
function DoAjaxSearch() {
	if (typeof AjaxSearchRoot == "undefined") {
		AjaxSearchRoot = "";
	}
	if (typeof AjaxSearchRootSubDir == "undefined") {
		AjaxSearchRootSubDir = "";
	}
	if (typeof AjaxSearchParams == "undefined") {
		AjaxSearchParams = "";
	}
	if (AjaxSearchLast != document.forms.srch.searchparams.value) {
	if (AjaxSearchReq && AjaxSearchReq.readyState < 4) {
		AjaxSearchReq.abort();
	}
	if ( document.forms.srch.searchparams.value == "") {
		AjaxSearchHide();
		return false;
	}
	if (window.XMLHttpRequest) {
	} else if (window.ActiveXObject) {
		AjaxSearchReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	AjaxSearchReq.onreadystatechange= AjaxSearchProcessReqChange;
	AjaxSearchReq.open("GET", AjaxSearchRoot + "ajax_bandlist.php?query=" + document.forms.srch.searchparams.value + AjaxSearchParams);
	AjaxSearchLast = document.forms.srch.searchparams.value;
	AjaxSearchReq.send(null);
	}
}

function AjaxSearchProcessReqChange() {
	if (AjaxSearchReq.readyState == 4) {
		if(AjaxSearchReq.responseText.length > 70){
			var res = $("AjaxResult");
			res.style.display = "block";
			var sh = $("AjaxShadow");
			sh.innerHTML = AjaxSearchReq.responseText;
		}					 
	}else AjaxSearchHide();
}

function AjaxSearchSubmit() {
	alert("testi");
	var highlight = $("AjaxHighlight");
	if (highlight && highlight.firstChild) {
		document.forms.srch.searchparams.value = highlight.firstChild.innerHTML;
		document.forms.srch.submit();
		return false;
	} 
	else {
		return true;
	}
}

function closeResults() {
    $("AjaxResult").style.display = "none";
}
function popup(url, width, height){
	win = (window.open(url,"window","toolbar=0,directories=0,menubar=0,status=no,resizable=yes,location=0,scrollbars=yes,copyhistory=0,width=" + width + ",height=" + height));
	if(win && !(win.opener)) win.opener=window;
	win.focus();
	return false;
}


Array.prototype.inArray = function (value)
// Returns true if the passed value is found in the
// array.  Returns false if it is not.
{
var i;
for (i=0; i < this.length; i++) {
    // Matches identical (===), not just similar (==).
    if (this[i] == value) {
        return true;
    }
}
return false;
};

function createAjaxCaller(){
	var request_o; //declare the variable to hold the object.
	var browser = navigator.appName; //find the browser name
	if(browser == "Microsoft Internet Explorer"){
		/* Create the object using MSIE's method */
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		/* Create the object using other browser's method */
		request_o = new XMLHttpRequest();
	}
	return request_o; //return the object			
}

function gotoshop(fo){
	var typesel;
	var letter;
	var section;
	
	if(fo.sel.options[fo.sel.selectedIndex].value == "shopinfo") parent.location = "/shopinfo.php";
	if(fo.sel.options[fo.sel.selectedIndex].value == "postages") parent.location = "/shopinfo.php#postages";
	if(fo.sel.options[fo.sel.selectedIndex].value == "howtoorder") parent.location = "/shopinfo.php#howtoorder";
	if(fo.sel.options[fo.sel.selectedIndex].value == "checkout") parent.location = "/checkout.php";
	if(fo.sel.options[fo.sel.selectedIndex].value == "viewbytag") parent.location = "/viewbytag.php";
	
	arr = fo.sel.options[fo.sel.selectedIndex].value.split(";");
	if(arr.length != 3) return false;
	typesel = arr[0];
	letter = arr[1];
	section = arr[2];

	if(section.length > 0){
		parent.location = "/shop.php?section=" + section;
	} else parent.location = "/shop.php?typesel=" + typesel + "&letter=" + letter;
	return false;
}

function togglevisibility(s, i){
	if(i == 1){
		if($('smallimg_href_' + s) && $('smallimg_placeholder_' + s)){
			var sip = $('smallimg_placeholder_' + s);
			sip.src = $('smallimg_href_' + s).value;
			sip.show();
			//Element.observe(sip, 'load', function(){ sip.show();});
		}
		
		ele = $('description'+s);
		ele.style.display = '';
	
/*		ele = $('show'+s);
		ele.style.display = 'none';
	
		ele = $('hide'+s);
		ele.style.display = '';
	*/
	}else{
		ele = $('description'+s);
		ele.style.display = 'none';
	/*
		ele = $('show'+s);
		ele.style.display = '';
	
		ele = $('hide'+s);
		ele.style.display = 'none';
		*/
	}
}






function changeallany(n){
	if(n != 0 && n != 1) return false;
	allany = n;
	document.getElementById('all').className = 'taglink_' + (n == 0 ? "off": "on");
	document.getElementById('any').className = 'taglink_' + (n == 1 ? "off": "on");
	document.cookie = "allany=" + n;
}

function setTagOnOff(ctrl,tagid){
	if(ctrl){
		if(tags_arr.inArray(tagid)){
			for(i = 0; i < tags_arr.length; i++){
				if(tags_arr[i] == tagid){
					tags_arr.splice(i,1);
				}
			}
			ctrl.className = 'taglink_off';
		}else{
			tags_arr.push(tagid);
			ctrl.className = 'taglink_on';
		}
	}		
	document.cookie = "seltags=" + tags_arr.join('|');
}


var http = createAjaxCaller();
var allany = getCookie('allany');
var tags_arr = new Array();

function ajax_fetch(){
	$('tagrecords').innerHTML = 'Please wait, fetching results!';
	
	if(http){
		if(http.readyState != 4 && http.readyState != 1){
			http.abort();
		}
	}
	http = createAjaxCaller();
	http.open('get', 'ajax_viewbytag.php?id=' + tags_arr.join('|') + "&amp;aa=" + allany);
	http.onreadystatechange = ajax_parse; 
	http.send(null);
}

function ajax_parse(){
	if(http.readyState == 4){
		var response = http.responseText;
		$('tagrecords').innerHTML = response;
	}
}
function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
	  begin = dc.indexOf(prefix);
	  if (begin != 0) return null;
	} else
	  begin += 2;
	var end = document.cookie.indexOf(";", begin);
	if (end == -1)
	  end = dc.length;
	return unescape(dc.substring(begin + prefix.length, end));
}

