// Cross browser mechanism for deselecting any selection
function clearSelection() {
	var sel;
	if(document.selection && document.selection.empty) {
		document.selection.empty();
	} else if (window.getSelection) {
		sel = window.getSelection();
		if (sel && sel.removeAllRanges) {
			sel.removeAllRanges();
		}
	}
}

;(function($) {
	$.fn.tooltip = function(settings) {
		var options = {
			positions: ["right"],
			shadow: true,
			padding: 10,
			width: 400,
			spikeLength: 10,
			spikeGirth: 10,
			cornerRadius: 20,
			fill: 'rgba(200, 200, 200, .8)',
			strokeWidth: 1,
			strokeStyle: '#888',
			cssStyles: {
				color: '#FFF', 
				fontWeight: 'bold', 
				fontSize: "90%", 
				textAlign: "center"
			}
		};

		$.extend(true, options, settings);
		
		if (settings) { $.extend(options, settings); }; 
		this.bt(options);
		
		return this;
	};
})(jQuery);

function uiHoverOn (e) {
	$(this).uihover(true);
}
function uiHoverOff (e) {
	$(this).uihover(false);
}

$.fn.uihover = function(toggle) {
	this.toggleClass('ui-state-hover', toggle);
	return this;
};
$.fn.uihighlight = function(toggle) {
	this.toggleClass('ui-state-highlight', toggle);
	return this;
};
$.fn.uiselected = function(toggle) {
	this.toggleClass('ui-state-selected', toggle);
	return this;
};

$.fn.log = function (msg) {
	return this;
};

var buttonise = function() { 
	$("input[type=submit], input[type=button], button, a.button").button(); 
};

$(document).ajaxSuccess(buttonise);
$(buttonise);

Date.prototype.toTmDateString = function() {
	return this.toString('d/MM/yyyy');
};

Date.parseTmDateString = function(d) {
	return this.parseExact(d, 'd/MM/yyyy');
};

Date.prototype.toGrailsDateString = function() {
	return this.toString('yyyy-MM-dd HH:mm:ss.0');
};

Date.prototype.toLongDateString = function() {
	return this.toString('dddd, MMMM d yyyy');
};

$.extend($.bt.defaults, {
		padding: 10,
		// width: 100,
		spikeLength: 10,
		spikeGirth: 10,
		cornerRadius: 10,
		fill: 'rgba(0, 0, 0, .8)',
		strokeWidth: 2,
		strokeStyle: '#999',
		cssStyles: {color: '#FFF', fontWeight: 'bold'}
});

$.fn.edit_in_place = function(callback){
	var $element = this;
	if($element.length>1){console.error("Call $().edit_in_place only on a singular jquery object.");}
	var $edit = $('<input type="text" class="edit_in_place" />');
	$edit.css({'height' : $element.height()-2, 'width' : $element.width()-2});
	$element.hide();
	$element.after($edit);
	$edit.focus();
	
	var save = function(e) {
		e.preventDefault();
		$edit.hide();
		$element.show();
		if($edit.val()!=='') callback($element, $edit.val());
		// This causes a TypeError, no idea why! I'd rather use it...
		// callback.apply($element, $edit.val());
		$edit.remove();
	};
	
	var discard = function(e) {
		$edit.remove();
		$element.show();
	};
	
	$edit.bind('blur', function(e) { // on blur, forget edits and reset.
		save(e);
	}).keydown(function(e){
		if(e.which===27) discard(e);
		if(e.which===13 || e.which===9){ // Enter or Tab: run the callback with the value
			save(e);
		}
	}).click(function(e) {
		e.preventDefault();
		e.stopPropagation();
	});
};

function roundDownToTwoPlaces (number) {
	return Math.floor(number * 100) / 100;
}

function openA4Window(name, href) {
	window.open(href, name, "status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=1,scrollbars=1height=600px,width=810px");
}