// Globals
var globalScrollableWindow = null;

var globalTrialError = false;
var globalPaymentError = false;
var globalSignUpError = false;

window.addEvent('domready', function(e) {
	globalScrollableWindow = new Fx.Scroll(window);
	
	if (globalTrialError) globalScrollableWindow.toElement('scrollToAnchor');
	if (globalPaymentError) globalScrollableWindow.toElement('scrollToAnchor');
	if (globalSignUpError) globalScrollableWindow.toElement('scrollToAnchor');
	
	if ($('menu-main-menu')) {
		$('menu-main-menu').getLast().addClass('last');
		
		$$('#menu-main-menu > li').each(function(itm) {
			itm.addEvent('mouseenter', function(e) {
				itm.addClass('hover');
			});
			
			itm.addEvent('mouseleave', function(e) {
				itm.removeClass('hover');
			});
		});
		
		$$('.sub-menu').each(function(subMenu){
			
			subMenu.getFirst().addClass('first');
			subMenu.getLast().addClass('last');
			
			new Element('li', {'class': 'subMenuArrow'}).inject(subMenu);
		});
	}
	
	if ($('ourdiskTrialForm')) new FreeTrialForm();
	if ($('ourdiskPaymentForm')) new PaymentSignUpForm({ formId: 'ourdiskPaymentForm' });
	if ($('ourdiskSignUpForm')) new PaymentSignUpForm({ formId: 'ourdiskSignUpForm', isSignUpForm: true });
});


var FreeTrialForm = new Class({
	workingDelayID: null,
	delayID: null,
	
	initialize: function() {
		this.initThemeCheck();
		
		$$('#trialBanner .signUpLink').each(function(lnk) {
			lnk.addEvent('click', function(e) {
				e.preventDefault();
				globalScrollableWindow.toElement('scrollToAnchor');
			});
		});
	},
	
	initThemeCheck: function() {
		this.workingDelayID = 0;
		this.delayID = this.checkName.delay(500, this);
		
		$('ourdiskTrialForm').getElement('input[name="siteName"]').addEvent('keyup', function(e){
			this.delayID = this.checkName.delay(500, this);
		}.bind(this));
		
		$('ourdiskTrialForm').getElement('input[name="siteName"]').addEvent('blur', function(e){
			this.delayID = this.checkName.delay(500, this);
		}.bind(this));
	},
	
	checkName: function() {
		if(this.workingDelayID > 0) return;

		this.workingDelayID = this.delayID;

		new Request({
			'url': '/wordpress/wp-content/plugins/account-signup/includes/ajax/check_theme_name.ajax.php',
			'data': 'theme_name=' +$('ourdiskTrialForm').getElement('input[name="siteName"]').get('value'),
			'method':'post',

			'onSuccess': function(responseText) {
				try {
					var result = JSON.decode(responseText);
					if (result) {
						if (result.status == "fail") {
							$('siteNameInputFields').addClass('error');
							$('siteNameInputError').set('html', result.message);

						} else if (result.status == "success") {
							$('siteNameInputFields').removeClass('error');
							$('siteNameInputError').set('html', '');

						}
					}
				} catch(err) {
					alert('Fail to check theme name');
				}
				
				this.lastDelayID = this.workingDelayID;
				this.workingDelayID = 0;

				if (this.delayID > this.lastDelayID) this.checkName();
				
			}.bind(this),

			onFailure: function(xhr){
				alert('Failed to request');
			}
		}).send();
	}
});


var PaymentSignUpForm = new Class({
	Implements: [Options],
	
	options: {
		'formId': null,
		'isSignUpForm': false
	},
	
	initialize: function(options) {
		this.setOptions(options);
		
		if (this.options.formId == null) return;
		if (this.options.isSignUpForm) this.initThemeCheck();
		
		$$('#paymentBanner .signUpLink').each(function(lnk) {
			lnk.addEvent('click', function(e) {
				e.preventDefault();
				$$('#' +this.options.formId+ ' input[name="plan"]').each(function(ipt) {
					if (ipt.get('value') == e.target.getProperty('href').substr(1)) {
						globalScrollableWindow.toElement('scrollToAnchor');
						ipt.setProperty('checked', 'checked');
						if($('monthlyAmount')){
							$('monthlyAmount').fade('hide');
							$('monthlyAmount').set('html', e.target.getPrevious('span.planPricing').get('html'));
							$('selectedMonthlyAmount').set('value', e.target.getPrevious('span.planPricing').get('html'));
							$('monthlyAmount').fade('in');
						}
					}
				}.bind(this));
			}.bind(this));
		}.bind(this));
		
		$$('#' +this.options.formId+ ' input[name="plan"]').each(function(ipt) {
			ipt.addEvent('change', function(e) {
				if($('monthlyAmount')){
					$('monthlyAmount').fade('hide');
					$('monthlyAmount').set('html', e.target.getParent('tr').getElement('span.planPricing').get('html'));
					$('selectedMonthlyAmount').set('value', e.target.getParent('tr').getElement('span.planPricing').get('html'));
					$('monthlyAmount').fade('in');
				}
			});
		});
	},
	
	initThemeCheck: function() {
		this.workingDelayID = 0;
		this.delayID = this.checkName.delay(500, this);
		
		$(this.options.formId).getElement('input[name="siteName"]').addEvent('keyup', function(e){
			this.delayID = this.checkName.delay(500, this);
		}.bind(this));
		
		$(this.options.formId).getElement('input[name="siteName"]').addEvent('blur', function(e){
			this.delayID = this.checkName.delay(500, this);
		}.bind(this));
	},
	
	checkName: function() {
		if(this.workingDelayID > 0) return;

		this.workingDelayID = this.delayID;

		new Request({
			'url': '/wordpress/wp-content/plugins/account-signup/includes/ajax/check_theme_name.ajax.php',
			'data': 'theme_name=' +$(this.options.formId).getElement('input[name="siteName"]').get('value'),
			'method':'post',

			'onSuccess': function(responseText) {
				try {
					var result = JSON.decode(responseText);
					if (result) {
						if (result.status == "fail") {
							$('siteNameInputFields').addClass('error');
							$('siteNameInputError').set('html', result.message);

						} else if (result.status == "success") {
							$('siteNameInputFields').removeClass('error');
							$('siteNameInputError').set('html', '');

						}
					}
				} catch(err) {
					alert('Fail to check theme name');
				}
				
				this.lastDelayID = this.workingDelayID;
				this.workingDelayID = 0;

				if (this.delayID > this.lastDelayID) this.checkName();
				
			}.bind(this),

			onFailure: function(xhr){
				alert('Failed to request');
			}
		}).send();
	}
});


