(function ($) {
	var onDocumentReady = function () {
		var slideshow = $('#slideshow'),
			slides = slideshow.children('li'),
			slideshowLength = slides.length, 
			pager = $('<ul id="slideshowPager" class="clr"></ul>'),
			header = $('<h4 class="irpl png header-03"></h4>'),
			pagerAnchorBuilder = function (index, slide) {
				var cl = 'irpl page-' + (index + 1);
				if (index == 0) {
					cl += ' page-first';
				}
				if (index == (slideshowLength - 1)) {
					cl += ' page-last';
				}
				return '<li class="' + cl + '"><a href="javascript:void(0)" class="irplc">' + (index + 1) + '</a></li>';
			},
			beforeCallback = function (currentSlide, nextSlide, options, forwardFlag) {
				var c = $(currentSlide), 
					n = $(nextSlide),
					ci = c.attr('class').substr(c.attr('class').indexOf('slide-') + 6, 2),
					ni = n.attr('class').substr(n.attr('class').indexOf('slide-') + 6, 2);
				if (c.is('.link')) {
					$('#slide-' + ci + '-link').hide();
				}
				if (n.is('.link')) {
					$('#slide-' + ni + '-link').show();
				}
				header.removeClass('header-03-' + ci).addClass('header-03-' + ni).text(n.text());
			};
		$(slides).find('a').bind('click', function () { return false; });
		if (slideshowLength > 1) {
			$('#sidebar h3').after(header);
			slideshow.after(pager);
			pager.css('width', (slideshowLength * 12 + ((slideshowLength - 1) * 5)) + 'px');
			var startingSlide = 0;
			if (window.location.hash.length > 0) {
				var temp = +window.location.hash.replace(/^#/, '');
				if (temp && temp > 0 && temp <= slideshowLength) {
					startingSlide = temp - 1;
				}
			}
			var slideshowOptions = {
				speed: 'slow',
				timeout: 3500,
				pause: true,
				pauseOnPagerHover: true,
				pager: '#slideshowPager',
				pagerAnchorBuilder: pagerAnchorBuilder,
				before: beforeCallback,
				startingSlide: startingSlide
			};
			/*
			if (navigator.userAgent.indexOf('Chrome') > -1) {
				slideshowOptions.fx = 'scrollHorz';
			}
			*/
			slides.each(function () {
				var $t = $(this);
				$t.css('width', $t.children('a,span').width() + 'px');
			});
			if (startingSlide > 1) {
				slideshowOptions.timeout = 0;
			}
			$('#slideshow').cycle(slideshowOptions);	
		}
	};
	$(document).bind('ready', onDocumentReady);
})(jQuery);
