tnlottery.money = new (function($){

	var $me = this;

	$me.init = function() {

		if($('#zip-code-input').length > 0) {
			$('#zip-code-input').ForceNumericOnly();

/*
			$.getJSON($templateDir+'/xml/zipcode.json', function(data) {
				$zipcodes = data;
			});
*/
		}

		$('#zip-code-submit').click(function() {
//			$me.flashHighlightCounty($zipcodes[$('#zip-code-input').val()]);

			var $values = $('#zip-form').find(':input').serialize();
			$me.flashHighlightCounty($values);

			return false;
		});


		if($('#zip-form').length > 0) {

			$('#zip-form').submit(function() {
//				$me.flashHighlightCounty($zipcodes[$('#zip-code-input').val()]);

				var $values = $('#zip-form').find(':input').serialize();
				$me.flashHighlightCounty($values);

				return false;
			});

		}

	}; //End init


	$me.countyNameConvert = function() {
		return arguments[1].toUpperCase() + arguments[2];
	};

	$me.flashHighlightCounty = function($values) {
		// If not in the list, display error message
		// Else send to Flash... flash should automatically display popup

		$.post('/county', 
					$values,
					function(data) {
						var $county = $me.formatCounty(data);
						if($county == "Nozip") {
							$('#zip-code-error').html("Zip code not found.");
						}
						else {
							$('#zip-code-error').empty();
							swfobject.getObjectById("flashcontent").onAutoHighlight($county);
							$me.moneyOverlay($county);
						}
				});
	}; //End flashHighlightCounty



	$me.formatCounty = function(county) {
		county = county.replace(/\n/g,"");
//		county = county.toLowerCase();
		county = county.replace(/\b([a-z])(\w*)\b/g, $me.countyNameConvert);
		county = county.replace(/ /g, "_");
		return county;
	}; //End formatCounty


	$me.initializeScrollbar = function() {

		/* County Popup - Scrollbars if necessary */
		$('.scrollme').jScrollPane({scrollbarWidth:14,animateTo:false});

		/* This should be called when a user clicks on the flash county icon
		But has to be done after jScrollPane has been initialized*/
		$('.category-data').each(function() {
			$(this).children('.jScrollPaneContainer').hide();
		});

		tnlottery.showOverlay(50);

		/* County Popup - Expand/hide text */ 
		$('.category-more').click(function() {
	
			var $clickText = $(this);
			var $expandme = $(this).parents('.category').find('.jScrollPaneContainer');

			// Close all panes that are expanded
			$('.category-data .jScrollPaneContainer:visible').each(function() {
				$(this).slideUp(500, function() {
					var $myscroll = $(this).parents('.category').find('.scrollme');
					$myscroll[0].scrollTo(0);
				});
			});


			// Expand
			if($($expandme).is(':hidden')) {
				$expandme.slideDown(500,function() {
					$clickText.html('- click to hide');
					$(this).parents('.category').find('.category-more').html('+ click to expand');
				});
			}
			else {
				$expandme.slideUp(500,function() {
					$clickText.html('+ click to expand');
				});
			}
		});


	}; //End initializeScrollbar


	// Numeric only control handler
	$.fn.ForceNumericOnly = function() {
    return this.each(function() {
			$(this).keydown(function(e) {
				var key = e.charCode || e.keyCode || 0;
        // allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
        return (
        	key == 8 || 
          key == 9 ||
					key == 13 ||
          key == 46 ||
          (key >= 37 && key <= 40) ||
          (key >= 48 && key <= 57) ||
          (key >= 96 && key <= 105));
				})
		})
	};


	$me.moneyOverlay = function(county) {

		var $county = county.replace(/_/g, " ");
		var $values = "county="+$county;
		$.get('/popup-money', 
			$values,
			function(data) {
				$('#overlayContent').html(data);
				$me.initializeScrollbar();
			});
	};



	$(document).ready(function(){
		tnlottery.money.init();
	});

})(jQuery);





