$(document).ready(function() {

	$('div.Slider920 ul').each(function(i, e) {
		$(this).data('items', $(this).find('li').length);
		$(this).data('current', 1);
		$(this).data('interval', setInterval(function() {Slider920_AutoSlide(i)}, 10000));
		$(this).data('index', i);

		$(this).children().each(function(i,e) {
			if (i == 0) {
				var link = $(this).children('a').attr('href');
				if (link == "")
					$(this).parent().parent().next().css('cursor', 'auto');
				else
					$(this).parent().parent().next().css('cursor', 'pointer');
			}
		});

	});

	function Slider920_AutoSlide(index) {
		$('div.Slider920 ul').each(function(i, e) {

			// alert('i: '+i+' index: '+index);

			if (i != index)
				return;

			var cur = $(this).data('current');
			var total = $(this).data('items');
			var left = 904;

			// Don't autoslide hidden sliders
			if (!$(this).is(":visible"))
				return;
	
			if (cur == total) {
				left = -904 * (total - 1);
				cur = 0;
			}

			$(this).children().each(function(i,e) {
				if (i == cur) {
					var link = $(this).children('a').attr('href');
					if (link == "")
						$(this).parent().parent().next().css('cursor', 'auto');
					else
						$(this).parent().parent().next().css('cursor', 'pointer');
				}
			});
		
	
			$(this).animate({left: '-='+left+''}, 1500, 'easeOutExpo');
			$(this).data('current', cur+1);
		});
	}

	$('div.Slider920 div.Overlay').click(function() {
		var curr = $(this).prev().children('ul').first().data('current');
		$(this).prev().children('ul').children().each(function(i,e) {
			if (i == (curr - 1)) {
				var link = $(this).children('a').attr('href');
				if (link == "")
					return;
				else
					location.href=link;
			}
		});
	});

	// Stop slider if mouse is over overlay and start it when it is out
	$('div.Slider920 div.Overlay').mouseover(function() {
		var interval_id = $(this).prev().children('ul').first().data('interval');
		clearInterval(interval_id);
	});

	$('div.Slider920 div.Overlay').mouseout(function() {
		var index = $(this).prev().children('ul').first().data('index');
		var interval = setInterval(function() {Slider920_AutoSlide(index)}, 10000);
		$(this).prev().children('ul').first().data('interval', interval); // Save new interval id
	});

	// Do the same for left and right buttons
	$('div.Slider920 div.Left').mouseover(function() {
		var interval_id = $(this).prev().prev().children('ul').first().data('interval');
		clearInterval(interval_id);
	});

	$('div.Slider920 div.Left').mouseout(function() {
		var index = $(this).prev().prev().children('ul').first().data('index');
		var interval = setInterval(function() {Slider920_AutoSlide(index)}, 10000);
		$(this).prev().prev().children('ul').first().data('interval', interval); // Save new interval id
	});

	$('div.Slider920 div.Right').mouseover(function() {
		var interval_id = $(this).prev().prev().prev().children('ul').first().data('interval');
		clearInterval(interval_id);
	});

	$('div.Slider920 div.Right').mouseout(function() {
		var index = $(this).prev().prev().prev().children('ul').first().data('index');
		var interval = setInterval(function() {Slider920_AutoSlide(index)}, 10000);
		$(this).prev().prev().prev().children('ul').first().data('interval', interval); // Save new interval id
	});

	// Handle arrow clicks
	$('div.Slider920 div.Left').click(function() {
		var cur = $(this).parent().children('div.Viewport').children('ul').first().data('current');
		var total = $(this).parent().children('div.Viewport').children('ul').first().data('items');
		var left = 904;

		if (cur == 1) {
			left = -904 * (total - 1);
			cur = total+1;
		}

		$(this).parent().children('div.Viewport').children('ul').each(function(i, e) {
			$(this).animate({left: '+='+left+''}, 1500, 'easeOutExpo');
		});
		$(this).parent().children('div.Viewport').children('ul').first().data('current', cur-1);

		$(this).parent().children('div.Viewport').children('ul').children().each(function(i,e) {
			if (i == (cur - 2)) {
				var link = $(this).children('a').attr('href');
				if (link == "")
					$(this).parent().parent().next().css('cursor', 'auto');
				else
					$(this).parent().parent().next().css('cursor', 'pointer');
			}
		});

		return false;
	});

	$('div.Slider920 div.Right').click(function() {
		var cur = $(this).parent().children('div.Viewport').children('ul').first().data('current');
		var total = $(this).parent().children('div.Viewport').children('ul').first().data('items');
		var left = 904;

		if (cur == total) {
			left = -904 * (total - 1);
			cur = 0;
		}

		$(this).parent().children('div.Viewport').children('ul').each(function(i, e) {
			$(this).animate({left: '-='+left+''}, 1500, 'easeOutExpo');
		});
		$(this).parent().children('div.Viewport').children('ul').first().data('current', cur+1);

		$(this).parent().children('div.Viewport').children('ul').children().each(function(i,e) {
			if (i == cur) {
				var link = $(this).children('a').attr('href');
				if (link == "")
					$(this).parent().parent().next().css('cursor', 'auto');
				else
					$(this).parent().parent().next().css('cursor', 'pointer');
			}
		});

		return false;
	});
});

