/* FORMS
-------- */
(function ($j) {
	$j.extend($j.fn, {
		clearingInput: function (options) {
			var defaults = {blurClass: 'blur'};

			options = $j.extend(defaults, options);

			return this.each(function () {
				var input = $j(this).addClass(options.blurClass);
				var form = input.parents('form:first');
				var label, text;

				text = options.text || textFromLabel() || input.val();

				if (text) {
					input.val(text);

					input.blur(function () {
						if (input.val() === '') {
							input.addClass(options.blurClass).val(text);
						}
					}).focus(function () {
						if (input.val() === text) {
							input.val('');
						}
						input.removeClass(options.blurClass);
					});

					form.submit(function() {
						if (input.hasClass(options.blurClass)) {
							input.val('');
						}
					});

					input.blur();
				}

				function textFromLabel() {
					label = form.find('label[for=' + input.attr('id') + ']');
					// Position label off screen and use it for the input text
					return label ? label.css({position: 'absolute', left: '-9999px'}).text() : '';
				}
			});
		}
	});
})(jQuery);

/* TOOLTIPS
----------- */
$j(function () {
    $j('.tooltipContainer').each(function () {
										   
        // options
        var distance = 10;
        var time = 250;
        var hideDelay = 250;

        var hideDelayTimer = null;

        // tracker
        var beingShown = false;
        var shown = false;

        var trigger = $j('.trigger', this);
        var popup = $j('.tooltip', this).css('opacity', 0);

        // set the mouseover and mouseout on both element
        $j([trigger.get(0), popup.get(0)]).mouseover(function () {
            // stops the hide event if we move from the trigger to the popup element
            if (hideDelayTimer) clearTimeout(hideDelayTimer);

            // don't trigger the animation again if we're being shown, or already visible
            if (beingShown || shown) {
                return;
            } else {
                beingShown = true;

                // reset position of popup box
                popup.css({
                    bottom: 90,
                    left: 10,
                    display: 'block' // brings the popup back in to view
                })

                // (we're using chaining on the popup) now animate it's opacity and position
                .animate({
                    bottom: '+=' + distance + 'px',
                    opacity: 1
                }, time, 'swing', function() {
                    // once the animation is complete, set the tracker variables
                    beingShown = false;
                    shown = true;
                });
            }
        }).mouseout(function () {
            // reset the timer if we get fired again - avoids double animations
            if (hideDelayTimer) clearTimeout(hideDelayTimer);

            // store the timer so that it can be cleared in the mouseover if required
            hideDelayTimer = setTimeout(function () {
                hideDelayTimer = null;
                popup.animate({
                    bottom: '+=' + distance + 'px',
                    opacity: 0
                }, time, 'swing', function () {
                    // once the animate is complete, set the tracker variables
                    shown = false;
                    // hide the popup entirely after the effect (opacity alone doesn't do the job)
                    popup.css('display', 'none');
                });
            }, hideDelay);
        });
    });
});

/* OVERLAYS
----------- */
(function($) {

    $.effects.fade = function(o) {

        return this.queue(function() {

            // Create element
            var el = $(this);

            // Set options
            var speed = o.options.speed || 1000;
            var mode = o.options.mode || 'show'; // Set Mode

            // Animate
            if (mode == 'show') {
                el.fadeIn(speed);
            } else {
                el.fadeOut(speed);
            }
            ;
            el.queue('fx', function() {
                el.dequeue();
            });
            el.dequeue();
        });
    };

})(jQuery);
		
function overlay(divId) {
	$j('.dialog').dialog('destroy');
	$j('#' + divId).dialog({
		autoOpen: false,
		dialogClass: 'overlay',
		draggable: false,
		hide: 'fade',
		modal: true,
		resizable: false,
		show: 'fade',
		width: 450
	});
	$j('#' + divId).dialog('open');
}

function overlayClose(divId) {
    $j('#' + divId).dialog('close');
}

/* AUTOTABS
----------- */
/*
 * Autotab - jQuery plugin 1.0
 * http://dev.lousyllama.com/auto-tab
 * 
 * Copyright (c) 2008 Matthew Miller
 * 
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 * 
 * Revised: 2008/05/22 01:23:25
 */

