var GirlStudio = Class.create();

GirlStudio.prototype = {

	processingPopup : false,
	tabLinks : new Array(),
	tabs : new Array('login', 'shopping'),
	popups : new Array('tellFriend', 'getGoss'),

	//Constructor
	initialize: function() {
		
		$('closePopup').observe('click', (function() { this.hideAll(); this.showTab('login'); }).bind(this));
		
		this.tabLinks = $$('#loginBox div.links a');
		
		$('tabHolder').style.display = 'block';
		$('popup').style.display = 'block';
		
		this.hideAll();
		this.showTab('login');
		
		this.tabLinks.each(function(link){
								link.observe('click', (function(event) {
														var toShow = link.rel.substring(4);
														GS.hideAll();
														switch(link.rel.substring(0,3)){
															case "tab":
																GS.showTab(toShow);
																break;
															case "pop":
																GS.showPopup(toShow);
																break;
														}
														Event.stop(event);
													}).bind(this));
							});
											
		$('tellFriendForm').observe('submit', (function(event) {
												Event.stop(event);
												if(!this.processingPopup){
													this.processingPopup = true;
													this.processPopup('tellFriend');
												}
											}).bind(this));
											
		$('getGossForm').observe('submit', (function(event) {
												Event.stop(event);
												if(!this.processingPopup){
													this.processingPopup = true;
													this.processPopup('getGoss');
												}
											}).bind(this));
											
		var regForm = $('regForm');
		if(regForm) {
			$('regForm').observe('keyup', (function(event) {
											GS.checkFields(event);
										}).bind(this));
		}
		
		var addItemForm = $('addItem');
		if(addItemForm){
			var price = $('price').innerHTML;
			addItemForm.observe('submit', (function(event) {
												/*Event.stop(event);
												new Ajax.Request('add-item', {
													parameters : $('addItem').serialize(),
													evalJSON : true, 
													onCreate: function() {
														
													},
													onFailure: function() {
														
													},
													onSuccess: function(transport){
														$('body').innerHTML = (transport.responseText);
														var json = transport.responseJSON;
														$('shoppingLink').innerHTML = "Shopping Bag ("+transport.responseJSON.total+")";
													}
												});*/
											}).bind(this));
											
			$('quantity').observe('keyup', (function(event) {
												if(!$('quantity').value.match(/^\d+$/)){
													var quantity = 1;
												}
												else {
													var quantity = $('quantity').value;
												}
												$('price').innerHTML = (quantity*price).toFixed(2);
											}).bind(this));
		}
		
		var addressForm = $('addressForm');
		if(addressForm){
			this.deliveryFields();
			$('ship_to').observe('change', (function() {
												this.deliveryFields();
											}).bind(this));
		}
		
		$('shopping').observe('mouseover', (function() {
												$('shoppingContents').style.display = 'block';		
											}));
											
		$('shopping').observe('mouseout', (function() {
												$('shoppingContents').style.display = 'none';		
											}));
		
	},
	
	deliveryFields: function() {
		if($('ship_to').value=="Billing Address"){
			$('delAddress').hide();
		}
		else {
			$('delAddress').show();
		}	
	},
	
	hideAll: function() {
		this.hideTabs();
		this.hidePopups();
		this.tabLinks.each(function(link) {
			link = $(link);
			link.removeClassName('selected');
			link.blur();
		});
	},
	
	showTab: function(tab) {
		$(tab).show();
		$(tab+'Link').addClassName('selected');
	},
	
	hideTabs: function() {
		this.tabs.each(function(tab) {
							tab = $(tab);
							tab.hide();
						});
	},
	
	showPopup: function(popup){
		$('popup').style.display = 'block';
		$(popup).show(); //content
		$(popup+'Link').addClassName('selected');
	},
	
	hidePopups: function() {
		$('popup').hide();
		this.popups.each(function(popup){
							popup = $(popup);
							popup.hide();
						 });
	},
	
	processPopup: function(popup){
		try {
			$(popup+'Form').getInputs('text').each(function(el) {
														if(el.value==""){
															throw "All fields required";
														}
													});
			$(popup+'Form').getInputs('checkbox').each(function(el) {
														if(!el.checked){
															throw "You must agree to the Terms &amp; Conditions";
														}
													});
			new Ajax.Request('ajax/'+popup+'.inc.php', {
				parameters : $(popup+'Form').serialize(),
				evalJSON : true, 
				onCreate: function() {
					$(popup+'Msg').innerHTML = '<img src="images/ajax-loader.gif" alt="Sending..." />';
				},
				onFailure: function() {
					$(popup+'Msg').innerHTML = 'An error occurred. Please try again.';
					GS.processingPopup = false;
				},
				onSuccess: function(transport){
					$(popup+'Msg').innerHTML = transport.responseJSON.message;
					GS.processingPopup = false;
				}
			});
		}
		catch(e){
			$(popup+'Msg').innerHTML = e;
			GS.processingPopup = false;
		}
	},
	
	checkFields: function(event){
		var errorCol = "#ffaaaa";
		var okCol = "#f5f5f5";
		element = Event.element(event);
		switch(element.name){
			case "user":
				if(element.value.match(/^([^@.]+?)@([^@.]+?)\.([^@.]+)$/)){ //rough check for an actual email address
					new Ajax.Request('ajax/user.inc.php', {
						parameters : 'user='+element.value,
						evalJSON : true,
						onFailure: function(){
							$('regUserMsg').innerHTML = "";
						},
						onSuccess: function(transport){
							$('regUserMsg').innerHTML = transport.responseJSON.message;
						}
					});
					element.style.background = okCol;
				}
				else {
					element.style.background = errorCol;
					$('regUserMsg').innerHTML = "";
				}
				break;
			case "pass": case "pass2":
				pass = $$('#regForm input[name=pass]')[0].value;
				pass2 = $$('#regForm input[name=pass2]')[0].value;
				var msg = "";
				if(pass!=""){
					if(pass.length<5){
						msg = "Passwords must be at least 5 characters long";
					}
					else if(pass2!="" && pass!=pass2){
						msg = "Passwords do not match";
					}
				}
				else if(pass2!=""){
					msg = "Passwords do not match";
				}
				if(msg==""){
					element.style.background = okCol;
				}
				else {
					element.style.background = errorCol;
				}
				$('regPassMsg').innerHTML = msg;
				break;
			case "dob[d]":
				if(!element.value.match(/^\d{1,2}$/)){
					element.style.background = errorCol;
				}
				else {
					element.style.background = okCol;
				}
				break;
			case "dob[M]":
				if(!element.value.match(/^\d{1,2}$/)){
					element.style.background = errorCol;
				}
				else {
					element.style.background = okCol;
				}
				break;
			case "dob[Y]":
				if(!element.value.match(/^\d{4}$/)){
					element.style.background = errorCol;
				}
				else {
					element.style.background = okCol;
				}
				break;
			case "first_name": case "last_name":
				if(!element.value.match(/^.+$/)){
					element.style.background = errorCol;
				}
				else {
					element.style.background = okCol;
				}
				break;
		}
	}

}

document.observe('dom:loaded', function () { GS = new GirlStudio(); });
if(productAdded){
	document.observe('dom:loaded', function () { GS.hideAll(); GS.showTab('shopping'); $('shoppingContents').style.display = 'block'; });
}