/*
* Author:      Marco Kuiper (http://www.marcofolio.net/)
*/
var SearchUrl = "0";
var PortalId = "0";
google.load("jquery", "1.3.1");
google.setOnLoadCallback(function()
{
	// Safely inject CSS3 and give the search results a shadow
	var cssObj = { 'box-shadow' : '#888 5px 10px 10px', // Added when CSS3 is standard
		'-webkit-box-shadow' : '#888 5px 10px 10px', // Safari
		'-moz-box-shadow' : '#888 5px 10px 10px'}; // Firefox 3.5+
	$("#suggestions").css(cssObj);
	
	// Fade out the suggestions box when not active
	 $("input").blur(function(){
	 	$('#suggestions').fadeOut();
	 });
});

var timerLokup=0;

function lookup(input) {
    if (timerLokup != 0)
        clearTimeout(timerLokup);
    timerLokup = setTimeout(function() { lookupPreview(input); }, 60*6);
}

function lookupPreview(input) {
    var inputString = input.value;
    if (inputString.length <= 3) {
        $('#suggestions').fadeOut(); // Hide the suggestions box
    } else {

        // get the position of the input field right now (in case the DOM is shifted)
        var pos = findPos(input);
        // either use the specified width, or autocalculate based on form element
        //var iWidth = (options.width > 0) ? options.width : $input.width();
        // reposition
       

        $('#suggestions').css({
            top: (pos.y + input.offsetHeight) + "px",
            left: pos.x + "px"
        });


        $.post(SearchUrl, { queryString: "" + inputString + "", PortalID: "" + PortalId + "" }, function(data) { // Do an AJAX call
            $('#suggestions').fadeIn(); // Show the suggestions box
            $('#suggestions').html(data); // Fill the suggestions box
        });
    }
}

function findPos(obj) {
    var curleft = obj.offsetLeft || 0;
    var curtop = obj.offsetTop || 0;
    while (obj = obj.offsetParent) {
        curleft += obj.offsetLeft
        curtop += obj.offsetTop
    }
   
    if (curleft + 320 > document.body.clientWidth) {
        curleft = document.body.clientWidth - 325;
        }
    
    return { x: curleft, y: curtop };
}
