

















/*
*   Handles selecting different user trips
*/

(function($) {
$.ti.trips = $.prototype = {

    html:   $(['<div id="selectTrips">',
                '<label for="selectOtherTrips">Saved trips</label>',
                '<select id="selectOtherTrips">',
                '</select>',
                '<input type="image" src="/shared/resource/images/itinerary_planner/nz/buttons/go.png" alt="Share" id="selectOtherTripsSubmit" value="" />',
            '</div>'].join('')),

    change: function() {
        var trip, select, newTrip = true;

        $('#selectOtherTrips').empty();
        $('#selectOtherTripsSubmit').unbind('click.switchTrip');
        if($.ti.tripData.trip.myTrips) {
        	// Check if user has only one trip and new trip has just been created
        	if ($.ti.tripData.trip.myTrips.myTrip.length === 1 && $.ti.tripData.trip.id !== $.ti.tripData.trip.myTrips.myTrip[0].id) {
        		$('#otherTrips').prepend($.ti.trips.html);
        		select = $('<option value="' + $.ti.tripData.trip.myTrips.myTrip[0].id + '">' + $.ti.tripData.trip.myTrips.myTrip[0].tripName + '</option>');
                $('#selectOtherTrips').append(select);
                $('#selectOtherTrips').append('<option value="' + $.ti.tripData.trip.id + '" selected="selected">' + $.ti.tripData.trip.tripName + '</option>');
        	}

            // Check that there is more than one
            if($.ti.tripData.trip.myTrips.myTrip.length > 1) {
                // Append select if not already present
                if(!document.getElementById('selectTrips')) {
                    $('#otherTrips').prepend($.ti.trips.html);
                }
                for(trip in $.ti.tripData.trip.myTrips.myTrip) {
                    if($.ti.tripData.trip.myTrips.myTrip.hasOwnProperty(trip)) {
                        select = $('<option value="' + $.ti.tripData.trip.myTrips.myTrip[trip].id + '">' + $.ti.tripData.trip.myTrips.myTrip[trip].tripName + '</option>');
                        if($.ti.tripData.trip.myTrips.myTrip[trip].id === $.ti.tripData.trip.id) {
                            $(select).attr('selected', 'selected');
                            newTrip = false;
                        }
                        $('#selectOtherTrips').append(select);
                    }
                }
                // Detect if this is newly created trip
                if (newTrip) $('#selectOtherTrips').append('<option value="' + $.ti.tripData.trip.id + '" selected="selected">' + $.ti.tripData.trip.tripName + '</option>');
            }
        } else {
            $('#selectTrips').remove();
        }
        $('#selectOtherTripsSubmit').bind('click.switchTrip', function(){
            $.ti.recalculateCalendarGrid.clearGrid();
            $('.day', '#calendarDays').remove();
			$.ti.cookie.setCookie('ItineraryPlanner', $.ti.tripData.trip.id, 'null');
            $.ti.ajax.postData('/nz/trip/?tripId=' + $("#selectOtherTrips").val(), $.ti.tripData,  $.ti.panel.saveTripAndPromos, $.ti.panel.useTripData);
        });
    },

    getNewEvents: function() {
        $('#createNewTrip').unbind('click');
        $('#createNewTrip').click(function() {
            if($.ti.tripData.trip.role == "O" && !$.ti.tripData.trip.login) {
                $.ti.messages.getNewTrip();
            } else {
                $.ti.trips.getNewTrip();
            }
        });
    },

    getNewTrip: function() {
        $.ti.bag.reset();
        $.ti.recalculateCalendarGrid.clearGrid();
        $('.day', '#calendarDays').remove();
        $.ti.ajax.postData('/nz/trip/?tripId=-1', $.ti.tripData, $.ti.panel.saveTripAndPromos, $.ti.panel.useTripData);
    }
};
})(jQuery);
