/*
*	Site Plugins
*
*/

(function($){ 
	$.fn.extend({
				
		//	add a spinner to element / $('element').spin()
		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)
		scrollTo: function(speed) {
			var offset = $(this).offset().top - 30
			$('html,body').animate({scrollTop: offset}, speed || 1000)
			return this
		},



		//	Block slider
		slider: function(mode, speed, time) {
		
			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(); }) }, (time * 1000));
					break;
				case 'down':
					setTimeout(function() { $this.slideDown(speed, function() { $(this).remove(); }) }, (time * 1000));
					break;
			}
		},

		//	replace an element plugin
		replaceWith: function(o)
		{ 
			return this.after(o).remove(); 
		},
		
		//	Resets the form data.  Causes all form elements to be reset to their original value.
		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();
			});
		},
		
		//	Changing articles font size
		changeTextSize: function(content){
			var obj = this;
			var whichLink = $(obj).children().filter('a');
			$('.sml').addClass('active');

			return whichLink.each(function() {
				$(this).click(function(){
					var className = $(this).attr('class');
					var whichLinkClass = 'a.'+(className).substring(0,3);
					$(content).removeClass('sml').removeClass('med').removeClass('lrg').addClass(className);
					$(whichLink).removeClass('active');
					$(whichLinkClass).addClass('active');
					return true;
				})
			})
		},
	
		//	ajax submit
		contactform: 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;
				}
			)
		},
	
		//	accordion
		accordion: function(options) {  
			var obj = this;
			
		//	$('blockquote:not(.first)',obj).hide();
			$('blockquote.first',obj).show();
			$('h2', obj).hover(function(){ $(this).addClass('hover') }, function(){$(this).removeClass('hover')})
			$('h2', obj).click(
				function() {
					var eblock = $(this).next();
					if((eblock.is('blockquote')) && (eblock.is(':visible'))) {
						return false;
					}
					if((eblock.is('blockquote')) && (!eblock.is(':visible'))) {
						$('blockquote:visible', obj).slideUp('normal');
						eblock.slideDown('normal');
						return false;
					}
				}
			)
		},
	
		//	live date
		livedata: function(options) {  
			var defaults = {  
				url: 'http://www.google.com/',
				timer: 15,
				cookie_name: 'live',
				cookie_expires: 5,
				animate: false
			};
			var options = $.extend(defaults, options);
			var live_interval;	
			
			var obj = $(this);
			var state = getState();
		
			function load_live_data() {
				clearTimeout(live_interval);
				$(obj).load(options.url, function() {
					$(obj).css({'background':'transparent'});
					var last_id = $('span:eq(0):hidden', obj).html();
					if(state.last_id != last_id ) {
						state.last_id = last_id;
						setState();
						if(options.animate)	setTimeout(animate, 1000);
					}
					live_interval = setTimeout(load_live_data, (options.timer * 1000));
				});
			}
			load_live_data();
			
			function setState() {
				var props = [];
				for ( entry in state ) {
					var value = ( state[ entry ] && state[ entry ].constructor === String ) ? '"' + state[ entry ] + '"' : state[ entry ]; 
					props.push( entry + ':' + value );
				}
				props = props.join( ',' );
				
				var expiration = new Date();
				expiration.setDate( expiration.getDate() + options.cookie_expires );
				document.cookie = [ 'livedata={', props, '}; expires=', expiration.toUTCString() ,';' ].join( '' );
			};
			function getState() {
				var re = new RegExp( /livedata=({[^;]+})(;|\b|$)/ );
				var match = re.exec( document.cookie );
				return ( match && match[ 1 ] ) ? eval( '(' + match[ 1 ] + ')' ) : { last_id:null };
			};
			function animate(){ 
				$('.first_one', obj).animate( { opacity: 0 }, { duration:300 } ).animate( { opacity:1 }, 300 ).animate( { opacity: 0 }, { duration:300 } ).animate( { opacity:1 }, 300 ).animate( { opacity: 0 }, { duration:300 } ).animate( { opacity:1 }, 300 ); 
			};
				
		}
	
	}); 
})(jQuery);

