jQuery.fn.formify = function(){
	
	$(this).submit(function(e){

		var returnVal = true;
		var errors = [];
		// iterate through text fields and see if nothing has been entered
		
		$(this).find(":text").each(function() {
			
			if(! $(this).val() && $(this).hasClass("required")) {
				
				// if class = required and no value entered prevent submit and change field class to has-error
				// code redundancy FIX
				errors.push("There are required fields that are missing");
				returnVal = false;
				$(this).addClass("has-error")
			
			} 
			else if($(this).val() &&  $(this).hasClass("email")) {
				
				// check against Regex for email format
				var emailFormat = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
				if(! emailFormat.test($(this).val())){
					// code redundancy FIX
					errors.push("Please enter a valid email address");
					returnVal = false;
					$(this).addClass("has-error");
				}
			}
			
		});
		
		// if error div exists remove
		$(this).find("div.error").remove();
		
		// if errors, don't redirect and create error message
		if(!returnVal) { 
			e.preventDefault();
			$('<div class="error"><ul></ul></div>').prependTo(this).hide().fadeIn();
			
			// for each error create list item
			jQuery.each(errors, function(){
				$("div.error ul").append("<li>" + this + "</li>")
			});
			
		}
		
	});
	return $(this);
} // end formify

// start tabify extesion ________________________________________________________________*/

jQuery.fn.tabify = function() {
	// create tabs container
	$(this).wrap("<div class='tabs_container'></div>")
 
   	var tabList = $(this);
 	
	var tabs = [];

	// populate tabs
	$(this).find("a").each(function(index){

		// get ids for ech of the target elem
		var tabId = $(this).attr("href");
		tabs.push(tabId);

		// insert tabs into tabs_container
		$(".tabs_container").append($(tabId));

		// hide all but first target elems
		if(index != 0) {
			$(tabId.toString()).hide();
		} else {
			$(this).parent("li").addClass("selected");
		}

	}); // end each

	// join tabs[] and create jQuery object 
	// THERE HAS TO BE A BETTER WAY TO DO THIS
	var tabStr = tabs.join(", ");
	var tabsObj = $(tabStr);

	$(this).find("a").click(function(e){ 

		// hide all target elems
		tabsObj.hide();
		
		//remove selected class
		tabList.find("li").each(function(){
			$(this).removeClass("selected");
		});
		
		// add selected class
		$(this).parent("li").addClass("selected");
		
		// show elem corresponding to the clicked link
		$($(this).attr("href")).show();
		
		//prevent default action
		e.preventDefault();
		
	}); // end click
	return this;
}
// end tabify extension

// handle external links ________________________________________________________________*/

$(document).ready(function(){
	$("a[rel=external]")
	.attr("title", function(){
		 var curTitle =  $(this).attr("title") ? $(this).attr("title") + " - " : "";
		 return curTitle + "Opens in new window";
	})
	.click(function(e){
		window.open($(this).attr("href"));
		e.preventDefault();
	});
});

// checkUserName ________________________________________________________________*/
// USED ON REGISTER PAGE

$(document).ready(function(){
	// only create link for JS enabled browsers
	$("#registerform").find("#username").after('  <a href="#" id="checkUserName">Is it Available?</a><br /><span id="yesOrNo"></span>');
	$("#checkUserName").click(function(e){
		
	    if(! $('#username').val()) {

			$("#yesOrNo").html("Please Enter Username");

		} else {

			$("#yesOrNo")
			// empty old value if there is one
			.empty()
			// append loading graphic  
			.append('<img src="/images/ajax-loader.gif" />')
			//make request
			.load("http://" + location.hostname + "/user/nameunique/" + $("#username").val() + "",  {submitMethod:"ajax"}, function(){
				$(this).hide().fadeIn();
			});
	
		}
		e.preventDefault();
	});
	
});

/* LEGACY BROWSER STUFF ________________________________________________________________*/

// add and remove focus for form fields in IE6
$(document).ready(function(){
	$('input')
	.focus(function(){
		$(this).addClass("focus");
	})
	.blur(function(){
		$(this).removeClass("focus");
	});
});	
