var p08Nav = function(id) {
	var scrollTimeout = 250;
	var scrollActiveMaximumDuration = 150;
	var scrollActiveDurationMultiplier = 0.8;
	var scrollOutDuration = 500;
	var scrollOffset = 50; //Negative numbers for scrollOffset will create negative values, with overflow, it doesn't matter though.
	
	$('#'+id).click(function(event) {
		var eventTarget = $(event.target);
		
		if(eventTarget.is('dt'))
			switchToCollection(eventTarget.parent(), id); //clicked definition term (aka collection header)
		
		if(eventTarget.is('a')) {
			if(eventTarget.parent().is('dd')) //filter link
				switchToFilter(eventTarget, id);
		}
		//event.preventDefault();
	}).addClass('left-nav-active');
	
	$('#'+id+' ul').each(function(){
		var scrollDefault = 0; //Default value to scroll to when navigation is at rest
		
		$(this).hover(
			function(event) { //over
				if(!$(this).data('totalHeight')) {
					var listHeight = $(this).height();
					$(this).height('auto');
					$(this).data('totalHeight', $(this).height());
					$(this).height(listHeight);
				}
			},
			function(event) { //out
				var el = this;
				setTimeout(function() {
					$(el).animate({
						scrollTop:scrollDefault
					}, scrollOutDuration);
				}, scrollTimeout);
			}
		);
		
		$(this).mousemove(function(event) {
			var mouseLoc =  event.pageY - $(this).offset().top;
			var mouseLocNormal = (mouseLoc / $(this).height()).toFixed(2); //normalized relative mouse location
			var tempScrollTo = parseInt(mouseLocNormal * $(this).data('totalHeight')) - scrollOffset;
			
			var scrollDuration = parseInt(Math.abs(tempScrollTo - $(this).scrollTop()) * scrollActiveDurationMultiplier); //Dynamic duration
			scrollDuration = (scrollDuration > scrollActiveMaximumDuration) ? scrollActiveMaximumDuration : scrollDuration;
			
			$(this).stop();
			$(this).animate({
				scrollTop:tempScrollTo
			}, scrollDuration);
		});
		
		//Begin ugly way to calculate position, due to method used to hide/show content area that prevents the snapping effect. assuming each item is 10px tall.
		listElements = $(this).children('li');
		
		listElements.each(function(index, obj) {
			if($(obj).children('.active')[0] != null)
				scrollDefault = 12 * index;
		});
		//End of the ugly!
		
		//Scroll to the selected item
		$(this).animate({
			scrollTop:scrollDefault
		}, scrollOutDuration);
		
		$(this).addClass('scroll-height');
	});
	
	$('#'+id).css('overflow', 'hidden');
}

function switchToCollection(collectionObj, navId) {
	var collectionHeader = collectionObj.children('dt');
	collectionObj.toggleClass('section-visible');
	
	if(collectionObj.hasClass('section-visible')) {
		collectionHeader.html(collectionHeader.html().replace("+","-"));
		
		$('#'+navId+' dl').each(function(index, dl) {
			var collectionHeader = $(dl).children('dt');
			
			if(dl != collectionObj[0]) {
				collectionHeader.html(collectionHeader.html().replace('-', '+'));
				$(dl).removeClass('section-visible');
			}
		});
	} else {
		collectionHeader.html(collectionHeader.html().replace('-', '+'));
	}
}

function switchToFilter(filterObj, navId) {
	filterObj.toggleClass('filter-list-visible');
	
	if(filterObj.hasClass('filter-list-visible')) {
		filterObj.html(filterObj.html().replace("+","-"));
		filterObj.next().removeClass('list-collapsed');
	} else {
		filterObj.html(filterObj.html().replace("-","+"));
		filterObj.next().addClass('list-collapsed');
	}
	
	filterObj.parent().children('a').each(function(index, anchor) {
		if(anchor != filterObj[0]) {
			$(anchor).removeClass('filter-list-visible');
			$(anchor).html($(anchor).html().replace("-","+"));
			$(anchor).next().addClass('list-collapsed');
		}
	});
}

function setupNav() {
	var filterHash = {
		family:0,
		material:1,
		size:2
	};
	try {
		if(filter == 'newwatches') {
			switchToCollection($($('#left-nav dl')[1]), 'left-nav');
			switchToFilter($($('#left-nav dd > a')[0]), 'left-nav');
		} else {
			switchToCollection($($('#left-nav dl')[0]), 'left-nav');
			switchToFilter($($('#left-nav dd > a')[filterHash[filter]]), 'left-nav');
		}
	}catch(e) { //Display default nav config if filter variable doesn't exist
		switchToCollection($($('#left-nav dl')[0]), 'left-nav');
		switchToFilter($($('#left-nav dd > a')[0]), 'left-nav');
	}
}

