/**
 * @filename activityControls.js
 * 
 * @description 
 * 
 * @author Diana Chow
 * @email diana.chow at garmin dot com
 *
 * Copyright(c) 2008, Garmin International
 */

function openPopUp(url, name, params) {
	var win = window.open(url, name, params);
}

var ActivityControlsComponentConstants = {
	controlButtons: {
		DELETE: 'actionDelete',
		PRIVACY: 'actionPrivacy',
		PRIVACY_TEXT: 'actionPrivacyText',
		PRIVATIZE: 'actionPrivate',
		PRIVATIZE_ICON: 'privateIcon',
		PUBLICIZE: 'actionPublic',
		PUBLICIZE_ICON: 'publicIcon',
		SHARE: 'actionShare'
	},
	
	dropDowns: {
		PRIVACY: 'privacyDropDown'		
	},
	
	DISABLED_BUTTON_CLASS: 'disabled'
};

ActivityControlsComponent = function(){
	return {
		init : function(){
			this.activityService = new Garmin.service.ActivityClient();
			this.fields = Garmin.form.ActivityFormFields;
		
			this.getPageComponents();
			this.personalizeButtons();
			this.giveButtonsWings();
			this.initializeDropDownAwesomeness();
		},
		
		personalizeButtons : function() {
			if(Activity.IS_OWNER) {
				this.enableDeleteButton();
				this.enablePrivacyMenu();
			}
			
			if(Activity.IS_PRIVATE) {
				this._privatizeActivityDisplay();
			} else {
				this._publicizeActivityDisplay();
			}
		},
		
		disableDeleteButton : function() {
			this.deleteButton.parent().hide();
			this.deleteButton.addClass(ActivityControlsComponentConstants.DISABLED_BUTTON_CLASS);
		},
		
		enableDeleteButton : function() {
			this.deleteButton.parent().show();
			this.deleteButton.removeClass(ActivityControlsComponentConstants.DISABLED_BUTTON_CLASS);
		},

		disablePrivacyMenu : function() {
			this.privacyButton.parent().hide();
			this.privacyButton.addClass(ActivityControlsComponentConstants.DISABLED_BUTTON_CLASS);
		},
		
		enablePrivacyMenu : function() {
			this.privacyButton.parent().show();
			this.privacyButton.removeClass(ActivityControlsComponentConstants.DISABLED_BUTTON_CLASS);
		},
		
		disableShareMenu : function() {
			this.shareButton.addClass(ActivityActionsComponentConstants.DISABLED_BUTTON_CLASS);			
		},
		
		enableShareMenu : function() {
			this.shareButton.removeClass(ActivityActionsComponentConstants.DISABLED_BUTTON_CLASS);
		},
		
		getPageComponents : function() {
			this.privacyButton = Ext.get(ActivityControlsComponentConstants.controlButtons.PRIVACY);
			this.privacyDropDown = Ext.get(ActivityControlsComponentConstants.dropDowns.PRIVACY);

			this.deleteButton = Ext.get(ActivityControlsComponentConstants.controlButtons.DELETE);

			this.privatizeButton = Ext.get(ActivityControlsComponentConstants.controlButtons.PRIVATIZE);
			this.publicizeButton = Ext.get(ActivityControlsComponentConstants.controlButtons.PUBLICIZE);
			
			this.shareButton = Ext.get(ActivityControlsComponentConstants.controlButtons.SHARE);			
		},
		
		initializeDropDownAwesomeness : function() {
			
			// Privacy Drop Down
			Ext.each(
				[this.privacyButton, this.privacyDropDown],
				function(element) {
					element.on('mouseover', function() {
						this.privacyDropDown.show();
					}.bind(this));
				}.bind(this)
			);

			Ext.each(
				[this.privacyButton, this.privacyDropDown],
				function(element) {
					element.on('mouseout', function() {
						this.privacyDropDown.hide();
					}.bind(this));
				}.bind(this)
			);
		},
		
		giveButtonsWings: function() {
			this.actionizeButton(this.deleteButton, this.deleteActivity);
			this.actionizeButton(this.privatizeButton, this.privatizeActivity);
			this.actionizeButton(this.publicizeButton, this.publicizeActivity);
		},
		
		actionizeButton: function(element, fnc) {
			element.on('click', fnc.bind(this));
		},
		
//		deactionizeButton: function(element) {
//			element.removeAllListeners();  EXT SUCKS!!!!!!! this method doesn't work.
//		},
		
		
		privatizeActivity: function() {
			this.activityService.updatePrivacy(Activity.ACTIVITY_ID, 'private', {
				onSuccess: function() {
					this._privatizeActivityDisplay();
				}.bind(this),						
				onFailure: function(xhr) {
					this.fields.alertError(xhr.statusText);
//					this.privacyButton.dom.innerHTML = bundle_activity_api.privacy_public;
				}.bind(this)
			});
		},
		
		_privatizeActivityDisplay: function() {
			this.privacyButton.removeClass(ActivityControlsComponentConstants.controlButtons.PUBLICIZE_ICON);
			this.privacyButton.addClass(ActivityControlsComponentConstants.controlButtons.PRIVATIZE_ICON);
//			this.privacyButton.dom.innerHTML = bundle_activity_api.privacy_private;

            // No sharing of privates!
            this.disableShareMenu();
            Activity.IS_PRIVATE = true;
		},
		
		publicizeActivity: function() {
			this.activityService.updatePrivacy(Activity.ACTIVITY_ID, 'public', {
				onSuccess: function() {
					this._publicizeActivityDisplay();
				}.bind(this),						
				onFailure: function(xhr) {
					this.fields.alertError(xhr.statusText);
//					this.privacyButton.dom.innerHTML = bundle_activity_api.privacy_private;
				}.bind(this)
			});
		},
		
		_publicizeActivityDisplay: function() {
			this.privacyButton.removeClass(ActivityControlsComponentConstants.controlButtons.PRIVATIZE_ICON);
			this.privacyButton.addClass(ActivityControlsComponentConstants.controlButtons.PUBLICIZE_ICON);		
//			this.privacyButton.dom.innerHTML = bundle_activity_api.privacy_public;

            this.enableShareMenu();
            Activity.IS_PRIVATE = false;
		},
		
		deleteActivity: function() {
			Ext.MessageBox.show({
				title: bundle_activity_api.delete_activity, 
				msg: bundle_activity_api.delete_message, 
				buttons: {yes: bundle_activity_api.yes, no: bundle_activity_api.no}, 
				fn:	function(selection) {
						if(selection == 'yes') {
							this.activityService.deleteActivity(Activity.ACTIVITY_ID, {
								onSuccess: function() {
									// Redirect to the activities page
									location.href = '/activities';
								}
							});
						} else {
						}
					}, 
				scope: this, 
				icon:Ext.MessageBox.QUESTION
			});
		}
		
	 };
}();
Ext.EventManager.onDocumentReady(ActivityControlsComponent.init, ActivityControlsComponent, true);