/*
*	Site jQuery 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
	};

	$.fn.idle = function(time) { 
		var time = time || 1000;
		var o = $(this); 
		o.queue(function() { 
			setTimeout(function() { o.dequeue(); }, time);
		});
		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(); 
	};
	

	//	hotels minimenu tabbing
	$.fn.menutabs = function()
	{ 
		//	Do the actual thing, yeah?
		return this.each(function() {

			var obj = $(this);
			var hash = window.location.hash.replace('#', '');
			
			var showtab = function (){
				var tabIndex = $('ul:first li a', obj).removeClass('active').index(this);
				$(this).addClass('active').blur();
				$('div.tab', obj).hide().eq(tabIndex).show();
			};

			var currentTab = $('ul:first li a', obj)
				.bind('click', showtab)
				.filter('a[rel=' + hash + ']');

			if (currentTab.size() == 0) {
				currentTab = $('li a:first', obj);
			}
			showtab.apply(currentTab.get(0));
		})
	};

	/*
	 * jQuery Slideshow Plugin v1.3
	 * Author: Matt Oakes
	 * URL: http://www.matto1990.com/jquery/slideshow/
	 *
	 * Modifications by Meinhard Benn (http://benn.org/):
	 *  - fadetime setting
	 */
	
	$.fn.slideshow = function(options) {
		var settings = {
			fadetime: 'slow',
			timeout: '2000',
			type: 'sequence',
			pauselink: null,
			playcallback: null,
			pausecallback: null
		};
		if (options) {
			$.extend(settings, options);
		}
		
		var	pauseState = 0,
			current = 1,
			last = 0,
			timer = '';
		
		var change = function () {
			if ( pauseState == 0 ) {
				for (var i = 0; i < slides.length; i++) {
					$(slides[i]).css('display', 'none');
				}
				$(slides[last]).css('display', 'block').css('zIndex', '0');
				$(slides[current]).css('zIndex', '1').fadeIn(settings.fadetime);
				
				if ( settings.type == 'sequence' ) {
					if ( ( current + 1 ) < slides.length ) {
						current = current + 1;
						last = current - 1;
					}
					else {
						current = 0;
						last = slides.length - 1;
					}
				}
				else if ( settings.type == 'random' ) {
					last = current;
					while (	current == last ) {
						current = Math.floor ( Math.random ( ) * ( slides.length ) );
					}
				}
				else {
					alert('type must either be \'sequence\' or \'random\'');
				}
				timer = setTimeout(change, settings.timeout);
			}
		};
		
		var pause = function() {
			if ( pauseState == 0 ) {
				pauseState = 1;
				clearTimeout(timer);
				if ( settings.playcallback != null ) {
					settings.pausecallback($(settings.pauselink));
				}
			}
			else {
				pauseState = 0;
				change();
				if ( settings.playcallback != null ) {
					settings.playcallback($(settings.pauselink));
				}
			}
			return false;
		};
		
		this.css('position', 'relative');
		var slides = this.find('li').get();
		$.each(slides, function(i){
			$(slides[i]).css('zIndex', slides.length - i).css('position', 'absolute').css('top', '0').css('left', '0');
		});
		if ( settings.type == 'sequence' ) {
			timer = setTimeout(change, settings.timeout);
		}
		else if ( settings.type == 'random' ) {
			do {
				current = Math.floor ( Math.random ( ) * ( slides.length ) );
			} while ( current == 0 );
			timer = setTimeout(change, settings.timeout);
		}
		else {
			alert('type must either be \'sequence\' or \'random\'');
		}
		
		if ( settings.pauselink != null ) {
			$('#' + settings.pauselink).click(pause);
		}
		
		return this;
	};
	
})(jQuery);
