$(document).ready(function(){

	// add delay function..
	jQuery.fn.delay = function(time,func){
	    this.each(function(){
	        setTimeout(func,time);
	    });
	    return this;
	};

	// ----------------------->
	
	var anim = {
		
		// animation properties..
		showDuration: 6000,
		fadeDuration: 1000, 
		slideDuration: 800,
			
		// initialize function..
		init: function() {
			// Initialize the animation..
			// console.log( 'Animation :: Initailizing...' );
			
			$.each( $('#header-anim img'), function( index, value ) {
				var src = $(value).attr('src');
				$(value).load( function() { $(this).addClass('loaded'); } );
				$(value).attr( 'src', src + '#1' );
			} );
		},
	
		// start the animation..
		start: function() {
			// Set the animation to the beginning..
			// console.log( 'Animation :: Starting animation...' );
			
			// reset photos..
			$('#header-anim .photos img').css( 'display', 'none' );
			$('#header-anim .photos').css( 'display', 'block' );
			
			// reset logo..
			$('#header-anim .logo').css( 'display', 'none' );
			
			// reset text..
			$('#header-anim .text p').css( 'display', 'none' );
			$('#header-anim .text p:first-child').css( 'display', 'block' );
			$('#header-anim .text p:first-child').addClass('current');
			$('#header-anim .text').css( 'display', 'block' );
			$('#header-anim .text').css( 'right', -( $('#header-anim .text').width() ) );
			
			
			var loadedPhotos = $('#header-anim .photos img.loaded');
			
			if( loadedPhotos.length > 3 ) {
				// animation quick start..
				$('#header-anim .loading').hide();
				$('#header-anim .logo').fadeIn( anim.fadeDuration, 'easeOutSine' );
				$('#header-anim .photos img.loaded:first-child').addClass('current');
				$('#header-anim .photos img.loaded:first-child').fadeIn( anim.fadeDuration, anim.animSlideTextIn );
				$('#header-anim').delay( anim.showDuration, function() { anim.go() } );
			} else {
				this.waitForPhotos();
			}
		},
		
		waitForPhotos: function() {
			var loadedPhotos = $('#header-anim .photos img.loaded');
			if( loadedPhotos.length > 3 ) {
				// animation slow start..
				$('#header-anim .loading').fadeOut( anim.fadeDuration, function() {
					$('#header-anim .logo').fadeIn( anim.fadeDuration, 'easeOutSine' );
					$('#header-anim .photos img.loaded:first-child').addClass('current');
					$('#header-anim .photos img.loaded:first-child').fadeIn( anim.fadeDuration, anim.animSlideTextIn );
					$('#header-anim').delay( anim.showDuration, function() { anim.go() } );
				} );
			} else {
				$('#header-anim').delay( 500, function() { anim.waitForPhotos() } );
			}
		},
		
		// advance to next slide.. various animations..
		go: function() {
			// console.log('Animation :: Advancing...');
			this.animSlideTextOut( function() {
				anim.nextText();
				anim.animSlideTextIn();
				anim.animPhoto();
				
				$('#header-anim').delay( anim.showDuration, function() { anim.go() } );
			} );
		},
		
		testTextSlides: function() {
			this.animSlideTextOut( function() {
				anim.animSlideTextIn();
			});
		},
		
		// do the text, slide-in animation..
		animSlideTextIn: function( cb ) {
			var textWidth = $('#header-anim .text').width();
			$('#header-anim .text').css( 'right', -textWidth );
			$('#header-anim .text').animate( {right:0}, anim.slideDuration, 'easeOutSine', cb );
		},
		
		// do the text, slide-out animation..
		animSlideTextOut: function( cb ) {
			var textWidth = $('#header-anim .text').width();
			$('#header-anim .text').animate( {right:-textWidth}, anim.slideDuration, 'easeInSine', cb );
		},
		
		// make the next piece of text visible..
		nextText: function() {
			var currTxt = $('#header-anim .text p.current');
			var nextTxt = this.nextElement(currTxt);
			
			currTxt.css('display','none').removeClass('current');
			nextTxt.css('display','block').addClass('current');
		},
		
		animPhoto: function( cb ) {
			var currImg = $('#header-anim .photos img.current');
			
			var nextImg = currImg.next('.loaded');
			if( nextImg.length == 0 ) {
				nextImg = $('#header-anim .photos img.loaded:first-child');
			}
			
			currImg.fadeOut( anim.fadeDuration );
			nextImg.fadeIn( anim.fadeDuration, function() {
				currImg.removeClass('current');
				nextImg.addClass('current');
				if( cb ) {
					cb(this);
				}
			} );
		},
		
		// function to select the next sibling.. loops..
		nextElement: function( el ) {
			var nextEl = el.next();
			if( nextEl.length == 0 ) {
				nextEl = el.siblings(':first-child');
			}
			return nextEl;
		}
	}
	
	anim.init();
	anim.start();	
});
