/*
---

name: JC

description: Contains the custom event domready.

license: MIT-style license.

requires: [Core/DomReady, Core/Request, Core/JSON]

provides: JC

...
*/


(function(global, $, undef){

global.addEvent('domready', function(){

	var domainCheck = $('domaincheck');
	if (domainCheck){

		var spinner = new Element('img', {
			src: BASEPATH + '/images/spinner.gif',
			styles: {opacity: 0},
			tween: {link: 'chain'}
		}).inject(domainCheck);

		var result = new Element('div.result', {
			events: {click: function(){
				this.fade('out').get('tween').chain(function(){
					result.empty();
				});
			}},
			styles: {opacity: 0},
			tween: {link: 'chain'}
		}).inject(domainCheck, 'bottom');


		var domainName = domainCheck.getElement('input[name=domain]'),
			domainTLD = domainCheck.getElement('select[name=ext]');

		var request = new Request.JSON({
			url: BASEPATH + '/libs/whois.php',
			method: 'get',
			onRequest: function(){
				spinner.fade('in');
			},
			onSuccess: function(data, txt){
				result.set('html', (data.availability && data.availability != 'taken' ? DOMAIN_AVAILABLE_TEXT : DOMAIN_NOT_AVAILABLE_TEXT)(data.domain));
			},
			onFailure: function(){
				result.set('html', DOMAIN_NOT_AVAILABLE_TEXT());
			},
			onComplete: function(){
				spinner.fade('out');
				result.fade('in');
			}
		});

		domainCheck.addEvent('submit', function(event){
			event.stop();

			result.set('text', '');
			result.fade('out');

			var domainNameValue = domainName.get('value');
			if (domainNameValue){
				request.send({data: {q: domainNameValue + '.' + domainTLD.get('value')}});
			} else {
				result.set('text', DOMAIN_NOT_AVAILABLE_TEXT()).fade('in');
			}
		});

	}

	$$('form:not(#domaincheck)').addEvent('submit', function(event){
		event.stop();

		this.adopt(new Element('input', {
			type: 'hidden',
			name: 'spm_key',
			value: SPM_KEY
		})).submit();

	});

	var authKey = $('authKeyRow');
	if (authKey){
		$('moveDomain').addEvent('change', function(){
			authKey.setStyle('display', this.checked ? 'block' : 'none');
		});
		authKey.setStyle('display', 'none');
	}

	$$('a.popUp').addEvent('click', function(event){
		event.stop();
		popUp(this.href, this.get('text'));
	});


});


global.popUp = function(url, title){
	new MooDialog.IFrame(url, {
		'class': 'MooDialogPopUp',
		title: title
	});
};


})(this, document.id)


