LoginFormDisplay = Class.create();
LoginFormDisplay.prototype = {

  	initialize: function(options) {
		
		this.options = options;
		this.loginBoxId = options['loginBoxId'];  
		this.loginLinkId = options['loginLinkId'];
				
		Event.observe(this.loginLinkId, 'click', this.displayBox.bind(this)); 

	},           
	displayBox: function(event) {   
		
		if (event != null){
			event.stop();  
    	}

	    // stop observing for show click
		Event.stopObserving(this.loginLinkId);
	
		// start observing for hide click
		Event.observe(this.loginLinkId, 'click', this.hideBox.bind(this));                                             

		$(this.loginBoxId).show(); 
		$('login-box-username').focus();
	},                              
	hideBox: function(event) {
		
		if (event != null){
			event.stop();  
    	}
		
		$(this.loginBoxId).hide();  
		                                      
		// stop observing for hide click
		Event.stopObserving(this.loginLinkId);
		
		// start observing for show click
		Event.observe(this.loginLinkId, 'click', this.displayBox.bind(this));
		
	}
	
}
	                  
	
FeatureBoxFader = Class.create();
FeatureBoxFader.prototype = {
	
	initialize: function(featureBoxId, itemClass) {
		      
		this.featureBoxId = featureBoxId;   
		this.itemClass = itemClass;                       
		this.allChildren = $(this.featureBoxId).childElements(); 
		this.totalChildren = this.allChildren.length - 1;
		this.currentIndex = 0;  
		this.fadeDuration = .3;
		
		this.updater = new PeriodicalExecuter(this.change.bind(this), 6);
		
	},
	change: function(event){

	    var me = this;
	    var currElem = $(this.allChildren[this.currentIndex]);
	
		if (currElem.visible()){
			if (this.currentIndex < this.totalChildren){
				this.currentIndex += 1;
				currElem.fade({
					duration: me.fadeDuration
				});
				$(this.allChildren[this.currentIndex]).appear({
					duration: me.fadeDuration
				});                                    
			} else {
				this.currentIndex = 0;
				currElem.fade({
					duration: me.fadeDuration
				});
				this.allChildren[0].appear({
					duration: me.fadeDuration
				});
			}
		} else {
			currElem.appear({
				duration: me.fadeDuration
			});
		}
		
	}
	
}   


EventAttendingToggle = Class.create();
EventAttendingToggle.prototype = {

  	initialize: function(options) {
		
		this.options = options;
		this.eventAddId = options['eventAddId'];
		this.eventRemoveId = options['eventRemoveId'];  
		this.eventMsgId = options['eventMsgId'];
				
		Event.observe(this.eventAddId, 'click', this.attending.bind(this));
		Event.observe(this.eventRemoveId, 'click', this.notAttending.bind(this));
		
	},
	attending: function(event) {

		var me = this;                                               
		event.stop();

		var url = $(this.eventAddId).href;
		new Ajax.Request(url, {
			onSuccess: function(response) {
				var msg = response.responseText;
				console.log(msg);
				if (msg.indexOf('You are attending this event') >= 0){
					$(me.eventMsgId).innerHTML = "This event is currently on your list.  If you changed your mind are not attending, click \"Remove Event\" below.";
					$(me.eventMsgId).removeClassName("attend-no");
					$(me.eventMsgId).addClassName("attend-yes");
					$(me.eventAddId).hide();      
					$(me.eventRemoveId).show();
				}
			}
		});
		
		
	},
	notAttending: function(event) {

		var me = this;                                               
		event.stop();

		var url = $(this.eventRemoveId).href;
		new Ajax.Request(url, {
			onSuccess: function(response) {
				var msg = response.responseText;
				if (msg.indexOf('You are not attending this event') >= 0){
					$(me.eventMsgId).innerHTML = "This event is not currently on your list.  To add this event to your list, click \"Save Event\" below.";
					$(me.eventMsgId).removeClassName("attend-yes");
					$(me.eventMsgId).addClassName("attend-no");					
					$(me.eventRemoveId).hide();
					$(me.eventAddId).show();
				}
			}
		});
		
	}
	
}   

function checkboxShowHide(me, elemToToggle){
	if (me.checked){
		$(elemToToggle).show();
	} else {
		$(elemToToggle).hide();
	}
}

function updateCartReturn() {
	$("checkout_button").observe('click', function(){
		$("return_url").value = "cart/checkout";
		$("update_cart_form").submit(); 
		return false;
	});
 
	$("update_button").observe('click', function(){
		$("return_url").value = "cart/";
		$("update_cart_form").submit(); 
		return false;
	});       
}	                         
