var Site = {
	config : {
		base_url : '/'
	},
	start : function() {
		(function($) {
			// On Load 
			$(window).load(function() {
				if ($.browser.msie && parseInt($.browser.version) < 7) {
					$('.trans_img').pngFix({ blankgif : Site.config.base_url + 'images/graphics/blank.gif' });
				}
			});
			
			// On Dom Ready
			$(function() {
				if ($('#set_region_link')) {
					$('.set_region_links').click(function() {
						$('#set_region_map_container').modal({'close' : false, 'overlay' : 80, 'containerId' : 'set_region_container'});
					});
				}
				
				/* 
				//Deprecated
				if ($('#enter_zip_code')) {
					var enter_zip_code = $('#enter_zip_code').val();
					$('#enter_zip_code').focus(function() {
						if (jQuery(this).val() == enter_zip_code) {
							jQuery(this).val('');
						}
					}).blur(function() {
						if (jQuery(this).val() == '') {
							jQuery(this).val(enter_zip_code);
						}
					});
				}
				*/

			});
		})(jQuery);
	},
	home_start : function() {

		// On Dom Ready
		jQuery(function($) {

			$('.home_footer_tr_bottom a.details_link').each(function(k, v) {
				$('#home_feature_' + k).click(function() {
					$this = $(v);
					if ($this.attr('target')) {
						window.open($this.attr('href'), '_blank');
					} else {
						window.location = $this.attr('href');
					}
				});
			});

		});

	},
	
	start_cart : function() {
		
	jQuery(function($) {

			$('.addtocart').each(function() { 

					var $this = $(this);

					$this.click(function(){
						
						$.ajax({
					   	  type: "GET",
						  data: "name="+this.id,
						  url: Site.config.site_url + 'plans/calc_cart',
						  cache: false,
						  success: function(html){
						    $("#loadinfo").html(html);
						  }
						});
						
						$('#addtocart_div').modal({persist : true, 'close' : true, 'overlay' : 80, 'containerId' : 'addtocart_container'});
						
					});
			  });
		
		
		// CONFIRM REMOVAL

				$('.removal').each(function() { 

						var $this = $(this);

						$this.click(function(){

							$.ajax({
						   	  type: "GET",
							  data: "name="+this.id,
							  url: Site.config.site_url + 'cart/removal',
							  cache: false,
							  success: function(html){
							    $("#loadinfo").html(html);
							  }
							});

							$('#removal_div').modal({persist : true, 'close' : true, 'overlay' : 80, 'containerId' : 'removal_container'});

						});
				  });

			});

	},
	
	start_phone_details : function() {

			jQuery(function($) {
				// This makes the tabs interface.
				// Find all phone tab links and attach click events.
				$('#phone_breadcrumbs').find('li a').click(function(e) {
					e.preventDefault();
					// Make new tab element jQuery object
					var new_tab = $(this);
					// Find tab that's set to on.
					var old_tab = $($('#phone_breadcrumbs').find('li a.on').get(0));
		
					// Check if tab is already selected.
					if (new_tab[0] === old_tab[0]) return false;
		
					// Use id names to build ids for containers
					var new_container = $('#' + (new_tab.attr('id')).split('__')[0]);
					var old_container = $('#' + (old_tab.attr('id')).split('__')[0]);
		
					$(old_tab).removeClass('on');
					$(new_tab).addClass('on');
					// If browser is ie6 we don't use effect.
					if ($.browser.msie && $.browser.version < 7) {
						$(old_container).hide();
						$(new_container).show();
					}
					else {
						$(old_container).hide("slow");
						$(new_container).show("slow");
					}
		
				});
			});

	},
	start_plans : function() {

		jQuery(function($) {

			$('#family_plan_price_btn').click(function(e) {
				e.preventDefault();
				$('#family_plan_prices').modal({persist : true, 'close' : true, 'overlay' : 80, 'containerId' : 'family_plan_prices_container'});
			});
			
			$('#addtocart_div').click(function(e) {
				var $target = $(e.target);
				if ($target.is(".simplemodal-close")) {
					$.modal.close();
					return false;
				} 
			});
			
			
			
			$('.addtocart').each(function() { 

					var $this = $(this);

					$this.click(function(){
						
						$.ajax({
					   	  type: "GET",
						  data: "name="+this.id,
						  url: Site.config.site_url + 'plans/calc_cart',
						  cache: false,
						  success: function(html){
						    $("#loadinfo").html(html);
						  }
						});
						
						$('#addtocart_div').modal({persist : true, 'close' : true, 'overlay' : 80, 'containerId' : 'addtocart_container'});
						
						
						
					});
			  });
			

			/* Hard Coded, this needs to be pulled from db */
			var additional_line_price = 30;
			var family_plan_price = 40;
			var family_plan_savings_lines_orig = $('#family_plan_savings_lines').html();
			var family_plan_savings_orig = $('#family_plan_savings').html();
			$('#family_plan_savings_select').change(function() {
				var lines = jQuery(this).val();
				var additional_lines = lines - 1;
				var total_nodiscount = family_plan_price + (additional_lines * family_plan_price);
				var total = family_plan_price + (additional_lines * additional_line_price);
				if (lines == 0) {
					$('#family_plan_savings_lines').html(family_plan_savings_lines_orig);
					$('#family_plan_savings').html(family_plan_savings_orig);
				}
				else {
					$('#family_plan_savings_lines').html('<h6>' + lines + '</h6><h6>Line' + (lines > 1 ? 's' : '') + '</h6><h5>$' + total + '</h5><p>per month</p>');
					$('#family_plan_savings').html('<h5>$' + (total_nodiscount - total) + '</h5><p>per month</p>');
				}
			});
			
			Site.custom_checkboxes('#savings_calculator_current_features');
			Site.custom_radios('#savings_calculator_pocket_plans');
			$('#savings_calculator_pocket_plans span:first').click();
			$('#savings_calculator_calc_link').click(function(e) {
				e.preventDefault();
				var fields = ['savings_calculator_min_month', 'savings_calculator_cost_month', 'savings_calculator_current_features_value', 'savings_calculator_pocket_plans_value', ];
				var postdata = {};
				var empty_fields = false;
				$.each(fields, function(k, v) {
					postdata[v] = $('#' + v).val();
					if (k < 2 && postdata[v] == '') {
						empty_fields = true;
					}
				});
				if (empty_fields) {
					$('#savings_calc_notice').hide();
					$('#savings_calc_notice').fadeIn('slow');
					return false;
				}
				else {
					$('#savings_calc_notice').css('display', 'none');
				}

				$('#savings_calc_status').toggle();
				$.post(Site.config.site_url + 'plans/calc_savings', postdata, function(data) {
					$('#savings_calculator').html(data);
					$('#savings_calc_status').toggle();
					//$('#savings_calculator').toggle();
					$('#savings_calculator').modal({persist : true, 'close' : true, 'overlay' : 80, 'containerId' : 'savings_calculator_container'});
			 	});
			});
			$('.numeric').numeric();

		});
		
	},

	custom_checkboxes : function(id_selector) {
		var val = [];
		$(id_selector + ' span').click(function() {
			$(this).toggleClass('on');

			val = [];
			$(id_selector + ' span.on').each(function() {
				val.push(jQuery(this).text());
			});
			$(id_selector + '_value').val(val);
		});
	},
	
	custom_radios : function(id_selector) {
		var val = '';
		$(id_selector + ' span').click(function() {
			$(id_selector + ' span.on').each(function() {
				$(this).removeClass('on');
			})
			val = '';
			$(this).addClass('on');
			//$(id_selector + '_value').val($(this).text());
			$(id_selector + '_value').val($(this).attr('id') ? $(this).attr('id').split('_').pop() : $(this).text());
		});
	}

};

