// initialize autocomplete and show text in textarea

function autoFill(id, v){
	$(id).css({ color: "#b2adad" }).attr({ value: v }).focus(function(){
		if($(this).val()==v){
			$(this).val("").css({ color: "#333" });
		}
	}).blur(function(){
		if($(this).val()==""){
			$(this).css({ color: "#b2adad" }).val(v);
		}
	});
}

function selectItem(li) {
	
}

function formatItem(row) {
	return '<a href="'+row[0]+'">'+row[1]+'</a>';
}

$(document).ready(function() {

	

	
	// autocomplete
	autoFill($("#objectsearch"), "Ange gata, stad eller kommun");
	
$("#loginlink").click( function() {
	return false;
});
$("#loginlink").qtip({
   content: {
	url: '/ajax/login.php',
	title: {
		text: '',
		button: 'Stäng'
    }
   },
   show: {
		when: 'click'
   },
   hide: 'change',   
   style: { 
		width: 250,
		tip: 'topMiddle',

		border: {
			width: 1,
			radius: 5
		}		
		
		}, 
	position: {
		corner: {
			target: 'bottomLeft'
		},
		adjust: {
			x: -100
		}
	}
	
	
  });
	

	
	
	$("#objectsearch").autocomplete("/ajax/autosearch.php", {
	delay: 150, minChars: 3, width: 350, selectFirst: false,
	formatItem: function (row){
		return row[1];
	},
	formatResult: function (row){return "Laddar...";}
	});
	
	$("#objectsearch").result(function(event, data, formatted) {
		$("#objectsearch").attr("disabled", "disabled");
		document.location.href = data[0];
	});
	
	
	// delete favourite
	$('img.deletefavourite').click( function() {
		if(confirm('Vill du ta bort favoriten?')) {
			$(this).parent().parent().remove();
			$.post("/ajax/delete_favourite.php",
				{ id: $(this).attr('id'), uid: $(this).attr('alt') }, 
					function () {
					}
			);	 
		}
		else {
			return false;
		}
		
	});
	
});

var poste = "";
var isquote = 0;
var name = "";
var fullps = "";

function quote(value,val2){
	var item = "p-"+value;
	var myRegExp = /<div class="quote_indent">/;
	var myRegExp2 = /<DIV class="quote_indent">/;
	var myRegExp3 = /<DIV class=quote_indent>/;
	name = val2;
	poste = document.getElementById(item).innerHTML;
	
	var nposte = poste.replace(/<br>/g, "");
	var nposte = nposte.replace(/<BR>/g, "");
	poste = nposte;
	
	isquote = 1;
	var matchPos1 = poste.search(myRegExp);
	var matchPos2 = poste.search(myRegExp2);
	var matchPos3 = poste.search(myRegExp3);
	
	if(matchPos1 != -1 || matchPos2 != -1 || matchPos3 == 0)	{
		if(matchPos3 == 0) {
				var crpost = poste.split('<DIV class=quote_indent>');
				var headlp = crpost[0];
				var lpnq = crpost[1].split('</DIV></DIV>');
				var lpqnok = lpnq[1];			
		} else {
			if(matchPos1 == -1) {
				var crpost = poste.split('<DIV class="quote_indent">');
				var headlp = crpost[0];
				var lpnq = crpost[1].split('</DIV></DIV>');
				var lpqnok = lpnq[1];
			} else {
				var crpost = poste.split('<div class="quote_indent">');
				var headlp = crpost[0];
				var lpnq = crpost[1].split('</div></div>');
				var lpqnok = lpnq[1];
			}
		}
	}
	else{
		var lpqnok = poste;
	}
	
	fullps = '<div class="quote_indent">Citerar kommentar från <b>'+name+'</b>:<div class="quote">"'+lpqnok+'"</div></div>';
	document.getElementById("citfield").innerHTML = 'Citerar kommentar från <b>'+name+'</b>:<div class="prquote">"'+lpqnok+'"</div>';
	
	//document.getElementById("nqt").innerHTML = document.getElementById("nqt").innerHTML+'<br><br>';
}

function msjsend(){
	var text = document.getElementById("messh").value;
	
	text = text.replace(/([^\s\t\r\n<>]{5})/g, '$1<wbr>');
	
	var rone = document.getElementById("mess");
	if(isquote != 0)
	{
		rone.value = fullps+'<br/><br/>'+text;
	}
	else
	{
		rone.value = text;
	}
	return true;
}