(function($j) {

$j.fn.autotab = function(options) {
	var defaults = {
		format: 'all',			// text, numeric, alphanumeric, all
		maxlength: 2147483647,	// Defaults to maxlength value
		uppercase: false,		// Converts a string to UPPERCASE
		lowercase: false,		// Converts a string to lowecase
		nospace: false,		// Remove spaces in the user input
		target: null,			// Where to auto tab to
		previous: null			// Backwards auto tab when all data is backspaced
	};

	$j.extend(defaults, options);

	var check_element = function(name) {
		var val = null;
		var check_id = $j('#' + name)[0];
		var check_name = $j('input[name=' + name + ']')[0];

		if(check_id != undefined)
			val = $j(check_id);
		else if(check_name != undefined)
			val = $j(check_name);

		return val;
	};

	var key = function(e) {
		if(!e)
			e = window.event;

		return e.keyCode;
	};

	// Sets targets to element based on the name or ID passed
	if(typeof defaults.target == 'string')
		defaults.target = check_element(defaults.target);

	if(typeof defaults.previous == 'string')
		defaults.previous = check_element(defaults.previous);

	var maxlength = $j(this).attr('maxlength');

	// Each text field has a maximum character limit of 2147483647

	// defaults.maxlength has not changed and maxlength was specified
	if(defaults.maxlength == 2147483647 && maxlength != 2147483647)
		defaults.maxlength = maxlength;
	// defaults.maxlength overrides maxlength
	else if(defaults.maxlength > 0)
		$j(this).attr('maxlength', defaults.maxlength)
	// defaults.maxlength and maxlength have not been specified
	// A target cannot be used since there is no defined maxlength
	else
		defaults.target = null;

	// IE does not recognize the backspace key
	// with keypress in a blank input box
	if($j.browser.msie)
	{
		this.keydown(function(e) {
			if(key(e) == 8)
			{
				var val = this.value;

				if(val.length == 0 && defaults.previous)
					defaults.previous.focus();
			}
		});
	}

	return this.keypress(function(e) {
		if(key(e) == 8)
		{
			var val = this.value;

			if(val.length == 0 && defaults.previous)
				defaults.previous.focus();
		}
	}).keyup(function(e) {
		var val = this.value;

		switch(defaults.format)
		{
			case 'text':
				var pattern = new RegExp('[0-9]+', 'g');
				var val = val.replace(pattern, '');
				break;

			case 'alpha':
				var pattern = new RegExp('[^a-zA-Z]+', 'g');
				var val = val.replace(pattern, '');
				break;

			case 'number':
			case 'numeric':
				var pattern = new RegExp('[^0-9]+', 'g');
				var val = val.replace(pattern, '');
				break;

			case 'alphanumeric':
				var pattern = new RegExp('[^0-9a-zA-Z]+', 'g');
				var val = val.replace(pattern, '');
				break;

			case 'all':
			default:
				break;
		}

		if(defaults.nospace)
		{
			pattern = new RegExp('[ ]+', 'g');
			val = val.replace(pattern, '');
		}

		if(defaults.uppercase)
			val = val.toUpperCase();

		if(defaults.lowercase)
			val = val.toLowerCase();

		this.value = val;

		/**
		 * Do not auto tab when the following keys are pressed
		 * 8:	Backspace
		 * 9:	Tab
		 * 16:	Shift
		 * 17:	Ctrl
		 * 18:	Alt
		 * 19:	Pause Break
		 * 20:	Caps Lock
		 * 27:	Esc
		 * 33:	Page Up
		 * 34:	Page Down
		 * 35:	End
		 * 36:	Home
		 * 37:	Left Arrow
		 * 38:	Up Arrow
		 * 39:	Right Arrow
		 * 40:	Down Arroww
		 * 45:	Insert
		 * 46:	Delete
		 * 144:	Num Lock
		 * 145:	Scroll Lock
		 */
		var keys = [8, 9, 16, 17, 18, 19, 20, 27, 33, 34, 35, 36, 37, 38, 39, 40, 45, 46, 144, 145];
		var string = keys.toString();

		if(string.indexOf(key(e)) == -1 && val.length == defaults.maxlength && defaults.target)
			defaults.target.focus();
	});
};

})(jQuery);

/* SHOWING AND HIDING
--------------------- */
function toggleDiv(divId) {
    $j('#' + divId).slideToggle().delay(1000);
}

function openDiv(divId) {
    $j('#' + divId).slideDown(1000);
}

function closeDiv(divId) {
    $j('#' + divId).slideUp(1000);
}

/* ANIMATED SCROLLING
--------------------- */
function scrollWindow(divId) {
	$j('html, body').animate({
		scrollTop: $j('#'+divId).offset().top
	}, 1000);
}

