

window.onresize  = OnResize;
document.onclick = OnClick;

function OnResize() { 
	
    document.getElementById('top_result').style.left = ax_findPosX(document.getElementById('top_search')) - 198;
    document.getElementById('top_result').style.top  = ax_findPosY(document.getElementById('top_search')) + 18;
    return false;
}


function OnClick() {

     document.getElementById('top_result').style.visibility = 'hidden';
}



function ax_findPosX(obj) {
	
    var curleft = 0;
    if (obj.offsetParent) {
        while (1) {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
              break;
          obj = obj.offsetParent;
        }
    }
    else if (obj.x) {
        curleft += obj.x;
    }
    return curleft;
}


function ax_findPosY(obj) {
	
    var curtop = 0;
    if (obj.offsetParent) {
        while (1) {
            curtop += obj.offsetTop;
            if(!obj.offsetParent) {
                break;
            }
            obj = obj.offsetParent;
        }
    }
    else if(obj.y) {
        curtop += obj.y;
    }
    return curtop;
}

        
document.getElementById('top_search').focus();

var seq = 0;

var ax_keyboard_marked_row_index = -1;
var ax_keyboard_marked_max_index = -1;


function ajax_search(event) {

    // handle keyup
    if (event.keyCode == 38) {
    	
    	// no results
    	if (ax_keyboard_marked_max_index == -1) {
    	    return;
    	}
    	
    	// no row marked, mark last
    	if (ax_keyboard_marked_row_index == -1) {
    	    return ax_keyboard_mark(ax_keyboard_marked_max_index);
    	}
    	
    	// first row marked, mark last
    	if (ax_keyboard_marked_row_index == 0) {
    	    return ax_keyboard_mark(ax_keyboard_marked_max_index);
    	}
    	
    	// some row marked, mark prior
  	return ax_keyboard_mark(ax_keyboard_marked_row_index - 1);
    }
    
    // handle keydown
    if (event.keyCode == 40) {
    	
    	// no results
    	if (ax_keyboard_marked_max_index == -1) {
    	    return;
    	}
    	
    	// no row marked, mark first
    	if (ax_keyboard_marked_row_index == -1) {
    	    return ax_keyboard_mark(0);
    	}
    	
    	// last row marked, mark first
    	if (ax_keyboard_marked_row_index == ax_keyboard_marked_max_index) {
    	    return ax_keyboard_mark(0);
    	}
    	
    	// some row marked, mark next
	return ax_keyboard_mark(ax_keyboard_marked_row_index + 1);
    }
    
    // handle escape
    if (event.keyCode == 27) {
	document.getElementById('top_result').style.visibility = 'hidden';
	return;
    }
    
    // handle enter
    if (event.keyCode == 13) {
    	
    	// only handle enter key if row marked
    	if (ax_keyboard_marked_row_index > -1) {
	    document.getElementById('ax').rows[ax_keyboard_marked_row_index].onclick();
	    return;
	}
    }
    
    
    // init ajax
    var xmlHttp;
            
    try { 
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
           xmlHttp = new ActiveXObject('Msxml2.XMLHTTP');
        }
        catch (e) {
            try {
                xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
            }
            catch (e) {
                alert('Your browser does not support AJAX!');
                return false;
            }
        }
    }

   
    // position results div properly
    OnResize();


    // update status 
    //document.getElementById('top_status').innerHTML = 'working...';


    // display results
    xmlHttp.onreadystatechange = function() {
  
        if (xmlHttp.readyState == 4) {
                
            document.getElementById('top_result').style.visibility = 'visible';
                                
            document.getElementById('top_result').innerHTML = '';
                
            /*
            arr = xmlHttp.responseText.split('###SPLIT###');
            for (i in arr) {
                document.getElementById('top_result').innerHTML += '<p>' + arr[i] + '</p>';
            }
            */
            
            // extract info
            info = xmlHttp.responseText.split('###SPLIT###');
            
            // skip results if too old
            if (info[0] < seq) {
            	document.getElementById('top_result').innerHTML = 'skipped';
	    }
            
            // update max index for keybord navigation and reset keyboard_marked_row
            ax_keyboard_marked_max_index = info[1];
    	    //ax_keyboard_marked_row_index = -1;
            
            // show results
            document.getElementById('top_result').innerHTML = info[2];
            
	    // change height            
            //document.getElementById('top_result').style.height = 100;
            
            //document.getElementById('top_status').innerHTML = 'done';
        }
    }
    
    
    // hide results (keywords)
    if (document.getElementById('top_search').value == '') {
        document.getElementById('top_result').style.visibility = 'hidden';
        //document.getElementById('top_status').innerHTML = 'Ready';
    }
    
    
    // request results 
    else {
    	seq++;
        var url='/external/scripts/ajax_search.php';
        url = url + '?search=' + document.getElementById('top_search').value + '&seq=' + seq;
        xmlHttp.open('GET', url, true);
        xmlHttp.send(null);
    }
}



