

















/*
*  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>Duplica l&#39;opzione</h2>',
                    '<p>Questa opzione esiste già in questo viaggio</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 + 'Il tuo viaggio è stato condiviso con amici</p>',
                      "</div>"
                     ].join(''));
        $.ti.messages.settings.content = html;
        $.ti.popup.init($.ti.messages.settings);
    },


    // Trip saved
    tripSaved: function() {
        var html = $(['<div id="swapableContent">',
                    '<h2>Questo viaggio è stato salvato</h2>',
                    '<p>Il viaggio e ogni cambiamento fatto sono stati definitivamente salvati</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>Elimina tutte le opzioni</h2>',
                    '<p>Conferma che vuoi cancellare tutte le opzioni dal tuo Calendario di viaggio. Non potrai più tornare indietro</p>',
                    '<div class="inputs">',
                        '<input type="image" src="/shared/resource/images/itinerary_planner/it/buttons/warn-cancel.png" height="22" id="closeClearCalender" />',
                        '<input type="image" src="/shared/resource/images/itinerary_planner/it/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>Elimina l&#39;opzione</h2>',
                    '<p>Hai scelto di cancellare un elemento che hai selezionato  per il tuo Calendario di viaggio</p>',
                    '<p>Facendolo questo rimuoverai tutti i dettagli relativi a questa opzione dal tuo Calendario di viaggio. Sei sicuro di volerlo fare?</p>',
                    '<div class="inputs">',
                        '<input type="image" src="/shared/resource/images/itinerary_planner/it/buttons/warn-cancel.png" height="22" id="closeDeleteFromCalender" />',
                        '<input type="image" src="/shared/resource/images/itinerary_planner/it/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>Questa opzione non è disponibile</h2>',
                    '<p>Scusa, questa opzione è disponbile soltanto per chi ha creato un Trip planner</p>',
                    '<p>crea il tuo Trip Planner qui su irlanda-travel.com. Aggiungi date, posti dove stare, cose da fare e da vedere, offerte e idee brillanti alla tua Trip Planner List, </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>Sei sicuro?</h2>',
                  '<p>Assicurati di aver salvato  il viaggio prima di crearne uno nuovo, altrimenti perderai tutte le informazioni che hai troveto (ricorda che puoi salvare più di un viaggio alla volta!)</p>',
                  '<p>Per essere sicuro di poter rivisitare il tuo Trip Planner, salvalo qui e torna indietro di nuovo e di nuovo.</p>',
                  '<div class="inputs">',
                        '<input type="image" src="/shared/resource/images/itinerary_planner/it/buttons/login.png" height="22" id="loginNow" />',
                        '<input type="image" src="/shared/resource/images/itinerary_planner/it/buttons/register.png" height="22" id="registerNow" />',
                        '<input type="image" src="/shared/resource/images/itinerary_planner/it/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>Alcune di queste date non sono valide</h2>',
                    '<p>Scusa, alcune opzioni del tuo Trip Planner non sono più valide, Probabilmente perche sono cambiate le date delle tue opzioni</p>',
                    '<p>Queste opzioni verranno rimosse dal tuo calendario di viaggio, ma rimarranno nella tua lista di viaggio così potrai reinserirli</p>',
                    '<div class="inputs">',
                        '<input type="image" src="/shared/resource/images/itinerary_planner/it/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>Le attività sono state rimosse dal tuo viaggio</h2>',
                    '<p>Le seguenti attività non esistono più e sono state rimosse dal tuo viaggio</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>Opzioni di stampa</h2>',
                    '<p>Vuoi includere le direzioni quando stampi il viaggio?</p>',
                    '<div class="inputs">',
                        '<input type="image" src="/shared/resource/images/itinerary_planner/it/buttons/no-button.png" height="22" id="printNoDirections" style="padding-right: 1em" />',
                        '<input type="image" src="/shared/resource/images/itinerary_planner/it/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('/it/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('/it/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);
