/* Copyright (c) 2010 HID Digital Solutions. */
$(document).ready(function() {

	// Startup vars.
	var iPosition = 0;
	var iWidth = 958;
	var oSlides = $('.slide');
	var iSlides = oSlides.length;
	var iPaused = 0;

	// Make first position in navigator active.
	$('#slidePosition_' + iPosition).addClass('active');

	// Set width of slides container.
	$('#slidesContainer').css({
		'width' : (iWidth * iSlides)
	});

	// Change slide on a regulare base.
	oInterval = setInterval(function() {

		// When slideshow is not paused.
		if (iPaused == 0) {

			// Old position is not longer active.
			$('#slidePosition_' + iPosition).removeClass('active');

			// Add 1 to position.
			iPosition = iPosition + 1;
			if (iPosition >= iSlides) {
				iPosition = 0;
			}

			// Slide.
			slide();

		}

	}, 10000);


	// When user clicks on a specific position in the navigator.
	$('.slideTo').click(function(event) {

		// Pause the slideshow.
		iPaused = 1;
		$('#slidePosition_pause').addClass('active');

		// Deactivate current position in navigator.
		$('#slidePosition_' + iPosition).removeClass('active');

		// Get id of clicked item.
		var sId = $(event.target).parent().attr('id');
		var aId = sId.split('_');
		iPosition = aId[1];

		// Activate slide.
		slide();

	});


	// Slide animation.
	function slide() {

		// Make position in navigator active.
		$('#slidePosition_' + iPosition).addClass('active');

		// Animate.
		$('#slidesContainer').animate({
			'marginLeft' : iWidth * (-iPosition)
		});

	}

	// When clicking pause button.
	$('.slidePause').click(function(event) {

		// When already paused.
		if ($('#slidePosition_pause').attr('class') == 'active') {

			// Activate again.
			$('#slidePosition_pause').removeClass('active');
			iPaused = 0;

		// Not paused.
		} else {

			// Stop slideshow.
			$('#slidePosition_pause').addClass('active');
			iPaused = 1;

		}

	});

	$('#slidesContainer').touchwipe({
		wipeLeft: function() { 
			iPosition = iPosition + 1;
			if (iPosition > (iSlides - 1)) {
				iPosition = 0;
			}
			slide();
		},
		wipeRight: function() {
			iPosition = iPosition - 1;
			if (iPosition < 0) {
				iPosition = (iSlides - 1);
			}
			slide();
		},
		min_move_x: 20,
		preventDefaultEvents: true
	});

});
