
/**
* Site behaviors by body#controller.action
*/
$(document).ready(function ()
{
	/**
	* Confirm links.
	*/
	$('a.remove').click(function (event)
	{
		if (!confirm(this.title+'?'))
		{
			event.preventDefault();
		}
	});

	/**
	* Load lists.
	*/
	/*$('.paginator').eachUnique(function ()
	{
		$(this).css({opacity: .5}).addClass('paginator-loading')
			.load(window.location.href, function ()
			{
				$(this).css({opacity: 1}).removeClass('paginator-loading');
			});
	});*/
	$('a.fancybox').fancybox();
});

/**
* jQuery pugins.
*/
jQuery.fn.extend(
{
	/**
	* Allows a window to scroll to an element.
	*/
	scrollTo: function (speed, easing)
	{
		return this.each(function ()
		{
			var targetOffset = $(this).offset().top - 20;
			$('html,body').animate({scrollTop: targetOffset}, speed, easing);
		});
  	},

  	/**
  	* Checks and notes whether an element has behaviors already.
  	*/
  	bindUnique: function (event, callback)
  	{
  		//this.unbind(event, callback);
  		//return this[event](callback);

  		return this.each(function ()
  		{
  			var key = new String(callback).replace(/[^a]/, '');
  			this.__uniqueEvents = this.__uniqueEvents || [];
  			if (jQuery.inArray(key, this.__uniqueEvents) == -1)
  			{
  				$(this)[event](callback);
  				this.__uniqueEvents[this.__uniqueEvents.length] = key;
  			}
  		});
  	},
  	
  	/**
  	* Apply a method to elements only once
  	*/
  	eachUnique: function (callback)
  	{
  		return this.each(function ()
  		{
  			var key = new String(callback).replace(/[^a]/, '');
  			this.__uniqueCallbacks = this.__uniqueCallbacks || [];
  			if (jQuery.inArray(key, this.__uniqueCallbacks) == -1)
  			{
  				callback.call(this);
  				this.__uniqueCallbacks[this.__uniqueCallbacks.length] = key;
  			}
  		});
  	},

	/**
	* Validate a simple JS form.
	*/
	validate: function ()
	{
		var result = true;

		// Required.
		this.find('div.required input, div.required textarea, div.required select, input.required, textarea.required, select.required').each(function ()
		{
			if (!$(this).attr('disabled') && !$(this).val())
			{
				result = false;
				$(this).css({backgroundColor: '#FFAABB'}).animate({backgroundColor: 'white'});
			}
		});

		return result;
	}
});
