var $j = jQuery.noConflict();

(function($j) {
	$j.extend($j.fx.step,{
	    backgroundPosition: function(fx) {
            if (fx.state === 0 && typeof fx.end == 'string') {
                var start = $j.curCSS(fx.elem,'backgroundPosition');
                start = toArray(start);
                fx.start = [start[0],start[2]];
                var end = toArray(fx.end);
                fx.end = [end[0],end[2]];
                fx.unit = [end[1],end[3]];
			}
            var nowPosX = [];
            nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
            nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];           
            fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];
            
           function toArray(strg){
               strg = strg.replace(/left|top/g,'0px');
               strg = strg.replace(/right|bottom/g,'100%');
               strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
               var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
               return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
           }
        }
	});
})(jQuery);

(function($) {
  $.fn.vCenter = function(options) {
 var pos = {
   sTop : function() {
  return window.pageYOffset
  || document.documentElement && document.documentElement.scrollTop
  ||  document.body.scrollTop;
   },
 sLeft : function() {
  return window.pageXOffset
  || document.documentElement && document.documentElement.scrollLeft
  ||  document.body.scrollLeft;
   },
 wWidth : function() {
  return window.innerWidth
  || document.documentElement && document.documentElement.clientWidth
  || document.body.clientWidth;
   },
   wHeight : function() {
  return window.innerHeight
  || document.documentElement && document.documentElement.clientHeight
  || document.body.clientHeight;
   }
 };
 return this.each(function(index) {
   if (index == 0) {
  var $this = $(this);
  var elHeight = $this.height();
  var elWidth = $this.width();
  var elTop = pos.sTop() + (pos.wHeight() / 2) - (elHeight / 2);
  var elLeft = pos.sLeft() + (pos.wWidth() / 2) - (elWidth / 2);
  $this.css({
    position: 'absolute',
    marginTop: '0',
    top: elTop,
	left: elLeft
  });
   }
 });
};
})(jQuery);


$j(function () {
/* dom ready now */

$j('#top-nav a')
	.css( {backgroundPosition: "0 12px"} )
	.hover(
		function(){
			$j(this).stop().animate(
				{backgroundPosition:"(0 2px)"}, 
				{duration:250})
			},
		function(){
			$j(this).stop().animate(
				{backgroundPosition:"(0 12px)"}, 
				{duration:125})
		});
		
	$j("#reg-close").hide();
	
	$j(".tell-friend").click(
		function(e) {
			e.preventDefault();			
			$j("#page-overlay").show();
			$j("#content-section").show();
			$j("#content-section").vCenter();
	});
	
	$j("#reg-cancel").click(
		function(e) {
			$j("#content-section").hide();
			$j("#page-overlay").hide();
			$j("#t_form").show();
			$j("#t_thanks").hide();
			$j("#t_error").hide();
			$j("#reg-create").show();
			document.forms['tafform'].reset();
			$j('#yname_v').removeClass("v-progress v-ok v-fail");
			$j('#yname_v').html('<span class="valid-text"> </span>');
			$j('#email_v').removeClass("v-progress v-ok v-fail");
			$j('#email_v').html('<span class="valid-text"> </span>');
			$j("#reg-close").hide();
			$j("#reg-cancel").show();
		});
		
	$j("#reg-close").click(
		function(e) {
			$j("#content-section").hide();
			$j("#page-overlay").hide();
			$j("#t_form").show();
			$j("#t_thanks").hide();
			$j("#t_error").hide();
			$j("#reg-create").show();
			document.forms['tafform'].reset();
			$j('#yname_v').removeClass("v-progress v-ok v-fail");
			$j('#yname_v').html('<span class="valid-text"> </span>');
			$j('#email_v').removeClass("v-progress v-ok v-fail");
			$j('#email_v').html('<span class="valid-text"> </span>');
			$j("#reg-close").hide();
			$j("#reg-cancel").show();
		});
		
	$j("#email")
		.focus( 
			function() {
				$j('#email_v').removeClass("v-ok v-fail");
				$j('#email_v').addClass('v-progress');
				$j('#email_v').html('<span class="valid-text"> Who are we contacting?</span>');
			}
		)
		.blur (
			function() {
				if (this.value.length == 0) {
					$j('#email_v').removeClass("v-ok v-fail v-progress");
					$j('#email_v').html('<span class="valid-text">  </span>');
				} else {
					if (this.value.match(/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/)) {
						// probably valid
						$j('#email_v').removeClass("v-progress v-fail");
						$j('#email_v').addClass('v-ok');
						$j('#email_v').html('<span class="valid-text"> OK</span>');
						email_check = 1;
					} else {
						// probably not
						$j('#email_v').removeClass("v-progress v-ok");
						$j('#email_v').addClass('v-fail');
						$j('#email_v').html('<span class="valid-text"> Email looks invalid</span>');
						email_check = 0;
					}	
					}
			}
		);
		
	$j("#yname")
		.focus( 
			function() {
				$j('#yname_v').removeClass("v-ok v-fail");
				$j('#yname_v').addClass('v-progress');
				$j('#yname_v').html('<span class="valid-text"> Who are you?</span>');
			}
		)
		.blur (
			function() {
				if (this.value.length != 0) {
					// probably valid
					$j('#yname_v').removeClass("v-progress v-fail");
					$j('#yname_v').addClass('v-ok');
					$j('#yname_v').html('<span class="valid-text"> OK</span>');
					yname_check = 1;
				} else {
					// probably not
					$j('#yname_v').removeClass("v-progress v-ok");
					$j('#yname_v').addClass('v-fail');
					$j('#yname_v').html('<span class="valid-text"> Tell us your name please</span>');
					yname_check = 0;
				}	
			}
		);
		
		$j("#reg-create").click(
			function() {
				if (yname_check && email_check) {
					// Good to go
					$data = "email=" + $j("#email").val() + "&yname=" + $j("#yname").val() + "&comment=" + $j("#comment").val();
					$j.ajax({
						    url: '/ajax/tell_friend.html',
						    type: 'GET',
						    dataType: 'text',
							data: $data,
						    timeout: 3000,
						    error: function() {
								$j("#t_form").hide();
								$j("#t_error").show();
								$j("#reg-create").hide();
								$j("#reg-close").show();
								$j("#reg-cancel").hide();
						    },
						    success: function(json){
								$j("#t_form").hide();
								$j("#t_thanks").show();
								$j("#reg-create").hide();
								$j("#reg-close").show();
								$j("#reg-cancel").hide();
						    }
						});
					
				} else {
					if (yname_check == 0) {
						$j('#yname_v').removeClass("v-progress v-ok");
						$j('#yname_v').addClass('v-fail');
						$j('#yname_v').html('<span class="valid-text"> We need to know your name</span>');
					}
					if (email_check == 0) {
						$j('#email_v').removeClass("v-progress v-ok");
						$j('#email_v').addClass('v-fail');
						$j('#email_v').html('<span class="valid-text"> Email looks invalid</span>');
					}
				}
			}
		);
	
});
