



/*AJAX-REPRESENTATIVES*/



/* * *
	REPRESENTANTIVES
* * */
var representatives = new Object();
Object.extend(representatives, {	
	/* GET */
	get: function(url, state){
		if(/(contato)/.test(document.location.href)){
			var href = 'pt/representantes/'+state
			if(/(p=)/.test(document.location.href))
				document.location.href = '?p='+href;
			else
				document.location.href = href;
		}else{
			this.wait(true);
			new Ajax.Request( 
				url, {
					method: 'get',
					onComplete: this.getReturn.bind(this),
					onFailure: this.onFailure.bind(this)
				}
			);
		}
	},
	getReturn: function(ret){
		$('representatives_ajax').innerHTML = email.decode(ret.responseText);
		this.addTalk(false);
		this.wait(false);
	},
	
	/* ADD TALK */
	addTalk: function(){
		$A(document.getElementsByClassName('iTalk')).each(function(value){
			Event.observe(value, 'click', representatives.actTalk.bindAsEventListener(representatives));
		});
	},
	
	/* ACT TALK */
	actTalk: function(e){
		Event.stop(e);
		var link = Event.element(e);
		var form = link.parentNode.getElementsByTagName('form')[0];
		
		Form.getElements(form).each(function(element){
			if(element.getAttribute('name') != 'id')
				new inputValue(element);								
		});
		IEHover.onTagName(false, 'button', form);
		
		Element.show(form);
		Element.hide(link);
	},
	
	/* WAIT */
	wait: function(status){
		if(status){
			Element.hide('representatives_ajax');
			Element.show('representatives_loading');
		}else{
			Element.hide('representatives_loading');
			Element.show('representatives_ajax');
		}
	},
	
	/* ON FAILURE */
	onFailure: function(transport){
		alert('Problemas com a aplicação.\n\nFalha na requisiçao AJAX.\n\nOcorrência: '+transport.status);
	}
});


/*-----------------------------------------------------------------------------------------------*/

ContentLoaded(window, function(){
	$('representatives_ajax').innerHTML = email.decode($('representatives_ajax').innerHTML);
	representatives.addTalk();
});
