/*
Name:		ANTAR Letter form - enhancement
Author:		Rburnie, Hugeobject, http://www.hugeobject.com.au

			REQUIRES: Mootools < 1.2 Beta or 1.2 beta with previous compatability
			
*/

var ANTAR_letter = new Class({
	initialize: function(letters) {
		this.letters = letters;
		this.glow_color = '#ffffcc';
		this.form = $('ANTAR_letter');
		this.campaign_id = $('campaign_id'); // hidden feild that stores the campaign id
		this.domain = document.domain;
		
		if(!$chk(this.form) || !$chk(this.campaign_id)) { return; }

		this.setup_politicians();
		this.setup_letter();
		
		// Make form visible and enabled
		var submit_but = $E('button[type=submit]',this.form);
		if($chk(submit_but)) {
			this.form.setStyle('display','block');
			submit_but.setProperty('disabled',false);
		}
	},
	
	setup_politicians: function() {
		this.location = $('location');
		this.country = $('country');
		this.postcode = $('postcode');
		
		this.postcode_error = new Element('span').appendText('Required').setStyles({
			'color':'#f00',
			'margin-left':5,
			'visibility':'visible'
		});
		this.postcode_error.inject(this.postcode,'after');
		
		if(!$chk(this.location) || !$chk(this.country) || !$chk(this.postcode)) { return; }
		
		// Create a fieldset for politicians, inject it after the location fieldset and hide it for now
		this.politicians = new Element('fieldset').setProperty('id','politicians');
		var legend = new Element('legend').appendText('Please choose the politician(s) you would like to send a letter to').inject(this.politicians);
		this.politicians_list = new Element('ol').inject(legend,'after');
		
		this.politicians.addEvent('hide',function() {
			this.setStyle('display','none');
		});
		
		this.politicians.addEvent('show',function() {
			this.setStyle('display','block');
		});
		
		if(this.postcode.getProperty('value').trim() == ''){
			this.politicians.fireEvent('hide');
		}
		
		this.politicians.inject(this.location,'after');
		
		this.get_politicians();
		
		// Behaviour: Country dropdown
		this.country.addEvent('change',function() {
			this.get_politicians();
		}.bind(this));
		
		// Behaviour: postcode
		//this.timer = null;
		//this.postcode.addEvent("keypress", function(){
		//	clearTimeout(this.timer);
		//	this.timer = setTimeout(function() {
		//		this.get_politicians();
		//	}.bind(this),500);
		//}.bind(this));
		
		
	},
	
	get_politicians: function() {
		var postcode = this.postcode.getProperty('value').trim();
		
		if(this.country.getProperty('value')!=11) {
			this.postcode.setProperty('value','');
			postcode = '';
			this.postcode_error.setStyle('visibility','hidden');
		}
		
		// url: http://[domain]/letters/ajax/[campaign_id]/get_politicians/[post_code] <-- NB: [postcode] = '' if non aus
		var url = 'http://' + this.domain + '/letters/ajax/' + this.campaign_id.value + '/get_politicians/' + postcode;
		
		//alert(url);//return;// REMOVE THIS RETURN LATER SO AJAX REQUEST RUNS
		
		var get_politicians = new Ajax(
			url,
			{
				method: 'get',
				update: this.politicians_list
			}
		).request().chain(function() {
			this.politicians.fireEvent('show');
			this.postcode_error.setStyle('visibility','hidden');
		}.bind(this));
	},
	
	setup_letter: function() {
		this.letter_field = $('the_letter');
		letter_radios = $ES('input[name=lettertype]',this.form);
		if(!$chk(this.letter_field) || letter_radios.length == 0) { return; }
		
		this.letter_field.setStyles({
			'overflow': 'auto',
			'height':400
		});
		
		letter_radios.each(function(r,i){
			var method = (!window.ie ? 'change' : 'click');
			r.addEvent(method,function(e){
				this.get_letter(r.getProperty('id'));
			}.bind(this));
		}.bind(this));
		
	},
	
	get_letter: function(id) {
		
		var bg_color = this.letter_field.getStyle('background-color');
		var glow = new Fx.Style(this.letter_field,'background-color',{duration: 500});

		this.letter_field.value = this.letters[id];
		glow.start(this.glow_color,bg_color);
		
	}
});

