$(document).ready(function () {	
	var enigmatic = {
		fontFamily: 'Enigmatic',
		hover: true
	};
	
	Cufon.replace('h1, h2,#hints-tips h3, h4, h5, h6,a.button-gray, a.button-hint, ul#nav-main li, ul#nav-utility li, div#modal-window h2, div#modal-window p, div.buy-game-group ul li a.btn-buy-now, body#page-guide div.columns div.copy a.btn-buy-now, body.french div.buy-game-group ul li a.btn-buy-now-ca, a.btn-generic', enigmatic);
	Cufon.now();
	
	
	function lineUpTheSquigglyBorder(){
	    var self = $(this),
	    distanceBetweenSquiggly = 28,
	    remainder = self.height() % distanceBetweenSquiggly;
	    if (remainder !== 0){
	        self.height(self.height() + (distanceBetweenSquiggly-remainder)); 
	    } 
	   
	    self.find(".article").height(self.find(".article").height()+ (distanceBetweenSquiggly-remainder+13)); 
	}
	
	function lineUpTheCheckers(){
	    var self = $(this),
	    distanceBetweenCheckers = 34,
	    remainder = self.height() % distanceBetweenCheckers;

	    if (remainder !== 0){
	        self.height(self.height() + (distanceBetweenCheckers-remainder)); 
	    } 
	    
	    //add code to position the character wrapper
	    
	}

	//line up the squiggle border for all articles
	$('.article-middle').each(function(indexInArray,valueOfElement){
	    lineUpTheSquigglyBorder.call(valueOfElement);
	})
	
	$('#main-content-body').each(function(indexInArray,valueOfElement){
	    lineUpTheCheckers.call(valueOfElement);
	});
	
	
	
});

(function($){
	$.fn.extend({
		modalPanel: function() {
			function modalHide() {
				$(document).unbind('keydown', handleEscape);
				var remove = function() { $(this).remove(); };
				overlay.fadeOut(remove);
				modalWindow.fadeOut(remove).empty();
				if (typeof document.body.style.maxHeight === 'undefined') { //if IE 6
					$('body','html').css({
						height: 'auto',
						overflow: 'auto',
						width: 'auto'
					});
				}
			}
			
			function handleEscape(e) {
				if (e.keyCode == 27) {
					modalHide();
				}
			}
			
			var overlay = $('<div id="overlay"></div>');
			var modalWindow = $('<div id="modal-window"></div>');
			
			return this.each(function() {
				$(this).click(function(e) {					
					$('body').append(overlay.click(function() {
						modalHide();
					}));
					
					$('body').append(modalWindow);
					
					overlay.fadeIn(500);
					
					e.preventDefault();
					
					$(document).keydown(handleEscape);
					
					var bodyHeight = $(document.body).height();
					var bodyWidth = $(document.body).width();
					var viewportHeight = $(document.viewport).height();
					var viewportWidth = $(document.viewport).width();
					var modalHeight = modalWindow.height();
					var modalWidth = modalWindow.width();
					
					var retailersContent = $('div#modal-retailers').html();
					
					modalWindow.html(retailersContent).fadeIn(200);
					
					function positionModal() {
						var bodyWidth = $(document.body).width();
						modalWindow.css({
							left: (bodyWidth - modalWidth) / 2 + 'px',
							top: '285px'
						});
					}
					positionModal();

					$(window).bind('resize', function() {
						positionModal();
					});
					
					if (typeof document.body.style.maxHeight === 'undefined' || navigator.platform == "Nintendo Wii") {
						overlay.css({
							height: (viewportHeight > bodyHeight ? viewportHeight : bodyHeight) + 'px'
						});
					}
	
					Cufon.refresh();
					
					modalWindow.append($('<a id="btn-modal-close"><span class="offscreen">Close</span></a>').click(function() {
						modalHide();
					}));
				});
			});
		}
	});
})(jQuery);

$(function() {
	$('a.btn-buy-now-ca').modalPanel();
});