function setupHorizontalNav() {
	$('#p08-nav').addClass('p08-nav-active');
	var totalWatches = $('#p08-sets > ul li a');
	var sliderDuration = 700;
	var sliderEasing = 'easeInOutExpo';
	
	if(totalWatches.length == 1) {
		$('#p08-nav').css('display', 'none');
	} else if(totalWatches.length <= 10) {
		$('#p08-previous').css('visibility', 'hidden');
		$('#p08-next').css('visibility', 'hidden');
		$('#p08-watch-total').css('visibility', 'hidden');
		
		//Set spacing
		$('#p08-nav #p08-sets ul').css('paddingLeft', (10-totalWatches.length)*20 / 2);
	}
	
	var wrapperWidth = $('#p08-sets-wrapper').width();
	
	$('#p08-sets').width($('#p08-sets > ul').length*wrapperWidth);	
	
	$('#p08-sets > ul li a').each(function(index, watch) {
		if($(watch).hasClass('active'))
			$('#p08-sets-wrapper').scrollTo($(watch).parent().parent(), sliderDuration, {axis:'x',	easing: sliderEasing, onAfter: function() {
				if($(watch).parent().parent().next().length == 0) {
					$('#p08-next a').animate({opacity: 0}, 'fast', function(){$(this).css('visibility', 'hidden')});
					$('#p08-watch-total').animate({opacity: 0}, 'fast', 'linear', function(){$(this).css('visibility', 'hidden')});
				} else if($(watch).parent().parent().prev().length == 0) {
					$('#p08-previous a').animate({opacity: 0}, 'fast', 'linear', function(){$(this).css('visibility', 'hidden')});
				}
			}});
	});

	$('#p08-next').click(function(event) {
		if(($('#p08-sets-wrapper').scrollLeft()+wrapperWidth) > 0) {
			$('#p08-previous a').css('visibility', 'visible').animate({opacity: 1}, 'fast', 'linear');
		}
		
		if(($('#p08-sets-wrapper').scrollLeft()+wrapperWidth*2) <= $('#p08-sets').width()) {
			$('#p08-sets-wrapper').scrollTo('+='+wrapperWidth+'px', sliderDuration, {axis:'x', easing: sliderEasing, onAfter: function() {
				if(($('#p08-sets-wrapper').scrollLeft()+wrapperWidth) == $('#p08-sets').width()) {
					$('#p08-next a').animate({opacity: 0}, 'fast', 'linear', function(){$(this).css('visibility', 'hidden')});
					$('#p08-watch-total').animate({opacity: 0}, 'fast', 'linear', function(){$(this).css('visibility', 'hidden')});
				}
			}});
		}
		event.preventDefault();
	});

	$('#p08-previous').click(function(event) {
		if(($('#p08-sets-wrapper').scrollLeft()+wrapperWidth) == $('#p08-sets').width()) {
			$('#p08-next a').css('visibility', 'visible').animate({opacity: 1}, 'fast', 'linear');
			$('#p08-watch-total').css('visibility', 'visible').animate({opacity: 1}, 'fast', 'linear');
		}
		
		if(($('#p08-sets-wrapper').scrollLeft()-wrapperWidth) >= 0) {
			$('#p08-sets-wrapper').scrollTo('-='+wrapperWidth+'px', sliderDuration, {axis:'x', easing: sliderEasing, onAfter: function() {
				if(($('#p08-sets-wrapper').scrollLeft()) == 0) {
					$('#p08-previous a').animate({opacity: 0}, 'fast', 'linear', function(){$(this).css('visibility', 'hidden')});
				}
			}});
		}
		
		event.preventDefault();
	});
}

function setupFamilyList() {
	$('#left-nav ul').click(function(e){try{document.jsAPI.trackCatalogueClick(e.target.href, "filter-list:" + e.target.rel, currentWatchId)}catch(e){}});
	$('#p08-nav ul').click(function(e){try{document.jsAPI.trackCatalogueClick($(e.target).parent().attr("href"), "bottom-nav", currentWatchId)}catch(e){}});
}

function setupNavClicks() {
	$('#brochure-toggle').click(function(){try{document.jsAPI.trackLink(currentWatchId.toLowerCase()+":catalogue", "explore", currentWatchId.toLowerCase()+":brochure:cover")}catch(e){}});
	$('#left-arrow-catalogue').click(function(){try{document.jsAPI.trackLink(currentWatchId.toLowerCase()+":catalogue", "left-arrow", previousWatchId.toLowerCase()+":catalogue")}catch(e){}});
	$('#right-arrow-catalogue').click(function(){try{document.jsAPI.trackLink(currentWatchId.toLowerCase()+":catalogue", "right-arrow", nextWatchId.toLowerCase()+":catalogue")}catch(e){}});
}

//register
onloadHTMLHandlers[onloadHTMLHandlers.length] = 'p08Nav("left-nav");setupFamilyList();setupNavClicks();setupNav(); setupHorizontalNav();';