var slideSwitch = false;
var airline;
var country_id;
var language;

function header()
{
    if (slideSwitch == true)
    {
        $('selectionDropdown').style.display = 'none';
//        new Effect.Fade('selectionDropdown', {duration: 0.5});
        slideSwitch = false;
    }
    else
    {
        $('selectionDropdown').style.display = 'block';
//        new Effect.Appear('selectionDropdown', {duration: 0.5});
        slideSwitch = true;
    }

    return false;
}

function setCountries(al, preselect)
{
    $('als_af').style.color = '#58585A';
    $('als_af').style.fontWeight = 'normal';

    $('als_klm').style.color = '#58585A';
    $('als_klm').style.fontWeight = 'normal';

    $('als_ae').style.color = '#58585A';
    $('als_ae').style.fontWeight = 'normal';

    $('als_ka').style.color = '#58585A';
    $('als_ka').style.fontWeight = 'normal';

    $('als_' + al).style.color = '#62B4E8';
    $('als_' + al).style.fontWeight = 'bold';

    select_countries = $('countries');
    for(i=select_countries.options.length-1;i>=0;i--)
    {
        select_countries.remove(i);
    }
    airline = al;

    if (airline == 'af')
    {
        $('form_airline').value = 'airfrance';

        var counter = 0;
        for (i=1;i<country_af.length; i++)
        {
            select_countries.options[counter] = new Option(country_af[i]['name'], country_af[i]['id']);

            if (country_af[i]['value'] == preselect)
            {
                select_countries.options[counter].selected = 'true';
            }
            counter++;
        }
    }
    else if (airline == 'ae')
    {
        $('form_airline').value = 'aireurope';

        var counter = 0;
        for (i=1;i<country_ae.length; i++)
        {
            select_countries.options[counter] = new Option(country_ae[i]['name'], country_ae[i]['id']);

            if (country_ae[i]['value'] == preselect)
            {
                select_countries.options[counter].selected = 'true';
            }

            counter++;
        }
    }
    else if (airline == 'ka')
    {
        $('form_airline').value = 'kenyaairways';

        var counter = 0;
        for (i=1;i<country_ka.length; i++)
        {
            select_countries.options[counter] = new Option(country_ka[i]['name'], country_ka[i]['id']);

            if (country_ka[i]['value'] == preselect)
            {
                select_countries.options[counter].selected = 'true';
            }

            counter++;
        }
    }
    else
    {
        $('form_airline').value = 'klm';

        var counter = 0;
        for (i=1;i<country_klm.length; i++)
        {
            select_countries.options[counter] = new Option(country_klm[i]['name'], country_klm[i]['id']);

            if (country_klm[i]['value'] == preselect)
            {
                select_countries.options[counter].selected = 'true';
            }

            counter++;
        }
    }
}

