var PackageList = {

	currency: null,

	details: {
		product_notes: {
			'cast': '',
			'castwin': '<br /><strong>IMPORTANT: CENTOVA CAST ON WINDOWS IS NOT A STANDALONE SOLUTION.</strong>  Windows support is an add-on for the Linux-based Centova Cast product. You must have an existing Centova Cast/Linux installation before purchasing the Windows service.  If you require further clarification please contact the helpdesk prior to ordering.'
		},
		capacity_suitability: {
			'1': 'Individual broadcasters with one radio station',
			'2': 'Evaluation purposes',
			'10': 'Stream hosting providers or multiple-station broadcasters',
			'25': 'Stream hosting providers',
			'0': 'Stream hosting providers'
		},
		capacity_streams: {
			'1': '1 station',
			'2': '2 stations',
			'10': '10 stations',
			'25': '25 stations',
			'0': 'Unlimited stations'
		},
		term_maintenance: {
			'trial': 'Included',
			'monthly': 'Included',
			'annually': 'Included',
			'one-time': '12 Months'
		},
		term_titles: {
			'trial': 'Free',
			'monthly': 'Monthly',
			'annually': 'Annually',
			'one-time': 'One-time payment'
		},
		install_service: {
			'0': 'None',
			'1': 'Yes'
		}
	},

	update_pricing: function() {
		var product = $('input[name=product]:checked').val(),
			capacity = $('input[name=streams]:checked').val(),
			term = $('input[name=term]:checked').val(),
			install = $('input[name=install]:checked').val();

		if (term=='trial') {
			$('#trialstreams').show();
			$('#nontrialstreams').hide();
			if (capacity!='2') {
				$('input[name=streams]:checked').attr('checked','');
				$('#streams2').attr('checked','chceked');
				capacity = 2;
			}
		} else {
			$('#nontrialstreams').show();
			$('#trialstreams').hide();
			if (capacity=='2') {
				$('#streams2').attr('checked','');
				$('#streams10').attr('checked','chceked');
				capacity = 10;
			}
		}

		$('#pkg_suitability').html( this.details.capacity_suitability[ capacity ] );
		$('#pkg_streams').html( this.details.capacity_streams[ capacity ] );
		$('#pkg_support').html( this.details.term_maintenance[ term ] );
		$('#pkg_updates').html( this.details.term_maintenance[ term ] );
		$('#pkg_term').html( this.details.term_titles[ term ]);
		$('#pkg_install').html( this.details.install_service[ install ] );

		$('#notes').html(  this.details.product_notes[product] );

		if (product=='castwin') {
			if (capacity=='1') $('#streams10').attr('checked','checked');
			$('#individual').hide();
		} else {
			$('#individual').show();
		}
		var lictype = '';
		if (capacity=='1') {
			lictype = 'individual';
			$('#onetimepayment').hide();
			if (term=='one-time') {
				term = 'annually';
				$('#termannually').attr('checked','checked');		
			}
		} else if (term=='one-time') {
			$('#onetimepayment').show();
			lictype = 'owned';
		} else {
			$('#onetimepayment').show();
			lictype = 'leased';
		}

		if (term!='trial') {
			var productid = window.packages[product][lictype][capacity][term][0];
			var productprice = window.packages[product][lictype][capacity][term][1];
		} else {
			productid = 0;
			productprice = 0;
		}

		var onetimeprice = 0, recurringprice = 0;

		if (install=='1') {
			onetimeprice += window.packages[product].addons.install['one-time'][1]
		}

		var totalstr = '', intltotalstr = '';

		if (term=='one-time') {
			onetimeprice += productprice;

			totalstr = 'USD $'+this.format_currency(onetimeprice)+' one-time payment';
			intltotalstr = this.get_localized_currency(onetimeprice)+' one-time payment';
		} else {
			recurringprice += productprice;
			if (recurringprice==0) {
				totalstr = intltotalstr = 'Free';
			} else {
				totalstr = 'USD $'+this.format_currency(recurringprice)+' '+this.details.term_titles[term].toLowerCase();
				intltotalstr = this.get_localized_currency(recurringprice)+' '+this.details.term_titles[term].toLowerCase();
			}

			if (onetimeprice) {
				totalstr += ' plus $'+this.format_currency(onetimeprice)+' one-time payment';
				intltotalstr += ' plus '+this.get_localized_currency(onetimeprice)+' one-time payment';
			}
		}

		$('#pkg_total').html(totalstr);
		if (this.currency!='USD') {
			$('#intlprice').css({ fontSize: '10px' }).html('<br/>(approx. <strong>'+intltotalstr+'</strong>)');
		}
	},

	radio_changed: function() {
		PackageList.update_pricing();
	},

	format_currency: function(amount) {
		var i = parseFloat(amount);
		if(isNaN(i)) { i = 0.00; }
		var minus = '';
		if(i < 0) { minus = '-'; }
		i = Math.abs(i);
		i = parseInt((i + .005) * 100);
		i = i / 100;
		s = new String(i);
		if(s.indexOf('.') < 0) { s += '.00'; }
		if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
		s = minus + s;

		s = s.replace(/\.00$/,'');
		return s;
	},

	get_localized_currency: function(amount) {
		if (!window.exchange_rates || !this.currency || this.currency=='USD') return '';

		var rate = window.exchange_rates[this.currency];
		if (!rate) return '';

		return this.currency+' '+this.format_currency(amount * rate);
	},

	get_preferred_currency: function() {
		if (!document.cookie) return 'USD';

		var cookies = document.cookie.split(';');
		for (var i=0; i<cookies.length; i++) {
			var cookiedata = cookies[i].split('=');
			if (cookiedata[0]=='currency') {
				var currency = cookiedata[1];
				if (currency && currency.match && currency.match(/^[A-Z]{3}/)) {
					return currency.substr(0,3);
				}
			}
		}

		return 'USD';
	},

	initialize: function() {
		$('#nojs').hide();
		$('input[type=radio]').change(this.radio_changed);

		this.currency = this.get_preferred_currency();

		$('#havejs').show();
		setTimeout(this.radio_changed,1);

	}
}

$(function(){
	PackageList.initialize();
})

