

/*
 * Scripts
 *
 */
var Engine = {

	// calculator
	calculator : {
		init : function(){
			this.pages 		= 1;
			this.done		= false;
			
			this.base = [];
			this.subs = [];

			this.prices();
			this.create();
			this.shortcuts();
			this.triggers();
			
			this.containerCurrency.eq(0).trigger('click');
		},
		// trigger the calculations and populate fields
		evaluate : function(){
			this.calculate();
			
			this.containerPages.text(this.pages);
			this.containerTotal.html(this.totalHTML);
			//this.containerOrder.attr('href',this.containerCheckout + '/' + this.pages + '/' + this.abbr);
			
			if(this.done){
				this.containerFormPrice.html(this.totalHTML);
				this.containerFormPages.val(this.pages);
				this.containerFormCurrency.val(this.abbr);
			}
			this.done = true;
		},
		// grabs the prices from html
		prices : function(){
			$('#calculator div.data ul li span.additional').each(function(i){
				Engine.calculator.subs[i] = parseInt($(this).html());			
			});
			$('#calculator div.data ul li span.first').each(function(i){
				Engine.calculator.base[i] = parseInt($(this).html()) - Engine.calculator.subs[i];
			});
		},
		// replaces prices with slider
		create : function(){
			var code = '';
			
			code += '<div class="clearfix"><h2>Calculate Cost</h2><h3 class="pages">Emails: <span>1</span></h3></div>' + "\n";
			code += '<ul class="slider"><li class="drag" id="slider"><span class="holder" id="knob"></span></li></ul>' + "\n";
			code += '<div class="wrapper clearfix">';
			code += '<p class="total">Total: <span>0</span></p>';
			code += '<p class="currency"><a href="#EUR" class="euro current" title="EUR">&#8364;</a><a href="#USD" class="dollar" title="USD">$</a><a href="#GBP" class="pound" title="GBP">&#163;</a>';
			code += '</div>' + "\n";

			$('#calculator div.data').after(code).remove();
		},
		// sets shortcuts
		shortcuts : function(){
			// shortcuts
			this.containerPages 	= $('#calculator h3.pages span');
			this.containerCurrency	= $('#calculator p.currency a');
			this.containerTotal		= $('#calculator p.total span');
			this.containerOrder		= $('#calculator p.checkout a');
			this.containerCheckout	= this.containerOrder.attr('href');
			
			this.containerFormPrice		= $('#order strong.price');
			this.containerFormPages		= $('#f-pages');
			this.containerFormCurrency	= $('#f-currency');
		},
		// calculate the price
		calculate : function(){
			var base 		= this.base[this.currency];
			var page		= this.subs[this.currency];	
			this.total 		= this.pages * page + base;
			this.totalHTML 	= this.sign + '' + '' + this.total;
		},
		// add events
		triggers : function(){
			
			var parent = this;
		
			// currency change
			this.containerCurrency.click(function(e){
				 parent.currency = $(this).parent().find('a').index(this);
				 parent.sign = $(this).html();
				 parent.abbr = $(this).attr('href').substr($(this).attr('href').indexOf('#')+1);
				 
				 $(this)
					.addClass('active')//.blur()
					.parent().find('a').not(this).removeClass('active');
					
					
				parent.evaluate();
				return false;
			});
			
			// drag
			$('#slider').slider({
				handle : $('#knob'),
				minValue: 1, 
				maxValue: 20, 
				min: 1, 
				max: 20,
				stepping: 1,
				current: 1,
				slide: function(e, ui) { 
					parent.pages = ui.value;
					parent.evaluate();
				}
			});
			
		}
	}
}

$(document).ready(function(){
	Engine.calculator.init();
});
