var updateTaxes = {

	selectors : {
		'select' : 'select.updateTaxes',
		'countrySelect' : 'select.updateTaxesCountry',
		'price' : 'span.totalMoney',
		'taxes' : 'span.taxAdded',
		'notice' : 'span.taxNotice',
		'taxFormField' : 'input#tax_rate',
		'radioAmount' : 'input[type=radio][name=amount]',
		'paypalTitle' : 'input[type=hidden][name=item_name]'
	},
	rates : {},
	states : {},
	$j : {},
	hasMultiplePrices : false, //set to true when there are more than one pricing option
	lang : 'en',
	text : {
		no_tax : {
			en : 'no applicable taxes',
			fr : 'aucunes taxes applicables'
		},
		inc_tax : {
			en : 'including %d% %s',
			fr : 'incluant la %s de %d%'
		}
	},

	init : function() {
		for(i in this.selectors) {
			this.$j[i] = $(this.selectors[i]);
			this.$j[i].each(function(){
				var $currentElement = $(this);
				if(i == 'taxFormField')
					$currentElement.data('original',$currentElement.val());
				else
					$currentElement.data('original',$currentElement.html());
			});
		}
		if(this.$j.radioAmount.size() > 0)
			this.hasMultiplePrices = true;
			
		this.currentCountry = 'CA';
		this.bind();
	},

	setLanguage : function(lang) {
		this.lang = lang;
	},

	setRates : function(rates) {
		this.rates = rates;
	},

	setStates : function(states) {
		this.states = states;
	},

	update : function(province) {
		if(this.currentCountry == 'CA' && typeof(this.rates[province]) != 'undefined') {
			var current = this.rates[province];
			this.updateTaxRate(current.percent, current.type);
		} else if(this.currentCountry == 'CA') {
			this.reset();
		}
	},

	numberFormat : function(number, decimalPlaces) {
		var num = number.toFixed(decimalPlaces);
		if(this.lang == 'fr')
			num = num.replace('.',',');
		return num;
	},

	updateTaxRate : function(percent, type) {
		var obj = this;
		this.$j.price.each(function(i){
			var newPrice = (obj.$j.price.eq(i).data('original').replace(',','.') * (1 + percent * 0.01));
			newPrice = obj.numberFormat(newPrice,2);
			obj.$j.price.eq(i).text(newPrice);
			if(type != false)
				obj.$j.taxes.eq(i).text(obj.text.inc_tax[obj.lang].replace('%d', percent).replace('%s', type));
			else
				obj.$j.taxes.eq(i).text(obj.text.no_tax[obj.lang]);
		});
		this.$j.taxFormField.val(percent);
		this.$j.notice.html('&nbsp;');
	},

	updateCountry : function(country) {
		this.currentCountry = country;
		if(country != 'CA') {
			this.$j.select.html(this.jsonToOption(this.states[country]));
			this.updateTaxRate(0,false);
		} else {
			this.reset();
		}
	},

	reset : function() {
		for(i in this.$j) {
			this.$j[i].each(function(){
				var $currentElement = $(this);
				if(i == 'taxFormField')
					$currentElement.val($currentElement.data('original'));
				else
					$currentElement.html($currentElement.data('original'));
			});
		}
	},

	jsonToOption : function(json) {
		var html = '<option value=""></option>';
		for(i in json) {
			html += '<option value="' + i + '">' + json[i]['name'] + '</option>';
		}
		return html;
	},

	updatePrice : function(newPrice) {
		var obj = this;
		this.$j.price.each(function(i){
			newPrice = obj.numberFormat(parseInt(newPrice),2);
			obj.$j.price.eq(i).data('original',newPrice);
			obj.$j.price.eq(i).text(newPrice);
		});
		var province = this.$j.select.val();
		if(this.currentCountry == 'CA' && typeof(this.rates[province]) != 'undefined') {
			this.updateTaxRate(this.rates[province].percent, this.rates[province].type);
		}

	},

	updatePaypalTitle : function(title) {
		var newTitle = this.$j.paypalTitle.val().replace(/\[.*?\]$/i,'[' + title + ']');
		this.$j.paypalTitle.val(newTitle);
	},

	bind : function() {
		var obj = this;
		this.$j.select.change(function(){
			obj.update($(this).val());
		});
		this.$j.countrySelect.change(function(){
			obj.updateCountry($(this).val());
		});
		this.$j.radioAmount.change(function(){
			obj.updatePrice($(this).val());
			//obj.updatePaypalTitle($(this).next('label').find('span.title').text());
			obj.updatePaypalTitle($('label[for=' + $(this).attr('id') + ']').find('span.title').text());
		});
	}

}

$(document).ready(function(){
	updateTaxes.init();
});

