/**
 * WPL shared jquery stuff
 */
(function($) {
	/**
	 * Block selector
	 *
	 * Used by the time block selector form item
	 */
	$.fn.makeBlockSelector = function(blocked, input, options) {
		options = $.extend( {
			containerClass: 'blockSelectorContainer',
			availableClass: 'available',
			blockedClass: 'blocked',
			blockNum: 96
		}, options || {});
		
		return this.each( function() {
			var container = this;
		
			// add in blocks
			for (var i = 0; i < options.blockNum; i++) {
				if ($.inArray(i, blocked) !== -1) {
					$('<div></div>').appendTo(container).addClass(options.blockedClass);
				} else {
					$('<div></div>').appendTo(container).addClass(options.availableClass)
				}
			}
			
			$(container)
				.addClass(options.containerClass)
				.selectable({
					filter: '.' + options.availableClass,
					stop: function() {
						var result = '';
						$(".ui-selected", this).each(function(){
							var index = $(container.childNodes).index(this);
							if (result.length !== 0) {
								result += ',';
							}
							result += index;
						});
						input.val(result);
					}
				});
		});
	};
	
	
	// Private function for closing the tooltip box
	var closeTooltip = function () {
		$("#wplTooltip").hide();
	};
	
	// Are the tooltips active?
	var $toolsActive = true;
	
	/**
	 * Activate the tooltips
	 */
	$.fn.wplTooltipsActivate = function() {
		$toolsActive = true;
	}
	
	/**
	 * Deactivate all tooltips
	 */
	$.fn.wplTooltipsDeactivate = function() {
		$toolsActive = false;
		closeTooltip();
	}
	
	/**
	 * Tooltip
	 *
	 * A tooltip which can contain links etc.
	 */
	$.fn.wplTooltip = function(options) {
		options = $.extend({
			content: function (el) {
				return $('.tooltipContent', el).html();
			},
			tooltipOn: function () {},
			tooltipOff: function () {},
			offset: {
				top: 20,
				left: 0
			},
			useHoverIntent: false
		}, options || {});
		
		if ($("#wplTooltip").size() == 0) {
			// Initialise the tooltip markup which we reuse (there can only ever 
			// be one tooltip shown at a time)
			$("body").append('<div id="wplTooltip"></div>');
			$("#wplTooltip")
				.css({position: "absolute", "z-index": "1001", "display": "none"})
				.mouseleave(closeTooltip);
		}

		// Add the close handler from these particular tooltips
		$("#wplTooltip").mouseleave(options.tooltipOff);
		
		// This is the mouseover we attach to the elements which are trigging
		// the tooltip
		var triggerElementMouseover = function(e) {
			if (!$toolsActive) {
				return;
			}
			var offset = $(this).offset();

			$("#wplTooltip")
				.html(options.content(this));
				
			$("#wplTooltip")
				.css({"left": offset.left + options.offset.left, "top" : offset.top + options.offset.top})
				.show();
				
			options.tooltipOn(this);
		}
		
		var triggerElementMouseout = function(e) {
			// close tooltip if we didn't mouse onto it
			if (!e.relatedTarget || (e.relatedTarget.id != 'wplTooltip' && $(e.relatedTarget).parents('#wplTooltip').size() == 0)) {
				closeTooltip();
				options.tooltipOff();
			}
		}
		if (options.useHoverIntent) {
			return this.hoverIntent(triggerElementMouseover, triggerElementMouseout);
		} else {
			return this.hover(triggerElementMouseover, triggerElementMouseout);
		}
	}
	
	// Attach one of those annoying messages into a form field which disappear
	// when you click into it
	$.fn.attachFormElMessage = function (message) {
		var $this = this;		
		$(document).ready(function()
		{
			if (!$this.val()) {
				$this.val(message);
			}
		});

		this.bind('blur', function() {
			if (!$this.val()) {
				$this.val(message);
			}
		});

		this.bind('focus', function() {
			if ($this.val() == message) {
				$this.val('');
			}
		});

		if (this.wplFormItem) {
			// the form element is associated with a wplFormItem, so we attach a special 
			// validator to disallow submission in the case of default message
			// not being changed!
			this.wplFormItem.addValidator(
				new WplFormValidators.validators.notEqual(message, "'" + message + "' must be changed!")
			);
		}

		return this;
	}
})(jQuery);
