$.fn.accessKeys = function(){
	
	var keys = new Array(0);
	
	var find = function(ary, element){
	    for(var i=0; i<ary.length; i++){
	        if(ary[i] == element){
	            return true;
	        };
	    };
	    return false;
	};
	
	$(this).each(function(i){
		
		for (var j=0; j<$(this).text().length; j++){
			
			var char = $(this).text().charAt(j);
			
			//alert(char);
			
			if(!find(keys,char.toLowerCase())&&char.match(/[A-Za-z_0-9]/)){
				regexp = new RegExp(char,"i");
				$(this).html( $(this).html().replace(regexp,'<u>'+char+'</u>') );
				$(this).attr( 'accesskey', char.toLowerCase() );
				break;
			};
			
		};
		
		keys[i] = char.toLowerCase();
	});
	
};

function stripeTables(){
	$("table.striped tbody tr").removeClass("odd");
	$("table.striped tbody tr:nth-child(odd)").addClass("odd");
}

function fadeNotifiers(){
	$("p.notify").fadeOut("slow");
}

$.postJSON = function(url, data, callback) {
	$.post(url, data, callback, "json");
};

$(document).ready(function(){
	
	
	if($('#news_nav').is('div')){
		if($('#contentwrap').height()<$('#news_nav').height()){
			$('#contentwrap').height($('#news_nav').height());
		};
	};
	
	$('#mainmenu a, #colone a, ul.vnav a, p.newsitem a').accessKeys();
	
	$('img.switch')
		.css('cursor','pointer')
		.click(function(){
			
			var style = $(this).attr('id').split('-')[1];
			if(style!=='basic'){
				$.cookie('style',style,{ path: '/' });
			} else {
				$.cookie('style',null,{ path: '/' });
			};
			document.location=document.location;
		});
	
	
	stripeTables();
	
	t=setTimeout("fadeNotifiers()",3000)
	
	// functions which extend jquery
	$.fn.pause = function(milli,type) {
		milli = milli || 1000;
		type = type || "fx";
		return this.queue(type,function(){
			var self = this;
			setTimeout(function(){
				$.dequeue(self);
			},milli);
		});
	};

	$.fn.clearQueue = $.fn.unpause = function(type) {
		return this.each(function(){
			type = type || "fx";
			if(this.queue && this.queue[type]) {
				this.queue[type].length = 0;
			}
		});
	};
	/*
	 * Allows only valid characters to be entered into input boxes.
	 * Note: does not validate that the final text is a valid number
	 */
	jQuery.fn.numeric = function(decimal, callback){
		decimal = decimal || ".";
		callback = typeof callback == "function" ? callback : function(){};
		this.keypress(
			function(e)
			{
				var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
				// allow enter/return key (only when in an input box)
				if(key == 13 && this.nodeName.toLowerCase() == "input")
				{
					return true;
				}
				else if(key == 13)
				{
					return false;
				}
				var allow = false;
				// allow Ctrl+A
				if((e.ctrlKey && key == 97 /* firefox */) || (e.ctrlKey && key == 65) /* opera */) return true;
				// allow Ctrl+X (cut)
				if((e.ctrlKey && key == 120 /* firefox */) || (e.ctrlKey && key == 88) /* opera */) return true;
				// allow Ctrl+C (copy)
				if((e.ctrlKey && key == 99 /* firefox */) || (e.ctrlKey && key == 67) /* opera */) return true;
				// allow Ctrl+Z (undo)
				if((e.ctrlKey && key == 122 /* firefox */) || (e.ctrlKey && key == 90) /* opera */) return true;
				// allow or deny Ctrl+V (paste), Shift+Ins
				if((e.ctrlKey && key == 118 /* firefox */) || (e.ctrlKey && key == 86) /* opera */
				|| (e.shiftKey && key == 45)) return true;
				// if a number was not pressed
				if(key < 48 || key > 57)
				{
					/* '-' only allowed at start */
					if(key == 45 && this.value.length == 0) return true;
					/* only one decimal separator allowed */
					if(key == decimal.charCodeAt(0) && this.value.indexOf(decimal) != -1)
					{
						allow = false;
					}
					// check for other keys that have special purposes
					if(
						key != 8 /* backspace */ &&
						key != 9 /* tab */ &&
						key != 13 /* enter */ &&
						key != 35 /* end */ &&
						key != 36 /* home */ &&
						key != 37 /* left */ &&
						key != 39 /* right */ &&
						key != 46 /* del */
					)
					{
						allow = false;
					}
					else
					{
						// for detecting special keys (listed above)
						// IE does not support 'charCode' and ignores them in keypress anyway
						if(typeof e.charCode != "undefined")
						{
							// special keys have 'keyCode' and 'which' the same (e.g. backspace)
							if(e.keyCode == e.which && e.which != 0)
							{
								allow = true;
							}
							// or keyCode != 0 and 'charCode'/'which' = 0
							else if(e.keyCode != 0 && e.charCode == 0 && e.which == 0)
							{
								allow = true;
							}
						}
					}
					// if key pressed is the decimal and it is not already in the field
					if(key == decimal.charCodeAt(0) && this.value.indexOf(decimal) == -1)
					{
						allow = true;
					}
				}
				else
				{
					allow = true;
				}
				return allow;
			}
		)
		.blur(
			function()
			{
				var val = jQuery(this).val();
				if(val != "")
				{
					var re = new RegExp("^\\d+$|\\d*" + decimal + "\\d+");
					if(!re.exec(val))
					{
						callback.apply(this);
					}
				}
			}
		)
		return this;
	}
	
	$(".confirm").click(function(){return confirm(this.title+"\nAre you sure you want to continue?");});
	$(".required").keyup(function(){
		if(this.value){$(this).removeClass("warn");
		}else{$(this).addClass("warn");}
	});
	$(".numbers").numeric();
	$("a[@href]").each(function(i){if(this.rel=="external"){this.target='_blank';}});
	$("input[@type]").each(function(i){$(this).addClass(this.type);}); //class inputs with their type (for m$ie6)
	
	$(".validate").submit(function(){
		valid = true;
		e='';
		i=0;
		$(".match", this).each(function(i){
			
			if(i){
				valid=(this.value==value)
			}
			value=this.value;
			i++;
		});
		
		if(!valid) e='Passwords do not match\n'
		
		$(".required", this).each(function(i){
			if(!this.value){
				$(this).addClass("warn");
				e=e+$(this).attr("title")+"\n";
				valid=false;
			} else {
				$(this).removeClass("warn");
			}
		});
		if(!valid){alert("You missed out the following fields\n\n"+e);}
		return valid;
	});
	
	$('a[@rel=file]').each(function(){
		$(this).addClass(this.href.split('.').pop().toLowerCase()).addClass('file')
	});
	
});

jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        };
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            };
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        };
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                };
            };
        };
        return cookieValue;
    };
};