jQuery.fn.numeric = function(decimal, callback)
{
	decimal = decimal || ".";
	callback = typeof callback == "function" ? callback : function(){};
	this.keypress(
		function(e)
		{
			var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
			// allow enter/return key (only when in an input box)
			if(key == 13 && this.nodeName.toLowerCase() == "input")
			{
				return true;
			}
			else if(key == 13)
			{
				return false;
			}
			var allow = false;
			// allow Ctrl+A
			if((e.ctrlKey && key == 97 /* firefox */) || (e.ctrlKey && key == 65) /* opera */) return true;
			// allow Ctrl+X (cut)
			if((e.ctrlKey && key == 120 /* firefox */) || (e.ctrlKey && key == 88) /* opera */) return true;
			// allow Ctrl+C (copy)
			if((e.ctrlKey && key == 99 /* firefox */) || (e.ctrlKey && key == 67) /* opera */) return true;
			// allow Ctrl+Z (undo)
			if((e.ctrlKey && key == 122 /* firefox */) || (e.ctrlKey && key == 90) /* opera */) return true;
			// allow or deny Ctrl+V (paste), Shift+Ins
			if((e.ctrlKey && key == 118 /* firefox */) || (e.ctrlKey && key == 86) /* opera */
			|| (e.shiftKey && key == 45)) return true;
			// if a number was not pressed
			if(key < 48 || key > 57)
			{
				/* '-' only allowed at start */
				if(key == 45 && this.value.length == 0) return true;
				/* only one decimal separator allowed */
				if(key == decimal.charCodeAt(0) && this.value.indexOf(decimal) != -1)
				{
					allow = false;
				}
				// check for other keys that have special purposes
				if(
					key != 8 /* backspace */ &&
					key != 9 /* tab */ &&
					key != 13 /* enter */ &&
					key != 35 /* end */ &&
					key != 36 /* home */ &&
					key != 37 /* left */ &&
					key != 39 /* right */ &&
					key != 46 /* del */
				)
				{
					allow = false;
				}
				else
				{
					// for detecting special keys (listed above)
					// IE does not support 'charCode' and ignores them in keypress anyway
					if(typeof e.charCode != "undefined")
					{
						// special keys have 'keyCode' and 'which' the same (e.g. backspace)
						if(e.keyCode == e.which && e.which != 0)
						{
							allow = true;
						}
						// or keyCode != 0 and 'charCode'/'which' = 0
						else if(e.keyCode != 0 && e.charCode == 0 && e.which == 0)
						{
							allow = true;
						}
					}
				}
				// if key pressed is the decimal and it is not already in the field
				if(key == decimal.charCodeAt(0) && this.value.indexOf(decimal) == -1)
				{
					allow = true;
				}
			}
			else
			{
				allow = true;
			}
			return allow;
		}
	)
	.blur(
		function()
		{
			var val = jQuery(this).val();
			if(val != "")
			{
				var re = new RegExp("^\\d+$|\\d*" + decimal + "\\d+");
				if(!re.exec(val))
				{
					callback.apply(this);
				}
			}
		}
	);
	return this;
}

Site.start();

jQuery(document).ready(function() {
	jQuery("#submitRegionZip").click(setRegion);
	jQuery("#regionZip").keyup(function(e) {
		// Submit on enter key
		if(e.keyCode == 13) {
			setRegion();
			return false;
		} else {
			return true;
		}
	});
});

function setRegion() {
	// Validate zip code
	var re_zip = /^\d{5}$/;
	
	if(!re_zip.exec(jQuery("#regionZip").val())) {
		jQuery("#zipError").text("Please provide a valid ZIP code.");
		jQuery("#regionZip").val("");
	} else {
		jQuery("#zipError").text("");
		window.location = Site.config.site_url + "zipcode/index/" + jQuery('#regionZip').val();
	}
}
