/**
 * Ausgeklappte BreadNav Scrollen
 * 
 * @author Holger Szüsz
 */

var BreadScroll = {
	itemWidth: 		130,
	realWidth:		0,
	offsetRight:	160,
	offsetLeft:		60,
	maxLeft:		0,
	
	resize: function(jQueryNotationRootElement) {
		var windowWidth = $(window).width();
		this.realWidth	= $(window).width()-140;
		
		$(jQueryNotationRootElement+'>div.scrollable').width(((windowWidth-this.offsetLeft)-this.offsetRight));
		
		this.getMaxMarginLeft(jQueryNotationRootElement);
		
		if (($(jQueryNotationRootElement + '>div.scrollable>div.items').css('margin-left').replace(/px/, "") / -1) > (this.maxLeft / -1)) {
			$(jQueryNotationRootElement + '>div.scrollable>div.items').animate({
				marginLeft: this.maxLeft + "px"
			}, 500);
		}
		
		this.checkScrolling(jQueryNotationRootElement);
	},
	
	getMaxMarginLeft: function(jQueryNotationRootElement) {
		var itemCount 			= $(jQueryNotationRootElement+'>div.scrollable>div.items>div').length;
		var visibleItemCount	= Math.floor($(jQueryNotationRootElement+'>div.scrollable').width() / this.itemWidth);
		
		this.maxLeft = "-" + (this.itemWidth * (itemCount-visibleItemCount) -20);
	},
	
	checkScrolling: function(jQueryNotationRootElement) {
		var itemCount 			= $(jQueryNotationRootElement+'>div.scrollable>div.items>div').length;
		var visibleItemCount	= Math.floor(this.realWidth / this.itemWidth);
		
		if(itemCount <= visibleItemCount) {
			$(jQueryNotationRootElement+'>div.back').fadeOut(500);
			$(jQueryNotationRootElement+'>div.go').fadeOut(500);
			$(jQueryNotationRootElement+'>div.scrollable').css('margin-left','20px');
			$(jQueryNotationRootElement+'>div.scrollable').width(($(window).width()-140));
		} else {
			$(jQueryNotationRootElement+'>div.back').fadeIn(500);
			$(jQueryNotationRootElement+'>div.go').fadeIn(500);
			$(jQueryNotationRootElement+'>div.scrollable').css('margin-left','0');
			$(jQueryNotationRootElement+'>div.scrollable').width((($(window).width()-this.offsetLeft)-this.offsetRight));
		}
	},
	
	moveRight: function(jQueryNotationRootElement) {
		this.getMaxMarginLeft(jQueryNotationRootElement);
		
		if (($(jQueryNotationRootElement + '>div.scrollable>div.items').css('margin-left').replace(/px/, "") / -1) < (this.maxLeft / -1)) {
			var marginLeftWert = ($(jQueryNotationRootElement + '>div.scrollable>div.items').css('margin-left').replace(/px/, "")) - 200;
			
			if((marginLeftWert/-1) > (this.maxLeft/-1)) {
				marginLeftWert = this.maxLeft;
			}
			
			$(jQueryNotationRootElement + '>div.scrollable>div.items').animate({
				marginLeft: marginLeftWert + "px"
			}, 500);
		}
	},
	
	moveLeft: function(jQueryNotationRootElement) {
		if($(jQueryNotationRootElement + '>div.scrollable>div.items').css('margin-left').replace(/px/,"") < 0) {
			var marginLeftWert = eval(($(jQueryNotationRootElement + '>div.scrollable>div.items').css('margin-left').replace(/px/,"")) + "+200");
			if(marginLeftWert > 0) {
				marginLeftWert = 0;
			}
			
			$(jQueryNotationRootElement + '>div.scrollable>div.items').animate({
				marginLeft:marginLeftWert + "px"
			},500);
		}
	},
	
	resetMargin: function(jQueryNotationRootElement) {
		$(jQueryNotationRootElement + '>div.scrollable>div.items').animate({
			marginLeft: "0px"
		}, 500);
	}
};

