$(document).ready(function() {

	$("a.grouped_elements").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200
	});
	
	// Used for Terms & Conditions & Video
		jQuery("a.fancybox").fancybox({
		'padding'		: 0
	});


	//$('.basic').find('ul').addClass('columnlist');

	$("#loginbutton").click(function () {
		var _currentState =$('#login').css('height');
		if (_currentState=='0px') {
			$('#login').animate({"height":"80px"}, 300, function(){
				$('#loginbutton').parents('li').addClass('closelogin');
				$('#loginbutton').text('Close');
			});
		} else {
			$('#login').animate({"height":"0px"}, 300, function(){
				$('#loginbutton').parents('li').removeClass('closelogin');
				$('#loginbutton').text('Login');
				$(this).css({'display':'none'});
			});
		}		
	});
	
// List Splitter
	$('.columnlist').easyListSplitter({ 
		colNumber: 2, 
	   direction: 'horizontal' 
	});	
	




	
	// Bind forms to 'clearmyfields' function
	    $("#member_form").bind("submit", clearmyfields); 
	    
	    $("#freeform").bind("submit", clearmyfields); 
	    
	    $("#comment_form").bind("submit", clearmyfields);      
	    
	  	$("#contactform").bind("submit", clearmyfields); 
	  					
	
	// Functions for Login Form
	
	    var options = { 
	        target:        '#ffajaxreturn',   // target element(s) to be updated with server response 
	        beforeSubmit:  clearDefaults,  // pre-submit callback 
	        success:       showResponse,  // post-submit callback 
	 
	        // other available options: 
	        //url:       url         // override for form's 'action' attribute 
	        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
	        //dataType:  'html',        // 'xml', 'script', or 'json' (expected server response type) 
	        clearForm: true        // clear all form fields after successful submit 
	        //resetForm: true        // reset the form after successful submit 
	 
	        // $.ajax options can be used here too, for example: 
	        //timeout:   3000 
	    }; 
	 
	    // bind form using 'ajaxForm' 
	    $('#loginform').ajaxForm(options);
	
	
		// Clear default login fields before attempting login
		function clearDefaults(formData, jqForm, options) { 
			$("#loginform input:text,input:password,textarea").each(function(){
				
				if ($(this).attr("alt")) {
					var _alt = $(this).attr("alt");
					if($(this).val()==_alt) {
						$(this).val('');
					}
				}
			}); 
			$("#loginerror").html("Attempting Login");
		    $('.loginsumbit').attr('value','');
		    $('.loginsumbit').attr('disabled', 'disabled').addClass("submitting");
		    return true; 
		} 	
		
		
		// Response from login form 
		function showResponse(responseText, statusText, xhr, $form)  { 
		    if (responseText == 'success') {
		
				//Hide Login Bar
				$("#loginwrapper").css({display: "none"});
				$("#loginbutton").css({backgroundPosition: '-476px 0px'});
				loginBeingShown = false;
		
				//Replace Login Button
				$(".nav_login").replaceWith('<li class="nav_logout"><a href="/?ACT=10">Logout</a></li>');
				document.location = document.location.href;
		
		    } else {
				//

				
		    	$('.loginsumbit').attr('value','Sign In');
		    	$('.loginsumbit').attr('disabled', '').removeClass("submitting");
		    	// the div id "returnerrormessage" is in the user message template - it's only used for this
		    	$("#loginerror").html($(responseText).find('#returnerrormessage ul li').html());
		    }
		    
		    
		    
		    //Reset Default Text Fields after a submit
		    $("input:text,input:password,textarea").each(function(){		
				if ($(this).attr("alt")) {
					var _class = '';
					var _name = '';
					var _id = '';
					var _alt='';
				
					
					if($(this).attr('type')=='password'){
						_alt = $(this).attr("alt");
						_class = $(this).attr("class");
						_name = $(this).attr("name");
						_id = $(this).attr("id");
		
						if ($(this).val()=='') {
							$(this).replaceWith('<input type="text" class="'+_class+'" name="'+_name+'" id="'+_id+'" value="'+_alt+'" alt="'+_alt+'" />');
							$('#'+_id).attr("value",_alt); // This is for Safari to force value if back button is pressed
							$('#'+_id).bind('focus',fieldcheck);
						}
						
					} else {
						_alt = jQuery(this).attr("alt");
						if ($(this).val()=='') {
							$(this).val(_alt);
						}
					}			
				}
			});		    
		} 
	// End functions for Login Form			  
		
		
	// Toggle Board Title Div on Registration Form 
	jQuery('input[name=boardmember]:radio').bind('load, change',regboardcontent);
	function regboardcontent(el) {
		    if(jQuery("input[name=boardmember]:checked").val() == 'Yes') {
		    	jQuery('.boardtitle').css({display: 'block'}); 
		    } else {
		    	jQuery('.boardtitle').css({display: 'none'});
		    }
	}		
		
			
	// Default Form Fields Focus and Blur Functionality
		$("input:text,textarea").focus(function() {
			if (jQuery(this).attr("alt")) {
				var _alt = jQuery(this).attr("alt");
				if (jQuery(this).val()==_alt) {
					jQuery(this).val('');
				}
			}
		});
		
		$("input:text,textarea").blur(function() {
			if (jQuery(this).attr("alt")) {
				var _alt = jQuery(this).attr("alt");
				if (jQuery(this).val()=='') {
					jQuery(this).val(_alt);
				}
			}	
		});	
				
		// If this for field is a password, replace it with text, then change it back to password on Focus
		$("input:text,input:password,textarea").each(function(){		
			if ($(this).attr("alt")) {
				var _class = '';
				var _name = '';
				var _id = '';
				var _alt='';
			
				
				if($(this).attr('type')=='password'){
					_alt = $(this).attr("alt");
					_class = $(this).attr("class");
					_name = $(this).attr("name");
					_id = $(this).attr("id");
	
					if ($(this).val()=='') {
						$(this).replaceWith('<input type="text" class="'+_class+'" name="'+_name+'" id="'+_id+'" value="'+_alt+'" alt="'+_alt+'" />');
						$('#'+_id).attr("value",_alt); // This is for Safari to force value if back button is pressed
						$('#'+_id).bind('focus',fieldcheck);
					}
					
				} else {
					_alt = jQuery(this).attr("alt");
					if ($(this).val()=='') {
						$(this).val(_alt);
					}
				}			
			}
		});	
	
	
		// Swap Text/Password Fields for Password inputs - continued
			function fieldcheck(el) {
				if($(this).attr('type')=='text'){
					
					_alt = $(this).attr("alt");
					_class = $(this).attr("class");
					_name = $(this).attr("name");
					_id = $(this).attr("id");			
					
					$(this).replaceWith('<input type="password" class="'+_class+'" name="'+_name+'" id="'+_id+'" alt="'+_alt+'" value="" />')
					$('#'+_id).focus();
					$('#'+_id).bind('blur',fieldcheck);
		
				}
		
				
				if($(this).attr('type')=='password'){
					if($(this).val()==''){
						_alt = $(this).attr("alt");
						_class = $(this).attr("class");
						_name = $(this).attr("name");
						_id = $(this).attr("id");
						
						$(this).replaceWith('<input type="text" class="'+_class+'" name="'+_name+'" id="'+_id+'" value="'+_alt+'" alt="'+_alt+'" />');
						$('#'+_id).bind('focus',fieldcheck);
					}
				}
			}

	



});

// Function to Clear form defaults before submission
clearmyfields = function(el) {
	var _thisFormID = jQuery(this).attr('id');
	jQuery("#"+_thisFormID + " input:text,input:password,textarea").each(function(){
		if (jQuery(this).attr("alt")) {
			var _alt = jQuery(this).attr("alt");
			if(jQuery(this).val()==_alt) {
				jQuery(this).val('');
			}
		}
	});
}
		
