var RegWizard = new Class({
	
	options: {
	
		waitMsg: "Attendere, stiamo creando il tuo sito web"
		
	},
	
	initialize: function(options){
		
		this.setOptions(this.options, options);
		
		this.sldDetail = new Fx.Slide('slider', {mode: 'horizontal'}); //.hide();
		this.sldThumbs = new Fx.Slide('thumbs'); //.hide();
		
		this.currTmpl = 0;
		this.bComplete = true;
		this.bCatComplete = true;
		this.validation = false;
		this.glbCk = new Array();
		this.imgLoadedCounter = 0;
		
		this.ajaxCatFunction = this.ajaxCatCall.bind(this);
	},
 
 	start: function(){
 	
 		this.sldDetail.slideIn();
 		this.sldThumbs.slideIn();
 		
 	},
 
 	selTemplate: function(selTmpl, bForce){
		
		$('template').value = $('thumb_' + selTmpl).alt;
		
		if(!$defined(bForce)) bForce = false;
		if(!bForce && selTmpl == this.currTmpl) return;
		if(!this.bComplete) return;
		
		this.bComplete = false;
		
 		$('thumb_' + this.currTmpl).className = 'cornice';
		$('thumb_' + selTmpl).className = 'corniceactive';
 		
 		var self = this;
		this.sldDetail.slideOut().chain(function(){
			
			$('detail_container_' + self.currTmpl).setStyle('display', 'none');
			$('detail_container_' + selTmpl).setStyle('display', '');

			self.sldDetail.slideIn().chain(function(){
				self.currTmpl = selTmpl;
				self.bComplete = true;
			});
		});
 	
 	},
 	
 	changeCat: function(){
		
		if(!this.bCatComplete) return;
		this.bCatComplete = false;
		
		this.sldThumbs.slideOut().chain(function(){
			
			$('thumbs').hide();
			$('loading').show();
			
		});
		
		this.sldDetail.slideOut().chain(function(){
		
			$('slider').hide();
			$('loading-detail').show();
		
		}).addEvent('onComplete', this.ajaxCatFunction);
	},
	
	ajaxCatCall: function(){
	
		var url = 'crea-sito-ajax.ctr?op=1&categoria=' + $('categoria').value;
		var a = new Ajax(url, {method: 'get', onComplete: this.getCatResponse.bind(this)}).request();
	
	},
	
	getCatResponse: function(responseTxt, responseXML){
		
		if(responseXML == null) return;
		
		var nodes = responseXML.documentElement.childNodes;
        if(nodes.length == 0) return;
		
		var item = null;
		var id = null;
		var header = null;
		var data = null;
		var curr = 0;
		var id_gabbia = null;
		var declinazione = null;
		
		for (var i = 0; i < nodes.length; i++){
		
 			item = nodes.item(i);
 			
 			if (item.nodeType != 1) continue;
 			
 			id = item.getAttribute('id');
 			header = item.getAttribute('header');
 			id_gabbia = item.getAttribute('id_gabbia');
 			declinazione = item.getAttribute('declinazione');
            data = item.firstChild.data;
            
            $('thumb_' + curr).src = data + $('thumb_' + curr).width;
            $('thumb_' + curr).alt = id + '_' + header + '_' + id_gabbia + '_' + declinazione;
            $('thumb_' + curr).onload = this.imgLoaded.bind(this);
            
            $('detail_' + curr).src = data + $('detail_' + curr).width;
            $('detail_' + curr).onload = this.imgLoaded.bind(this);
            
            curr++;
		}
	},
	
	imgLoaded: function(){
	
		this.imgLoadedCounter++;
		
		if(this.imgLoadedCounter == 8)
		{
			this.sldDetail.removeEvent('onComplete', this.ajaxCatFunction);
		
			$('loading').hide();
			$('thumbs').show();
			this.sldThumbs.slideIn();
			
			$('loading-detail').hide();
			$('slider').show();
			this.selTemplate(0, true);
			
			this.bCatComplete = true;
			
			this.imgLoadedCounter = 0;
		}
	},
	
	
	checkTitle: function(){
	
		
		$('titolo').value = $('titolo').value.replace(/[<>]/g, "");
	},
	
	checkUrl: function(){
	
		var errLabel = val.validateField('url', true, val.options.regex.DOMAIN, 3);
		if(errLabel != 'correct'){
			if(errLabel == 'wrong')
				$('url').value = $('url').value.replace(/[^a-zA-Z0-9]/g, "").toLowerCase();
			else{
				val.setError('url', errLabel);
				return false;
			}
		}
		
		return this.checkFldAjax('url', $('url').value);
	},
	
	checkUsr: function(){
	
		var errLabel = val.validateField('username', true, val.options.regex.USERNAME, 4);
		if(errLabel != 'correct'){
			val.setError('username', errLabel);
			return false;
		}
		
		return this.checkFldAjax('username', $('username').value);
	},
	
	checkEmail: function(){
	
		var errLabel = val.validateField('email', true, val.options.regex.EMAIL, 6);
		if(errLabel != 'correct'){
			val.setError('email', errLabel);
			return false;
		}
		
		//return this.checkFldAjax('email', $('email').value);
		
		this.glbCk['email'] = true;
		val.setValid('email');
		return true;
		
	},
	
	checkConfirmEmail: function(){
	
		if($('email').value != $('confirm_email').value){
			val.setError('confirm_email', 'no_match');
			return false;
		}
		
		val.setValid('confirm_email');
		return true;
		
	},
	

	checkNome: function(){
	
		var errLabel = val.validateField('nome', true, val.options.regex.NOME, 2);
		if(errLabel != 'correct'){
			val.setError('nome', errLabel);
			return false;
		}
		
		//return this.checkFldAjax('email', $('email').value);
		
		this.glbCk['nome'] = true;
		val.setValid('nome');
		return true;
		
	},

	checkCognome: function(){
	
		var errLabel = val.validateField('cognome', true, val.options.regex.NOME, 2);
		if(errLabel != 'correct'){
			val.setError('cognome', errLabel);
			return false;
		}
		
		//return this.checkFldAjax('email', $('email').value);
		
		this.glbCk['cognome'] = true;
		val.setValid('cognome');
		return true;
		
	},
		
	checkFldAjax: function(fld, val){
		
		var data = {op: 2, fld: fld, val: val};
		var self = this;
		
		var url = 'crea-sito-ajax.ctr?' + Object.toQueryString(data);
		var ajax = new Ajax(url, {method: 'get'});
	    ajax.addEvent('onComplete', function(text, xml){ self.getCkFldResponse(text, fld, self); });
	    ajax.request();
		
		return true;
	},
	
	getCkFldResponse: function(responseTxt, fld, prnt){
	
		if(responseTxt != 'correct'){
		
			val.setError(fld, responseTxt);
			prnt.glbCk[fld] = false;
			return false;
		}
		
		val.setValid(fld);
		prnt.glbCk[fld] = true;
		return true;
	},
	
	submitForm: function(){
	
		var ck = true;
		
		// url
		//this.glbCk['url'] = true;
		ck = !this.checkUrl() ? false : ck;
		
		// titolo
		ck = val.validateFieldAndSetError('titolo', true) != true ? false : ck;
		
		// descrizione
		ck = val.validateFieldAndSetError('descrizione', true) != true ? false : ck;
		
		// username
		//this.glbCk['usr'] = true;
		ck = !this.checkUsr() ? false : ck;
		//ck = val.validateFieldAndSetError('username', true, val.options.regex.USERNAME, 4) != true ? false : ck;
		
		// password
		ck = val.validateFieldAndSetError('password', true, val.options.regex.PASSWORD, 4) != true ? false : ck;
		
		// email
		//this.glbCk['email'] = true;
		ck = !this.checkEmail() ? false : ck;
		//ck = val.validateFieldAndSetError('email', true, val.options.regex.EMAIL, 6) != true ? false : ck;
		
		// confirm_email
		ck = !this.checkConfirmEmail() ? false : ck;

		// nome
		ck = !this.checkNome() ? false : ck;
		//ck = val.validateFieldAndSetError('nome', true) != true ? false : ck;
		
		// cognome
		ck = !this.checkCognome() ? false : ck;
		//ck = val.validateFieldAndSetError('cognome', true) != true ? false : ck;
		
		// dati personali
		if(!$('dati_personali_1').checked){
			ck = false;
			val.setError('dati_personali', 'required');
		} else val.setValid('dati_personali');
		
		// condizioni di servizio
		if(!$('condizioni_servizio_1').checked){
			ck = false;
			val.setError('condizioni_servizio', 'required');
		} else val.setValid('condizioni_servizio');
		
		//alert (ck  + ' - ' + this.glbCk['url'] + ' - ' + this.glbCk['username'] + ' - ' + this.glbCk['email']);
		if(ck && this.glbCk['url'] && this.glbCk['username'] && this.glbCk['email']){
			var self = this;
			this.box = new Box({onComplete: function(){self.ajaxRegCall();}});
			this.box.open(this.options.waitMsg, true);
		} else{
			$('glb_error').show();
		}
	},
	
	ajaxRegCall: function(){
	
		$('wizard').send().addEvent('onComplete', this.siteCreated.bind(this));
	},
	
	siteCreated: function(responseTxt){
	
		if($type(parseInt(responseTxt)) == 'number') window.location = '/www/registrato.ctr?first=1&id_sito=' + responseTxt;
		else this.box.loadContent(responseTxt);
	}/*,
	
	publishSite: function(){
	
		this.box.loadContent();
		var url = 'crea-sito-ajax.ctr?op=4';
		var a = new Ajax(url, {method: 'get', onComplete: this.getPubResponse.bind(this)}).request();
	},
	
	getPubResponse: function(responseTxt, responseXML){
	
		this.box.loadContent(responseTxt);
		
	}*/

});
RegWizard.implement(new Options);
RegWizard.implement(new Events);