
jQuery(document).ready(function() {
	slider.init();
	submenu.init();
	finder.init();
})

/*
 * find button, click slides down the form
 */
var finder = {
	status : 0,
	init: function() {
		jQuery('#finder').bind('click',function() {
			if( !finder.status ) /* when on the result page, prevent slidetoggling */
				jQuery('#find').slideToggle();
			return false;
		})
	}
}

var slider = {

	/*
	 * different racing speeds
	 */
	speed : {
		reset	 : 500,
		expand   : 750,
		contract : 750
	},

	/**
	 * start up the engine, BRUM
	 */
	init: function() {
		jQuery('#slider a').hover(
		  function () {
			slider.expand( parseInt( jQuery(this).attr('rel') ) );
			slider.contracters( parseInt( jQuery(this).attr('rel') ) );
		  },
		  function () {
			slider.reset();
		  }
		);
	},

	/**
	 * find the slides if we know which one not to find
	 */
	contracters : function ( id ) {
		if( id != 1 ) { slider.contract( 1 ) }
		if( id != 2 ) { slider.contract( 2 ) }
		if( id != 3 ) { slider.contract( 3 ) }
		if( id != 4 ) { slider.contract( 4 ) }
	},

	/**
	 * expand a single slide
	 */
	expand : function( id ) {
		jQuery('#s' + id ).stop().animate({
			width : '410px'
		},slider.speed.expand,'easeOutQuad').find('span').css({
			backgroundPosition : '0 0'
		});
	},

	/**
	 * make a single slide smaller
	 */
	contract : function( id ) {
		jQuery('#s' + id ).stop().animate({
			width : '180px'
		},slider.speed.contract,'easeOutQuad').find('span').css({
			backgroundPosition : '0 0'
		});
	},

	/**
	 * reset the slider
	 */
	reset : function () {
		jQuery('#slider a').stop().animate({
			width: '237px'
		},slider.speed.reset,'easeOutQuad').find('span').css({
			backgroundPosition : '0 0'
		});
	}

}

var submenu = {

	timer : [],

	width : 0,

	init: function() {

		jQuery('.sm').each(function() {
			var link = jQuery(this);
			var i = parseInt( link.attr('rel') );
			var menu = link.parent().find('ul');
			link.hover(function() {
				clearTimeout( submenu.timer[i] );
				link.addClass('cur');
				menu.fadeIn();
			},function() {
				submenu.timer[i] = setTimeout(
					function(){
						link.removeClass('cur');
						menu.hide();
					},
					250
				);
			});
			menu.hover(function() {
				clearTimeout( submenu.timer[i] );
			},function() {
				submenu.timer[i] = setTimeout(
					function(){
						link.removeClass('cur');
						menu.hide();
					},
					250
				);
			});
		});

	}

}

var timer = [];
var width = 0;
var i;
jQuery('.sm').each(function() {
 var link = jQuery(this);
 var menu = jQuery(this).find('ul');
 menu.css('left',width);
 width = width + link.outerWidth() + 10;
 link.hover(function() {
	menu.fadeIn();
 },function() {
	timer[i] = setTimeout(
		function(){
			menu.hide();
		},
		250
	);
 });
 menu.hover(function() {
	clearTimeout( timer[i] );
 },function() {
	timer[i] = setTimeout(
		function(){
			menu.hide();
		},
		250
	);
 });
 i++;
});

/*
 * Smoother animations, prot prot prot -> prrrrrr
 */
jQuery.extend( jQuery.easing,{
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	}
})


