var Rectangle = function(x, y, width, height) {
	return {
		origin: { x: x || 0, y: y || 0 },
		size: { width: width || 0, height: height || 0 }
	};
};

$(document).ready(function(){
	
$('.datepicker').datepicker();

$('.slider').each(function(){
	
	var settings = {
		speed: 1000,
		delay: 5000,
		easing: 'easeOutExpo'
	};
	
	var $self = $(this),
		mask = {
			bounds: new Rectangle(null, null, 1598, 477),
			frame: new Rectangle(658, null, 600, 350)
		},
		$items = $self.children().each(function(i){
			$(this).css({ 
				position: 'absolute', 
				top: 0, 
				left: i * mask.frame.size.width 
			});
		}),
		length = $items.length,
		lastOriginX = 0;
		last = length - 1;
		
	// Update mask bounds
	var updateMaskBounds = function() {
		mask.bounds.origin.x = Math.ceil(($(window).width() - mask.bounds.size.width) / 2);
	};
	
	var shiftItems = function(shift) {
		var frameOriginX = mask.bounds.origin.x + mask.frame.origin.x;
		var offset = shift || frameOriginX - Math.ceil(frameOriginX / mask.frame.size.width) * mask.frame.size.width;
		var shift = shift || false;
		var lastOriginX = $self.children().last().position().left || 0;
		$items.each(function(i){
			var $item = $(this),
				originX = shift 
				        ? Math.floor($item.position().left)+ parseInt(shift) 
				        : (i * mask.frame.size.width + offset);
			
			$item.animate({ 
				left: originX
			}, shift ? settings.speed : 0, settings.easing, function() {
				var visible = (originX * -1) < mask.frame.size.width;
				if ( ! visible) {
					$item.css('left', lastOriginX);
					$item.appendTo($items.parent());
					// shift the item to the end of the stack
					$items = $items.not($item).add($item);
				}
			});
			
		});
	};
	
	updateMaskBounds();
	shiftItems();
	
	var interval, 
		stopSlide = function() { 
			clearInterval(interval); 
		},
		startSlide = function() { 
			interval = setInterval(function(){
				shiftItems(-mask.frame.size.width);
			}, settings.delay); 
		};

	var timeout;
	
	// workaround to fix IE9.
	var wWidth = $(window).width();
	$(window).resize(function(){
		var cWidth = $(window).width();
		if (wWidth != cWidth) {
			wWidth = cWidth;
			stopSlide();
			clearInterval(timeout);
			updateMaskBounds();
			shiftItems();
			timeout = setTimeout(function(){ startSlide(); }, 50);
		}
	});
	startSlide();
});


$.fn.pagenav = function() {
	var $items = this,
		$contents = $([]),
		selectFirst;
	
	var select = function($nav) {
		$items.not($nav.addClass('selected')).removeClass('selected');
		$contents.not($($nav.attr('href')).show()).hide();
		selectFirst = null;
	};
	
	$items.each(function(i){
		var $item = $(this),
			$content = $($item.attr('href'));
			
		if ($content.length) {
			$contents.push($content.get(0));
			var hash = window.location.hash.toString().substring(1);
			if (hash == $content.attr('id')) {
				selectFirst = $item;
			}
		}
	}).click(function(e) {
		e.preventDefault();
		var href = $(this).attr('href');
		if (href != window.location.hash) {
			window.location.hash = href;
			select($(this));
		}
	});
	
	if ( ! selectFirst) 
		selectFirst = $items.eq(0);
	
	select(selectFirst);	
};

$('a', '.nav-content').pagenav();

$('.replace-table').each(function(){
	var $self = $(this)
		$newTable = $('#' + $self.attr('data-table')).find('table').clone().removeAttr('id'),
		id = $self.attr('data-row');
	
	if ($newTable.length && id--) {
		$newTable
			.find('caption').remove().end()
			.find('tbody tr').not(':eq('+id+')').remove();
		
		$self.replaceWith($newTable);
	}
});

$('.gallery a, .fancybox').fancybox();

});

