var Tabs = new Class({

	//Definition des variables propres à l'objet
	vars: {
		tab: [],
		tabs: [],
		tabsBtns: [],
		listTabs: [],
		tabsWithErrors: [],
		elementToHide: [],
		display: [],
		exit: []
	},

	/**
	 * Function initialize
	 * 
	 * @description : 
	 * @param :  tab $tab 
	 * @access : public
	 * @return : void
	 * @date : 2010-03-25
	 * @author : François Guémard <f.guemard@hegyd.com>
	 */
	initialize: function(myTab){
		//Initialisation des variables de class
		this.vars.tab           = myTab;
		this.vars.tabs          = this.vars.tab.getElements('li');
		this.vars.tabsBtns      = this.vars.tab.getElements('a');
		this.vars.listTabs      = this.vars.tabs.getProperty('id');
		this.vars.elementToHide = $$('.hidden');
		this.vars.display       = new Array();

		//Initialisation de la class par l'appel de la méthode initTabs
		this.initTabs(this.vars.tab);
	},

	/**
	 * Function initTabs 
	 * 
	 * @description : 
	 * @param :  tab $tab 
	 * @access : public
	 * @return : void
	 * @date : 2010-03-25
	 * @author : François Guémard <f.guemard@hegyd.com>
	 */
	initTabs: function(tab){

		var objectTab = this;

		/*Masquage de tous les onglets et repérages des erreurs*/
	    for (i = 0;i<objectTab.vars.listTabs.length;i++) {
	    	var elements = $$('.'+objectTab.vars.listTabs[i]);

	        elements.setStyle('display','none');
	
	        for (j = 0;j<elements.length;j++) {
	        	var element = elements[j];

	            if (element.hasClass('plus')){
	            	objectTab.vars.tabsWithErrors[objectTab.vars.listTabs[i]] = true;
	            }
	        }
	    }
	    /**/
	
		/*Placement du flag open sur l'onglet avec des erreurs*/
		for (i = 0;i<objectTab.vars.listTabs.length;i++) {
			var element = objectTab.vars.listTabs[i];

			if (objectTab.vars.tabsWithErrors[element] == true) {
				var state = $(element).getProperty('class');
	
	            $$('.activ').removeClass('activ');
	            $(element).addClass('activ');
	
	            break;
			}
		}
	    /**/
	
		/*Affichage de l'onglet avec le flag 'open' et masquage des autres*/
		for (i = 0;i<objectTab.vars.tabs.length;i++) {
	        var tab           = objectTab.vars.tabs[i];
	    	var selector      = tab.getProperty('id');
			var elements      = $$('.'+selector);
			var styleDisplay  = 'none';
	
	        if (tab.hasClass('activ')) styleDisplay = '';
	
	      	for (j = 0;j<elements.length;j++) {
	    	    var element = elements[j];
	        	element.setStyle('display',styleDisplay);
	        }
	
	    }
	    /**/
	
		this.hideElements(objectTab.vars.elementToHide);
			
		//Comportement au clic sur l'onglet
		objectTab.vars.tabsBtns.addEvent('click', function(e){
			if(e != undefined) new Event(e).stop();
			
			var tabOpen  = objectTab.vars.tab.getChildren('.activ');
			var parentLi = this.getParent('li');

			if (tabOpen.getProperty('rel') == 'tab1') {
				objectTab.inscriptionClient();
			}

			if (!parentLi.hasClass('open') && objectTab.vars.exit != true) {
				objectTab.showTab(this.getProperty('rel'));
			}

		});
	
		//Comportement au clic sur le bouton suivant
		var btns = $('btn_next').getElements('span');
		
		btns.addEvent('click', function(e){
			var btn  = this;
			var btnClass = btn.getProperty('class');

			if (btnClass == 'tab1') objectTab.inscriptionClient();

			for (i = 0;i<objectTab.vars.listTabs.length;i++) {
				var tabTmp = objectTab.vars.listTabs[i];
					
				if (tabTmp>btnClass && objectTab.vars.exit != true) {
					objectTab.showTab(tabTmp);
					break;
				}
			}
		});
	
		//Comportement au clic sur le bouton precedent
		var btns = $('btn_prev').getElements('span');
		
		btns.addEvent('click', function(e){
			var btn      = this;
			var btnClass = btn.getProperty('class');
	
			for (i = objectTab.vars.listTabs.length;i>=0;i--) {
				var tabTmp = objectTab.vars.listTabs[i];

				if (tabTmp<btnClass) {
					objectTab.showTab(tabTmp);
					break;
				}
			}
		});
	
		//Comportement au clic sur un fieldset enroulable / deroulable
		$$('.hidden').addEvent('click', function(e){
	
			if(e != undefined) {
	
				new Event(e).stop();
		
			}
			
			var idParent = this.getProperty('id');
	
			if (objectTab.vars.display[idParent] == null || objectTab.vars.display[idParent] == 'none') objectTab.vars.display[idParent] = '';
			else objectTab.vars.display[idParent] = 'none';
	
			$$('.'+idParent).setStyle('display',objectTab.vars.display[idParent]);

			if (objectTab.vars.display[idParent] == ''){
				this.addClass('show');
			} else {
				this.removeClass('show');
			}
	
		});
	
	},

	/**
	 * Function hideElements 
	 * 
	 * @description : 
	 * @param :  elements $elements 
	 * @access : public
	 * @return : void
	 * @date : 2010-03-25
	 * @author : François Guémard <f.guemard@hegyd.com>
	 */
	hideElements: function(elements) {
	
		var objectTab = this;

		for(i = 0;i<elements.length;i++) {
			var element = elements[i];
			var id      = element.getProperty('id');
			var childs  = $$('.'+id);

			element.setStyle('cursor','pointer');
			if (element.hasClass('show')) element.removeClass('show');
			objectTab.vars.display[id] = 'none';

			for (j = 0;j<childs.length;j++) {
				var child = childs[j];
				if (!child.hasClass('plus')) child.setStyle('display','none');
	        }
		}
	            
	},

	/**
	 * Function showTab 
	 * 
	 * @description : 
	 * @param :  tabName $tabName 
	 * @access : public
	 * @return : void
	 * @date : 2010-03-25
	 * @author : François Guémard <f.guemard@hegyd.com>
	 */
	showTab: function(tabToOpenId) {
		var objectTab	  = this;
		var tabOpen       = objectTab.vars.tab.getChildren('.activ');
		var tabOpenId     = tabOpen.getProperty('id');
		var tabToOpen     = objectTab.vars.tab.getChildren('#'+tabToOpenId);
		var elementToHide = $$('.hidden');
	
		tabOpen.removeClass('activ');
		$$('.'+tabOpenId).setStyle('display','none');
	
		tabToOpen.addClass('activ');
		$$('.'+tabToOpenId).setStyle('display','');
	
		//Masquage des champs facultatifs
		this.hideElements(objectTab.vars.elementToHide);
	
		this.showInputErrors($$('.'+tabToOpenId));
	
	}, 

	/**
	 * Function showInputErrors 
	 * 
	 * @description : 
	 * @param :  inputs $inputs 
	 * @access : public
	 * @return : void
	 * @date : 2010-03-25
	 * @author : François Guémard <f.guemard@hegyd.com>
	 */
	showInputErrors: function(inputs) {
		for (i = 0;i<inputs.length;i++) {
		        var input = inputs[i];
			if (input.hasClass('plus')) {
				input.setStyle('display','');
			}
		}
	},

	/**
	 * Function inscriptionClient 
	 * 
	 * @description : 
	 * @access : public
	 * @return : void
	 * @date : 2010-03-26
	 * @author : François Guémard <f.guemard@hegyd.com>
	 */
	inscriptionClient: function() {
		var objectTab = this;
		var client_id = $$('input[name=\'client[id]\']').getProperty('value');

		if (client_id == null || client_id == 0) {
			var inputName      = $$('input[name=\'announcement[name]\']');
			var inputFirstname = $$('input[name=\'announcement[firstname]\']');
			var inputPhone     = $$('input[name=\'announcement[phone]\']');
			var inputEmail     = $$('input[name=\'announcement[email]\']');
			var inputCompany   = $$('input[name=\'announcement[company]\']');

			//Cas d'une annonce d'acquisition
			if (inputName == '') {
				var inputName      = $$('input[name=\'acquisition[name]\']');
				var inputFirstname = $$('input[name=\'acquisition[firstname]\']');
				var inputPhone     = $$('input[name=\'acquisition[phone]\']');
				var inputEmail     = $$('input[name=\'acquisition[email]\']');
				var inputCompany   = $$('input[name=\'acquisition[company]\']');
			}

			var name           = inputName.get('value');
			var firstname      = inputFirstname.get('value');
			var phone          = inputPhone.get('value');
			var email          = inputEmail.get('value');
			var company        = inputCompany.get('value');

			if (name != '' && firstname != '' && email != '' && phone != '') {
				var jsonRequest = new Request.JSON({
					url: 'ajax-inscription.php',
					method: 'POST',
					async: false,
					onRequest: objectTab.showLoader(),
					onSuccess: function(responseJSON) {
						if (responseJSON.exit == true){
							objectTab.vars.exit = true;
							$('important').set('text',responseJSON.error);
							inputEmail.getParent('td').grab($('important'));
							$('important').setStyle('display','');
						} else {
							$('important').setStyle('display','none');
							$('client_id').setProperty('value',responseJSON.client_id);
							$$('.client_error').setStyle('display','none');
							$$('.client_error').removeClass('tab1');
							$('formClient').setStyle('display','none');
							objectTab.vars.exit = false;
						}
					},
					onComplete: objectTab.hideLoader()
				});
				jsonRequest.send('name='+name+'&firstname='+firstname+'&phone='+phone+'&email='+email+'&company='+company);
			}
		} else {
			objectTab.vars.exit = false;
		}
	},

	/**
	 * Function showLoader 
	 * 
	 * @description : 
	 * @access : public
	 * @return : void
	 * @date : 2010-03-29
	 * @author : François Guémard <f.guemard@hegyd.com>
	 */
	showLoader: function() {
		$('loader').setStyle('display','');
	},

	/**
	 * Function hideLoader 
	 * 
	 * @description : 
	 * @access : public
	 * @return : void
	 * @date : 2010-03-29
	 * @author : François Guémard <f.guemard@hegyd.com>
	 */
	hideLoader: function() {
		$('loader').setStyle('display','none');
	}
		
});


