$(document).ready(function() {
	var $panels = $('#slider .scrollContainer > div');
	var $container = $('#slider .scrollContainer');

	// if false, we'll float all the panels left and fix the width of the container
	var horizontal = true;

	// float the panels left if we're going horizontal
	if (horizontal) {
		$panels.css({
			'float' : 'left',
			'position' : 'relative' // IE fix to ensure overflow is hidden
		});

		// calculate a new width for the container (so it holds all panels)
		$container.css('width', $panels[0].offsetWidth * $panels.length);
	}

	// collect the scroll object, at the same time apply the hidden overflow to remove the default scrollbars that will appear
	var $scroll = $('#slider .scroll').css('overflow', 'hidden');

	// apply our left + right buttons
	$scroll
		.before('<img class="scrollButtons left" src="images/btn-coda_left.gif" alt="Previous Magazine"  onmouseout="this.src=\'images/btn-coda_left.gif\'" onmouseover="this.src=\'images/btn-coda_left_hover.gif\'" />')
		.after('<img class="scrollButtons right" src="images/btn-coda_right.gif" alt="Next Magazine"  onmouseout="this.src=\'images/btn-coda_right.gif\'" onmouseover="this.src=\'images/btn-coda_right_hover.gif\'" />');

	// handle nav selection
	function selectNav() {
		; // not used
	}

	$('#slider .navigation').find('area').click(selectNav);

	// go find the navigation link that has this target and select the nav
	function trigger(data) {
		var el = $('#slider .navigation').find('area[href$="' + data.id + '"]').get(0);
		selectNav.call(el);
	}

	if (window.location.hash) {
		trigger({ id : window.location.hash.substr(1) });
	} else {
		// $('map area.default').click();
	}

	// subtract the amount of padding to the offset
	var offset = parseInt((horizontal ?
		$container.css('paddingTop') :
		$container.css('paddingLeft'))
		|| 0) * -1;

	var scrollOptions = {
		target: $scroll, // the element that has the overflow
		items: $panels, // can be a selector which will be relative to the target
		navigation: '.navigation area',
		prev: 'img.left', // selectors are NOT relative to document, i.e. make sure they're unique
		next: 'img.right',
		axis: 'xy', // allow the scroll effect to run both directions
		onAfter: trigger, // our final callback
		offset: offset,
		duration: 500, // duration of the sliding effect
		easing: 'swing'
	};

	// apply serialScroll to the slider
	$('#slider').serialScroll(scrollOptions);

	// apply localScroll to hook any other arbitrary links to trigger the effect
	$.localScroll(scrollOptions);

	// move the slider in to position, setting the duration to 1 because we don't want it to scroll on the initial page load
	// scrollOptions.start = $('#KD');
	// $('#slider').trigger('goto', [ 3 ]);
	// $('map area.default').click();
	scrollOptions.duration = 1;
	$.localScroll.hash(scrollOptions);

	// png fix
	$('div.overlay').show();
	$('div.overlay img').pngfix();
});