/**
 * Step Scrolling v1: Tomáš Hospodka - FG Forrest - www.fg.cz
 *
 * Step Scrolling is (c) 2008 FG Forrest and is released under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 * 
 * Vyžaduje jQuery 1.2.3+, jQuery Cookie plugin (http://www.stilbuero.de/2006/09/17/cookie-plugin-for-jquery/)
 *
 */
$.stepscrolling = new Object();
$.fn.stepscrolling = function(o) {
	$.stepscrolling.o = o;
	return this.each(function () {
		$.stepscrolling.init(this,$.stepscrolling.o);
	});
}
/* inicializace */
$.stepscrolling.init = function (ssId,o) {
	var c = $.stepscrolling;
	c.ssId = ssId;
	/** 
	 * Options 
	 */
	c.itemHeight = o.itemHeight ? o.itemHeight : 100;
	c.itemWidth = o.itemWidth ? o.itemWidth : 100;
	c.activeClass = o.activeClass ? o.activeClass : 'act';
	c.totalItems = o.totalItems ? o.totalItems : $('*',c.ssId).size();
	c.horiz = o.direction == 'vertical' ? false : true;
	c.itemsToShow = o.itemsToShow ? o.itemsToShow : 1;
	c.cpId = o.controlPanel ? '#'+o.controlPanel : '#control-panel';
	c.cookie = o.cookieName ? o.cookieName : c.ssId.id;
	/**
	 * Settings
	 */
	 // nastavíme cookie pokud neexistuje, platnost 1 den
	 if(!$.cookie(c.cookie)){
	 	$.cookie(c.cookie, 0);
	 }
	 // zjistíme aktuální pozici prvku při načtení stránky
	var position = $(c.ssId).offset();
	c.defPositionLeft = position.left;
	c.defPositionTop = position.top;
	// nadefinujeme posun o kolik posouvat
	if(c.horiz){
		c.setShift = c.itemWidth; 
	}else{
		c.setShift = c.itemHeight; 
	}
	// pokud je méně položek než je možné zobrazit skryjeme navig panel
	if(c.totalItems<=c.itemsToShow){
		$(c.cpId).hide();
	}
	var defPosition = 0;
	// zjistíme viditelné rozmezí aktivního prvku
	var firtActPosition = 0;
	var lastActPosition = 0;
	$('li',c.ssId).each(function(i){
		if ( $(this).hasClass(c.activeClass) ){
			firstActPosition = (i*c.setShift)*-1;
			lastActPosition = firstActPosition+((c.itemsToShow-1)*c.setShift);
		}
	});
	
	// zjistíme zda hodnota v cookies je ve viditelné zóně
	if($.cookie(c.cookie)>=firstActPosition && $.cookie(c.cookie)<=lastActPosition){
		defPosition = $.cookie(c.cookie);	
	}else{
		defPosition = firstActPosition;	
	}
			
	//alert('Aktuální prvek Position: '+ actPosition+'Cookies: '+$.cookie(c.cookie)+'');
	c.end = (c.itemsToShow*c.setShift) - (c.totalItems*c.setShift);
	
	if(defPosition >= 0){
		defPosition = 0;
		$(c.cpId+' a:first-child').fadeTo("slow", 0.22);
		$(c.cpId+' a:first-child span').css({ cursor:"default" });
	}else if(defPosition <= c.end){
		defPosition = c.end;
		$(c.cpId+' a:last-child').fadeTo("slow", 0.22);
		$(c.cpId+' a:last-child span').css({ cursor:"default" });
	}
	// animovat menu při načtení?
	c.anim = false;
	c.setPos(defPosition);
	c.getInfo(defPosition);
}
$.stepscrolling.setPos = function (v) { 
	var c = $.stepscrolling;
	v = $.intval(v);
	$.cookie(c.cookie, v);
	if(c.horiz) {
		if(c.anim){
			$(c.ssId).animate( { left:v+"px"}, 500 );
		}else{
			$(c.ssId).css("left",v+"px");
		}
	}else{
		if(c.anim){
			$(c.ssId).animate( { top:v+"px"}, 500 );
		}else{
			$(c.ssId).css("top",v+"px");
		}
	}
}
$.stepscrolling.getPos = function() {
	var c = $.stepscrolling;
	var position = $(c.ssId).offset();
	if(c.horiz) {
		return position.left - c.defPositionLeft;
	}
	else {
		return position.top - c.defPositionTop;
	}
}
$.stepscrolling.getInfo = function(pos) {
	var c = $.stepscrolling;
	
	var sItem = ((pos*-1)/c.setShift)+1;
	var eItem = sItem+(c.itemsToShow-1);
	
	$(c.cpId+' div strong:first-child').replaceWith('<strong>' + sItem + '-' + eItem + '</strong>');
	$(c.cpId+' div strong:last-child').replaceWith('<strong>' + c.totalItems + '</strong>');
	
}
$.stepscrolling.next = function() {
	var c = $.stepscrolling;
	var pos = c.getPos();	

	var newPos = (Math.round(pos/c.setShift) * c.setShift) - c.setShift;
	
	if(newPos < c.end){
		newPos = c.end;
		$(c.cpId+' a:last-child').fadeTo("slow", 0.22);
		$(c.cpId+' a:last-child span').css({ cursor:"default" });
	}else if(newPos == c.end){
		$(c.cpId+' a:last-child').fadeTo("slow", 0.22);
		$(c.cpId+' a:last-child span').css({ cursor:"default" });
		$(c.cpId+' a:first-child').fadeTo("slow", 1);
		$(c.cpId+' a:first-child span').css({ cursor:"pointer" });
	}else{
		$(c.cpId+' a:first-child').fadeTo("slow", 1);
		$(c.cpId+' a:first-child span').css({ cursor:"pointer" });
	}
	c.anim = true;
	c.setPos(newPos);
	c.getInfo(newPos);
}
$.stepscrolling.prev = function() {
	var c = $.stepscrolling;
	var pos = c.getPos();	

	var newPos = (Math.round(pos/c.setShift) * c.setShift) + c.setShift;
	
	if(newPos > 0){
		newPos = 0;
		$(c.cpId+' a:first-child').fadeTo("slow", 0.22);
		$(c.cpId+' a:first-child span').css({ cursor:"default" });
	}else if(newPos == 0){
		$(c.cpId+' a:first-child').fadeTo("slow", 0.22);
		$(c.cpId+' a:first-child span').css({ cursor:"default" });
		$(c.cpId+' a:last-child').fadeTo("slow", 1);
		$(c.cpId+' a:last-child span').css({ cursor:"pointer" });
	}else{
		$(c.cpId+' a:last-child').fadeTo("slow", 1);
		$(c.cpId+' a:last-child span').css({ cursor:"pointer" });
	}
	c.anim = true;
	c.setPos(newPos);
	c.getInfo(newPos);
}

$.intval = function(v) {
	v = parseInt(v);
	return isNaN(v) ? 0 : v;
};
/* Obslužné funkce */
$(document).ready(function() {
	$('#items').stepscrolling({
				'itemHeight':88,
				'itemWidth':205,
				'activeClass':'act', // třída označující aktuální položku - default 'act'
				'totalItems':$('li',$('#items')).size(), // celkový počet položek
				'itemsToShow':4, // počet zobrazovaných položek
				'controlPanel':'control-panel', //id controlního panelu default #control-panel
				'direction':'vertical' //'horizontal' scrolling vertikální nebo horizontální (default horizontální)
	});	
});
function goNext() {
	$.stepscrolling.next();
}
function goPrev() {
	$.stepscrolling.prev();
}
function debug() {
	alert($.stepscrolling.getPos());
}
