function addShadow(sId)
{
    var oPos   = $('#' + sId).offset();
    var iLeft  = oPos.left + 4;   
    var iTop   = oPos.top + 4;
    
    var iWidth  = $('#' + sId).outerWidth();
    var iHeight = $('#' + sId).outerHeight();
    
    if ((sId=='suggestions-departurePoint')||(sId=='suggestions-departurebPoint')||(sId=='suggestions-departurecPoint'))
    {
        var iLayer = 1;
    }
    else
    {
        var iLayer = 0;
    }
    
    $('body').append('<div id="' + sId + '-shadow"></div>');
    $('#' + sId + '-shadow').css('position'        , 'absolute');
    $('#' + sId + '-shadow').css('top'             , iTop);
    $('#' + sId + '-shadow').css('left'            , iLeft);
    $('#' + sId + '-shadow').css('width'           , iWidth);
    $('#' + sId + '-shadow').css('height'          , iHeight);
    $('#' + sId + '-shadow').css('background-color', '#CCCCCC');
    $('#' + sId + '-shadow').css('z-index'         , iLayer);
    
    $('#' + sId + '-shadow').css('-moz-opacity', '.50');
    $('#' + sId + '-shadow').css('opacity'     , '.50');
    $('#' + sId + '-shadow').css('filter'      , 'alpha(opacity=50)');    
}

function removeShadow(sId)
{
    $('#' + sId + '-shadow').remove();
}

function getSuggestions(sType, sLookup)
{
    $('#message-departurePoint').hide(); 
    $('#message-departurebPoint').hide(); 
    $('#message-departurecPoint').hide(); 
    $('#message-arrivalPoint').hide(); 
    $('#message-arrivalbPoint').hide(); 
    $('#message-arrivalcPoint').hide(); 
    
    $('#suggestions-' + sType + 'Point').hide();
    removeShadow('suggestions-' + sType + 'Point');

    if ( sLookup.length > 0 )
    {
        $('#loader-' + sType + 'Point').css('display', 'inline-block');
 
        var iTime = new Date().getTime();
        $('#time').val(iTime);         
 
        $.getJSON('/autocomplete/suggestions.php?lookup=' + sLookup + '&time=' + iTime, function(oResponse){
            if ( iTime >= $('#time').val() )
            {
                oAirports = oResponse; 
             
                if ( oAirports.length > 0 )
                {
                    aCountries = new Array();
                    aCodes     = new Array(); 
                    aAirports  = new Array();
                 
  
                        $('#suggestions-' + sType + 'Point').children().remove(); 
                    
                        for(var i = 0; i < oAirports.length; ++i)
                        {
                            var sAirport = oAirports[i]['airport'];
                            
                            var oRegex   = new RegExp(sLookup, 'i');
                            var sMatch   = oRegex.exec(sAirport);
                            var sAirport = sAirport.replace(oRegex, '<b>' + sMatch + '</b>');
                            
                            var sId = 'suggestion' +  i + '-' + sType + 'Point';
                            var sHtml = '<li><a href="#" id="' + sId + '" onclick="setSuggestion(\'' + sType + '\', ' + i + '); return false;" onmouseover="showSelect(\'' + sType + '\', ' + i + ')">' + sAirport + '</a></li>';
                            $('#suggestions-' + sType + 'Point').append(sHtml);                    
                        }


                        $('#suggestions-' + sType + 'Point').show();
                        addShadow('suggestions-' + sType + 'Point');
                }
                else
                {
                    $('#message-' + sType + 'Point').fadeIn('slow', function(){
                        $('#message-' + sType + 'Point').fadeOut('slow'); 
                    });
                }
            }
            
            $('#loader-' + sType + 'Point').hide();
        }); 
    }
}

function showSelect(sType, aiId)
{
    for(var i = 0; i < oAirports.length; ++i)
    {
        $('#suggestion' + i + '-' + sType + 'Point').css('background-color', '#FFFFFF');
        $('#suggestion' + i + '-' + sType + 'Point').css('color', '#000000')
    }
 
    $('#suggestion' + aiId + '-' + sType + 'Point').css('background-color', 'rgb(66, 111, 217)'); 
    $('#suggestion' + aiId + '-' + sType + 'Point').css('color', '#FFFFFF');

    
    iId = aiId;   
}

