/**
* @license
* jQuery Tools @VERSION / Scrollable Navigator
*
* NO COPYRIGHTS OR LICENSES. DO WHAT YOU LIKE.
*
* http://flowplayer.org/tools/scrollable/navigator.html
*
* Since: September 2009
* Date: @DATE
*/
var items;

(function ($) {

	var t = $.tools.scrollable;

	t.navigator = {

		conf: {
			navi: '.navi #pages',
			naviItem: null,
			activeClass: 'active',
			indexed: false,
			idPrefix: null,

			// 1.2
			history: false
		}
	};

	function find(root, query) {
		var el = $(query);
		return el.length < 2 ? el : root.parent().find(query);
	}

	// jQuery plugin implementation
	$.fn.navigator = function (conf) {

		// configuration
		if (typeof conf == 'string') { conf = { navi: conf }; }
		conf = $.extend({}, t.navigator.conf, conf);

		var ret;

		this.each(function () {

			if ($(this).data("scrollable") != null) {
				var api = $(this).data("scrollable"),
				navi = conf.navi.jquery ? conf.navi : find(api.getRoot(), conf.navi),
				buttons = api.getNaviButtons(),
				cls = conf.activeClass,
				history = conf.history && $.fn.history;

				// @deprecated stuff
				if (api) { ret = api; }

				api.getNaviButtons = function () {
					return buttons.add(navi);
				};


				function doClick(el, i, e) {
					api.seekTo(i);
					if (history) {
						if (location.hash) {
							location.hash = el.attr("href").replace("#", "");
						}
					} else {
						return e.preventDefault();
					}
				}

				function els() {
					return navi.find(conf.naviItem || '> *');
				}

				function addItem(i) {
					var item = $("<" + (conf.naviItem || 'a') + "/>").click(function (e) {
						doClick($(this), i, e);
					}).attr("href", "#" + i);

					// index number / id attribute

					if (i === 0) { item.addClass(cls); }
					if (conf.indexed) { item.text(i + 1); }
					if (conf.idPrefix) { item.attr("id", conf.idPrefix + i); }

					return item.appendTo(navi);
				}


				// generate navigator
				if (els().length) {
					els().each(function (i) {
						$(this).click(function (e) {
							doClick($(this), i, e);
						});
					});

				} else {
					$.each(api.getItems(), function (i) {
						addItem(i);
					});
				}


				// C2 - wrap every element in an li
				$.each(api.getItems(), function (i) {
					$('ul.navi #pages a').eq(i).wrap('<li />');
				});

				// Kill the wrapper div and move all the contents up one level
				$('.navi #pages').replaceWith(function () {
					return $(this).contents();
				});

				// Apply a class to the fist and last element so we can style it up
				//$('ul.navi li a').eq(0).addClass('first');
				//$('ul.navi li a').eq($('ul.navi li').length - 1).addClass('last');




                //Robert - inner hero fader

                //loop through and add index class to each slide, use this and onSeek to find div to fade in per slide
                $('.heroFade.scrollable .items > div').addClass(function () {
                    return 'hero-' + $(this).index();
                });

                //animate inital load first slide
                $('.heroFade.scrollable .items .hero-0').find('.fader').delay(2000).fadeIn('slow').delay(4000).fadeOut('slow');

                //amite the rest onSeek
                api.onSeek(function() {
                    
                    $('.heroFade.scrollable .items .fader').css('display', 'none');

                    var heroPosition = this.getIndex()
                    $('.heroFade.scrollable .items .hero-'+ heroPosition +' .fader').delay(2000).fadeIn('slow').delay(4000).fadeOut('slow');

                });










				api.onBeforeSeek(function (e, index) {

					items = $('ul.navi li a');

					//console.log('here 1');
					setTimeout(function () {
						if (!e.isDefaultPrevented()) {


							$('ul.navi li a').removeClass(cls)
							$('ul.navi li a').eq(index + 1).addClass(cls)
							//console.log(items.eq(index).attr('href'));


							//console.log('here 3');
							//var el = els().eq(index);
							//console.log(els('a').attr('href'));
							//if (!e.isDefaultPrevented() && el.length) {
								//console.log('here 4 : ' + els("a").attr('href') + ' - ' + index);
								//els("a").removeClass(cls).eq(index).addClass(cls);
							//}
						}
					}, 1);
				});

				function doHistory(evt, hash) {
					var el = els().eq(hash.replace("#", ""));
					if (!el.length) {
						el = els().filter("[href=" + hash + "]");
					}
					el.click();
				}

				// new item being added
				api.onAddItem(function (e, item) {
					item = addItem(api.getItems().index(item));
					if (history) { item.history(doHistory); }
				});

				if (history) { els().history(doHistory); }

			}

		});

		return conf.api ? ret : this;
	};

})(jQuery); 
