window.onload = initall;
var obj = "";	

function initall(){
	
	if(screen.width <= 699){
	  var tag = document.createElement("h4");
	  tag.setAttribute('style', 'color:red;');
	  var text = document.createTextNode("Byt till den mobila hemsidan.");	
	  tag.appendChild(text);
	  tag.onmouseover = function(){
		tag.style.cursor = "pointer";
	  }
	  tag.onclick = function(){
		del_cookie('view');
		window.location = "http://www.synonymer.se/mobil/?view=mobil";
	  }	  
	  var h2 = document.getElementsByTagName('h2');
	  h2[0].parentNode.insertBefore(tag,h2[0]);
	}	

	document.frmSyn.query.focus();
	var intialAC = document.getElementById('dropLanguage').value;
	var intialAC = changeAutoComplete(intialAC);
	obj = new Ajax.Autocompleter('query','update',intialAC, {afterUpdateElement : updateForm});
		
		obj.url = intialAC;
		function updateForm() {
			document.frmSyn.submit();
		}
	
	var results = document.getElementById('results');
	var itemsAdj = document.getElementsByName('itemAdj');
	var itemsSub = document.getElementsByName('itemSub');
	var itemsVerb = document.getElementsByName('itemVerb');
	
	var max = 2;
	
	minimize(max, itemsAdj);
	minimize(max, itemsVerb);
	minimize(max, itemsSub);
	
}

function sveClick2(){
	var sve = document.getElementById('sveChoice');
	sve.setAttribute('disabled','disabled');
	//sve.setAttribute('selected','false');
	//return false;
}

function otherClick2(){
	
	
	var sve = document.getElementById('sveChoice');
	sve.removeAttribute('disabled',0);
	//sve.setAttribute('selected','false');
	//return false;
}

  
  function changeAutoComplete(query){
				
		var lang = document.getElementById('dropLanguage').value;
		file = "";	
		
		if(lang == 'svesyn' || lang == "" || lang == 'null'){
			file = 'response.php';
			sveClick2();
			}
		else if(lang == 'engsyn'){		
			file = 'responseEngSyn.php';
			otherClick2();
			}
		else if(lang == 'engsve'){	
			file = 'responseEngSve.php';
			otherClick2();
			}
		else if(lang == 'sveeng'){	
			file = 'responseSveEng.php';
			otherClick2();
			}
		else if(lang == 'frasve'){
			file = 'responseFreSve.php';
			otherClick2();
			}
		else if(lang == 'svefra'){
			file = 'responseSveFre.php';
			otherClick2();
			}
		else if(lang == 'gersve'){
			file = 'responseGerSve.php';
			otherClick2();
			}
		else if(lang == 'frasve'){
			file = 'responseFreSve.php';
			otherClick2();
			}
		else if(lang == 'sveger'){
			file = 'responseSveGer.php';
			otherClick2();
			}
		else if(lang == 'spasve'){
			file = 'responseSpaSve.php';
			otherClick2();
			}	
		else if(lang == 'svespa'){
			file = 'responseSveSpa.php';
			otherClick2();
			}
		
		obj.url = file;
		
		return file;
	}
  

function minimize( max,  thediv){
	
	if(thediv.length > max){
			
			
			for( i = max; i < thediv.length; i++){
				thediv[i].style.display = "none"; //to hide it
			
			}
			
			var hiding = thediv.length - max;
			
			
			
			var p = document.createElement('i');
			var innerp = document.createTextNode('Se fler definitioner ('+hiding+')');
			p.appendChild(innerp);
			p.style.color = "red";
			
			var p2 = document.createElement('i');
			var innerp2 = document.createTextNode('Se färre definitioner');
			p2.appendChild(innerp2);
			p2.style.color = "red";
			
			thediv[max-1].appendChild(p);
			thediv[thediv.length-1].appendChild(p2); 
			
			p.onclick = function(){
				p.style.display = "none"; // hide the fler sammanhang
				p2.style.display = "block";
				for( i = max; i < thediv.length; i++){
						thediv[i].style.display = "block"; //to show it
				} 

			}
			
			p.onmouseover = function(){
				p.style.color = "green"; // hide the fler sammanhang
				p.style.cursor= 'pointer';
			}
			p.onmouseout = function(){
				p.style.color = "red"; // hide the fler sammanhang
			}
			
			p2.onclick = function(){
				p2.style.display = "none"; // hide the fler sammanhang
				p.style.display = "block";
				for( i = max; i < thediv.length; i++){
						thediv[i].style.display = "none"; //to show it
				} 

			}
			
			p2.onmouseover = function(){
				p2.style.color = "green"; // hide the fler sammanhang
				p2.style.cursor= 'pointer';
			}
			p2.onmouseout = function(){
				p2.style.color = "red"; // hide the fler sammanhang
			}
		
		}

}

function del_cookie(name) {
	document.cookie = name +
	'=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
} 

function getCookie(c_name)
			{
			var i,x,y,ARRcookies=document.cookie.split(";");
			for (i=0;i<ARRcookies.length;i++)
			{
			  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
			  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
			  x=x.replace(/^\s+|\s+$/g,"");
			  if (x==c_name)
				{
				return unescape(y);
				}
			  }
			}

function setCookie(c_name,value,exdays)
			{
				var exdate=new Date();
				exdate.setDate(exdate.getDate() + exdays);
				var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
				document.cookie=c_name + "=" + c_value;
			}

function decode(utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
		
	
