$(document).ready(
    function () {
        //getCityFrom();
        //tabSpo();
    }
);


function getCityFrom() {
    $.ajax({
        url: 'http://webservice.samaraintour.ru/aviaspo/JsonCityFrom',
        type: 'GET',
        contentType: "application/javascript",
        crossDomain: true,
        jsonCallback: 'jsonCallback',
        dataType: 'jsonp',
        success: function (data) {
            var select = $('#cityFrom');
            $("input[id$='cityFrom']");
            var options = select.attr('options');
            select.attr('disabled', '');
            $('option', select).remove();
            $.each(data, function (index, value) {
                options[options.length] = new Option(value.name, value.id);
            });

        }
    });
}

function selCityFrom() {

    if ($("#cityFrom :selected").val() < 1) {
        var index = $("#cityFrom option").index($("#cityFrom option:selected"));
        index++;

        $('#cityFrom option:eq(' + index + ')').attr("selected", "selected");

    }
    //$("#city [value='1']").attr("selected", "selected");
    var select = $('#aviaCompany');
    var options = select.attr('options');
    select.attr('disabled', '');
    $('option', select).remove();

    var select1 = $('#cityTo');
    var options1 = select1.attr('options');
    select1.attr('disabled', '');
    $('option', select1).remove();

    getCityTo();

    tabSpo();
}

function selCityTo() {

    if ($("#cityTo :selected").val() < 1) {
        var index = $("#cityTo option").index($("#cityTo option:selected"));
        index++;

        $('#cityTo option:eq(' + index + ')').attr("selected", "selected");

    }

    //$("#city [value='1']").attr("selected", "selected");

    var select = $('#aviaCompany');
    var options = select.attr('options');
    select.attr('disabled', '');
    $('option', select).remove();

    getAviaCompany();

    tabSpo();
}

function getCityTo() {


    $.ajax({
        url: 'http://webservice.samaraintour.ru/aviaspo/JsonCityTo',
        type: 'GET',
        data: 'from=' + $("#cityFrom :selected").val(),
        contentType: "application/javascript",
        crossDomain: true,
        jsonCallback: 'jsonCallback',
        dataType: 'jsonp',
        success: function (data) {
            var select = $('#cityTo');
            var options = select.attr('options');
            select.attr('disabled', '');
            $('option', select).remove();
            $.each(data, function (index, value) {
                options[options.length] = new Option(value.name, value.id);
            });
        }
    });

    
    
}

function getAviaCompany() {
    $.ajax({
        url: 'http://webservice.samaraintour.ru/aviaspo/JsonAviaCompany',
        type: 'GET',
        data: 'from=' + $("#cityFrom :selected").val() + '&to=' + $("#cityTo :selected").val(),
        contentType: "application/javascript",
        crossDomain: true,
        jsonCallback: 'jsonCallback',
        dataType: 'jsonp',
        success: function (data) {
            var select = $('#aviaCompany');
            var options = select.attr('options');
            select.attr('disabled', '');
            $('option', select).remove();
            $.each(data, function (index, value) {
                options[options.length] = new Option(value.name, value.id);
            });

        }
    });
}

function selAviaCompany() {
    tabSpo();
}

function tabSpo() {

    $('#result').append('<img src="pic/loader.gif" alt=Загрузка..." id="loading" />');
    $("#loading").fadeOut();


    var str = '';
    var price = '';
    var priceFromTo = '';
    var s = 0;
    var color = '';

    $.ajax({
        url: 'http://webservice.samaraintour.ru/aviaspo/JsonResultSpo',
        type: 'GET',
        data: 'from=' + $("#cityFrom :selected").val() + '&to=' + $("#cityTo :selected").val() + '&company=' + $("#aviaCompany :selected").val(),
        contentType: "application/javascript",
        crossDomain: true,
        jsonCallback: 'jsonCallback',
        dataType: 'jsonp',
        success: function (data) {
            str = '<table><tr><th>Авиакомпания</th><th>Направление</th><th>Туда</th><th>Туда и обратно</th><th>Действует до</th><th>Инфо</th></tr><tr>\n';
            $.each(data, function (index, value) {

                color = '';
                s++;
                if (s % 2 > 0) {
                    color = ' style=\"background-color:#f5ffb5;\"';
                }

                price = '';
                if (value.price != '')
                    price = '<a href=\"/specorder/' + value.priceid + '\">' + value.price + ' ' + value.rates + '</a>';

                priceFromTo = '';
                if (value.pricefromto != '')
                    priceFromTo = '<a href=\"/specorder/' + value.priceid + '/1\">' + value.pricefromto + ' ' + value.rates + '</a>';

                str = str + '<tr><td class=\"spo-avia\"' + color + '>' + value.company + '</td><td class=\"spo-from-to\"' + color + '>' + value.cityfrom + ' - ' + value.cityto + '</td><td class=\"spo-price-to\"' + color + '>' + price + '</td><td class=\"spo-price-to-back\"' + color + '>' + priceFromTo + '</td><td class=\"spo-date-to\"' + color + '>' + value.dateto + '</td><td class=\"spo-info\"' + color + '><a href=\"spodesc.aspx?id=' + value.priceid + '\" target=\"_blank\"><img src=\"http://www.samarabilet.ru/pic/view.gif\" alt=\"Инфо\"></a></td></tr>\n';

                //str = str + value.company + ' - ' + value.pricefromto + '<br />';



            });

            str = str + '</table>\n';
            $("#result").html(str);
            $("#spo-num").html(s);
        }
    });

    
    
/*    str = '<strong>Город вылета:</strong> ' + $("#cityFrom :selected").val() + '<br />';
    str = str + '<strong>Город прилета:</strong> ' + $("#cityTo :selected").val() + '<br />';
    str = str + '<strong>Авиакомпания:</strong> ' + $("#aviaCompany :selected").html();
    $("#result").html(str + 'test');
    */
}

