/**
 * @fileoverview EventBus.js
 * 
 * This class uses the jquery shout plugin to handle front-end events.  It can make service calls
 * based on events, and passes events back to other UI components on the page.
 * 
 * Docs: http://confluence.garmin.com:8090/display/connect/Event+Bus+%28Front-end%29
 * 
 * @requires jQuery, Client.js, ActivityClient.js, jquery.shout.js, Activity
 * @author Diana Chow, Devon Lindsey
 * Copyright(c) 2009, Garmin International
 */
EventBus = {
    /**
     * Makes appropriate service call based on eventName and data. If no service call, passes on the shout
     * to other components (changes the event name)
     * @param eventName {String} the name of the event
     * @param data {Object} data relating to the event
     */
    shout: function(eventName, data) {
        switch(eventName) {
            case EventBus.Events.CHANGE_ACTIVITY_TYPE:
                //TODO should ActivityID come in data object?
                activityClient.updateActivityType(Activity.ACTIVITY_ID, data.newValue, {
            		onSuccess: function(response) {
            			eval("var responseObject = " + response.responseText);
            			jQuery.shout(EventBus.Events.UPDATED_ACTIVITY_TYPE, {
                            key: responseObject.activityType.key, 
                            display: responseObject.activityType.display,
                            parent: {
                                key: responseObject.activityType.type.key,
                                display: responseObject.activityType.type.display
                            }
                        });
            		},						
            		onFailure: function(xhr) {
            		    jQuery.shout(EventBus.Events.UPDATED_ACTIVITY_TYPE_ERROR, {
            		        oldValue: data.oldValue,
            		        newValue: data.newValue,
            		        xhr: xhr
            		    });
            		    
            		    // TODO get rid of this alert when we have error listeners
            			alert(xhr.statusText);
            		}
            	});
            	break;
            case EventBus.Events.CHANGE_PRIVACY_PRIVATE:
            	//TODO should ActivityID come in data object?
    			activityClient.updatePrivacy(Activity.ACTIVITY_ID, 'private', {
    				onSuccess: function() {
	        			jQuery.shout(EventBus.Events.UPDATED_PRIVACY, {
	                        privacy: EventBus.Constants.PRIVATE
	                    });
    				},						
    				onFailure: function(xhr) {
    					alert(xhr.statusText);
    				}
	    		});
            	break;
            case EventBus.Events.CHANGE_PRIVACY_PUBLIC:
            	//TODO should ActivityID come in data object?
            	activityClient.updatePrivacy(Activity.ACTIVITY_ID, 'public', {
            		onSuccess: function() {
	            		jQuery.shout(EventBus.Events.UPDATED_PRIVACY, {
	            			privacy: EventBus.Constants.PUBLIC
	            		});
    				},						
    				onFailure: function(xhr) {
    					alert(xhr.statusText);
    				}
            	});
            	break;
            case EventBus.Events.CHANGE_FAVORITE_OFF:
            	activityClient.updateFavorite(Activity.ACTIVITY_ID, false, {
            		onSuccess: function() {
	            		jQuery.shout(EventBus.Events.UPDATED_FAVORITE, {
	            			favorite: false
	            		});
    				},
    				onFailure: function(xhr) {
    					alert(xhr.statusText);
    				}
            	});
            	break;
            case EventBus.Events.CHANGE_FAVORITE_ON:
            	activityClient.updateFavorite(Activity.ACTIVITY_ID, true, {
            		onSuccess: function() {
	            		jQuery.shout(EventBus.Events.UPDATED_FAVORITE, {
	            			favorite: true
	            		});
    				},
    				onFailure: function(xhr) {
    					alert(xhr.statusText);
    				}
            	});
            	break;
        	case EventBus.Events.CHANGE_LAYOUT:
        	    var settingsClient = new Garmin.service.settingsClient();
            	settingsClient.updateUserPreference('ActivityDetailsAxmBackingBean.collapsedComponents', Object.toJSON(data), {
            		onSuccess: function() {
	            		jQuery.shout(EventBus.Events.UPDATED_LAYOUT, {
	            		});
    				},						
    				onFailure: function(xhr) {
    					alert(xhr.statusText);
    				}
            	});
                break;
        	case EventBus.Events.GET_GEOCODER:
        		activityClient.getGeocoder(data.location, {
        			onSuccess: function(response) {
        				var json = jQuery.xml2json(response.responseText);
	        			jQuery.shout(EventBus.Events.GEOCODER_RESPONDED, {
	        				json: json
	        			});
	        		},						
	        		onFailure: function(xhr) {
	        			alert(xhr.statusText);
	        		}
        		});
        		break;
        	case EventBus.Events.CLOSE_WIDGET:
        		jQuery.shout(EventBus.Events.WIDGET_CLOSED, data);
        		break;
        	case EventBus.Events.SAVE_ACTIVITY:
        		jQuery.shout(EventBus.Events.ACTIVITY_SAVED, data);
        		break;
        	default:
        		// delegate all other events to the original event bus
        		jQuery.shout(eventName, data);
        	break;
        }
    }
};
 
EventBus.Events = {
	//shouts from user events
    CHANGE_LAYOUT: 'change-layout',
    UPDATED_LAYOUT: 'updated-layout',
    CHANGE_ACTIVITY_TYPE: 'change-activity-type',
    UPDATED_ACTIVITY_TYPE: 'updated-activity-type',
    CHANGE_PRIVACY_PRIVATE: 'change-privacy-private',
    CHANGE_PRIVACY_PUBLIC: 'change-privacy-public',
    CHANGE_FAVORITE_OFF: 'change-favorite-off',
    CHANGE_FAVORITE_ON: 'change-favorite-on',
    CHANGE_TIMEZONE: 'change-timezone',
    GET_GEOCODER: 'get-geocoder',
	CHANGE_TIMING_UNIT: 'change-timing-unit',
    CHANGE_HEARTRATE_UNIT: 'change-heartrate-unit',
    CHANGE_POWER_UNIT: 'change-power-unit',
    CLOSE_WIDGET: 'close_widget',
    SAVE_ACTIVITY: 'save_activity',
    //shouts from bused events
    UPDATED_ACTIVITY_TYPE_ERROR: 'updated-activity-type-error',
    UPDATED_PRIVACY: 'updated-privacy',
    UPDATED_FAVORITE: 'updated-favorite',
	UPDATED_TIMEZONE: 'updated-timezone',
    GEOCODER_RESPONDED: 'geocoder-responded',
    WIDGET_CLOSED: 'widget_closed',
    ACTIVITY_SAVED: 'activity_saved'
};

EventBus.Constants = {
	PRIVATE: 'private',
	PUBLIC: 'public'
};
