/*
	FG Slider 1.0
	written by Filip Hladík
	modifed for CHAR02
	http://www.fg.cz/
	created 17. 9. 2009

	Built for jQuery library
	http://jquery.com
	
*/

function fgSlider(objSlider, idSlider) {
	this.slider = $("#"+idSlider);
	this.itemCount = 0;
	this.itemHeight = 0;
	this.containerWidth = 0;
	this.contentWidth = 0;
	this.marginLeft = 0;
	this.driftStep = 0;

	this.init = function() {
		this.itemHeight = $(".scrollContainer li:first", this.slider).height();
		this.containerWidth = $(".scroll", this.slider).width();
		this.itemCount = parseInt($(".scrollContainer li", this.slider).size());
		this.driftStep = this.containerWidth/2;

		for(i=0;i<this.itemCount;i++){
			this.contentWidth += ($(".scrollContainer li:eq("+i+")", this.slider).width() + parseInt($(".scrollContainer li:eq("+i+")", this.slider).css("margin-right"))); 		
		}
		this.contentWidth += 1;
		//alert(this.contentWidth);
		$(this.slider).append('<a href="#" class="scrollButtons previous" onclick="'+objSlider+'.scrollPrevious(); return false"></a>');
		$(this.slider).append('<a href="#" class="scrollButtons next" onclick="'+objSlider+'.scrollNext(); return false"></a>');
	   	$(".scroll",this.slider).css("height",this.itemHeight+"px").css("overflow","hidden");

		$(".scrollButtons.previous", this.slider).addClass("disabled");
		if (this.containerWidth > this.contentWidth) $(".scrollButtons.next", this.slider).addClass("disabled");

		$(".scrollContainer", this.slider).css("width", this.contentWidth);
	}

	this.scrollNext = function() {
		if(!($(".scrollButtons.next", this.slider).hasClass("disabled"))){
			$(".scrollButtons.previous", this.slider).removeClass("disabled");

			if((-1*(this.marginLeft - this.driftStep)) + this.containerWidth > this.contentWidth){
				this.marginLeft = (this.containerWidth - this.contentWidth);
				$(".scrollButtons.next", this.slider).addClass("disabled");
			} else {
				this.marginLeft -= this.driftStep;
			}

			$(".scrollContainer", this.slider).animate({
				marginLeft: this.marginLeft+"px"
			},300);
		}

		return false;
	}

	this.scrollPrevious = function() {
		if(!($(".scrollButtons.previous", this.slider).hasClass("disabled"))){
			$(".scrollButtons.next", this.slider).removeClass("disabled");

			if((this.marginLeft + this.driftStep) >= 0){
				this.marginLeft = 0;
				$(".scrollButtons.previous", this.slider).addClass("disabled");
			} else {
				this.marginLeft += this.driftStep;
			}

			$(".scrollContainer", this.slider).animate({
				marginLeft: this.marginLeft+"px"
			},300);
		}

		return false;
	}

	this.init();
}
