$(document).ready(function() {
	//data
	$("table.simpleData tbody tr:has(td.id):not(:has(th)):even").addClass("even");
	$("table.simpleData tbody tr:has(td.id):not(:has(th)):odd").addClass("odd");
	$("table.simpleData tbody tr:has(td.id):not(:has(th))").addClass("hand");
	
	$("table.simpleData tr th:last-child").addClass("right");
	$("table.simpleData tr td:last-child").addClass("right");
	
	//simple data
	$("table.data tbody tr:has(td.id):not(:has(th)):even").addClass("even");
	$("table.data tbody tr:has(td.id):not(:has(th)):odd").addClass("odd");
	$("table.data tbody tr:has(td.id):not(:has(th))").addClass("hand");
	
	$("table.data tr th:last-child").addClass("right");
	$("table.data tr td:last-child").addClass("right");
	
	
	//header rollovers	
	$("img.rollover").each(function() {
		preload = $(this).attr("src");
		preload = preload.replace(/.jpg$/ig, "Over.jpg");
		preload = preload.replace(/.gif$/ig, "Over.gif");
		preload = preload.replace(/.png$/ig, "Over.png");
		$("<img>").attr("src", preload);
	})
	
	$("img.rollover").mouseover(function() {
		original = $(this).attr("src");
		
		if (!(original.match(/Over\.jpg/) || original.match(/Over\.gif/) || original.match(/Over\.png/))) {
			rollOver = original.replace(/.jpg$/ig, "Over.jpg");
			rollOver = rollOver.replace(/.gif$/ig, "Over.gif");
			rollOver = rollOver.replace(/.png$/ig, "Over.png");
			$(this).attr("src", rollOver);
		}
	});
	
	$("img.rollover").mouseout(function() {
		if (typeof(original) != "undefined") {
			$(this).attr("src", original);
		}
	});
	
	
	// forms
	// $("form input").keypress(function (event) {
	// 	if (event.keyCode == 13) {
	// 		$(this).parents("form").find("input.default").click();
	// 	}
	// });
	// 
	// $("form input.submit").click(function (event) {
	// 	event.preventDefault();
	// 	alert("will validate");
	// 	try {
	// 		alert($(this).parents("form").valid());
	// 	} catch (err) {	
	// 		alert("did validate");
	// 	}
	// 	
	// 	alert("did validate");
	// 	
	// 	return false;
	// 	$(this).parents("form").append("<input type='hidden' id='theSubmitButton'>");
	// 	$(this).parents("form").append("<input type='submit' style='display: none;' id='theSubmitButton'>");
	// 	$("input[type=hidden]#theSubmitButton").attr("name", $(this).attr("name")).attr('value', $(this).attr('value'));
	// 	$("input[type=submit]#theSubmitButton").click();
	// 	$("input").attr('disabled', 'disabled');
	// 	
	// 	return false;
	// });
	
	//$("input:button, input:submit, input:reset, a.button").button().show();
	
	//masking
	$('input.zip').mask("99999");
	$('input.phone').mask("999-999-9999");
	$('input.date').mask("99/99/9999");
	$("input.numeric").numeric();
	
	//konami code
	
	$(document).konami(function () {
		$("img").effect("bounce", {times:5}, 300);
		$("img").css({"-moz-transform" : "rotate(180deg)", "-webkit-transform" : "rotate(180deg)"});
		$("div").fliptext();
	});
	
	
	// Membership stuff
	$('.membershipStatus').each(function () {
		if ($(this).text() != 'Active' && $(this).text() != 'Member Club') {
			$(this).css("color", "#F00");
		}
	});
	
	$('.membershipExpirationDate').each(function () {
		var tempDate = new Date($(this).text());
		//00-69 should be 2000-2069 and 70-99 should be 1970-1999
		if (tempDate.getFullYear() < 1970) tempDate.setFullYear(tempDate.getFullYear() + 100); 
		
		var today = new Date();
		today = new Date(today.getFullYear(), today.getMonth(), today.getDate() + 30);
		
		if (tempDate < today) {
			$(this).css("color", "#F00");
		}
	});
});

jQuery.fn.konami = function(fn, params) {
	params = $.extend({}, $.fn.konami.params, params);
	this.each(function() {
		var tgt = $(this);
		tgt.bind('konami', fn).bind('keyup', function(event) {
			jQuery.fn.konami.checkCode( event, params, tgt ); 
		});
	});
	return this;
};

jQuery.fn.konami.params = {
    'code':[38,38,40,40,37,39,37,39,66,65],
    'step':0
};

jQuery.fn.konami.checkCode = function(event, params, tgt) {
    if(event.keyCode == params.code[params.step]) {
        params.step++;
    } else {
        params.step = 0;
    }

    if(params.step == params.code.length) {
        tgt.trigger('konami');
        params.step = 0;
    }
};

jQuery.fn.fliptext = function(){

  var charset ={a:"\u0250",b:"q",c:"\u0254",d:"p",e:"\u01DD",f:"\u025F",g:"\u0183",h:"\u0265",i:"\u0131",j:"\u027E",k:"\u029E",l:"l",m:"\u026F",n:"u",o:"o",p:"d",q:"b",r:"\u0279",s:"s",t:"\u0287",u:"n",v:"\u028C",w:"\u028D",y:"\u028E",z:"z",1:"\u21C2",2:"\u1105",3:"\u1110",4:"\u3123",5:"\u078E"   /* or u03DB */ ,6:"9",7:"\u3125",8:"8",9:"6",0:"0",".":"\u02D9",",":"'","'":",",'"':",,","Ã‚Â´":",","`":",",";":"\u061B","!":"\u00A1","\u00A1":"!","?":"\u00BF","\u00BF":"?","[":"]","]":"[","(":")",")":"(","{":"}","}":"{","<":">",">":"<",_:"\u203E","\r":"\n"};


  jQuery.expr[':'].nochildren = function(elem){
   return !elem.innerHTML.match(/\<\w/);
  };

  function flipStr(str) {
  	var result = "";
  	for (var x = str.length - 1; x >= 0; --x){
  		var c = str.charAt(x);
  		var r = charset[c];
  		result += typeof(r) != "undefined" ? r : c;
  	}

  	return result;
  };

  // we check all nodes to see if they have children by looking for < in their innerHTML
  // for a more pure DOM approach looking for textNodes only, see http://pastie.org/435207.txt
    this.find('*').andSelf().filter(':nochildren').each(function(){
 			var flipped = flipStr((this.innerText || this.textContent).toLowerCase());
			if (this.innerText) {
				this.innerText = flipped;
			} else {
				this.textContent = flipped;
			}

  });  

  return this;  

};

$.fn.numeric = function()
{
	this.keypress(function(e) {
		var allow = false;
		var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
		
		if (key >= 48 && key <= 57) allow = true;
		
		if (
			key == 13 ||
			key == 8 ||
			key == 9 ||
			key == 13 ||
			key == 35 ||
			key == 36 ||
			key == 37 ||
			key == 39
		) allow = true;
		
		if ($(this).val().indexOf(".") < 0 && key == 46) allow = true;
		
		return allow;
	});
	
	return this;
};
