/*

Functionality for enabling livesearch in the main search field on nytnorge.no

Written by:

Espen Christensen
Hyper Interaktiv

*/


// Variables needed by script
var changeTimeout; 
var currentSelection = 0;
var currentUrl = '';


// Function that fetches search results and loads it into the search-result container
function update() {

    // If not search-result exists, add it
    if (!($("#search-result").length > 0)) {
        $('#nav').append('<div id="search-result"></div>');
        $('#search-result').hide();
    	$('#search-result').fadeIn("fast");
    }
    
    // Fetch the search term from input field
    var search_term = $("input#q").val();
    
    // Check length of search term, if there's nothing in the field, remove the search-result container
    if (!(search_term.length == 0)) {
        $("#search-result").load("/sok/live/?q=" + search_term, null, showResponse);
    }
    else {
        $('#search-result').remove();
    }
    
}

// function for fading in the search-result container when its done loading results
function showResponse() {
    /*
	$('#search-result').hide();
	$('#search-result').fadeIn("fast");
	*/
}


// Here we bind events and other DOM related operations
$(document).ready(function() {

    // Bind keyup event to searchfield
    $('#search').bind("keyup", function(e) {
        if (!(e.keyCode == 38 || e.keyCode == 40 || e.keyCode == 27 || e.keyCode == 13)) {
            update();
        }
    });
    
    
    // Enable users to remove search-result field, by clicking outside of the container
    $('body').click(function() {
        $('#search-result').remove();
    });
    


    // Register keypress events on the whole document
    $(document).keydown(function(e) {
        switch(e.keyCode) {
            // User pressed "up" arrow
            case 38:
                navigate('up');
            break;
            // User pressed "down" arrow
            case 40:
                navigate('down');
            break;
            // User pressed "esc"
            case 27:
                $('#search-result').remove();
            break;
            // User pressed "enter"
            case 13:
                if(currentUrl != '' || currentUrl != false) {
                    e.preventDefault();
                    window.location = currentUrl;
                }
            break;
        }
    });
    
    // Simulate the "hover" effect with the mouse
    $("#search-result ul li a").hover(function () {
        var currentSelection = $("#search-result ul li a" ).index(this);
        setSelected(currentSelection);
    }, function() {
        $("#search-result ul li a").removeClass("itemhover");
        currentUrl = '';
    });
    
});




// Function to navigate the search-result container
function navigate(direction) {

    // Check if any of the menu items is selected
    if($("#search-result ul li .itemhover").size() == 0) {
        currentSelection = -1;
    }
    
    if(direction == 'up' && currentSelection != -1) {
        if(currentSelection != 0) {
            currentSelection--;
        }
    }
    else if (direction == 'down') {
        if(currentSelection != $("#search-result ul li").size() -1) {
            currentSelection++;
        }
    }
    setSelected(currentSelection);
}

// Function to set hover state on selected element
function setSelected(menuitem) {
    $("#search-result ul li a").removeClass("itemhover");
    $("#search-result ul li a").eq(menuitem).addClass("itemhover");
    currentUrl = $("#search-result ul li a").eq(menuitem).attr("href");
    console.log(String(currentUrl));
    if (String(currentUrl) == 'undefined') {
        currentUrl = false;
    }
}