(function($){
	
	$.fn.exists = function(){return $(this).length>0;}
			
	$(document).ready(function()
	{
		$('.footer .brands').slide_brands();
		$('.nav').a_hover_effect();
		$('.slideshow').add_slideshow();
	});
	
	$.fn.a_hover_effect = function()
	{
		var $nav = $(this);
		var $buttons = $nav.find('a');
		
		$buttons.append('<em></em>');
		
		$buttons.hover(function()
		{
			$(this).find('em').text($(this).attr('title'));
			$(this).find('em').stop(true, true).animate({opacity:'show', marginTop: '-75px'}, 'slow');	
		}, 
		function()
		{
			$(this).find('em').stop(true, true).animate({opacity:'hide', marginTop: '-85px'}, 'fast');
		});
	}
	
	$.fn.slide_brands = function()
	{
		var div = $(this);
		var a_left = div.find('a.left');
		var a_right = div.find('a.right');
		var inner = div.find('.inner');
		var ul = div.find('ul');
		var divWidth = div.width();
		var lastLi = ul.find('li:last-child');
		var firstLi = ul.find('li:first-child');
		var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth();
		
		var maxScroll = ulWidth - divWidth;
		var minScroll = 0;
		
		// scroll left
		a_left.click(function(){
			if(inner.scrollLeft() > 0 )
			{
				// find amount over the edge
				var scrollAmount = ulWidth - divWidth;
				if(scrollAmount > divWidth){scrollAmount = divWidth;}
				div.find('.inner').stop().animate({'scrollLeft' : inner.scrollLeft()-scrollAmount}, 1000);
			}
			return false;
		});
		
		// scroll right
		a_right.click(function(){
			if(inner.scrollLeft() < maxScroll )
			{
				// find amount over the edge
				var scrollAmount = maxScroll;
				if(scrollAmount > divWidth){scrollAmount = divWidth;}
				inner.stop().animate({'scrollLeft' : inner.scrollLeft()+scrollAmount}, 1000);
			}
			return false;
		});
		
		if(ulWidth < divWidth)
		{
			a_left.hide();
			a_right.hide();
		}
	
	}
	
	$.fn.add_slideshow = function() 
	{
		// if no buttons, return true and do nothing
		
		var $slidshow = $(this);
		
		function slideSwitch() 
		{
			var $active = $slidshow.find('.active');
			if ( $active.length == 0 ) $active = $slidshow.find('.slide:last');
		
			var $next =  $active.next().length? $active.next(): $slidshow.find('.slide:first');
		
			$active.addClass('last-active');
		
			$next.css({opacity: 0.0})
				.addClass('active')
				.animate({opacity: 1.0}, 1000, function() {
					$active.removeClass('active last-active');
				});
		}
		setInterval(function(){slideSwitch();}, 5000);
		
	}
		
})(jQuery);

