var WorkoutCreator = new Class ({
	initialize: function(options) {
	
		//setup DOM elements
		this.myOptions 		= options;
		this.myContainer	= $(this.myOptions.containerId);
		this.myMessage		= $(this.myOptions.msgId);
		this.mySource 		= $(this.myOptions.sourceId);
		this.myWorkoutAuth = $(this.myOptions.workoutAuthor);
		this.myWorkoutValidate = $(this.myOptions.workoutValidate);
		this.myShareLink 	= this.myContainer.getElement(this.myOptions.shareName);
		this.mySourceList 	= this.mySource.getElements('li');
		this.myTarget 		= $(this.myOptions.targetId);
		this.myTargetList 	= this.myTarget.getElements('li');

		//setup default states
		this.myShareLink.setStyle('opacity','0');
		this.myMessage.setStyle('opacity','0');
		
		//other options
		this.myMax 			= this.myOptions.max;
		this.tipsOptions 	= {
			showDelay: 0,
	        hideDelay: 0,
	        offsets: {x: -32, y: -26},
	        targetElm: this.myContainer,
			fixed: true
	    }
		this.myAlertDelay = 3000;

		//build elements
		this.myTarget.empty();
		this.setupName();
		this.setupValidate();
		var myTips = new Tips(this.mySourceList,this.tipsOptions);
		this.mySourceList.each(function(item){
			item.store('tip:title', item.getProperty('html'));
			item.addEvent('click', function(e) {
				e.stop();
				this.injectExercise(item);
			}.bind(this));
		}.bind(this));
	
	},
	setupName: function() {
		//setup name entry field
		this.myWorkoutAuth.addEvent('keyup', function(e) {
			this.updateSharing();
		}.bind(this));
	},
	setupValidate: function() {
		//setup validate
		this.myWorkoutValidate.addEvent('click', function(e) {
			e.stop();
			if (this.myTargetList.length < 5) {
				this.showAlert('Make sure you have at least 5 exercises selected.','error');
			} else if (this.myWorkoutAuth.getProperty('value') === "") {
				this.showAlert('Don\'t forget to enter your name!','error');
			}
		}.bind(this));
	},
	injectExercise: function(item) {
		this.myTargetList = this.myTarget.getElements('li');

		if (this.myTargetList.length < this.myMax) {
			//clone element
			var newExercise = item.clone();
			
			//setup fx + tips
			var myFx = new Fx.Tween(newExercise,{duration:500});
			var myTip = new Tips(newExercise,this.tipsOptions);
			
			//setup cloned element
			newExercise.store('tip:title', newExercise.getProperty('html'));
			newExercise.setStyle('opacity','0');
			
			
			//I6 FIX
			if ((Browser.Engine.trident) && (Browser.Engine.version <= 4)) {
				newExercise.addEvent('mouseenter', function(e) {
					newExercise.addClass('hover');
				});
				newExercise.addEvent('mouseleave', function(e) {
					newExercise.removeClass('hover');
				});						
			}
			
			//create remove btn
			var removeBtn = new Element ('a', {
    			'class': 'remove',
    			'html': 'remove'
			});
    		
    		removeBtn.addEvent('click', function(e) {
    			e.stop();
    			myTip.container.dispose();
    			this.showAlert('You have removed ' + newExercise.retrieve('tip:title') + ' from your workout.','alert');
    			myFx.start('opacity','0').chain(function(){
	    			newExercise.dispose();
	    			this.updateSharing();
    			}.bind(this));
    		}.bind(this));
    		
    		//inject elements into list
    		newExercise.inject(this.myTarget);
    		removeBtn.inject(newExercise);
    		this.showAlert('You have added ' + newExercise.retrieve('tip:title') + ' to your workout.','alert');
    		
    		//now show them
			myFx.start('opacity','1');
			this.updateSharing();
			
		} else {
			this.showAlert('Your workout is full. Please remove an exercise before you add another.','error');
		}
		
	},
	showAlert: function(msg,style) {
		//set message
		this.myMessage.setProperty('html','<p class="'+ style +'">' + msg + '</p>')
		this.myMessage.setStyle('opacity','1');
		
		//make sure timer is reset if a new alert is displayed
		if (this.myTimer) $clear(this.myTimer);
		this.myTimer = this.clearAlert.delay(this.myAlertDelay,this);
	},
	clearAlert: function(msg,style) {
		var myFx = new Fx.Tween(this.myMessage,{duration:1000});
		myFx.cancel();		
		myFx.start('opacity','0');
	},
	updateSharing: function() {
		//get updated exercise list + author name
		this.myTargetList 	= this.myTarget.getElements('li');
		this.authorName 	= this.myWorkoutAuth.getProperty('value')
				
		//validate form, hide sharing button if it's not ready yet
		if (this.myTargetList.length < 5 || this.authorName === "") {
			this.myShareLink.setStyle('opacity',0);
			this.myWorkoutValidate.setStyle('opacity',1);
		} else {
			//build title
			this.targetTitle = this.authorName + '\'s EA SPORTS Active custom workout';

			//build URL
			this.targetLink = 'http://' + document.location.host + document.location.pathname;
			this.targetLink += "?n=" + this.authorName + '&a=';
			
			//add exercise info
			this.myTargetList.each(function(item,index){
				if (index != 0) this.targetLink += ",";
				this.targetLink += item.className;
			}.bind(this));
			
			//set for sharing
			this.myShareLink.setProperty('href',encodeURI(this.targetLink))
			this.myShareLink.setProperty('title',this.targetTitle)
			this.myWorkoutValidate.setStyle('opacity',0);
			this.myShareLink.setStyle('opacity',1);
		}
	}
});	


var ShareWorkout = new Class ({
	initialize: function(containerId,sourceElm) {
		this.myContainer	= $(containerId);
		this.mySource 		= this.myContainer.getElement(sourceElm);
		this.mySourceList 	= this.mySource.getElements('li');
		this.tipsOptions = {
			showDelay: 0,
			hideDelay: 0,
			offsets: {x: -32, y: -26},
			targetElm: this.myContainer,
			fixed: true
		}
		var myTips = new Tips(this.mySourceList,this.tipsOptions);
		
		this.mySourceList.each(function(item){
			item.store('tip:title', item.getProperty('html'));
		}.bind(this));
	}
});	
