/**
* hoverIntent r6 // 2011.02.26 // jQuery 1.5.1+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne brian(at)cherne(dot)net
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev])}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob)},cfg.interval)}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev])};var handleHover=function(e){var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t)}if(e.type=="mouseenter"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob)},cfg.interval)}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob)},cfg.timeout)}}};return this.bind('mouseenter',handleHover).bind('mouseleave',handleHover)}})(jQuery);

/**
 * SureChem Global scripts
 */

// immediately allow CSS to target elements JQuery will change
$('html').addClass('jq');

var SC = SC || {};

SC.nullFn = function(){};
SC.nullEvt = function(e){
	e.stopImmediatePropagation();
	return false;
};
SC.nullJq = function(){return this;};
//(function($){$.fn.bgiframe=SC.nullJq;})(jQuery); // stub - later overridden in IE
 	
urchinTracker = SC.nullFn; // prevent errors on dev

$.SC = $.SC || {};

/**
 * Image preload
 * @author danielnashnz@gmail.com
 */
(function(SC) {
	var cache = [];
	SC.loadImages = function() {
		var 
			img, 
			i, 
			l = arguments.length
		;
		for (i=l;i--;) {
			img = document.createElement('img');
			img.src = arguments[i];
			cache.push(img);
		}
	};
})(SC);
	
/**
 * EnhanceLoginForm
 * 
 * Move labels into fields, disable multiple submissions
 * @author danielnashnz@gmail.com
 */
(function($) {
	SC.enhanceLoginForm = function(id, button, hideClass) {
		var c = { labels: {}, pwd: {}, form: {}, button: {}, valid: false };
		c.form = $(id + ' form');
		c.form
			.find('label')
				.each(
					function(){ // store labels
						var 
							t = $(this), 
							f, ftype
						;
						f = t.attr('for');
						ftype = $('#' + f).attr('type');
						if ( ftype == 'text' || ftype == 'password' ) {
							c.labels[f] =	{ jq: t, txt: t.text().toLowerCase() };
						}
					}
				)
			.end()
			.find('input[type="password"]:enabled:visible').wrap('<span class="password-group" />')
				.each(
					function(){ // make a f field
						var 
							r = $(this),
							html, id, f, fid
						;
						id = r.attr('id');
						fid = 'fake-' + id;
						if (c.labels[id]) {
							c.labels[fid] = { jq: c.labels[id].jq, txt: c.labels[id].txt };
							c.labels[id] = null;
						}
						html = r.parent('span.password-group').html()
							.replace(/id=([^ \t>]+)/, 'id="' + fid + '"')
							.replace(/name=([^ \t>]+)/, 'name="' + fid + '"')
							.replace(/type=([^ \t>]+)/, 'type="text"')
						;
						r.removeAttr('tabIndex').hide().after(html);
						f = $('#' + fid);
						c.pwd[fid] = { fake: f, real: r }; 
						f.bind('focus', fid, function(e){
							c.pwd[e.data].fake.hide().attr('tabindex',-1);;
							c.pwd[e.data].real.show().focus().attr('tabindex',2);
						});
						r.bind('blur', fid, function(e){
							if (this.value == '') {
								c.pwd[e.data].real.hide().attr('tabindex',-1);
								c.pwd[e.data].fake.show().attr('tabindex',2);
							}
							else {
								c.valid = true;
							}
						});
					}
				)
			.end()
			.find('input[type="text"]:enabled:visible')
				.each(
					function(i){ // look for fields to populate
						var 
							t = $(this),
							l, id
						;
						id = t.attr('id');
						l = c.labels[id];
						if (l) {
							t.bind('blur', l, function(e){
								if (this.value == '') {
									this.value = e.data.txt;
								}
							});
							if (!c.pwd[id]) {
								t.bind('focus', l, function(e){
									if (this.value == e.data.txt) {
										this.value = '';
									}
								});
							}
							t.blur();
							l.jq.hide();
						}
					}
				)
		;
		$(button)
			.find('input[type="submit"]:enabled:visible')
			.each(function(){c.button = $(this);})
			.addClass('interactive')
		;
		c.form.bind('submit', c, function(e){
				if (e.data.valid == true) {
					e.data.button.addClass('busy inactive').removeClass('interactive');
					e.data.valid == false;
					return true;
				}
				return false;
			}
		);
		$(id).removeClass(hideClass);
		$('input[type="text"][type="button"]:visible', c.form).each(function(i){$(this).attr('tabindex', i);});
	};
})(jQuery);

jQuery(function($) {
	SC.loadImages('/images/lowlight.png');
	SC.enhanceLoginForm('#login-bar', '#login-button', 'jq-hide');
});

