$(function() {
	/* calculator */
	var calculator = {
		videos_count_selector: '#calculator input#calculator_videos_count',
		images_count_selector: '#calculator input#calculator_images_count',
		videos_size_selector: '#calculator input#calculator_videos_size',
		formats_count_selector: '#calculator input#calculator_formats_count',
		iformats_count_selector: '#calculator input#calculator_iformats_count',
		price_selector: '#calculator_price',
		
		overtraffic_limits: {
		  low: 100 * 1024 * 1024 * 1024,
		  middle: 1000 * 1024 * 1024 * 1024,
		  high: 1000 * 1024 * 1024 * 1024
		},
		
		videos_count: function() { return parseFloat ($(this.videos_count_selector).val()) },
		images_count: function() { return parseFloat ($(this.images_count_selector).val()) },
		videos_size: function() { return parseFloat ($(this.videos_size_selector).val()) },
		formats_count: function() { return parseFloat ($(this.formats_count_selector).val()) },
		iformats_count: function() { return parseFloat ($(this.iformats_count_selector).val()) },
		
		to_gb: function(bytes) {
			return bytes / 1024 / 1024 / 1024;
		},
		
		plans: function() { if(plans.length == 0) { return false; } else { return plans; } },
		plan: function(chosen_plan) {
			var currentPlan;
			 $.each(plans, function(i, plan){
        		if(plan.name.toLowerCase() == chosen_plan.toLowerCase()) currentPlan = plan;
      		});
			return currentPlan;
		},
		
		applyPreset: function(name){
			var preset = this.presets[name];
			$(this.videos_count_selector).val(preset.videos_count);
			$(this.videos_size_selector).val(preset.videos_size);
			$(this.formats_count_selector).val(preset.formats_count);
			this.calculate();
			
			$('input[name=typeswitch]').filter('[value=video]').click();
			return true;
    	},
		
		initialize: function() {
			/* filling sample presets */
			this.presets = new Array();
			this.presets['High Volume User Generated Video'] = {videos_count: 19000, videos_size: 25, formats_count: 3};
			this.presets['Premium Video Portal'] = {videos_count: 3000, videos_size: 200, formats_count: 3};
			this.presets['Large Interactive Agency'] = {videos_count: 300, videos_size: 350, formats_count: 2};
			this.presets['Small Video Production Shop'] = {videos_count: 30, videos_size: 100, formats_count: 3};
			
			/* CALCULATE button */
			$('#calculator_execute').click(function(){ calculator.calculate() });
			
			/* validate/calculate on keyup */
			$('#calculator input[type=text]')
				.keyup(function() {
					var current = $(this);
					current.val( current.val().replace(/\D/g, '') );
					calculator.calculate();
					
					//while (calculator.calculate() == false && current.val().length > 1) {
					//	current.val( current.val().substring(0, current.val().length-1) );
					//}
				});
				
			/* video/images switching */
			var usageBasedPricing = $('.usageBasedPricing');
			var typeswitch = $('input[name=typeswitch]');
			var calculatorInputs = $('.calculatorInputs');
			typeswitch
			  .click(function() {
				 var value = $(this).val();
				 typeswitch.filter('[value=' + value + ']').attr('checked', 'true');
				 usageBasedPricing.hide().filter('.' + value).show();
				 calculatorInputs.hide().filter('.' + value).show();
				 calculator.calculate();
			  })
			  .first()
			  .click();

			/* monthly/prepay switching */
			var typeSelect = $('.planTypeSelect');
			var typePlans = $('.typePlans');  
			typeSelect
				.click(function() {
					typeSelect.removeClass('active');
				  	var type = $(this).addClass('active').text();
					typePlans
						.hide()
						.filter('.' + type)
						.show()
						.children()
						.first()
						.attr('checked', 'true');
					usageBasedPricing
						.filter('.video')
						.children()
						.hide()
						.filter('.' + type)
						.show();
					calculator.calculate();
			  })
			  .first()
			  .click();
			
			/* calculate on plan choose */
			$('[name=pricingPlan]').click(function(){ calculator.calculate() }).first().click();

			/* sample presets applying */
			$('.calculatorPresets a')
			  .click(function() {
				  calculator.applyPreset($(this).text());			
			  });
		},
		
		validateVideo: function(){
			if(!this.plans()) {
				//$(this.price_selector).val('sorry, an error occured');
				return false;
			}
			if(!this.videos_count()) {
				//$(this.price_selector).val('please enter average videos per month');
				//$(this.videos_count_selector).focus();
				return false;
			}
			if(!this.videos_size()) {
				//$(this.price_selector).val('please enter average size');
				//$(this.videos_size_selector).focus();
				return false;
			}
			if(!this.formats_count()) {
				//$(this.price_selector).val('please enter number of formats');
				//$(this.formats_count_selector).focus();
				return false;
			}
			return true;
		},
		
		validateImages: function(){
			if(!this.images_count()) {
				//$(this.price_selector).val('please enter average images per month');
				//$(this.images_count_selector).focus();
				return false;
			}
			if(!this.iformats_count()) {
				//$(this.price_selector).val('please enter number of formats');
				//$(this.iformats_count_selector).focus();
				return false;
			}	
			return true;
		},
		
		calculateVideo:function() {
			if (!this.validateVideo()) {
				$(this.price_selector).val('');
				return false;
			}
			
			var incoming_traffic = this.videos_count() * this.videos_size() * 1024 * 1024;
			var outgoing_traffic = incoming_traffic * 0.6;
			var traffic = incoming_traffic + outgoing_traffic * this.formats_count();
			var current_plan = this.plan($('[name=pricingPlan]:checked').val());
			var overtraffic = parseFloat(traffic) - (parseFloat(current_plan.incoming) + parseFloat(current_plan.outgoing));
			if(parseInt(traffic)<100){
				return false;
			}
			
			var price = parseInt(current_plan.price);
			var enprice=0;
			
			if (current_plan.name == 'Pre-pay 5GB') {
				enprice=4;
				if(overtraffic > 0) price += enprice * this.to_gb(overtraffic);
			} else if (current_plan.name == 'Pre-pay 25GB') {
				enprice=3;
				if(overtraffic > 0) price += enprice * this.to_gb(overtraffic);
			} else if (current_plan.name == 'Pre-pay 50GB') {
				enprice=2.5;
				if(overtraffic > 0) price += enprice * this.to_gb(overtraffic);
			} else if (current_plan.name == 'Pre-pay 100GB') {
				enprice=2;
				if(overtraffic > 0) price += enprice * this.to_gb(overtraffic);
			} else {
				/* monthly plans */
				if(overtraffic > 0 && overtraffic <= this.overtraffic_limits.low) {
					price += 2.5 * this.to_gb(overtraffic);
				} else if(overtraffic > this.overtraffic_limits.low && overtraffic <= this.overtraffic_limits.high) {
					price += 2 * this.to_gb(overtraffic);
				} else if(overtraffic > this.overtraffic_limits.high) {
					price += 1.8 * this.to_gb(overtraffic);
				}
			}

			$(this.price_selector).val(price.toFixed(2));
		},
		
		calculateImages:function() {
			if (!this.validateImages()) {
				$(this.price_selector).val('');
				return false;
			}
			
			if(this.images_count() <= 5000) icount = 0.03;
			if(this.images_count() <= 25000 && this.images_count() > 5000) icount = 0.02;
			if(this.images_count() > 25000) icount = 0.01;
			
			price = this.images_count() * this.iformats_count() * icount;
			
			$(this.price_selector).val(price.toFixed(2));
		},
		
		calculate:function() {
			if ($('[name=typeswitch]:checked').val() == 'video') {
				return this.calculateVideo();
			} else {
				return this.calculateImages();
			}
		}
	}

	calculator.initialize();
});
