/*
	FG Slider 1.1
	written by Filip Hladík (FG Forrest, a.s. - http://www.fg.cz)

	jQuery needed
	http://jquery.com

	//markup example
	<div class="slider" id="sliderYears">
		<ul>
			<li><img src="/img/u/motive-01.jpg" alt="" height="100" width="145" /></li>
			<li><img src="/img/u/motive-02.jpg" alt="" height="100" width="145" /></li>
			<li><img src="/img/u/motive-03.jpg" alt="" height="100" width="145" /></li>
			<li><img src="/img/u/motive-04.jpg" alt="" height="100" width="145" /></li>
			<li><img src="/img/u/motive-05.jpg" alt="" height="100" width="145" /></li>
			<li><img src="/img/u/motive-06.jpg" alt="" height="100" width="145" /></li>
			<li><img src="/img/u/motive-07.jpg" alt="" height="100" width="145" /></li>
			<li><img src="/img/u/motive-08.jpg" alt="" height="100" width="145" /></li>
		</ul>
	</div><!-- slider -->

	<script type="text/javascript">
	//<![CDATA[
		$(document).ready(function() {
			slider1 = new fgSlider("slider1", "sliderYears");
		});
	//]]>
	</script>

	// changelog

	====
	v1.1 - 2. 8. 2010
	- HTML code reduction
	- script counts with item margins
	- pridana funkce slideToPosition - kdykoliv je mozne posunout se na n-ty prvek (0-n) 

	=====
 	v1.0 - 17. 9. 2009
 	- initial release
 */
function FG_Slider(objSlider, idSlider) {
	this.slider = $("#"+idSlider);
	this.itemWidth = 0;
	this.itemMargins = 0;
	this.itemCount = 0;
	this.itemsVisible = 0;
	this.activeItem = 0;
	this.animationInProgress = false;

	this.init = function() {

		if ($.browser.msie && ($.browser.version.substr(0,3)=="6.0" || $.browser.version.substr(0,2)=="5.")) { return; }

		this.itemWidth = $("ul li:first", this.slider).width();
		this.itemMargins = parseInt($("ul li:first", this.slider).css("margin-left"))+parseInt($("ul li:first", this.slider).css("margin-right"));
		this.itemCount = parseInt($("ul li", this.slider).size());
		this.itemsVisible = parseInt(parseInt($(this.slider).width())/this.itemWidth);

		$(this.slider).append('<a href="javascript:'+objSlider+'.slidePrevious()" class="slideButtons previous">Předchozí</a>');
		$(this.slider).append('<a href="javascript:'+objSlider+'.slideNext()" class="slideButtons next">Následující</a>');

		$(".slideButtons.previous", this.slider).addClass("disabled");
		if (this.itemCount <= this.itemsVisible) $(".slideButtons.next", this.slider).addClass("disabled");

		$("ul", this.slider).wrap('<div class="slider-wrapper" style="overflow: hidden;"></div>');

		var ulWidth = this.itemCount*(this.itemWidth+this.itemMargins);
		$("ul", this.slider).css("width", ulWidth);
	};

	this.slidePrevious = function() {
		if (this.animationInProgress || this.activeItem <= 0) return;
		this.activeItem--;
		this.animationInProgress = true;
		$("ul", this.slider).animate({
			marginLeft: -1*(this.itemWidth + this.itemMargins)*this.activeItem+"px"
		}, 500, function() {
			eval(objSlider+".animationInProgress = false");
		});
		$(".slideButtons.next", this.slider).removeClass("disabled");
		if (this.activeItem <= 0) {
			$(".slideButtons.previous", this.slider).addClass("disabled");
		}

	};

	this.slideNext = function() {
		if (this.animationInProgress || this.activeItem >= (this.itemCount - this.itemsVisible)) return;
		this.activeItem++;
		this.animationInProgress = true;
		$("ul", this.slider).animate({
			marginLeft: -1*(this.itemWidth + this.itemMargins)*this.activeItem+"px"
		}, 500, function() {
			eval(objSlider+".animationInProgress = false");
		});

		$(".slideButtons.previous", this.slider).removeClass("disabled");
		if (this.activeItem >= (this.itemCount - this.itemsVisible)) {
			$(".slideButtons.next", this.slider).addClass("disabled");
		}
	};

	this.slideToPosition = function(pos) {
		if (pos == -1)
			return;
		$("ul li:eq("+pos+")", this.slider).addClass("active");
		var center = Math.floor(this.itemsVisible/2);
		var shiftItems = pos - center;
		if (shiftItems < 0) {
			shiftItems = 0;
		}
		if (pos >= (this.itemCount - center)) {
			var centerPosition = this.itemCount - 1 - 2*center ;
			shiftItems -= (pos-centerPosition);
			shiftItems += center;
		}

		this.activeItem = shiftItems;

		$(".slideButtons", this.slider).removeClass("disabled");
		if (this.activeItem <= 0) {
			$(".slideButtons.previous", this.slider).addClass("disabled");
		}
		if (this.activeItem >= (this.itemCount - this.itemsVisible)) {
			$(".slideButtons.next", this.slider).addClass("disabled");
		}

		$("ul", this.slider).animate({
			marginLeft: -1*(this.itemWidth + this.itemMargins)*(shiftItems)+"px"
		}, 500);
	};

	this.init();
}
