$(document).ready(function(){

  	$.validator.addMethod("phone", function(value, element) {
        return this.optional(element) || /^\+[0-9]{5,}$/i.test(value);
    }, "Phone must contain only numbers, + and -.");

  	$.validator.addMethod("blackberry_pin", function(value, element) {
        return this.optional(element) || /^[0-9a-zA-Z]{8}$/i.test(value);
    }, "PIN must be 8 alpha-numeric characters");

  	$.validator.addMethod("humanname", function(value, element) {
        return this.optional(element) || /^[A-Za-z\-\'\s]+$/i.test(value);
    }, "Name entry must contain only letters, hyphens, or apostrophes.");
  	
  	$.validator.addMethod("username", function(value, element) {
        return this.optional(element) || /^(?=.{6,})[a-z0-9]+$/i.test(value);
    }, "Username should be 6 characters or more, consist only of letters and numbers.");
  	
  	$.validator.addMethod("password", function(value, element) {
        return this.optional(element) || /^.*(?=.{7,})(?=.*\d)(?=.*[a-z]).*$/i.test(value);
    }, "Password should be 7 characters or more, consist only of letters and numbers with at least one of each");
  	
  	$('.slider-item').click( function() {
		$('.slider-popout').fadeOut();
  		if( $(this).hasClass('current') )
  			$(this).removeClass('current');
  		else
  		{
  			$('.slider-item').removeClass('current');
  			var rel = $(this).attr('rel');
  			$('#'+rel).fadeIn();
  			$(this).addClass('current');
  		}
  	});
  	
  	if( $().colorbox )
  		$('.colorbox').colorbox();
  	
  	$('#cboxLoadedContent a.overlay_link').live('click', function(e) {
  		e.preventDefault();
  		
  		var url = $(this).attr('href');
  	    $.ajax({
  	        type: 'GET',
  	        url: url,
  	        dataType: 'html',
  	        cache: true,
  	        beforeSend: function() {
  	            $('#cboxLoadedContent').empty();
  	            $('#cboxLoadingGraphic').show();
  	        },
  	        complete: function() {
  	            $('#cboxLoadingGraphic').hide();
  	        },
  	        success: function(data) {                  
  	            $('#cboxLoadedContent').append(data);
  	    	    $.colorbox.resize();
  	        }
  	    });
  	});
  	
  	$('.register-help-link').click( function() {
  		var parent = $(this).parent();
  		if( parent.find('.register-help-text').length > 0)
  		{
  	  		$('.register-help-text').detach();
  		}
  		else
  		{
  	  		$('.register-help-text').detach();
  	  		parent.append('<div class="register-help-text"><div class="register-help-close"></div>' + $(this).attr('title') + '</div>');
  		}
  	});
  	$('.register-help-close').live( 'click', function() {
  		var parent = $(this).parent();
  		parent.detach();
  	})
  	
  	$('#changeevent').click( function() {
  		$('#changeevent-select').toggle();
  		$('#changeevent-active').toggle();
  		$('#changeevent-inactive').toggle();
  		return false;
  	});
  	
  	$('#login').click( function() {
  		$('#loginpopup').toggle();
  		$('#loginpopup-active').toggle();
  		$('#loginpopup-inactive').toggle();
  		return false;
  	});
  	
  	$('.register-terms').mouseover( function() { $('.register-terms-text').show(); })
  	$('.register-terms').mouseout( function() { $('.register-terms-text').hide(); })
  	
  	var tallest_panel = 0;
  	$('.about-panel-match').each( function() { if( $(this).height() > tallest_panel ) tallest_panel = $(this).height(); });
  	$('.about-panel-match').height( tallest_panel );

  	$('textarea[maxlength]').each(function() {
        var maxlength = $(this).attr('maxlength');
        $(this).val($(this).val().slice(0, maxlength));
        $(this).live('keyup', function() {
            $(this).val($(this).val().slice(0, maxlength));
        });

    });
});
