﻿/*
	Slider
	Copyright Know IT 2010
*/

// ev. ändra om options till statiska och fixa till (vänta på klart beslut om utseende först)

var _slideAmount = 0;
var _animating = false;
var _offset = 0;
var _imageWidth = 0;

// Options
var _opt_imagesToScroll = 1;
var _opt_bounce = 30;


$(document).ready(function(){
	
	var firstTime = true;
	
	$("#le").click(function(event){
		event.preventDefault();
		if (firstTime)
		{
			firstTime = false;
			initialize();
		}
		slide(true);
	});
	$("#ri").click(function(event){
		event.preventDefault();
		if (firstTime)
		{
			firstTime = false;
			initialize();
		}
		slide(false);
	});

	checkToggleState();

});

function initialize()
{
	// Visa listans dolda element
	$("#slider-list > .hidden").removeClass('hidden');
	
	_offset = parseInt($("#slider-list li:first").css('padding-right'));
	_imageWidth = parseInt($(".i-first").width());
	
	_slideAmount = _offset + _imageWidth;
	
	var imageCount = $("#slider-list > li").size();
	var totalSize = (_imageWidth * imageCount) + (_offset * imageCount) + 25;
	$("#slider-list").css('width', totalSize);
}

function slide(left)
{
	if (!_animating)
	{
		_animating = true;
		
		//= Simple Sliding
//		$("#slider-list").animate(
//			{
//				marginLeft: left  ? ('-=' + _slideAmount) : ('+=' + _slideAmount)
//			}, 100,
//			function(){
//				checkToggleState();
//				_animating = false;
//			}
//		);

		//= Advanced sliding
		$("#slider-list").animate(
			{
				marginLeft: !left  ? ('+=' + _opt_bounce * _opt_imagesToScroll) : ('-=' + _opt_bounce * _opt_imagesToScroll)
			}, 200).animate(
			{
				marginLeft: !left  ? ('-=' + (_slideAmount + _opt_bounce) * _opt_imagesToScroll) : ('+=' + (_slideAmount + _opt_bounce) * _opt_imagesToScroll)
			}, 600,
				function(){
					checkToggleState();
					_animating = false;
				}
			);
	}
}

//	Det som gör att slidern byter håll
// 1) i Slide() .. Finns en negation (!) före left i conditional-checken
// 2) i checkToggleState() .. RI och LE har bytt plats (bortkommenterat)

function checkToggleState()
{
	var sliderList = $("#slider-list");
	
	var compareLeftWith = parseInt(sliderList.css('width')) - (_imageWidth * 2) - (_offset * 2);
	var compareRightWith = (_imageWidth) + (_offset);
	
	if (parseInt(sliderList.css('margin-left')) > compareRightWith)
	{
		//$("#ri").css('display', 'none');
		$("#le").css('display', 'none');
	}
	else if (parseInt(sliderList.css('margin-left')) < (compareLeftWith * -1))
	{
//		$("#le").css('display', 'none');
		$("#ri").css('display', 'none');
	}
	else
	{
		$("#le").css('display', 'block');
		$("#ri").css('display', 'block');
	}
}
