document.observe('dom:loaded', function() {
	Cufon.replace('.cufon');
	
// 	var e = $('form');
// 	if (e) {
// 		form.initialize(e);
// 	}
	
	e = $('languages');
	if (e) {
		dropdown.initialize(e);
	}
	
// 	e = $('calculator');
// 	if (e) {
// 		calculator.initialize(e);
// 	}
	
	if (!Prototype.Browser.WebKit) {
		placeholders.initialize();
	}
	
	e = $('chart');
	if (e) {
		chart.initialize(e);
	}
});

var dropdown = {
	initialize: function(dropdown_) {
		this.element = dropdown_;
		this.uls = this.element.select('ul');
		this.element.observe('mouseover', function() {dropdown.uls.invoke('toggle')});
		this.element.observe('mouseout', function() {dropdown.uls.invoke('toggle')});
	}
}

var form = {
	initialize: function(form_) {
		this.element = form_;
		this.verification_url = 'http://www.google.com/';
		this.vouchers = this.element.down('ul');
		this.inputs = this.vouchers.select('input');
		this.add_link = this.element.down('.add_voucher');
		
		this.submit_button = this.element.down('.submit');
		this.submit_button.observe('click', this.submit.bind(this));
		this.back_button = this.element.down('.back');
		this.back_button.observe('click', this.back.bind(this));
		
		this.setup = this.element.down('.step_one');
		this.result = this.element.down('.result');
		this.result_sent = this.element.down('.sent');
		this.result_recived = this.element.down('.recived');
		
		this.add_link.observe('click', this.add_voucher.bind(this));
		this.inputs.invoke('observe', 'keyup', this.on_voucher_progress.bind(this));
	},
	add_voucher: function(event) {
		if (event) {
			Event.stop(event);
		}
		
		if (this.vouchers.select('li').length < 5) {
			var source = this.vouchers.down('li'),
				destination = new Element('li'),
				inputs;
		
			destination.innerHTML = source.innerHTML;
			inputs = destination.select('input');
			destination.down('.currency').innerHTML = '<span></span>';
			this.vouchers.insert(destination);
		
			inputs.each(function(input) {
				input.writeAttribute({value: ''});
				input.observe('keyup', this.on_voucher_progress.bind(this));
				placeholders.add(input);
			}, this);
		}
	},
	on_voucher_progress: function(event) {
		// Voucher verification start responder, good place for AJAX request
		
		var li = Event.findElement(event, 'li'), request,
			params = {
				voucher_id: li.down('.voucher_id').value,
				value: li.down('.value').value
			};
		
		if (params.voucher_id && params.value) {
			li.removeClassName('ok');
			li.addClassName('progress');
			li.down('span').innerHTML = '';
			this.request(event, params);
		}
	},
	on_voucher_ok: function(event, currency) {
		// Voucher verification successful responder
		// currency String 'USD (dollars)'
		
		var html, li = Event.findElement(event, 'li');
		li.removeClassName('progress');
		li.addClassName('ok');
		html = currency.gsub(/(.*)\s(.*)/, function(match) {return '<strong>' + match[1] + '</strong> ' + match[2]});
		li.down('span').innerHTML = html;
		li.down('capcha').hide();
		
		this.update_log(currency);
	},
	on_voucher_failed: function(event) {
		// Voucher verification failed responder
		
		var li = Event.findElement(event, 'li');
		li.removeClassName('ok');
		li.removeClassName('progress');
		li.addClassName('failed');
		li.down('span').innerHTML = '';
	},
	request: function(event, params) {
		// OK Responce {status: 'ok', currency: 'USD (dollars)'}
		
		var request, url = this.verification_url + '?' + $H(params).toQueryString();
		
		request = new Ajax.Request(url, {
			onSuccess: function(transport) {
				var responce = transport.headerJSON;
				if (responce.status == 'ok') {
					form.on_voucher_ok(event, responce.currency);
				} else {
					form.on_voucher_failed(event);
				}
			},
			onFailure: function() {
				form.on_voucher_failed(event);
			}
		});
	},
	update_log: function(currency) {
		var vouchers = 0, amount = 0, html = [];
		
		this.vouchers.each(function(voucher) {
			if (voucher.hasClassName('ok')) {
				vouchers++;
				amount += parseInt(voucher.down('.value').value, 10);
			}
		});
		
		html.push(items(vouchers, ['Введен', 'Введено', 'Введено']));
		html.push(vouchers + ' ' + items(vouchers, ['ваучер', 'ваучера', 'ваучеров']));
		html.push('на сумму');
		html.push(amount + ' ' + currency);
		
		li.down('.notice span').innerHTML = html.join(' ');
	},
	all_valid: function() {
		var v = this.vouchers.select('li'),
			valid = this.vouchers.select('.ok');
		return v == valid && this.element.down('.recive input').value != 0;
	},
	submit: function(e) {
		if (this.all_valid()) {
			this.setup.hide();
			this.result.show();

			var vouchers = 0, amount = 0;
			this.vouchers.select('li').each(function(voucher) {
				if (voucher.hasClassName('ok')) {
					vouchers++;
					this.result_sent.insert('<br />' + voucher.down('.voucher_id').value);
					amount += parseInt(voucher.down('.value').value, 10);
				}
			}, this);
			this.result_sent.down('strong').insert({top: amount + ' '});
			this.result_recived.insert('<br />' + this.element.down('.recive input').value);
		}
	},
	back: function(e) {
		this.setup.show();
		this.result.hide();
		
		var lis = this.vouchers.select('li');
		if (lis.length == 5) {
			lis.last().remove();
			lis.pop();
		}
		this.add_voucher();
		lis.invoke('remove');
	}
}

var placeholders = {
	initialize: function() {
		this.elements = $$('input[placeholder]');
		this.elements.each(function(element) {
			this.add(element);
		}, this);
	},
	add: function(element) {
		element.addClassName('placeholder');
		if(element.value.length==0) element.value = element.readAttribute('placeholder');
		else element.removeClassName('placeholder');
		element.observe('mousedown', this.hide.bind(this));
		element.observe('focus', this.hide.bind(this));
		element.observe('blur', this.show.bind(this));
	},
	hide: function(e) {
		var element = Event.element(e);
		if (element.value == element.readAttribute('placeholder')) {
			element.removeClassName('placeholder');
			element.value = '';
		}
		element.focus();
	},
	show: function(e) {
		var element = Event.element(e);
		if (element.value == ''||element.value == element.readAttribute('placeholder')) {
			element.addClassName('placeholder');
			element.value = element.readAttribute('placeholder');
		}
	}
}

var calculator = {
	initialize: function(calculator_) {
		this.element = calculator_;
		this.results = this.element.select('li');
		this.amount = this.element.down('input');
		this.currency = this.element.down('select');
		this.amount.observe('keyup', this.update.bind(this));
		this.currency.observe('change', this.update.bind(this));
		this.update();
	},
	update: function() {
		var rub = exchange_rates[this.currency.value], v = parseFloat(this.amount.value);
		
		if (!v || v < 0) {
			v = 0;
		}
		
		this.results.each(function(result) {
			result.down('span').innerHTML = Math.round(exchange_rates[result.className] * rub * v);
		}, this);
	}
}

function items(amount, variants) {
	var variant = (amount%10==1 && amount%100!=11 ? 1 : amount%10>=2 && amount%10<=4 && (amount%100<10 || amount%100>=20) ? 2 : 3);
	return variants[variant-1];
}
