// Cart Class 

var Member = Class.create({

	initialize: function() {
	
		// instantiate service
		this.service = new MemberService();
		this.values = {};
		this.logged_in = false;
		
		document.observe('dom:loaded', function() {
		
			this.products_not_fav = $$('.product-favorite');
			
			if(this.products_not_fav.size()>0) {
			
				this.products_not_fav.each(function(fav_btn) {
				
					fav_btn.observe('click', function() {
						this.add_favorite(fav_btn.id);
					}.bind(this));
			
				}.bind(this));
				
			}
			
			
			this.products_fav = $$('.product-favorite-added');
			
			if(this.products_fav.size()>0) {
			
				this.products_fav.each(function(fav_btn) {
				
					fav_btn.observe('click', function() {
						this.remove_favorite(fav_btn.id);
					}.bind(this));
			
				}.bind(this));
			
			}
			
			if(live.page.page_url=="my_favorites") {
			
				this.share_form = $('share-form-container');
				this.share_table = $('share-table'); 
				this.share_address = $('share-to-address');
				this.share_message = $('share-message');
				this.share_btn = $('share-send');
			
			}
		
		
		}.bind(this));
	
	},
	
	login: function(values) {
	
		this.login_form = values.form;
	
		this.success_page = values.success_page;
		
		if(!values.username) {
			alert('You forgot to enter your email!');
			this.login_form.enable();
			return false;
		}
		
		if(!values.password) {
			alert('You forgot to enter your password!');
			this.login_form.enable();
			return false;
		}
	
		values = Object.toJSON(values);	
		
		live.modal.open_loader({
			height:75,
			width:400,
			img:'/images/public/loading.gif',
			title: 'Logging You In'
		});
			
		this.service.login.call(values);
		this.service.login.result = function(result) {
			
			if(result) {

				if(this.success_page) {
				
					window.location = this.success_page;
					
				} else {
				
					window.location = "/";
				
				}
				
			} else {
				
				live.modal.close();
				this.login_form.enable();
				alert('The email and password did not match our records. Please try again');
				
			}
			
		}.bind(this);	
		
	},
	
	logout: function() {
			
		user_confirm = confirm('Are you sure you would like to log out?');
		
		if(user_confirm) {
			
			this.service.logout.call();
			this.service.logout.result = function(result) {
				
				if(result) {
			
					window.location = "/";
			
				}
				
			}.bind(this);	
		
		}
		
	},
	
	update: function() {
			
		this.service.update.call();
		this.service.update.result = function(result) {
			
			if(result) {
		
				this.values = result;
				this.logged_in = true;
		
			}
			
		}.bind(this);	
		
	},
	
	set_values: function(values) {
	
		values = Object.toJSON(values);	
		
		this.service.setValues.call(values);
		this.service.setValues.result = function(result) {
			
			if(result) {
		
				this.values = result;
		
			}
			
		}.bind(this);	
		
	},
	
	change_password: function() {
	
		old_password = prompt('Please enter your current password','');

		if(old_password) {

			new_password = prompt('Please enter your new password','');
		
			if(new_password) {
			
				confirm_password = prompt('Please confirm your new password','');
				
				if(confirm_password) {
				
					if(new_password==confirm_password) {
		
						values = {
							old_password: old_password,
							new_password: new_password,
							confirm_password: confirm_password
						};
		
						values = Object.toJSON(values);
		
						this.service.change_password.call(values);
						this.service.change_password.result = function(result) {
			
							if(result) {
		
								alert('You have successfully changed your password');
								window.location.href = "/my_account/";
		
							}
			
						}.bind(this);
			
					} else {
					
						alert('You supplied mismatched passwords, please try again');
					
					}
					
				
				} else {
		
					return false;
				
				}
			
			} else {
		
				return false;
				
			}
			
		} else {
		
			return false;
		
		}	
		
	},
	
	add_favorite: function(item_id) {

		this.current_item_sku = item_id.gsub('product-favorite-','');
		this.current_item = $(item_id);
		
		this.service.add_favorite.call(this.current_item_sku);
		this.service.add_favorite.result = function(result) {
			
			if(result) {
			
				this.current_item.stopObserving('click');
		
				alert('Item Added to Your Favorites');
				this.current_item.removeClassName('product-favorite');
				this.current_item.addClassName('product-favorite-added');
				this.current_item.title = 'Remove Item From My Favorites';
				
				this.current_item.observe('click',function() {
					this.remove_favorite(this.current_item.id);
				}.bind(this));
				
			}
			
		}.bind(this);	
		
	},
	
	remove_favorite: function(item_id,animate_remove) {
	
		user_confirm = confirm('Are you sure you would like to remove this favorite?');
		
		if(user_confirm) {
	
			this.animate_remove = false;
			if(animate_remove) {
				this.animate_remove = true;
			}
	
			this.current_item_sku = item_id.gsub('product-favorite-','');
			this.current_item = $(item_id);
		
			this.service.remove_favorite.call(this.current_item_sku);
			this.service.remove_favorite.result = function(result) {
			
				if(result) {
			
					this.current_item.stopObserving('click');
			
					if(this.animate_remove) {
				
						Effect.DropOut(this.current_item.up(1),{duration:0.5});
				
					} else { 
		
						alert('Item Removed to Your Favorites');
					
						this.current_item.addClassName('product-favorite');
						this.current_item.removeClassName('product-favorite-added');
					
						this.current_item.title = 'Add Item To My Favorites';
				
						this.current_item.observe('click',function() {
							this.add_favorite(this.current_item.id);
						}.bind(this));
		
					}
		
				}
			
			}.bind(this);
			
		}	
		
	},
	
	share_popup: function(item_id) {
		
		this.current_item_sku = item_id.gsub('product-share-','');
		this.share_item = $(item_id).up(1).clone(true);	
			
		live.modal.open({
			content:'',
			height:520,
			width:600,
			title:'Share Your Favorite!'
		});
		
		live.modal.popup.content.appendChild(this.share_form);
		
		this.share_form.show();
		this.share_table.update('<tbody id="share-table-body"></tbody>');
		$('share-table-body').appendChild(this.share_item);
		
		$$('#share-table td.favorite-email')[0].remove();
		$$('#share-table td.favorite-star')[0].remove();
		
		Event.observe(this.share_btn,'click', this.share_favorite.bind(this));
		
		Event.observe('popup-modal-close', 'click', function() {
		
			this.share_form.hide();
			$('edit-form').appendChild(this.share_form);
			this.share_address.value = '';
			this.share_message.value = '';
			this.share_table.update('');
			
			Event.stopObserving(this.share_btn,'click');
		
		}.bind(this));
		
		//this.modal_content = '';
		//this.modal_content = $('share-form-container').innerHTML;
	
	},
	
	share_favorite: function() {
	
		this.share_form.hide();
		$('edit-form').appendChild(this.share_form);

		share_details = {
			from_address: Member.values.contact_email,
			to_address: this.share_address.value,
			body: $('message-preview').innerHTML,
			body_additional: this.share_message.value,
			from_name: Member.values.contact_first_name
		};		

		this.service.share_favorite.call(share_details);

		this.service.share_favorite.result = function(result) {
			
			if(result) {
					
				alert("You've Successfully Shared Your Favorite with "+$('share-to-address').value);
				this.share_address.value = '';
				this.share_message.value = '';
				this.share_table.update('');
				
				live.modal.close();
				Event.stopObserving(this.share_btn,'click');
			

			} else {
				
				//$('edit-form').appendChild($('share-form-container'));
				alert('The email address you supplied doesn\'t seem to be valid. Please recheck the address before sharing');
				$('popup-modal-content').appendChild(this.share_form);
				this.share_form.show();
			}
			
			
		}.bind(this);	
		
	},
	

	autofill_value: function(field,value) {
	
		this.checkout_form['cart_order['+field+']'].value = value;
	
	},
	
	autofill_checkout_form: function() {
	
		this.checkout_form = $('checkout');
		
		this.autofill_value('order_billing_first_name',this.values.contact_first_name);
		this.autofill_value('order_billing_last_name',this.values.contact_last_name);
		this.autofill_value('order_billing_street_1',this.values.contact_street_1);
		this.autofill_value('order_billing_street_2',this.values.contact_street_2);
		this.autofill_value('order_billing_city',this.values.contact_city);
		this.autofill_value('order_billing_state',this.values.contact_state);
		this.autofill_value('order_billing_zip',this.values.contact_zip);
		this.autofill_value('order_billing_country',this.values.contact_country);
		this.autofill_value('order_email',this.values.contact_email);
		this.autofill_value('order_billing_phone_1',this.values.contact_phone_1);
		
		Effect.Fade('autofill-btn',{duration:0.5});
		
	}
	
	
});

Member = new Member();
Member.update();