function setLanguages()
{
    var country_id = $F('countries');
    var lang_sel = $('languages');
    var airline = $('form_airline').value;

    lang_sel.innerHTML = '';

    if (airline == 'airfrance')
    {
        $('form_country').value = country_af[country_id]['value'];

        for (i=0;i<country_af[country_id]['languages'].length; i++)
        {
            list_item = document.createElement('li');
            list_item.className = language_af[country_af[country_id]['languages'][i]]['value'];
            link = document.createElement('a');
            link.setAttribute("href", '#');
            //link.setAttribute("class", language_af[country_af[country_id]['languages'][i]]['value']);

            link.setAttribute("name", language_af[country_af[country_id]['languages'][i]]['value']);
            link.onclick = new Function("changeSettings(this);");
            link.innerHTML = language_af[country_af[country_id]['languages'][i]]['name'];

            list_item.appendChild(link);
            lang_sel.appendChild(list_item);
        }
    }

    if (airline == 'klm')
    {
        $('form_country').value = country_klm[country_id]['value'];

        for (i=0;i<country_klm[country_id]['languages'].length; i++)
        {
            list_item = document.createElement('li');
            list_item.className = language_klm[country_klm[country_id]['languages'][i]]['value'];
            link = document.createElement('a');
            link.setAttribute("href", '#');
            //link.setAttribute("class", language_klm[country_klm[country_id]['languages'][i]]['value']);

            link.setAttribute("name", language_klm[country_klm[country_id]['languages'][i]]['value']);
            link.onclick = new Function("changeSettings(this);");
            link.innerHTML = language_klm[country_klm[country_id]['languages'][i]]['name'];

            list_item.appendChild(link);
            lang_sel.appendChild(list_item);
        }
    }

    if (airline == 'aireurope')
    {
        $('form_country').value = country_ae[country_id]['value'];

        for (i=0;i<country_ae[country_id]['languages'].length; i++)
        {
            list_item = document.createElement('li');
            list_item.className = language_ae[country_ae[country_id]['languages'][i]]['value'];
            link = document.createElement('a');
            link.setAttribute("href", '#');
            //link.setAttribute("class", language_ae[country_ae[country_id]['languages'][i]]['value']);

            link.setAttribute("name", language_ae[country_ae[country_id]['languages'][i]]['value']);
            link.onclick = new Function("changeSettings(this);");
            link.innerHTML = language_ae[country_ae[country_id]['languages'][i]]['name'];

            list_item.appendChild(link);
            lang_sel.appendChild(list_item);
        }
    }

    if (airline == 'kenyaairways')
    {
        $('form_country').value = country_ka[country_id]['value'];

        for (i=0;i<country_ka[country_id]['languages'].length; i++)
        {
            list_item = document.createElement('li');
            list_item.className = language_ka[country_ka[country_id]['languages'][i]]['value'];
            link = document.createElement('a');
            link.setAttribute("href", '#');
            //link.setAttribute("class", language_ae[country_ae[country_id]['languages'][i]]['value']);

            link.setAttribute("name", language_ka[country_ka[country_id]['languages'][i]]['value']);
            link.onclick = new Function("changeSettings(this);");
            link.innerHTML = language_ka[country_ka[country_id]['languages'][i]]['name'];

            list_item.appendChild(link);
            lang_sel.appendChild(list_item);
        }
    }
}

function changeSettings(obj)
{
    var airline = $('form_airline').value;
    $('form_language').value = obj.name;

//    new Effect.SlideUp('selectionDropdown', {duration: 0.5});
    $('selectionDropdown').style.display = 'none';
//    window.setTimeout('submitSettingsForm()', 500);
    submitSettingsForm();
}

function submitSettingsForm()
{
    $('settings').submit();
}

function setSelector(airline, country, language)
{
    if (airline == 'airfrance')
    {
        airline = 'af';
        setCountries('af', country);
    }

    if (airline == 'klm')
    {
        airline = 'klm';
        setCountries('klm', country);
    }

    if (airline == 'aireurope')
    {
        airline = 'ae';
        setCountries('ae', country);
    }

    if (airline == 'kenyaairways')
    {
        airline = 'ka';
        setCountries('ka', country);
    }

    setLanguages();
}

function updateFlightSelectorDest()
{
    new Ajax.Updater('flightSelectorEndWrapper', '/ajax_requests/update_destinations_airports.html', {asynchronous:true, parameters:Form.serialize($('flightSelector'))});
}

function updateFlightSelectorStart()
{
    new Ajax.Updater('flightSelectorStartWrapper', '/ajax_requests/update_start_airports.html', {asynchronous:true, parameters:Form.serialize($('flightSelector'))});
}

function updateEmCountries()
{
    new Ajax.Updater('partner_country_wrapper', '/ajax_requests/update_em_countries.html', {asynchronous:true, parameters:Form.serialize($('em_prom_filter'))});
}

function updateEmPartners()
{
    if ($('em_partner_id').value == '')
    {
        new Ajax.Updater('partner_id_wrapper', '/ajax_requests/update_em_partners.html', {asynchronous:true, parameters:Form.serialize($('em_prom_filter'))});
    }
}

function updateEmGlobalCountries()
{
    new Ajax.Updater('global_country_wrapper', '/ajax_requests/update_em_global_countries.html', {asynchronous:true, parameters:Form.serialize($('em__glob_prom_filter'))});
}

function updateEmGlobalPartners()
{
    if ($('em_global_partner_id').value == '')
    {
        new Ajax.Updater('global_partner_wrapper', '/ajax_requests/update_em_global_partners.html', {asynchronous:true, parameters:Form.serialize($('em__glob_prom_filter'))});
    }
}