
function advancedDisable() {
	document.getElementById("advancedCell").style.display = "none";
	document.getElementById("advancedButton").style.display = "inline";
	document.getElementById("simpleButton").style.display = "none";
	disableAdvancedForm(true);
}
function advancedEnable() {
	document.getElementById("advancedCell").style.display = "inline-block";
	document.getElementById("advancedButton").style.display = "none";
	document.getElementById("simpleButton").style.display = "inline";
	disableAdvancedForm(false);
}
function disableAdvancedForm(status) {
	var inputs = document.getElementById("advancedCell").getElementsByTagName("input");
	var selects = document.getElementById("advancedCell").getElementsByTagName("select");
	for (var i = 0; i < inputs.length; i++) {
		inputs[i].disabled = status;
	}
	for (var i = 0; i < selects.length; i++) {
		selects[i].disabled = status;
	}
}
function countCookie() {
	var now = new Date();
	var x = "count=" + document.woogleForm.count.value + ((document.woogleForm.countSave.checked) ? ("; expires=" + (now.getTime() + 1000 * 60 * 60 * 24 * 365 * 5)) : "");
	document.cookie = x;
}

/* Function called when pressing the "search" button in Woogle */
function callSearch() {
	var q = "?ref=wiki";
	
	/* Check if advanced cell exists */
	var aCell = document.getElementById("advancedCell");
	if (aCell != null){

		/* Iterate html input fields from advanced search */
		var inputs = aCell.getElementsByTagName("input");
		if (inputs != null){
			for (var i = 0; i < inputs.length; i++) {
				if (inputs[i].name != "countSave") {
					q += getInputQueryString(inputs[i]);
				}
			}
		}
		
		/* Iterate html select fields from advanced search */
		var selects = document.getElementById("advancedCell").getElementsByTagName("select");
		if (selects != null){
			for (var i = 0; i < selects.length; i++) {
				if (selects[i].name != "count") {
					q += getInputQueryString(selects[i]);
				}
			}
		}
	}
	
	if (document.woogleForm.search.value != "") {
		window.location.href = "/wiki/index.php/Woogle:" + URLEncode(document.woogleForm.search.value) + q;
	}
	return false;
}
function getInputQueryString(input) {
	if (!input.disabled && input.value != "" && (input.type != "checkbox" || input.checked)) {
		if (input.type == "select-multiple") {
			var qs = "";
			for (var i = 0; i < input.length; i++) {
				if (input.options[i].selected) {
					qs += "&" + input.name + "=" + URLEncode(input.options[i].value);
				}
			}
			return qs;
		}
		else {
			return "&" + input.name + "=" + URLEncode(input.value);
		}
	}
	else {
		return "";
	}
}
function URLEncode (clearString) {
  var output = "";
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != "") {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == " ")
        output += "+";
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += "%" + ( hexVal.length < 2 ? "0" : "" ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}
function URLDecode (encodedString) {
  var output = encodedString.replace(/\+/g, " ");
  var binVal, thisString;
  var myregexp = /(%[^%]{2})/;
  while ((match = myregexp.exec(output)) != null
             && match.length > 1
             && match[1] != "") {
    binVal = parseInt(match[1].substr(1),16);
    thisString = String.fromCharCode(binVal);
    output = output.replace(match[1], thisString);
  }
  return output;
}
function trim (zeichenkette) {
  return zeichenkette.replace(/^\s+/, "").replace(/\s+$/, "");
}

$(document).ready(function() {

	function openInNewTab() {
		
		var tooltip_options = {
			position: "right",
			fadeInTime: 200,
			fadeOutTime: 100,
			time: 100,
		}	
		$("span.woogle_resultlink").each( function() {
			$this = $(this);
			$temp = $this.clone();			
			$("a" , $temp).attr("target" , "_blank" ).text( $this.attr("title") ).removeAttr("title");
			
			var tooltip_html = $temp.html();		
			$this.tooltip( tooltip_html , tooltip_options );
		});	
	}
	
	openInNewTab();
});

