/*!
 * jQuery Form Plugin
 * version: 2.67 (12-MAR-2011)
 * @requires jQuery v1.3.2 or later
 *
 * Examples and documentation at: http://malsup.com/jquery/form/
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 */
;(function($) {

/*
	Usage Note:
	-----------
	Do not use both ajaxSubmit and ajaxForm on the same form.  These
	functions are intended to be exclusive.  Use ajaxSubmit if you want
	to bind your own submit handler to the form.  For example,

	$(document).ready(function() {
		$('#myForm').bind('submit', function(e) {
			e.preventDefault(); // <-- important
			$(this).ajaxSubmit({
				target: '#output'
			});
		});
	});

	Use ajaxForm when you want the plugin to manage all the event binding
	for you.  For example,

	$(document).ready(function() {
		$('#myForm').ajaxForm({
			target: '#output'
		});
	});

	When using ajaxForm, the ajaxSubmit function will be invoked for you
	at the appropriate time.
*/

$.fn.wnAjaxTab = function(options) {

	//tie up the onclick event
	$("div a", this).bind("click", function(){
		if ($().wnAjaxTabIsValidTabLink($(this).parent().attr("id"), options.prefix))
		{
			//this is a valid link, now check whether the current tab is disabled and needs to be enabled (whether user clicks an already enabled tab)
			if($().wnAjaxTabIsEnabled($(this).parent())==false)
			{
				//everything is ok, proceed further: hide all content, unhide the required content, mark the tab as enabled
				$().wnAjaxTabHideAll($(this).parent().parent(), options.prefix);
				$().wnAjaxTabEnableTab($(this).parent(), options.prefix);
				//recaluclate because user might have laoded more entries into the feed which in turn changed min-height
				$().wnAjaxTabSetMinHeight(options.prefix); 
			}
			return false;
		}
		else
		{
			//this is not a link we need, let browser process it normally
			return true;
		}
	
	});

	$().wnAjaxTabHideAll(this, options.prefix);
	$().wnAjaxTabEnableTab($("div#"+options.default_tab, this), options.prefix);
	$().wnAjaxTabSetMinHeight(options.prefix);
	
	return this;
};

$.fn.wnAjaxTabSetMinHeight = function(prefix) {
	$("div#"+prefix+"_main_box>div").each(function(){
		curr_min_height = $("div#"+prefix+"_main_box").css("min-height").replace("px", "");
		if (!curr_min_height || curr_min_height<$(this).outerHeight(true))
		{
			$("div#"+prefix+"_main_box").css("min-height", $(this).outerHeight(true)+"px");
		}
	});
	return true;

}

$.fn.wnAjaxTabHideAll = function(main_tab_box, prefix) {
	//hide all content boxes
	main_tab_box.children().each(function(){
		if ($().wnAjaxTabIsValidTabLink($(this).attr("id"), prefix))
		{
			$("div#"+prefix+"_"+$(this).attr("id")).css("display", "none");
		}
	});
	
}

$.fn.wnAjaxTabEnableTab = function(clicked_link_element, prefix) {
	//disable all tabs
	clicked_link_element.parent().children().removeClass("enabled_tab");
	//enable new tab
	clicked_link_element.addClass("enabled_tab");
	//show content box
	$("div#"+prefix+"_"+clicked_link_element.attr("id")).css("display", "block");
	
}

$.fn.wnAjaxTabIsEnabled = function(clicked_link_div) {
	if (clicked_link_div.is(".enabled_tab"))
	{
		return true;
	}
	else
	{
		return false;
	}
}


$.fn.wnAjaxTabIsValidTabLink = function(clicked_link, prefix) {
	if (clicked_link.length>0 && $("div#"+prefix+"_"+clicked_link).length>0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

})(jQuery);