function setSuggestion(sType, iAirport)
{
    var oAirport = oAirports[iAirport];
    var sCountry = oAirport['country'];
    var sCode    = oAirport['code'];
    var sAirport = oAirport['airport'];

    $('#' + sType + 'PointCode').val(sCode);
    $('#' + sType + 'PointCountry').val(sCountry);
    $('#' + sType + 'Point').val(sAirport);
    
    $('#suggestions-' + sType + 'Point').hide();  
    removeShadow('suggestions-' + sType + 'Point');
}

function scroll(sType, e)
{    
    if ( e.keyCode == 40 )
    { 
        if ( iId < oAirports.length - 1 ) 
        {
            if ( iId > - 1 )
            { 
                $('#suggestion' + iId + '-' + sType + 'Point').css('background-color', '#FFFFFF');
                $('#suggestion' + iId + '-' + sType + 'Point').css('color', '#000000')
            }
        
            ++iId;
        
            $('#suggestion' + iId + '-' + sType + 'Point').css('background-color', 'rgb(66, 111, 217)');
            $('#suggestion' + iId + '-' + sType + 'Point').css('color', '#FFFFFF');
        }
    }
    else if ( e.keyCode == 38 )
    {
        if ( iId > 0 )
        { 
            $('#suggestion' + iId + '-' + sType + 'Point').css('background-color', '#FFFFFF'); 
            $('#suggestion' + iId + '-' + sType + 'Point').css('color', '#000000'); 
        
            --iId;
        
            $('#suggestion' + iId + '-' + sType + 'Point').css('background-color', 'rgb(66, 111, 217)');
            $('#suggestion' + iId + '-' + sType + 'Point').css('color', '#FFFFFF');
        }
    }
    else if ( e.keyCode == 13 )
    {
        if ( iId != -1 )
        {
            setSuggestion(sType, iId);    
        }
    }        
}

function load()
{
    if ( ReadCookie('departurePoint').length == 0)
    {   
        $('#departurePoint').css('color', '#CCCCCC');   
        $('#departurePoint').val('Please enter an airport you wish to fly from');
        
    }

    if ( ReadCookie('departurebPoint').length == 0)
    {   
        $('#departurebPoint').css('color', '#CCCCCC');   
        $('#departurebPoint').val('Please enter an airport you wish to fly from');
        
    }

    if ( ReadCookie('departurecPoint').length == 0)
    {   
        $('#departurecPoint').css('color', '#CCCCCC');   
        $('#departurecPoint').val('Please enter an airport you wish to fly from');
        
    }
    
    if ( ReadCookie('arrivalPoint').length == 0)
    {
        $('#arrivalPoint').css('color', '#CCCCCC');   
        $('#arrivalPoint').val('Please enter an airport you wish to fly to');      
    }

    if ( ReadCookie('arrivalbPoint').length == 0)
    {
        $('#arrivalbPoint').css('color', '#CCCCCC');   
        $('#arrivalbPoint').val('Please enter an airport you wish to fly to');      
    }

    if ( ReadCookie('arrivalcPoint').length == 0)
    {
        $('#arrivalcPoint').css('color', '#CCCCCC');   
        $('#arrivalcPoint').val('Please enter an airport you wish to fly to');      
    }
}

