

















/*
*  Various error and warning pop ups controlled here.
*/

(function($) {
$.ti.messages = {

    settings: {
       width: '400px',
       callback: false
    },

    // ** Already Exists ** //
    // warning for when trying to add an already existing activity to the bag
    alreadyExists: function() {
        var html = $(['<div id="swapableContent" class="warning">',
                    '<h2>Duplicate Item</h2>',
                    '<p>This item already exists in this Trip</p>',
                '</div>'
                ].join(''));
        $.ti.messages.settings.content = html;
        $.ti.popup.init($.ti.messages.settings);
    },

    // shown wehn the trip has been shared successfully...
    tripShared: function(emailAddr) {
        var html = $(["<div id='swapableContent'>",
                      '<h2>Your trip has been shared</h2>',
                      '<p>' + emailAddr + 'Your trip has been shared with friends</p>',
                      "</div>"
                     ].join(''));
        $.ti.messages.settings.content = html;
        $.ti.popup.init($.ti.messages.settings);
    },


    // Trip saved
    tripSaved: function() {
        var html = $(['<div id="swapableContent">',
                    '<h2>This trip has been saved</h2>',
                    '<p>This trip and any changes which you have made have now been permanently saved.</p>',
                  '</div>'
                ].join(''));
        $.ti.messages.settings.content = html;
        $.ti.popup.init($.ti.messages.settings);
    },

    // ** Clear Calender ** //
    // warning for when the Clear Calender button is pressed
    clearCalender: function() {
        var html = $(['<div id="swapableContent" class="warning">',
                    '<h2>Clear all items</h2>',
                    '<p>Please confirm you&#039;d like to clear ALL items from your Trip Calender. This cannot be undone.</p>',
                    '<div class="inputs">',
                        '<input type="image" src="/shared/resource/images/itinerary_planner/au/buttons/warn-cancel.png" height="22" id="closeClearCalender" />',
                        '<input type="image" src="/shared/resource/images/itinerary_planner/au/buttons/warn-confirm.png" height="22" id="confirmClearCalender" />',
                    '</div>',
                  '</div>'
                ].join(''));
        // Create settings
        $.ti.messages.settings.content = html;
        $.ti.messages.settings.closeButton = ['#close', '#closeClearCalender'];
        $.ti.messages.settings.callback = $.ti.messages.clearCalenderEvents;
        // Call popup
        $.ti.popup.init($.ti.messages.settings);
    },

    // Events for the confirm button on Clear Calender
    clearCalenderEvents: function() {
        $('#confirmClearCalender').unbind('click.clearCalender');
        $('#confirmClearCalender').bind('click.clearCalender', function() {
            $.ti.recalculateCalendarActivities.clearActivities();
            $.ti.createMap.createMap();
            $.ti.popup.close();
        });
    },

    // ** Delete from Calender ** //
    // Warning for when user deletes bag item that has instanced on the calender //
    deleteFromCalender: function(listItem) {
        var html = $(['<div id="swapableContent" class="warning">',
                    '<h2>Delete Item</h2>',
                    '<p>You have chosen to delete an item that you have added to the Trip Calendar</p>',
                    '<p>Doing this will remove all instances of this item from your Trip Calendar. Are you sure you want to do this?</p>',
                    '<div class="inputs">',
                        '<input type="image" src="/shared/resource/images/itinerary_planner/au/buttons/warn-cancel.png" height="22" id="closeDeleteFromCalender" />',
                        '<input type="image" src="/shared/resource/images/itinerary_planner/au/buttons/warn-confirm.png" height="22" id="confirmDeleteFromCalender" />',
                    '</div>',
                '</div>'
                ].join(''));

        // Store the list Item reference as data in the confirm button
		$.ti.messages.deleteFromCalenderEvents.elem = listItem

        $.ti.messages.settings.content = html;

        $.ti.messages.settings.closeButton = ['#close', '#closeDeleteFromCalender'];
        $.ti.messages.settings.callback = $.ti.messages.deleteFromCalenderEvents;
        $.ti.popup.init($.ti.messages.settings);
    },

    deleteFromCalenderEvents: function() {
        $('#confirmDeleteFromCalender').unbind('click.DeleteFromCalender');
        $('#confirmDeleteFromCalender').bind('click.DeleteFromCalender', function(event) {
			$.ti.bag.deleteActivity($.ti.messages.deleteFromCalenderEvents.elem);
            $.ti.popup.close();
        });
    },

    // Single catch all message for reviewers attempting to trigger forbidden events.
    notForReviewer: function() {
        var html = $(['<div id="swapableContent" class="warning">',
                    '<h2>This option is not available</h2>',
                    '<p>Sorry, this option is only available to Trip Owners and those invited to contribute by the Trip Owner</p>',
                    '<p>Create your very own Trip Planner right here on Discover Ireland. Add dates, places to stay, things to see and do, offers and bright ideas into your Trip Planner List, drag each item across and place on your Trip Planner Calendar, then name your Trip, save and share with friends and family to see what they think! You can even invite them to review, comment or contribute to make sure your trip to the island of Ireland is absolutely perfect!</p>',
                  '</div>'
                ].join(''));
        $.ti.messages.settings.content = html;
        $.ti.popup.init($.ti.messages.settings);
    },

    // When a not logged in trip Owner requests a new trip
    getNewTrip: function() {
        var html = $(['<div id="swapableContent" class="warning">',
                  '<h2>Are you sure?</h2>',
                  '<p>Make sure you save this Trip now before creating a new Trip, otherwise you will lose all the information you have collected (Remember, you can save more than one Trip at a time!)</p>',
                  '<p>Want to make sure you can revisit your Trip Planner again, just save it here and come back again and again.</p>',
                  '<div class="inputs">',
                        '<input type="image" src="/shared/resource/images/itinerary_planner/au/buttons/login.png" height="22" id="loginNow" />',
                        '<input type="image" src="/shared/resource/images/itinerary_planner/au/buttons/register.png" height="22" id="registerNow" />',
                        '<input type="image" src="/shared/resource/images/itinerary_planner/au/buttons/get-new-trip.png" height="22" id="getNewTrip" />',
                    '</div>',
                  '</div>'
                ].join(''));

        $.ti.messages.settings.content = html;
        $.ti.messages.settings.callback = $.ti.messages.getNewEvents;
        $.ti.popup.init($.ti.messages.settings);
    },

    // Events fro the getNewTrip warning overlay
    getNewEvents: function() {
        // Get new trip button event
        $('#getNewTrip').bind('click.getNewTrip', function() {
            $.ti.trips.getNewTrip();
            $.ti.popup.close();
        });
        // Login button event
        $('#loginNow').bind('click.loginNow', function() {
            $.ti.popup.swapContent($.ti.login.loginHTML, '360px', $.ti.login.events);
        });
        // Register button event
        $('#registerNow').bind('click.registerNow', function() {
            $.ti.popup.swapContent($.ti.login.registerHTML, '360px', $.ti.login.registerEvents);
        })
    },

    // When a calender contians activities with dates that have become errored
    hasErrors: function() {
        var html = $(['<div id="swapableContent" class="warning">',
                    '<h2>Some of these dates are invalid</h2>',
                    '<p>Sorry, some of the items listed on your Trip Planner have become invalid. This is probably because dates have changed on these items.</p>',
                    '<p>We will remove these items from your Trip Calender, but they will remain in your Trip List so you can reinsert at a later date</p>',
                    '<div class="inputs">',
                        '<input type="image" src="/shared/resource/images/itinerary_planner/au/buttons/warn-confirm.png" height="22" id="removeErrors" />',
                    '</div>',
                  '</div>'
                ].join(''));
        $.ti.messages.settings.content = html;
        $.ti.messages.settings.callback = $.ti.messages.hasErrorsEvents;
        $.ti.popup.init($.ti.messages.settings);
    },

	hasErrorsEvents: function() {
		$('#removeErrors').bind('click.removeErrors', function() {
			$.ti.calendarErrors.remove();
			$.ti.popup.close();
		});
	},

    // When a calender contians activities with dates that have become errored
    hasProductErrors: function() {

        // determine how many products have been deleted
        productErrorCount = $.ti.utils.objectLength($.ti.calendarErrors.errorProducts);
        // loop over the errors and build a list of deleted products

        var errorListItems = '';

        for (count=0;count<productErrorCount;count++){
            var listItem = '<li>' + $.ti.calendarErrors.errorProducts[count][1] + '</li>';
            errorListItems = errorListItems + listItem;
        }

        var errorList = '<ul class="messageList">' + errorListItems + '</ul>';



        var html = $(['<div id="swapableContent" class="warning">',
                    '<h2>Activities have been removed from this trip</h2>',
                    '<p>The following activities no longer exist and have been removed from this trip.</p>',
                    errorList,
                  '</div>'
                ].join(''));
        $.ti.messages.settings.content = html;
        $.ti.messages.settings.callback = $.ti.messages.hasProductErrorsEvents;
        $.ti.popup.init($.ti.messages.settings);
    },

	hasProductErrorsEvents: function() {

	},

    // ** Check Print Options ** //
    // User offered choice as to whether or not to include directions in the print page //
    printOptions: function(listItem) {
        var html = $(['<div id="swapableContent">',
                    '<h2>Print options</h2>',
                    '<p>Do you wish to include directions when printing this trip?</p>',
                    '<div class="inputs">',
                        '<input type="image" src="/shared/resource/images/itinerary_planner/au/buttons/no-button.png" height="22" id="printNoDirections" style="padding-right: 1em" />',
                        '<input type="image" src="/shared/resource/images/itinerary_planner/au/buttons/warn-confirm.png" height="22" id="printDirections" />',
                    '</div>',
                '</div>'
                ].join(''));

        $.ti.messages.settings.content = html;
        $.ti.popup.init($.ti.messages.settings);

        $('#printNoDirections').click(function(){
            $.ti.popup.close();
            var printWindow = window.open('/au/trip/listings/print-trip/?id=' + $.ti.tripData.trip.id, 'printWindow', 'menubar=0,location=0,status=1,resizable=1,scrollbars=1,width=1050,height=640');
		});
        $('#printDirections').click(function(){
            $.ti.popup.close();
            var printWindow = window.open('/au/trip/listings/print-trip/?id=' + $.ti.tripData.trip.id + '&directions=true', 'printWindow', 'menubar=0,location=0,status=1,resizable=1,scrollbars=1,width=1050,height=640');
		});

    }

};
})(jQuery);
