﻿Array.prototype.remove = function(from, to) {
	var rest = this.slice((to || from) + 1 || this.length);
	this.length = from < 0 ? this.length + from : from;
	return this.push.apply(this, rest);
};

(function($) {
	$.fn.hoverEffect = function() {
		return this
			.bind("mouseenter", function() { $(this).addClass("hover"); })
			.bind("mouseleave", function() { $(this).removeClass("hover"); });
	};
	
	$.fn.button = function() {
		return this.each(function() {
			var $button = $(this);
			
			$button.hover(function() {
				$("button,a.button").removeClass("hoverButton");
				var $self = $(this);
				if (!$self.is(":disabled")) {
					$self.addClass("hoverButton");
				}
			},
			function() {
				$(this).removeClass("hoverButton");
			});
			
			$.fn.extend({
				setWorking: function(value) {
					var $self = $(this);
					var $img = $("img", $self);
					
					$self.attr("disabled", value);
					
					if (value) {
						$img.data("originalsrc", $img.attr("src"));
						$img.attr("src", imageurl("app/working-button.gif"));
					} else {
						$img.attr("src", $img.data("originalsrc"));
						$img.removeData("originalsrc");
					}
				}
			});

		});
	};
	
	$.fn.zenform = function(options) {
		options = options || {};
		options.validate = options.validate || {};
		options.ajax = options.ajax || {};
		
		return this.each(function() {
			var $form = $(this);
			
			$form.validate(options.validate);
			$form.submit(function() {
				if (!$form.valid()) {
					return false;
				}
				
				var $button = $("button[type=submit]", $form);
				$button.setWorking(true);
				
				var ajaxOptions = $.extend({}, options.ajax, {
					complete: function() {
						$button.setWorking(false);
						if (typeof(options.ajax.complete) == "function") {
							options.ajax.complete();
						}
					}
				});
				
				$form.ajaxSubmit(ajaxOptions);
				return false;
			});
		});
	};
		
	jQuery(function() {

		$(":input")
			.focus(function() { $(this).addClass("selected"); })
			.blur(function() { $(this).removeClass("selected"); });
			
		$("button,a.button,input[type=button]").button();
		
	});
		
})(jQuery);

