$(function(){
	function getUrlVars()
	{
	    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	    for(var i = 0; i < hashes.length; i++) {
	        return hashes[i];
	    }
	    return vars;
	}
	
	//
	$.extend({
	  timeout : function (func,delay) {
			// init
			if (typeof $.timeout.count == "undefined") $.timeout.count = 0; 	
			if (typeof $.timeout.funcs == "undefined") $.timeout.funcs = new Array(); 
			// set timeout
			if (typeof func =='string') return setTimeout(func, delay); 
			if (typeof func =='function') {
				$.timeout.count++;
				$.timeout.funcs[$.timeout.count] = func;
				return setTimeout("$.timeout.funcs['"+$.timeout.count+"']();", delay);
			}
		},
	  interval : function (func,delay) {
			// init
			if (typeof $.interval.count == "undefined") $.interval.count = 0; 	
			if (typeof $.interval.funcs == "undefined") $.interval.funcs = new Array(); 
			// set interval
			if (typeof func =='string') return setInterval(func, delay); 
			if (typeof func =='function') {
				$.interval.count++;
				$.interval.funcs[$.interval.count] = func;
				return setInterval("$.interval.funcs['"+$.interval.count+"']();", delay);
			}
		},
	  idle : function (func,delay) {
			// init
			if (typeof $.idle.lasttimeout == "undefined") $.idle.lasttimeout = null;
			if (typeof $.idle.lastfunc == "undefined") $.idle.lastfunc = null;
			// set idle timeout
			if ($.idle.timeout) { clearTimeout($.idle.timeout); $.idle.timeout = null; $.idle.lastfunc = null; }
			if (typeof(func)=='string') { 
				$.idle.timeout = setTimeout(func, delay); 
				return $.idle.timeout;
			}		
			if (typeof(func)=='function') { 
				$.idle.lastfunc = func;
				$.idle.timeout = setTimeout("$.idle.lastfunc();", delay);
				return $.idle.timeout;
			}
		},
	  clear : function (countdown) {
		clearInterval(countdown);
		clearTimeout(countdown);
	  }
	});
	
	$.fn.noselect = function () {
		return this.each(function(){
			if($.browser.mozilla){//Firefox
				$(this).css('MozUserSelect','none');
			}else if($.browser.msie){//IE
				$(this).bind('selectstart',function(){return false;});
			}else{//Opera, etc.
				$(this).mousedown(function(){return false;});
			}
		});
	};
	
	$.fn.notices = function(options) {
		$.notify($(this), options);
		return $(this);
	};

	$.notify = function(message, options) {
		var opts = $.extend({}, $.fn.notices.defaults, options || {});
		var ln, target;

		if ( message === null || message === undefined )
			return;
		// message is a jQuery node whose children need to be initialized as notices.
		if ( message instanceof jQuery ) {
			if (message.length == 0)
				return;
			if (message.is(":hidden"))
				message.show();
			opts.onOpen(ln = message.find("li").hide());
		}

		// message is a string: the text to attach to the notice.
		else if ( typeof message == "string" ) {
			target = $(opts.id);
			ln = target.find("li#"+opts.uniqueid);
			if ( opts.uniqueid && ln.length ) {
				opts.onBlink(ln);
				return;
			}

			ln = target.find("li");
			if ( ln.length >= opts.max )
				opts.onClose(ln.get(0));

			ln = $("<li></li>").attr({ 
					className: opts.className, 
					id: opts.uniqueid ? opts.uniqueid : undefined 
				})
				.html((opts.closebutton ? opts.closehtml + ' ' : '') + message)
				.appendTo(target)
				.hide();
			opts.onOpen(ln);
		}

		if (opts.closebutton)
			ln.find("a[href=#close]").click(function(){ opts.onClose($(this).parent()) });
		if (opts.closedblclick)
			ln.dblclick(opts.onClose).noselect();
		if (opts.hover)
			ln.hover(opts.onOver, opts.onOut);
		if (!opts.sticky)
			$.timeout(function(){ opts.onClose(ln) }, opts.timer);
	};

	$.fn.notices.defaults = {
		id				: "#notices",
		message 		: "",
		className		: "notice",
		max				: 3,

		uniqueid		: null,
		sticky			: false,
		timer			: 5000,
		hover			: true,
		blinktimes		: 3,
		closedblclick 	: true,
		closebutton		: false,
		closehtml		: '<a href="#close">x</a>',

		onOpen		: function(e) { $(e.target||e).slideDown(1000); },
		onClose		: function(e) { $(e.target||e).slideUp(1500, function(){ $(e.target||e).remove() }); },

		onOver		: function(e) { $(e.target||e).fadeTo(80, 0.95); },
		onOut		: function(e) { $(e.target||e).fadeTo(140, 0.85); },

		onBlink 	: function(e, options) {
			var opts = $.extend({}, $.fn.notices.defaults, options || {});

			var i, node = (e.data.node||e);
			for (i = 0; i < opts.blinktimes; i++) {
				node.fadeTo(200, 1.0);
				node.fadeTo(200, 0.5);
			}
			node.fadeTo(200, 0.85);
		}
	};
	
	//
	var get = getUrlVars();
	
	$('#email').focus(function(){
		var i = $(this);
		if (i.val() == 'Your Email Address')
			i.val('');
	}).blur(function(){
		var i = $(this);
		if (i.val() == '')
			i.val('Your Email Address');
	});
	$('#notices').notices();
	if (get == 'error')
		$.notify('Oh no! There was an unexpected error (I wish it had been expected)! A computer whiz has been notified!', {className: 'error', uniqueid: 'subscribe'});
	else if (get == 'success')
		$.notify('Yay! You\'ve been registered! Expect to hear from us soon!', {className: 'success', uniqueid: 'subscribe'});
	else if (get == 'subscribed')
		$.notify('You\'ve already been subscribed, silly!', {className: 'notice', uniqueid: 'subscribe'});
})