
/*
 *	REQUIRES JQUERY 1.4+
 */

var ielt9 = $.browser.msie && $.browser.version.substr(0,1)<9;

jQuery.fn.onImagesLoaded = function(_cb) { 
  return this.each(function() {
 
    var $imgs = (this.tagName.toLowerCase()==='img')?$(this):$('img',this),
        _cont = this,
            i = 0,
    _done=function() {
      if( typeof _cb === 'function' ) _cb(_cont);
    };
 
    if( $imgs.length ) {
      $imgs.each(function() {
        var _img = this,
        _checki=function(e) {
          if((_img.complete) || (_img.readyState=='complete'&&e.type=='readystatechange') )
          {
            if( ++i===$imgs.length ) _done();
          }
          else if( _img.readyState === undefined ) // dont for IE
          {
            $(_img).attr('src',$(_img).attr('src')); // re-fire load event
          }
        }; // _checki \\
 
        $(_img).bind('load readystatechange', function(e){_checki(e);});
        _checki({type:'readystatechange'}); // bind to 'load' event...
      });
    } else _done();
  });
};

var Header = (function() {
	var index = 0,
		basedir = '',
		images = [
		
		{ image : 'http://www.leadingedge.com/images/slideshow/slideshow_image_2.jpg', caption : '', link : '', linkText : '', credits : '' },		
				
				
				
		
				
		{ image : 'http://www.leadingedge.com/images/slideshow/slideshow_image_3.jpg', caption : '', link : '', linkText : '', credits : '' },		
		{ image : 'http://www.leadingedge.com/images/slideshow/slideshow_image_4.jpg', caption : '', link : '', linkText : '', credits : '' },		
		{ image : 'http://www.leadingedge.com/images/slideshow/slideshow_image_5.jpg', caption : '', link : '', linkText : '', credits : '' }		
		],
		settings = {
			dimensions : {
				imageWidth : 950,
				imageHeight : 528
			},
			speedclick : true,
			easing : 'easeOutQuad',
			duration : 900,
			autoplay : {
				enabled : true,
				idleStart : 8000,
				duration : 8000,
				idleReset : 30000
			},
			buttons : {
				easing : 'easeInOutExpo',
				duration : 100
			},
			caption : {
				easing : 'easeOutQuad',
				fadeInDuration : 1000,
				delay : 200,
				fadeOutDuration : 300
			},
			credits : {
				easing : 'easeOutQuad',
				fadeInDuration : 1000,
				delay : 200,
				fadeOutDuration : 300
			}
		},
		thisTime = 50000, lastTime = 0,
		autoplayInterval, idleTimeout;
	var $home_slider, $home_callout, $home_credits, $home_pagination_a;
	
	domready();
	init();
	
	function domready() {
		$(document).ready(function() {
			$('#home_previous')
				.mouseenter(function(e) { hover.apply(this, [true, 'left']); })
				.mouseleave(function(e) { hover.apply(this, [false, 'left']); })
				.click(previous);
			$('#home_next')
				.mouseenter(function(e) { hover.apply(this, [true, 'right']); })
				.mouseleave(function(e) { hover.apply(this, [false, 'right']); })
				.click(next);
			$home_pagination_a = $('#home_pagination').children('a');
			$home_pagination_a.click(function(e) { resetAutoplay(); goto($home_pagination_a.index($(this))); });
			$home_slider = $('#home_slider');
			$home_callout = $('#home_callout');
			$home_credits = $('#header_credits');
			
			startAutoplay();
		});
	}
	function init() {
		for(var i=0; i<images.length; i++)
			(new Image()).src = images[i].image;
	}
	
	function hover(on, side) {
		/*
		 *	This functionality was custom to USBid
		 */
		var values = ((on)
			? [-20, 18, 12 , 6]
			: [-16, 20, 8, 4]),
			obj = {};
			obj[side] = values[0];
		$(this).stop()
	       .animate(obj, { queue : false, easing : settings.buttons.easing, duration : settings.buttons.duration })
		   .children('img').stop().animate({ marginTop: values[1], marginBottom: values[1], height: values[2], width: values[3] }, { queue : false, easing : settings.buttons.easing, duration : settings.buttons.duration });
	}
	
	function autoplay() {
		if(!settings.autoplay.enabled) return;
		if(autoplayInterval == null)
			autoplayInterval = setInterval(autoplay, settings.autoplay.duration);
		clearTimeout(idleTimeout);
		next();
	}
	function startAutoplay() {
		if(settings.autoplay.enabled) {
			clearInterval(autoplayInterval);
			clearTimeout(idleTimeout);
			idleTimeout = setTimeout(autoplay, settings.autoplay.idleStart);
		}
	}
	function pauseAutoplay() {
		clearInterval(autoplayInterval);
		clearTimeout(idleTimeout);
	}
	function resetAutoplay() {
		if(settings.autoplay.enabled) {
			clearInterval(autoplayInterval);
			clearTimeout(idleTimeout);
			idleTimeout = setTimeout(autoplay, settings.autoplay.idleReset);
		}
	}
	function previous(e) {
		if(e != null) resetAutoplay();
		goto((index == 0 ? images.length - 1 : index - 1), 'left');
	}
	function next(e) {
		if(e != null) resetAutoplay();
		goto((index == images.length - 1 ? 0 : index + 1), 'right');
	}
	function goto(i, direction) {
		if(index == i) return;
		
		if(settings.speedclick) {
			lastTime = thisTime;
			thisTime = new Date().getTime();
		}
		
		if(direction == null)
			direction = (i > index) ? 'right' : 'left';
		
		index = i;
		updatePagination();
		
		$home_slider.add($home_callout).stop(1, 1);
		
		switch(direction) {
			case 'left':
				$home_slider.prepend('<img src='+basedir+images[i].image+' width="'+settings.dimensions.imageWidth+'" height="'+settings.dimensions.imageHeight+'" />');
				$home_slider.children('img:first').onImagesLoaded(function() {
					$home_slider.css('left', -settings.dimensions.imageWidth).animate({ left : 0 }, { queue : false, easing : settings.easing, duration : Math.min(settings.duration, thisTime - lastTime), complete : function() {$(this).children('img:gt(0)').remove();}});
				});
				break;
			case 'right':
				$home_slider.append('<img src='+basedir+images[i].image+' width="'+settings.dimensions.imageWidth+'" height="'+settings.dimensions.imageHeight+'" />');
				$home_slider.children('img:last').onImagesLoaded(function() {
					$home_slider.animate({ left : -settings.dimensions.imageWidth}, { queue : false, easing : settings.easing, duration : Math.min(settings.duration, thisTime - lastTime), complete : function() {
						$(this).css('left', 0).children('img:eq(0)').remove();
					}});
				});
				break;
		}
		updateCaption();
		updateCredits();
	}
	function updatePagination() {
		$home_pagination_a.each(function(i) {
			if(i == index) $(this).addClass('active');
			else $(this).removeClass('active')
		});
	}
	function updateCaption() {
		if(ielt9) {
			$home_callout.css('display', 'none').empty().html(images[index].caption).append('<a href="'+images[index].link+'" id="seemore" class="button">'+images[index].linkText+'</a>');
			$home_callout.delay(Math.min(settings.caption.fadeOutDuration + settings.caption.delay, thisTime - lastTime)).queue(function(next) {
				$home_callout.css('display', 'block');
				next();
			});
		} else {
			$home_callout.animate({ opacity : 0 }, { queue : false, easing : settings.caption.easing, duration : Math.min(settings.caption.fadeOutDuration, (thisTime - lastTime) / 2), complete : function() {
				$home_callout.empty().html(images[index].caption).append('<a href="'+images[index].link+'" id="seemore" class="button">'+images[index].linkText+'</a>');
				$home_callout.delay(Math.min(settings.caption.delay, thisTime - lastTime)).animate({ opacity : 1 }, { easing : settings.caption.easing, duration : Math.min(settings.caption.fadeInDuration, (thisTime - lastTime) / 2) });
			}});
		}
	}
	function updateCredits() {
		if(ielt9) {
			$home_credits.css('display', 'none').empty().html(images[index].credits);
			$home_credits.delay(Math.min(settings.credits.fadeOutDuration + settings.credits.delay, thisTime - lastTime)).queue(function(next) {
				$home_credits.css('display', 'block');
				next();
			});
		} else {
			$home_credits.animate({ opacity : 0 }, { queue : false, easing : settings.credits.easing, duration : Math.min(settings.credits.fadeOutDuration, (thisTime - lastTime) / 2), complete : function() {
				$home_credits.empty().html(images[index].credits);
				$home_credits.delay(Math.min(settings.credits.delay, thisTime - lastTime)).animate({ opacity : 1 }, { easing : settings.credits.easing, duration : Math.min(settings.credits.fadeInDuration, (thisTime - lastTime) / 2) });
			}});
		}
	}
	
	return {
		'domready' : domready,
		'pauseAutoplay' : pauseAutoplay,
		'previous' : previous,
		'next' : next,
		'goto' : goto
	};
})();