/*
// Old payment page controls
var PaymentForm = new Class({
	selectedPlan: null,
	selectedCurrency: null,
	
	initialize: function() {
		this.initCurrencySelectors();
		this.initPlanSelectors();
		this.selectCurrency('AUD');
	},
	
	initCurrencySelectors: function() {
		$$('#paymentBanner select[name="currency"]').each(function(itm) {
			itm.addEvent('change', function(e) {
				this.selectCurrency(e.target.getSelected()[0].get('value'));
			}.bind(this));
		}.bind(this));
		
		$$('#ourdiskPaymentForm select[name="currency"]').each(function(itm) {
			itm.addEvent('change', function(e) {
				this.selectCurrency(e.target.getSelected()[0].get('value'));
			}.bind(this));
		}.bind(this));
	},
	
	initPlanSelectors: function() {
		$$('#planTable input[name="plan"]').each(function(itm) {
			itm.addEvent('change', function(e) {
				this.selectedPlan = e.target.get('value');
				$('monthlyAmount').fade('hide');
				$('currencyISO').fade('hide');
				$('monthlyAmount').set('html', e.target.getParent('tr').getElement('span.planPricing').get('text'));
				$('currencyISO').set('html', this.selectedCurrency);
				$('monthlyAmount').fade('in');
				$('currencyISO').fade('in');
			}.bind(this));
		}.bind(this));
	},
	
	getCurrencySymbol: function() {
		switch(this.selectedCurrency){
			case "AUD":
				return "$";
				break;
				
			case "USD":
				return "$";
				break;

			case "NZD":
				return "$";
				break;

			case "SGD":
				return "$";
				break;

			case "HKD":
				return "$";
				break;

			case "CAD":
				return "$";
				break;

			case "JPY":
				return "&yen;";
				break;

			case "EUR":
				return "&euro;";
				break;

			case "GBP":
				return "&pound;";
				break;
			
			default:
				return "$";
		}
	},
	
	selectCurrency: function(currency) {
		this.selectedCurrency = currency;
		
		// Show payment method fieldset for each currency
		if (currency == 'USD') {
			$('creditCardPayment').setStyle('display', 'none');
			$('2checkOut').setStyle('display', 'block');
		} else {
			$('creditCardPayment').setStyle('display', 'block');
			$('2checkOut').setStyle('display', 'none');
		}
		
		// Set all currency the same
		$$('#paymentBanner select[name="currency"]').each(function(selectEl) {
			selectEl.getElements('option[value="' + currency + '"]').each(function(optEl){
				optEl.setProperty('selected', 'selected');
			});
		});
		
		$$('#ourdiskPaymentForm select[name="currency"]').each(function(selectEl) {
			selectEl.getElements('option[value="' + currency + '"]').each(function(optEl){
				optEl.setProperty('selected', 'selected');
			});
		});
		
		// Request for new pricing base on cureency selected
		new Request({
			'url': '/wordpress/wp-content/plugins/account-signup/includes/ajax/plans.ajax.php',
			'data': 'action=retrieveByCurrency&currency=' +currency,
			
			'onSuccess': function(responseText) {
				var result = JSON.decode(responseText);
				
				if(result){
					if(result.status == "fail") {
						alert(result.message);
						return;
					}
					
					// Place the new pricing for that currency
					result.each(function(data) {
						$('paymentBanner').getElement('#' + data.planName + 'Plan').getElement('.planPricing').set('html', '<strong>' + this.getCurrencySymbol() + data.price + '</strong>');
						$('planTable').getElement('#' + data.planName + 'Plan').getElement('.planPricing').set('html', this.getCurrencySymbol() + data.price + '/month');
						
						if(data.planID == this.selectedPlan) {
							console.warn(this.selectedPlan);
							$('monthlyAmount').set('html', this.getCurrencySymbol() + data.price + '/month');
							$('currencyISO').set('html', this.selectedCurrency);
						}
					}.bind(this));
					
					// Fade in all pricing
					$$('#paymentBanner span.planPricing').each(function(el) {
						el.fade('hide');
						el.fade('in');
					});
					
					$('monthlyAmount').fade('hide');
					$('monthlyAmount').fade('in');
					$('currencyISO').fade('hide');
					$('currencyISO').fade('in');

					$$('#planTable span.planPricing').each(function(el) {
						el.fade('hide');
						el.fade('in');
					});
				}
			}.bind(this),
			
			'onFailure': function(xhr) {
				alert('Failed to request');
			}
		}).send();
	}
});
*/
