﻿/*! Copyright (c) 2010 Geoff R G Williams, Primal Blaze Ltd
* Beta version. Redistribution and reuse not permitted.
* 
* Version 1.0
*/
(function($) {
	$.extend($.expr[":"], {
		data: function(a, i, m) {
			var exp = m[3].split(/\=/);
			exp[1] = $.trim(exp[1]).replace(/^\'/, "").replace(/\'$/, "");
			return $(a).data(exp[0]) == exp[1] || $(a).attr("data-" + exp[0]) == exp[1];
		},
		focused: function(a, i, m) {
			return a == document.activeElement;
		}
	});
})(jQuery);


jQuery.fn.press = function(downHandler, upHandler) {
	var down = false;
	var $this = $(this);
	$this.mousedown(function(e) {
		down = true;
		downHandler.apply(this, [e]);
		$(document).mouseup(function(e) {
			down = false;
		});
	}).mouseup(function(e) {
		upHandler.apply(this, [e]);
	}).mouseout(function(e) {
		if (down) {
			upHandler.apply(this, [e]);
		};
	});
};

jQuery.fn.caret = function(begin, end) {
	if (this.length == 0) return;
	if (typeof begin == 'number') {
		end = (typeof end == 'number') ? end : begin;
		return this.each(function() {
			if (this.setSelectionRange) {
				this.focus();
				this.setSelectionRange(begin, end);
			} else if (this.createTextRange) {
				var range = this.createTextRange();
				range.collapse(true);
				range.moveEnd('character', end);
				range.moveStart('character', begin);
				range.select();
			}
		});
	} else {
		if (this[0].setSelectionRange) {
			begin = this[0].selectionStart;
			end = this[0].selectionEnd;
		} else if (document.selection && document.selection.createRange) {
			var range = document.selection.createRange();
			begin = 0 - range.duplicate().moveStart('character', -100000);
			end = begin + range.text.length;
		}
		return {
			begin: begin,
			end: end
		};
	}
};

jQuery.fn.link = function (maxLengths) {
	var _shouldJump = false;
	var _shouldBackspace = false;
	var _shouldDelete = false;
	var _shouldLeft = false;
	var _shouldRight = false;
	var _justJumped = false;

	var $_this = this;

	$_this.keydown(function (e) {
		var $this = $(this);
		var partIndex = $_this.index(this);
		var caret = $this.caret();
		if (e.which == 9 && !e.shiftKey) {		//tab
			if (_justJumped) {
				e.preventDefault();
				_justJumped = false;
			};
		}
		else if (caret.begin == caret.end) {
			if (e.which == 8 && caret.begin == 0) {		//backspace at start of field
				_shouldBackspace = true;
			}
			else if (e.which == 46 && caret.begin == $this.val().length) {		//delete at end of field
				_shouldDelete = true;
			}
			else if (e.which == 37 && caret.begin == 0) {		//left arrow at start of field
				_shouldLeft = true;
			}
			else if (e.which == 39 && caret.begin == $this.val().length) {		//right arrow at end of field
				_shouldRight = true;
			};
		};
	}).keypress(function (e) {
		var $this = $(this);
		var partIndex = $_this.index(this);
		if (partIndex < 0) return;
		var partLength = maxLengths[partIndex];
		var caret = $this.caret();
		if (($this.val() + String.fromCharCode(e.which)).length == partLength && caret.begin == $this.val().length) {				//<-- check that cursor is at end of text box or else don't jump. then handle backspaces. delete. arrow keys.
			_shouldJump = true;
		};
	}).keyup(function (e) {
		var $this = $(this);
		var partIndex = $_this.index(this);
		if (_shouldJump) {
			var goToNext = false;
			do {
				partIndex++;
			} while ((goToNext = (partIndex < $_this.length)) && maxLengths[partIndex] <= 0);
			if (goToNext) {
				$_this.eq(partIndex).focus()[0].select();
				window.setTimeout(function () {
					_justJumped = true;
				}, 1);
			};
		}
		else if (_shouldBackspace && --partIndex >= 0) {		//backspace
			var $target = $_this.eq(partIndex).focus();
			var value = $target.val();
			$target.val(value.substring(0, value.length - 1));
			window.setTimeout(function () {
				$target.caret(value.length - 1);
			}, 1);
		}
		else if (_shouldDelete && ++partIndex < $_this.length && maxLengths[partIndex] > 0) {		//delete
			var $target = $_this.eq(partIndex).focus();
			var value = $target.val();
			$target.val(value.substring(1));
			window.setTimeout(function () {
				$target.caret(0);
			}, 1);
		}
		else if (_shouldLeft && --partIndex >= 0) {		//left
			var $target = $_this.eq(partIndex).focus();
			var value = $target.val();
			window.setTimeout(function () {
				$target.caret(value.length - 1);
			}, 1);
		}
		else if (_shouldRight && ++partIndex < $_this.length && maxLengths[partIndex] > 0) {		//right
			var $target = $_this.eq(partIndex).focus();
			var value = $target.val();
			window.setTimeout(function () {
				$target.caret(value.length > 0 ? 1 : 0);
			}, 1);
		}
		else {
			_justJumped = false;
		};
		_shouldJump = _shouldBackspace = _shouldDelete = _shouldLeft = _shouldRight = false;
	}).blur(function (e) {
		_justJumped = false;
	});
};

$(document).ready(function () {
	$(document).delegate("a.newWindow", "click", function (e) {
		var $this = $(this);
		var atts = "";
		if ($.metadata) {
			var supportedAtts = ["resizable", "toolbar", "location", "directories", "status", "menubar", "copyhistory", "fullscreen", "scrollbars", "width", "height", "top", "left"];
			for (var key in $this.metadata()) {
				if (Array.indexOf(supportedAtts, key) >= 0) {
					atts = (atts ? (atts + ",") : "") + key + "=" + $this.metadata()[key];
				};
			}
		};
		window.open($this.attr("href"), null, atts);
		return false;
	});
});

$(document).ready(function() {
	$(document).delegate("a.print", "click", function(e) {
		window.print();
		return false;
	});
});


