/*
*   Functionality for on page offers
*/
(function($) { 
$.ti.addActivities = $.prototype = {
    
    tripId: -1,
    
    init: function(){
        var offers, offersLength, count;
        
        offers = $('a.frLink');
        offersLength = offers.length;
        
        for (count = 0; count < offersLength; count += 1) {
            $(offers[count]).click(function(event){$.ti.addActivities.getActivity(event);});                
        }
        
        // Get trip id from rel tag
        if('#likeTrip'){
            this.tripId = $('#likeTrip').attr('rel');
        }
        
        // Set up add all activities to my trip
        if($('#addAll').length) {
            this.addAll();
        }
        
        // Set up use this trip as my trip
        if($('#useTrip').length) {
            this.useTrip();
        }
    },

    
/*    getActivity: function(event) {
        event.preventDefault();
        $.ti.ajax.getJson('/' + $.ti.panel.locale + '/trip/?opId=' + event.target.parentNode.rel, $.ti.addActivities.addToTrip);
    },*/

    getActivity: function(event) {
        event.preventDefault();

        // capture the mouse coords to eventually position the confirmation
        // pop-up...
        var ctx = { 
            x: '' + event.pageX,
            y: '' + event.pageY 
        };


        // Parameters to the async call...
        var data = { 
            bustCache: '' + Math.floor(Math.random() * 100001),
            opId: event.target.parentNode.rel 
        };

        $.ajax({
            url: '/' + $.ti.panel.locale + '/trip/',
            context: ctx,
            data: data,
            dataType: 'json',
            success: $.ti.addActivities.addToTripSuccess,
            error: $.ti.addActivities.addToTripFail
        });
    },

/*    
    addToTrip: function(data) {
        $.ti.bag.add(data.activity, true);
    },
    */

    addToTripSuccess: function(data)
    {
        $.ti.bag.myAdd(data.activity, true, this);
    },

    addToTripFail: function()
    {
        if (console)
            console.log("Add to trip failed");
    },
    
    // Add every activity from a public trip
    addAll: function() {
        $('#addAll').bind('click.addAll', function(){
            // Get id of trip with the items 
            var addTripId = $('#addAll').attr('rel');
            // Post the addtrip id, save and re-request current trip.
            $.ti.ajax.postData('/' + $.ti.panel.locale + '/trip/?addTripId=' + addTripId + '&tripId=' + $.ti.tripData.trip.id, $.ti.tripData, $.ti.panel.saveTripAndPromos, $.ti.panel.useTripData);
        });
    },
    
    useTrip: function() {
        $('#useTrip').bind('click.useTrip', function() {
            // Post trip to use id, save the current trip as well. Sends blank trip id.
            var useTripId = $('#useTrip').attr('rel');
            $.ti.ajax.postData('/' + $.ti.panel.locale + '/trip/?useTripId='+useTripId+'&tripId=', $.ti.tripData, $.ti.panel.saveTripAndPromos, $.ti.panel.useTripData);
        });
    }
};
})(jQuery);