// LIST VIEW AX
        
function ax_list_view_over(row)
{
    return ax_set_pointer(row, 'over');
}   


function ax_list_view_out(row)
{
    return ax_set_pointer(row, 'out');
}


function ax_list_view_click(row)
{
    return ax_set_pointer(row, 'click');
}


var ax_marked_row_index = -1;	 
 
function ax_set_pointer(row, action)
{
    // Define colours
    var default_colour = '#ffffff';
    var pointer_colour = '#f2f5f2';
    var marked_colour  = '#edf2ed';

    // Browser cannot get the row, exit
    if (typeof(row.style) == 'undefined') {
        return;
    }

    // Get the current row or exit
    var cells = null;
    if (typeof(document.getElementsByTagName) != 'undefined') {
        cells = row.getElementsByTagName('td');
    }
    else if (typeof(row.cells) != 'undefined') {
        cells = row.cells;
    }
    else {
        return;
    }

    // Detech COM ... with DOM compatible browsers except Opera that does not return  valid values with "getAttribute"
    dom_detect = typeof(window.opera) == 'undefined'  && typeof(cells[0].getAttribute) != 'undefined';

    // Count cells
    var row_cell_count = cells.length;
    
    // Determine what to do by action
    if (action == 'over') {

        // Current row is marked - disable over 
        if (row.rowIndex == ax_marked_row_index) {
            return;
        }
        
        // Mouse over - set pointer colour
        new_colour = pointer_colour;
    }
    else if (action == 'out') {
   
        // Current row is marked - disable over 
        if (row.rowIndex == ax_marked_row_index) {
            return;
        }
        
        // Mouse out - set default colour
        new_colour = default_colour;
    }
    else if (action == 'click') {

        // Unmark previous (if any)
        if (ax_marked_row_index > -1) {
            set_pointer(row.parentNode.rows[ax_marked_row_index], 'clear')
        }
        
        // Mouse click - set marked colour
        new_colour = marked_colour;
        
        // Save rowIndex
        ax_marked_row_index = row.rowIndex;
    }
    else if (action == 'clear') {
    	
    	// Clear marking - set default colour
        new_colour = default_colour;
    }
    else {
    	return;
    }

    // Set the new color...
    if (new_colour) {
    	var c = null;
	for (c = 0; c < row_cell_count; c++) {
	    
	    // ... with DOM compatible browsers except Opera
	if (dom_detect) {
	        cells[c].setAttribute('bgcolor', new_colour, 0);
	    } 
	    
	    // ... with other browsers
	    else {
	        cells[c].style.backgroundColor = new_colour;
	    }
	} 
    }

    return true;
} 


function ax_keyboard_mark(index)
{
    // unmark last row
    if (ax_keyboard_marked_row_index > -1) {
    	document.getElementById('ax').rows[ax_keyboard_marked_row_index].onmouseout();
    }
    
    // mark new row
    document.getElementById('ax').rows[index].onmouseover();

    // update index    
    ax_keyboard_marked_row_index = index;
}
