// Globals
var boxWidth = 100; /* width of the mouseover boxes left and right */
var brake = 80; /* for mouseover: the bigger this number, the slower the movement */
var autoScrollSpeed = 100;
var t;
var div;
var itemWidth;
var numberOfItems;
var wrapperWidth;
var maxScrollLeft;
	
window.onload=function () {
	div = $('.galleryWrapper');
	wrapperWidth = div.width();
	
	var extraWrapper = div.wrap('<div style="position:relative;" />');
	extraWrapper.append('<div class="galleryLeftArrow"><div class="galleryLeftArrowInner"></div></div>');
	extraWrapper.append('<div class="galleryRightArrow"><div class="galleryRightArrowInner"></div></div>');
	
	div.fadeTo(600, 1, function(){
		itemWidth = 0;
		var paddingItems = 0;
		var paddingWidth = 0;
		var items = $(".galleryItems li");
		items.each(function(i){
			itemWidth += $(this).outerWidth();

			if(paddingItems == 0 && itemWidth>wrapperWidth) {
				paddingItems = i;
				paddingWidth = itemWidth;
			}
		});
		
		items.slice(0,paddingItems+1).clone(true).appendTo($(".galleryItems"));
		
		maxScrollLeft = itemWidth;
		itemWidth += paddingWidth;
		
		$(".galleryItems").css({ width: itemWidth+"px", overflow: "auto" });
		
		t = setInterval(function() {goRight(1)}, Math.floor(1000 / autoScrollSpeed)); 
	
		div.mouseover(mouseEvent);
		div.mousemove(mouseEvent);			
		div.mouseout(function(e) {
			if(e.relatedTarget.nodeName!="IMG") {
				clearInterval(t);
				if(jQuery('.galleryLeftArrowInner').css('left')=='0px') {
					hideLeftArrow();
					hideRightArrow();
				}
				t = setInterval(function() {goRight(1)}, Math.floor(1000 / autoScrollSpeed));
			}
		});
	});
}

function mouseEvent(e) {
	var leftPos=div.scrollLeft();
	
	if(jQuery('.galleryLeftArrowInner').css('left')!='0px') {
		showLeftArrow();
		showRightArrow();
	}

	clearInterval(t);
	
	var relPos= e.pageX-$('.galleryWrapper').offset().left;
	
	if (relPos>(wrapperWidth-boxWidth)) {
		var acceleration = (((relPos-(boxWidth+100))/brake)^2);
		t = setInterval(function() {goRight(acceleration)}, 13); 
	
	} else if(relPos<boxWidth) {
		var acceleration = (((wrapperWidth-relPos-(boxWidth+100))/brake)^2);
		t = setInterval(function() {goLeft(acceleration)}, 13); 
	}	

}

function goLeft(acceleration) {
	if(div.scrollLeft()>0) {
		div.scrollLeft(div.scrollLeft()-acceleration);
	} else {
		div.scrollLeft(maxScrollLeft);
	}
}

function goRight(acceleration) {
	// check that lightbox is not open
	if ($("#jquery-overlay").length <= 0) {
		if(div.scrollLeft()<maxScrollLeft) {
			div.scrollLeft(div.scrollLeft()+acceleration);
		} else {
			div.scrollLeft(0);
		}
	}
}

function showLeftArrow() {
	jQuery('.galleryLeftArrowInner').animate({left : '0px'}, 200);
}

function hideLeftArrow() {
	jQuery('.galleryLeftArrowInner').animate({left : '23px'}, 200);
}


function showRightArrow() {
	jQuery('.galleryRightArrowInner').animate({left : '0px'}, 200);
}

function hideRightArrow() {
	jQuery('.galleryRightArrowInner').animate({left : '-23px'}, 200);
}