/*
*	Site Plugins
*
*/

(function($) {  
		  
	//	add a spinner to element / $('element').spin()
	$.fn.spin = function(append) {
		if (append)
			$(this).append('<img src="/images/spinner.gif" alt="" />')
		else
			$(this).after('<img src="/images/spinner.gif" alt="" />')
	};

	//	$('element').scrollTo(speed)
	$.fn.scrollTo = function(speed) {
		var offset = $(this).offset().top - 30
		$('html,body').animate({scrollTop: offset}, speed || 1000)
		return this
	};

	//	Block slider
	$.fn.slider = function(mode, speed, time, callback) {
	
		var mode = mode || 'up'; // if mode is undefined, use 'up' as default
		var speed = speed || 'slow'; // if speed is undefined, use 'slow' as default
		var $this = this;
		switch(mode) {
			case 'up':
				setTimeout(function() { $this.slideUp(speed, function() { $(this).remove(); if($.isFunction(callback)) { callback.call(this, $) } }) }, (time * 1000));
				break;
			case 'down':
				setTimeout(function() { $this.slideDown(speed, function() { $(this).remove(); if($.isFunction(callback)) { callback.call(this, $) } }) }, (time * 1000));
				break;
		}
	};

	//	replace an element plugin
	$.fn.replaceWith = function(o)
	{ 
		return this.after(o).remove(); 
	};
	
	//	Resets the form data.  Causes all form elements to be reset to their original value.
	$.fn.reset_form = function() {
		return this.each(function() {
			// guard against an input with the name of 'reset'
			// note that IE reports the reset function as an 'object'
			if (typeof this.reset == 'function' || (typeof this.reset == 'object' && !this.reset.nodeType))
				this.reset();
		});
	};
	$.fn.clearForm = function() {
		$( this ).
			find( ':text, :password, textarea' ).
			attr( 'value', '' ).end().
			find( ':checkbox, :radio' ).
			attr( 'checked', false ).end().
			find( 'select' ).
			attr( 'selectedIndex', -1 );
	}
	
	//	ajax submit
	$.fn.askus = function(options) {  
		var obj = this;
		var form = $(obj).prev().prev().filter('form');
		var title = $(obj).children().filter('strong').attr('title');
		var loader = $(obj).attr('title');
		var url = $(form).attr('action');
		var method = $(form).attr('method');
		
		$(form).submit(
			function() {
			
				$(obj).addClass('loader');
				$(obj).children().filter('strong').text(loader);
				
				$.ajax({
					url:  url,
					type: method,
					data: $(form).serialize(),
					cache: false,
					timeout: 3000,
					error: function(xhr, desc, e) {
						$(obj).removeClass('loader');
						strError = "Unable to submit form. Please try again later.";
						console.log(strError);
						alert("The following errors were encountered:\n" + strError + "\n" + desc);
					},
					success: function(r) {
						var res = $(form).next().filter('div');
						$(obj).removeClass('loader');
						$(obj).children().filter('strong').text(title);
						$(res).html(r)
						$(res).children().filter('p').slider('up', 'slow', 3);
						var ser = $(res).children().filter('span')
						if( (r+'').indexOf('mess=ok') > -1 ) { $(form).reset_form(); }
					}
				});			
				return false; 
			}
		)
	
		$(obj).click(
			function() {
				$(form).trigger('submit');
				return false;
			}
		)
	};
	
})(jQuery);

