/*
 * $Id: hh_hero.js 767 2011-04-11 11:53:39Z caleb $
 */


(function($) {
$.fn.extend({
	hero: function(options) {
		var defaults = {
			auto: false,			// whether to auto scroll
			trans: 800,				// trans fx delay
			slideon: 7000, 			// delay between auto transitions (default 7s: 7000)
			usenav: false, 			// fwd+back nav items
			hType: 'h1',			// heading type (h1-h5) to use for img alt text -> image overlay text, none
			pausable: true			// if auto scroll, pause when user hovers over hero
		}
		if(options) {
			$.extend(defaults, options);
		}
		o = defaults;
		return this.each(function() {
			// set prelim data
				o.parent = $(this);
				o.reel = $(o.parent).find('#heroreel');
				o.items = $(o.parent).find('.heroitem');
				o.tItems = $(o.items).length;
				o.navC = $('#heronav');
				o.navF = $('#herofwd');
				o.navB = $('#heroback');
				o.isHovered = false;
			// ensure the container hides overflow
				$(o.parent).css('overflow','hidden');
			// iterate through showcase items and search for image list items
				z = 0;
				var slides = '';
				$(o.items).each(function() {
					var item = $(this);
					// find imgs
						imgs = $(item).find('img');
					// apply a rel
						$(this).attr('rel','heroitem_'+z);
					// for each image, take the alt text and apply it as h.herotitle overlay
						if (o.Htype != 'none') {
							i=0;
							$(imgs).each(function() {
								text = '<span>'+$(this).attr('alt')+'</span>';
							});
							text = '<'+o.hType+' class="herotitle" rel="herotitle_'+z+'">'+text+'</'+o.hType+'>';
							$(item).append(text);
						}
					// create a link to this item & insert into the hero nav container
						href = '<a href="#" rel="heroitem_'+z+'"><span>'+z+'</span></a>';
						$(o.navC).append(href);
				z+=1;
				});
			// make the first items active
				$(o.items).filter(':first').addClass('active');
				$(o.navC).find('a').filter(':first').addClass('active');
			// find and bind actions to the nav controls
				$(o.navC).find('a').bind('click', function (event) {
					var item = parseInt($(this).attr('rel').replace('heroitem_',''));
					return gotoitem(item);
				});
			// add fwd+back links to reel container & bind actions
				if(o.usenav) {
					$(o.navF).append('<a href="#"><span>&rArr;</span></a>');
					$(o.navB).append('<a href="#"><span>&lArr;</span></a>');
					$(o.navB).find('a').bind('click', function(event) {
						return gotoitem(current-1);
					});
					$(o.navF).find('a').bind('click', function(event) {
						return gotoitem(current+1);
					});
				}
			// prep slider data & apply to reel
				var current = 0;
				o.iwidth = parseInt($('.heroitem').css('width').slice(0,-2));
				o.iheight = parseInt($('.heroitem').css('height').slice(0,-2));
				var reelW = o.tItems * o.iwidth;
				var reelH = o.iheight;
			// apply new height & width
				$(o.reel).css({'width':reelW+'px', 'height':reelH+'px'});
			// function to calc + animate the slides
				function gotoitem(pos) {
					// current pos: current * slideW
						cpos = current * o.iwidth;
					// is pos outside boundaries?
						if(pos == o.tItems) {
							pos = 0;
						}
						if(pos < 0) {
							pos = o.tItems-1;
						}
					// set anim direction
						var dir = pos < current ? -1 : 1,
					// set anim offset
						newpos = o.iwidth * pos;
					// debug
						//console.log('total slides:'+o.tItems+'; pos:'+pos+'; cpos:'+ current*o.iwidth +'; current:'+current+'; slideW:'+o.iwidth+'; dir:'+dir+'; newpos:'+newpos);
					// animate
						$(o.reel).animate({
							left: -newpos
						}, o.trans, function() {
							// unset current active
								$(o.reel).find('.active').removeClass('active');
								$(o.navC).find('.active').removeClass('active');
							// make new item active
								newslide = $(o.reel).find('div[rel="heroitem_'+pos+'"]').addClass('active');
							// make new nav href active
								newnav = $(o.navC).find('a[rel="heroitem_'+pos+'"]').addClass('active');
							// set current to new
								current = pos;
						});
				return false;
				}
			// bind back link
				$(o.dest).find('a.showcase_back').bind('click', function (event) {
					event.preventDefault();
					return gotoitem(current - 1);
				});
			// bind fwd link
				$(o.dest).find('a.showcase_fwd').bind('click', function (event) {
					event.preventDefault();
					return gotoitem(current + 1);
				});
			// public func for plugin additions
				$(this).bind('gotoitem', function (event, slide) {
					return gotoitem(slide);
				});
			// autoplay slide func
			function nextitem() {
				if(o.isHovered && o.pausable) {
					return false;
				}
				return gotoitem(current + 1);
			}
			// autoplay
				if(o.auto) {
					setInterval(function() {
						if(o.pausable == true) {
							$(o.parent).hover(
								function() {
									o.isHovered = true;
								},
								function() {
									o.isHovered = false;
								}
							);
						}
						$(o.parent).trigger('nextitem');
					}, o.slideon);
				}
			$(this).bind('nextitem', function () {
	            nextitem();
	        });
		});
	}
});
})(jQuery);

$(document).ready(function() { 
	if($('#herogallery').is(':visible')) {
		$('#herogallery').hero({usenav: false, auto:true});
	}
});
