$(function ()
{
	
	var opts = {'closedSlideWidth' : '60' };
	var slideWidget = $('#mainVisual');
	var slides = $('#mainVisual .slide');
    var speed = 1000;

	var slideWidth = slideWidget.width();
	var zIndexStart = 110;
	var startPos = 'r';
	var test = 0;
	
	if (slides.length)
	{
		slides.each(function (i)
		{
			if (i > 0)
			{
				var switchIndex = slides.length - i;
				var myLeft = (slideWidth - (opts.closedSlideWidth * switchIndex)) + 'px';

				$(this).css('left', myLeft);
				$(this).css('zIndex', zIndexStart + i + 1);
				startPos = 'r';
				$(this).addClass('right').find('h3').css('opacity', 0);
			}
			else
			{
				startPos = 'l';
				$(this).addClass('left').find('h3').css('opacity', 0.75);
			}
			$(this).data('index', i).data('pos',startPos);	
		}).end();
		
		slides.bind('gotoTarget',  function ()
		{
			var myTarget = $(this).find('a:first');
			if(myTarget.length)
			{
				window.location = myTarget.attr('href');	
			}
		});
		
		slides.slice(1).bind('flick', function ()
		{
			var myIndex = $(this).data('index');
			var myLeft;

			if (myIndex == 0)
			{
				return;	
			}

			var switchIndex = slides.length - myIndex;

			if ($(this).data('pos') == 'r')
			{
				myLeft = (opts.closedSlideWidth * myIndex) + 'px';
				$(this).removeClass('right').addClass('left').data('pos', 'l');
			}
			else
			{
//				myLeft = (slideWidth - (opts.closedSlideWidth * myIndex)) + 'px';
				myLeft = (slideWidth - (opts.closedSlideWidth * switchIndex)) + 'px';
				$(this).removeClass('left').addClass('right').data('pos', 'r');
			}
			if ($(this).is(':animated'))
			{
				$(this).stop();	
			}
			$(this).animate({'left': myLeft}, speed);
		}).end();

		slides.click(function ()
		{
			$(this).trigger('gotoTarget');
		});
		
		slides.mouseover(function ()
		{

			/* Change the "Active" slide */
			var activeSlide = slides.filter('.active').removeClass('active');
			activeSlide.find('h3').stop().animate({'opacity' : 0});
			slides.end(); // restore slides collection to unfiltered (full) set
			$(this).addClass('active').find('h3').stop().animate({'opacity' : 0.75});

			var myIndex = $(this).data('index');
			var i = 0;
			var selSlide = false;
			
			if(myIndex == 0)
			{
				slides.slice(1).filter(function () { return $(this).data('pos') == 'l'; }).trigger('flick').end();
				return;
			}
			
			if ($(this).data('pos') == 'r')
			{
				for(i = myIndex; i > 0; i--)
				{
					var selSlide = slides.filter(':eq(' + i + ')');
					if(selSlide.data('pos') != 'r')
					{
						break;	
					}
					selSlide.trigger('flick');
				}
			}
			else
			{
				slides.slice(myIndex + 1).filter(function () { return $(this).data('pos') == 'l'; }).trigger('flick').end();
			}
		});
	}
	
	/* Setup special fading buttons on the homepage */
	var homeFades = $('.homeFade');
	if (homeFades.length)
	{
		homeFades.append('<div class="fade-overlay"></div>');
		homeFades.find('.fade-overlay').css('opacity', 0);
		
		homeFades.bind('darken', function () 
		{
			$(this).find('.fade-overlay').stop().animate({'opacity': .5});
			//$(this).find('a p').stop().animate({'top': '55px'});
			
		});
		homeFades.bind('lighten', function () 
		{
			$(this).find('.fade-overlay').stop().animate({'opacity': 0});
			//$(this).find('a p').stop().animate({'top': '65px'});
		});
		homeFades.hover(function () { $(this).trigger('darken'); }, function () { $(this).trigger('lighten'); });
		homeFades.click(function () 
		{
			var myHref = $(this).find('a:first').attr('href');
			window.location = myHref;
			return false;
		});
	}	
	
	/* Setup rotating button on the homepage */
	var rotator = $('#homepageMenu .rotator');
	if (rotator.length)
	{
		rotator.data('index', 0);	
		var rotatorSlides = rotator.find('a');
		rotator.data('max', rotatorSlides.length - 1);
		/* Hide All But First Slide */
		rotatorSlides.slice(1).fadeOut(0).end();

		if (rotatorSlides.length > 1)
		{
			setInterval('rotateHomeButton()', 3000);
		}
	}
	
	rotator.click(function ()
	{
		var myHref = $(this).find('a:eq(' + curIndex + ')').attr('href');
		window.location = myHref;
		return false;		
	});

});

function rotateHomeButton()
{
	var rotator = $('#homepageMenu .rotator');
	var curIndex = rotator.data('index');
	var nextIndex = curIndex + 1;
	if (nextIndex > rotator.data('max'))
	{
		nextIndex = 0;	
	}
	var curSlide = rotator.find('a:eq(' + curIndex + ')');
	var nextSlide = rotator.find('a:eq(' + nextIndex + ')');
	curSlide.fadeOut();
	nextSlide.fadeIn();
	rotator.data('index', nextIndex);
}
