if (!window.console) {
	var console = {
		'log': function(){}
		, 'dir': function(){}
		, 'time': function(){}
		, 'timeEnd': function(){}
		, 'profile': function(){}
		, 'profileEnd': function(){}
	};
}

var domCache = {};
function $$(selector) {
	if (!domCache[selector]) {
		domCache[selector] = $(selector);
	}
	return domCache[selector];
}

(function ($) {
	$$('body').removeClass('nojs');
	$('#search-form input').one('focus', function(event) {
		$(this).val('');
	});

	startpos = $('.slider-container').data('start') - 1;
	$$('.slider-container').show().stop().animate({
		'scrollLeft': (startpos*$$('.slider-container').width())+'px'
	}, 500);

	setInterval(autoslide, 4000);

	function autoslide() {
		$('.next').click();
	}
	
	
	$('.slider').append($('.slider').find('´li:first').clone());
	var prevIndex = 0;
	$('.slideshow .controls li').click(function() {
		if ($(this).is('.prev,.next')) {
			return;
		}
		$('#startslide').remove();
		var index = $(this).index()-1;
		var distance = (index*$$('.slider-container').width())+'px';
		$(this).addClass('current').siblings().removeClass('current');
		$('.slider').show();

		var pxDist = parseInt(distance.replace('px', ''), 10);

		if (index === 1 && prevIndex == 0) {
			$$('.slider-container').stop().scrollLeft(0)
		}

		if (index === 0 && prevIndex == $('.slider li').length-2) {
			$$('.slider-container').stop().animate({
				'scrollLeft': ( ($('.slider li').length-1) * $$('.slider-container').width())+'px'
			}, 500);
		} else{
			$$('.slider-container').stop().animate({
				'scrollLeft': distance 
			}, 500);
		}
		/*
		$$('.slideshow .divider').animate({
			'background-position': -965+(index*37)+'px'
		}, 500);
		*/
		prevIndex = index;
	});
	$('.prev,.next').click(function() {
		var $next;
		var $current = $$('.slideshow').find('.current');
		if ($(this).is('.prev')) {
			$next = $current.prev(':not(.prev)').length ? $current.prev(':not(.prev)') : $current.siblings().filter(':last').prev();
		} else {
			$next = $current.next(':not(.next)').length ? $current.next(':not(.next)') : $current.siblings().filter(':eq(1)');
		}
		$next.click();
	});
})(jQuery);
