/*
    Initialize the objects when the dom is ready
*/
$(function () {

    AvailabilityRequest.bindEvents();
});

/*
 *      Private methods
 */
//  Bind button events
AvailabilityRequest.bindEvents = function () {
    $('#btnGetAvailability').bind('click', AvailabilityRequest.btnGetAvailability_Click);
    $('#btnNewSearch').bind('click', AvailabilityRequest.btnNewSearch_Click);
    $('#btnLastRequests').bind('click', AvailabilityRequest.btnLastRequests_Click);
    $('#btnBackLastRequests').bind('click', AvailabilityRequest.btnBackLastRequests_Click);
};

/*
 *      Event handlers
 */
AvailabilityRequest.btnBackLastRequests_Click = function (ev) {
    ev.preventDefault();
    AvailabilityRequest.LastRequestsDiv.hide();
    AvailabilityRequest.AvailabilityRequestDiv.fadeIn();
};

AvailabilityRequest.btnNewSearch_Click = function (ev) {
    ev.preventDefault();
    AvailabilityRequest.AvailabilityResponseDiv.hide();
    AvailabilityRequest.AvailabilityRequestDiv.fadeIn();
};

//  Display the inbound and outboud journeys
AvailabilityRequest.btnLastRequests_Click = function (ev) {

    ev.preventDefault();
    loading.show();
    
    AvailabilityRequest.getLastRequests(function (res) {
        AvailabilityRequest.showRequests(res.GetLastRequestsResult);
        loading.hide();
    }, AvailabilityRequest.handleAjaxError);
};

AvailabilityRequest.btnGetAvailability_Click = function (ev) {

    ev.preventDefault();

    var origin = $('#ddlOrigin').find(":selected").text(),
        destination = $('#ddlDestination').find(":selected").text();

    if (origin && destination && origin != destination && AvailabilityRequest.departureDate && AvailabilityRequest.returnDate) {
        msgError.hide();
        var data = {
            request: {
                Origin: origin,
                Destination: destination,
                DepartureDate: AvailabilityRequest.departureDate.toJSON(),
                ReturnDate: AvailabilityRequest.returnDate.toJSON()
            }
        };

        loading.show();

        AvailabilityRequest.doAvailabilityRequest(data, function(res) {
            loading.hide();
            AvailabilityRequest.showResults(res.GetAvailabilityResult);
        }, AvailabilityRequest.handleAjaxError);
    } else {
        msgError.show();
    }
};

/*
 *		Display
 */
AvailabilityRequest.showResults = function (data) {

    var inboundContainer = $('#inbound-journey-wrapper'),
        outboundContainer = $('#outbound-journey-wrapper'),
        journey;

    inboundContainer.empty();
    outboundContainer.empty();

    AvailabilityRequest.AvailabilityRequestDiv.hide();
    AvailabilityRequest.AvailabilityResponseDiv.fadeIn();

    for (var i = 0, ii = data.InboundJourneys.length; i < ii; i += 1) {
        journey = data.InboundJourneys[i];
        AvailabilityRequest.createJourney(journey, inboundContainer);
    }

    for (var j = 0, jj = data.OutboundJourneys.length; j < jj; j += 1) {
        journey = data.OutboundJourneys[j];
        AvailabilityRequest.createJourney(journey, outboundContainer);
    }
};

//  Create the markup and append to the object
AvailabilityRequest.createJourney = function (journey, obj) {

    var start = journey.DepartureStation.Name,
        end = journey.ArrivalStation.Name,
        arrivalTime = moment(journey.ArrivalTime).format("DD-MM-YYYY, hh:mm:ss"),
        departureTime = moment(journey.DepartureTime).format("DD-MM-YYYY, hh:mm:ss"),
        markup =
        "<div class=\"well\">" +
            "<h5>" + journey.Carrier + "</h5>" +
            "<div>" + start + " (" + arrivalTime + ") " + "</div>" +
            "<div>" + end + " (" + departureTime + ") " + "</div>" +
            "</div>";
    obj.append(markup);
};


AvailabilityRequest.showRequests = function (data) {

    var requestContainer = $('#last-request-wrapper'),
        request;

    requestContainer.empty();

    AvailabilityRequest.AvailabilityRequestDiv.hide();
    AvailabilityRequest.LastRequestsDiv.fadeIn();

    for (var i = 0, ii = data.length; i < ii; i += 1) {
        request = data[i];
        AvailabilityRequest.createRequest(request, requestContainer);
    }
};

//  Create the markup and append to the object
AvailabilityRequest.createRequest = function (request, obj) {

    var start = request.Origin,
        end = request.Destination,
        departureDate = moment(request.DepartureDate).format("DD-MM-YYYY, hh:mm:ss"),
        returnDate = moment(request.ReturnDate).format("DD-MM-YYYY, hh:mm:ss"),
        markup =
        "<div class=\"well\">" +
            "<div>" + start + " -> " + end + "</div>" +
            "<div>" + departureDate + " - " + returnDate + "</div>" +
            "</div>";
    obj.append(markup);
};


