var SplashGallery = {
	current: 0,
	last: 0,
	count: 0,
	multi: 10,
	actions: [],
	callerRunning: false,
	autoPlayMode: true,
	autoPlaySpeed: 8e3,
	restartAutoPlayDelay: 10e3,
	restartSent: false,
	hoverOnCaption: false,
	preloaderFallback: 0,
	
	init: function(){
		this.count = $('#sg_elements').children().length;
		this.createNavDots();
		this.bindEvents();
		
		this.preloader();
	},
	
	preloader: function(){
		var loadedImages = 0;
		
		$('#sg_elements').find('img').each(function(){
			if( $(this)[0].complete )
			{
				loadedImages++;
			}
		});
		
		if( loadedImages == SplashGallery.count ||  SplashGallery.preloaderFallback > 30 )
		{
			SplashGallery.startGallery();
		}
		else
		{
			SplashGallery.preloaderFallback++;
			setTimeout(SplashGallery.preloader,200);
		}
	},
	
	startGallery: function(){
		var sgElements = $('#sg_elements');
		
		sgElements.children().hide();
		sgElements.css('visibility','visible');
		sgElements.children().hide().first().fadeIn((150*this.multi));
		
		$('#splash_gallery').css('backgroundImage','none');
		
		setTimeout(SplashGallery.autoPlayer,SplashGallery.autoPlaySpeed);
	},
	
	autoPlayer: function(){
		if( SplashGallery.autoPlayMode && !SplashGallery.hoverOnCaption )
		{
			SplashGallery.goNext();
		}
		setTimeout(SplashGallery.autoPlayer,SplashGallery.autoPlaySpeed);
	},
	
	bindEvents: function(){
		$('#sg_nav div').live('click', function(){
			var _index = $(this).index();

			SplashGallery.stopAutoPlay();
			SplashGallery.addAction(_index);
			SplashGallery.startEvents();
		});
		
		$('#splash_gallery').hoverIntent(
			function(){
				SplashGallery.hoverOnCaption = true;
				SplashGallery.showCaption($('#sg_elements').children().eq(SplashGallery.current));
			},
			function(){
				SplashGallery.hoverOnCaption = false;
				SplashGallery.hideCaption($('#sg_elements').children().eq(SplashGallery.current));
			});
			
		$('.caption').click(function(){
				SplashGallery.goToSlideLink();
			});
			
		$('.arrow').hoverIntent(
			function(){
				var _this = $(this),
					_id   = _this.attr('id');
					
				if( _id == 'arrow_left')
				{
					_this.children().animate({ left: '10px'}, 300);
				}
				else
				{
					_this.children().animate({ right: '10px'}, 300);
				}
			},
			function(){
				var _this = $(this),
					_id   = _this.attr('id');
					
				if( _id == 'arrow_left')
				{
					_this.children().animate({ left: '-30px'}, 300);
				}
				else
				{
					_this.children().animate({ right: '-30px'}, 300);
				}
			})
			.click(function(){
				var _this = $(this),
					_id   = _this.attr('id'),
					goToNext = 0;
				
				SplashGallery.stopAutoPlay();
					
				if( _id == 'arrow_left')
				{
					if( (SplashGallery.current-1) >= 0 )
					{
						// SplashGallery.goTo(SplashGallery.current-1)
						goToNext = SplashGallery.current-1;
					}
					else
					{
						// SplashGallery.goTo((SplashGallery.count-1));
						goToNext = SplashGallery.count-1;
					}
				}
				else
				{
					if( (SplashGallery.current+1) < SplashGallery.count )
					{
						// SplashGallery.goTo(SplashGallery.current+1)
						goToNext = SplashGallery.current+1;
					}
					else
					{
						// SplashGallery.goTo(0);
						goToNext = 0;
					}
				}
				
				SplashGallery.stopAutoPlay();
				SplashGallery.addAction(goToNext);
				SplashGallery.startEvents();
			});
	},
	
	goToSlideLink: function(){
		var currentSlide = $('#sg_elements').children().eq(SplashGallery.current),
			_link = currentSlide.find('a').eq(0);
			
		window.location = _link.attr('href');
	},
	
	stopAutoPlay: function(){
		SplashGallery.autoPlayMode = false;
		if( !SplashGallery.restartSent )
		{
			SplashGallery.restartSent = true;
			setTimeout(SplashGallery.restartAutoPlay,SplashGallery.restartAutoPlayDelay);
		}
	},
	
	restartAutoPlay: function(){
		SplashGallery.restartSent = false;
		SplashGallery.autoPlayMode = true;
	},
	
	createNavDots: function(){
		var parentWidth = $('#sg_nav').width(),
			leftMargin  = Math.round((parentWidth-(this.count*12))/2);
		
		for(i=0; i<this.count; i++)
		{
			$('<div />').appendTo('#sg_nav');
		}
		
		$('#sg_nav').children().first().addClass('active').css('marginLeft',leftMargin+'px');
	},
	
	goTo: function(slide){

		if( slide > this.current )
		{
			this.last = this.current;
			this.current = slide;
			this.updateNav(slide);
			
			$('#sg_elements').children().eq(slide).fadeIn((200*this.multi),function(){
				$('#sg_elements').children().eq(SplashGallery.last).hide();
				SplashGallery.eventCaller();
			});
		}
		else if( slide < this.current )
		{
			this.last = this.current;
			this.current = slide;
			this.updateNav(slide);

			$('#sg_elements').children().eq(this.current).show();
			$('#sg_elements').children().eq(this.last).fadeOut((200*this.multi),function(){
				SplashGallery.eventCaller();
			});
		}
	},
	
	startEvents: function(){
		if( !this.callerRunning )
		{
			this.eventCaller();
		}
	},
	
	eventCaller: function(){
		this.callerRunning = true;
		
		if( this.actions.length > 0 )
		{				
			var nextAction = this.actions.shift();
			this.goTo(nextAction);
		}
		else
		{
			this.callerRunning = false;
		}
	},
	
	addAction: function(action){
		// if( this.actions.length < 3 )
		// 				{
		// 					this.actions.push(action);
		// 				}
		this.actions = [action];
	},
	
	updateNav: function(slide){
		$('#sg_nav').children().removeClass('active').eq(slide).addClass('active');
	},
	
	showCaption: function(slide){
		// var caption = slide.children('.caption');
		var caption = $('.caption');

		caption.children('.backdrop').fadeTo(600,0.8);	
		caption.children('.text').fadeIn(600);
	},
	
	hideCaption: function(slide){
		// var caption = slide.children('.caption');
		var caption = $('.caption');
		
		caption
			.children()
			.fadeOut(500, function(){
				// $(this).css('bottom','-286px');
			});
	},
	
	goNext: function(){
		if( this.autoPlayMode )
		{
			if( (this.current+1) < this.count )
			{
				// this.goTo(this.current+1);
				SplashGallery.addAction(this.current+1);
			}
			else
			{
				// this.goTo(0);
				SplashGallery.addAction(0);
			}
			SplashGallery.startEvents();
		}
	}
};

$(function(){
	Cufon.replace('.text > a',{fontFamily: 'ApexSansBoldT'});
	Cufon.replace('.text > p',{fontFamily: 'ApexSansBookT'});
	SplashGallery.init();
});
