/*
Function: chk_invert

	Javascript Function. found in Inverts checkboxes. Call with $(<checkbox identifier>).chk_invert();
	
See Also:

	<chk_toggle_on>, <chk_toggle_off>

*/
jQuery.fn.chk_invert = function()
{
	$(this).each(function(){
		is=$(this).attr("checked")?"":"checked";
		$(this).attr("checked",is);
	});
};

/*
Function: chk_toggle_on

	Sets all identified checkboxes to on. Call with $(<checkbox identifier>).chk_toggle_on();
	
See Also:

	<chk_invert>, <chk_toggle_off>
*/

jQuery.fn.chk_toggle_on = function()
{
	$(this).each(function(){
		$(this).attr("checked","checked");
	});
};

/*
Function: chk_toggle_ff

	Sets all identified checkboxes to off. Call with $(<checkbox identifier>).chk_toggle_ff();
	
See Also:

	<chk_invert>, <chk_toggle_on>
*/

jQuery.fn.chk_toggle_off = function()
{
	$(this).each(function(){
		$(this).removeAttr("checked");
	});
}

/*
Function: toggle_allbut
	
	make all classes of a certain type collapse except the one with id "me"
	
Arguments:
	
	type	-	class of elements to hide
	me		- 	id of element to show.
*/

function toggle_allbut(type,me)
{
	me="#"+me;
	type="."+type;
	$(type).each(function(){
		//alert($(this).attr("id"));
		if("#"+$(this).attr("id") != me)
			$(this).hide("slow");
		else
			$(this).show("slow");
	});
}

/*
Function: isemail

	checks if the element's value matches an email format. !! unverified.

*/
jQuery.fn.isemail =function()
{
	val=$(this).val();	
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(val);
}

function isemail(val)
{
	var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
	return pattern.test(val);
}

/*
Function: dropmenu

	creates a simple generic javascript powered drop menu with a nifty popup effect from an unordered list. Note that the element requires some styling.

*/
jQuery.fn.dropmenu=function()
{
	$(this).children("ul").css({display: "none"}); // Opera Fix
	$(this).children("li").hover(function(){
		$(this).find("ul:first").css({visibility: "visible",display: "none"}).show(268);
	},function(){
		$(this).find("ul:first").css({visibility: "hidden"});
	});
}

/*
Function: accord

	Creates a simpler accordion as opposed to jQuery ui's.
	single level. should extend later to have multilevels.
	//! may have unexpected bugs.
	
Required:

	the menu must be in the format. each header must have an "item" attribute with a unique identifier.
	
	<header item="1">First header</header>
	<body>first body luluiuilk</body>
	<header item="2">Second Header</header>
	<body>second body
	two asdasdasd</body>
	
	!! important: the containers mus not have inline attributes.
	!! uses .obfuscate css class for empty body. use same tag
	etc..

Options:-

	active	-	specify an active item to show.
	head	-	selector of elements to use as header. Default is h3
	body	-	selector of elements to use as body. Default is div
	
	
*/
jQuery.fn.accord=function(options)
{

	var def= {
		active:0,
		head:'h3',
		body:'div',
		
		event:'click',
		delay:268
	};
	opt=$.extend({},def,options);
	
	global=this;
	
	// hides everything.
	$(this).children(opt.body).clearQueue().css("display","none");

	$(this).children(opt.head).bind(opt.event,function(){
	
	
		if($(this).next(opt.body).hasClass("active") || $(this).next(opt.body).hasClass("obfuscate"))
			return;
			
		$(global).children(opt.head+'.active').clearQueue().removeClass("active");
		$(global).children(opt.body+'.active').clearQueue().toggle(opt.delay).removeClass("active");
		
		$(this).addClass("active").clearQueue();
		$(this).next(opt.body).clearQueue().toggle(opt.delay,function(){$(this).attr("style","");}).addClass("active");
		//if($(this).next(opt.body).attr("style") != "display: block;")//! ugly hack
			
	});
	
	//show the active thingy
	if(opt.active != 0)
		$(this).children(opt.head+'[item='+opt.active+']').clearQueue().tigger(opt.event);

}