$(document).ready(function(){
    load();
 
    $('body').click(function(e){
        $('#suggestions-departurePoint').hide();
        $('#suggestions-departurebPoint').hide();
        $('#suggestions-departurecPoint').hide();
        $('#suggestions-arrivalPoint').hide(); 
        $('#suggestions-arrivalbPoint').hide(); 
        $('#suggestions-arrivalcPoint').hide(); 
        
        removeShadow('suggestions-departurePoint');    
        removeShadow('suggestions-arrivalPoint');
        removeShadow('suggestions-departurebPoint');    
        removeShadow('suggestions-arrivalbPoint');
        removeShadow('suggestions-departurecPoint');    
        removeShadow('suggestions-arrivalcPoint');
    });

    $('#departurePoint').click(function(e){ 
        if ( $('#departurePoint').val() == 'Please enter an airport you wish to fly from' )
        {
            $('#departurePoint').css('color', '#000000'); 
            $('#departurePoint').val('');
        }
    });
 
    $('#departurePoint').keydown(function(e){ 
        if ( $('#suggestions-departurePoint').css('display') != 'none' )
        {
            scroll('departure', e);
        }
    });
   
    $('#departurePoint').keyup(function(e){
        if ( e.keyCode != 38 && e.keyCode != 40 && e.keyCode != 13 )
        {
            iId = -1; 
         
            getSuggestions('departure', $('#departurePoint').val());  
        }      
    });

   $('#departurebPoint').click(function(e){ 
        if ( $('#departurebPoint').val() == 'Please enter an airport you wish to fly from' )
        {
            $('#departurebPoint').css('color', '#000000'); 
            $('#departurebPoint').val('');
        }
    });
 
    $('#departurebPoint').keydown(function(e){ 
        if ( $('#suggestions-departurebPoint').css('display') != 'none' )
        {
            scroll('departureb', e);
        }
    });
   
    $('#departurebPoint').keyup(function(e){
        if ( e.keyCode != 38 && e.keyCode != 40 && e.keyCode != 13 )
        {
            iId = -1; 
         
            getSuggestions('departureb', $('#departurebPoint').val());  
        }      
    });


   $('#departurecPoint').click(function(e){ 
        if ( $('#departurecPoint').val() == 'Please enter an airport you wish to fly from' )
        {
            $('#departurecPoint').css('color', '#000000'); 
            $('#departurecPoint').val('');
        }
    });
 
    $('#departurecPoint').keydown(function(e){ 
        if ( $('#suggestions-departurecPoint').css('display') != 'none' )
        {
            scroll('departurec', e);
        }
    });
   
    $('#departurecPoint').keyup(function(e){
        if ( e.keyCode != 38 && e.keyCode != 40 && e.keyCode != 13 )
        {
            iId = -1; 
         
            getSuggestions('departurec', $('#departurecPoint').val());  
        }      
    });


    $('#arrivalPoint').click(function(e){ 
        if ( $('#arrivalPoint').val() == 'Please enter an airport you wish to fly to' )
        {
            $('#arrivalPoint').css('color', '#000000'); 
            $('#arrivalPoint').val('');
        }
    });

    $('#arrivalPoint').keydown(function(e){
        if ( $('#suggestions-arrivalPoint').css('display') != 'none' )
        {
            scroll('arrival', e); 
        }
    });
    
    $('#arrivalPoint').keyup(function(e){
        if ( e.keyCode != 38 && e.keyCode != 40 && e.keyCode != 13 )
        {
            iId = -1;
         
            getSuggestions('arrival', $('#arrivalPoint').val());   
        } 
    });

    $('#arrivalbPoint').click(function(e){ 
        if ( $('#arrivalbPoint').val() == 'Please enter an airport you wish to fly to' )
        {
            $('#arrivalbPoint').css('color', '#000000'); 
            $('#arrivalbPoint').val('');
        }
    });

    $('#arrivalbPoint').keydown(function(e){
        if ( $('#suggestions-arrivalbPoint').css('display') != 'none' )
        {
            scroll('arrivalb', e); 
        }
    });
    
    $('#arrivalbPoint').keyup(function(e){
        if ( e.keyCode != 38 && e.keyCode != 40 && e.keyCode != 13 )
        {
            iId = -1;
         
            getSuggestions('arrivalb', $('#arrivalbPoint').val());   
        } 
    });

    $('#arrivalcPoint').click(function(e){ 
        if ( $('#arrivalcPoint').val() == 'Please enter an airport you wish to fly to' )
        {
            $('#arrivalcPoint').css('color', '#000000'); 
            $('#arrivalcPoint').val('');
        }
    });

    $('#arrivalcPoint').keydown(function(e){
        if ( $('#suggestions-arrivalcPoint').css('display') != 'none' )
        {
            scroll('arrivalc', e); 
        }
    });
    
    $('#arrivalcPoint').keyup(function(e){
        if ( e.keyCode != 38 && e.keyCode != 40 && e.keyCode != 13 )
        {
            iId = -1;
         
            getSuggestions('arrivalc', $('#arrivalcPoint').val());   
        } 
    });

});