Cufon.replace('h2, .category-title h3, .cta a, #hp-eotm, #hp-quiz-subline, .vote-apple p, .cb-question, .question-number, #results-header h3, #zip-code-label');


var tnlottery = new (function($){

	var $me = this;

	$me.init = function() {
			
		var $this = this;

		if ($.browser.msie) {
			try {
				document.execCommand(
					"BackgroundImageCache",
					false, 
					true);
			} catch(err){}
		}
			
		/* Stay in Touch Popup*/
		$('#nav-stayintouch').click( function() {
			$.get('/stay-in-touch', function(data) {
				$('#overlayContent').html(data);
				$this.showOverlay();

				$('#newsletter-submit').click( function() {
					$me.submitEmail();
				});

				$('#newsletter-signup').submit( function() {
					$me.submitEmail();
				});
			});
		});



		/* Overlay click to close */
		$('#overlay, #popup-close').live('click', function() {
			if($('#overlay').css('opacity') == 0.6) {
				$this.hideOverlay();
			}
		});

		/* Pre-cache the popup bkgnd images */
		$me.preLoadImages($templateDir+"/images/popup-top.gif",
											$templateDir+"/images/popup-middle.gif",
											$templateDir+"/images/popup-bottom.gif",
											$templateDir+"/images/popup-hdr.jpg");

	}; //End init


	$me.submitEmail = function() {

		$email = $('#newsletter-signup').find('#email').val();
					
		if($('#newsletter-signup').find(':checked').val()) {
			if($me.validateEmail($email)) {
				$values = $('#newsletter-signup').find(':input').serialize();
				$.post('/newsletter-submit', 
							$values,
							function(data) {
								$('#response').html(data);	
								if($('#overlay').css('opacity') == 0.6) {
									setTimeout($me.hideOverlay,1500);	
								}
							});
			}	
			else {
				$('#response').html("Invalid email");	
			}
		}
		else {
			$('#response').html("Please confirm your age.");	
		}

}; //End submitEmail

	$me.hideOverlay = function() {
		$('#overlayContent').fadeOut(250, function() {
			$('#overlayContent').empty();
			$('#overlay').fadeOut(0.25);
				
			// Looks weird, but a fix for the JScrollPane to work correctly
			$('#overlayContent').fadeIn(0);  
				
			if(swfobject.getObjectById("flashcontent")) {
				swfobject.getObjectById("flashcontent").onRemoveOverlay();
			}
		});
	}; //End hideOverlay



	$me.showOverlay = function(hardSetAuto) {

		var $this = this;
		setPosition = ($.browser.msie && $.browser.version < 7) ? 'absolute' : 'fixed';
		//gotta get a math.max on there, some dialogs are displaying at negative y values, putting the close button above the window.
		autoPosition = Math.max(10,Math.floor(($(window).height() - $('#overlayContent').height())/2))+'px';
		if(setPosition == 'absolute') {
			autoPosition = parseInt(autoPosition, 10) + $(document).scrollTop() + 'px';
		}
		else if (hardSetAuto>0) {
			autoPosition = hardSetAuto+'px';
		}

		Cufon.replace('h2, .cta a, .category-title h3');
		$('#overlay').fadeTo(500, 0.6).css({height: $(document).height()+'px', display:'block'});
		$('#overlayContent').fadeTo(500, 1).css({display:'block', position: setPosition, top:autoPosition});
	}; //End showOverlay



  var cache = [];
  // Arguments are image paths relative to the current page.
  $me.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }


	$me.validateEmail = function(field) {
			apos=field.indexOf("@");
			dotpos=field.lastIndexOf(".");
			if (apos<1||dotpos-apos<2) {
				return false;
			}
			else {
				return true;
			}
	}


	/* This function is used by Flash */

	ipToCounty = function() {
		
		if($.cookie('currentCounty')) {
			var $county = $.cookie('currentCounty');
			$county = $county.replace(/ /g,"_");
			$county = $county.replace(/\+/g,"_");
			swfobject.getObjectById("flashfeature").onAutoHighlight($county);
			tnlottery.money.moneyOverlay($county);
		}
		else {
//			console.log("No county for this IP");
		}
	};	


	/* This function is used by the Scholarships and Grants page */

	sgOverlay = function(sg) {
		var $sg = "sg="+encodeURIComponent(sg);
		// Display Scholarship or Grant information
		$.post('/popup-scholarships-grants', 
			$sg,
			function(data) {
			$('#overlayContent').html(data);
			$me.showOverlay();
		});
	};
	

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


})(jQuery);





