// Globals
var boxWidth = 40; /* width of the mouseover boxes left and right */
var brake = 120; /* the bigger this number, the slower the movement */
var t;
var div;
var itemWidth;
var numberOfItems;
var wrapperWidth;
	
window.onload=function () {
	numberOfItems = jQuery(".fpGalleryItems").children().length;

	if (numberOfItems>0) {
		jQuery('.fpGallery').fadeIn('slow', function(){
			div = jQuery('.galleryWrapper');
			leftArrow = jQuery('.galleryLeftArrowInner');
			rightArrow = jQuery('.galleryRightArrowInner');	
			leftShadow = jQuery('.galleryLeftShadow');
			rightShadow = jQuery('.galleryRightShadow');				
			
			wrapperWidth = div.width();
			
			if(numberOfItems>3) {
				itemWidth = jQuery(".fpGalleryItems").children(":first-child").outerWidth();
				jQuery(".fpGalleryItems").css({ width: (itemWidth*numberOfItems)+"px", overflow: "auto" });
				showRightArrow();
			
				div.mouseover(mouseEvent);
				div.mousemove(mouseEvent);			
				div.mouseout(function() {
					clearInterval(t);
				});
								
				leftArrow.mouseover(mouseEvent);
				leftArrow.mousemove(mouseEvent);			
				leftArrow.mouseout(function() {
					clearInterval(t);
				});
				
				rightArrow.mouseover(mouseEvent);
				rightArrow.mousemove(mouseEvent);			
				rightArrow.mouseout(function() {
					clearInterval(t);
				});
				
				leftShadow.mouseover(mouseEvent);
				leftShadow.mousemove(mouseEvent);			
				leftShadow.mouseout(function() {
					clearInterval(t);
				});
				
				rightShadow.mouseover(mouseEvent);
				rightShadow.mousemove(mouseEvent);			
				rightShadow.mouseout(function() {
					clearInterval(t);
				});				
			}
	
			jQuery('.fpGalleryItems a').lightBox({fixedNavigation:false,txtImage: '', txtOf: 'af '});
		});
	}
}

function mouseEvent(e) {
	//console.log('mouseEven');
	var leftPos=div.scrollLeft();
	var maxScrollLeft = (itemWidth*numberOfItems)-wrapperWidth;

	//console.log('maxScrollLeft: '+ maxScrollLeft);
	//console.log('leftPos: '+ leftPos);

	if(leftPos>0 && jQuery('.galleryLeftArrowInner').css('left')!='0px') {
		showLeftArrow();
	} else if(leftPos<=0 && jQuery('.galleryLeftArrowInner').css('left')=='0px') {
		hideLeftArrow();
	}

	if(leftPos<maxScrollLeft && jQuery('.galleryRightArrowInner').css('left')!='0px') {
		showRightArrow();
	} else if(leftPos>=maxScrollLeft && jQuery('.galleryRightArrowInner').css('left')=='0px') {
		hideRightArrow();
	}
	
	if((leftPos>=0) && (leftPos<=maxScrollLeft)) {
		moveIt(e, wrapperWidth);
	}
}


function moveIt(e, wrapperWidth) {
	clearInterval(t);
	
	var relPos= e.pageX-jQuery('.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) {
	var div = jQuery('.galleryWrapper');
	div.scrollLeft(div.scrollLeft()-acceleration);
}

function goRight(acceleration) {
	var div = jQuery('.galleryWrapper');
	div.scrollLeft(div.scrollLeft()+acceleration);
}

function showLeftArrow() {
	jQuery('.galleryLeftArrowInner').animate({left : '0px'}, 200);
}

function hideLeftArrow() {
	jQuery('.galleryLeftArrowInner').animate({left : '28px'}, 200);
}


function showRightArrow() {
	jQuery('.galleryRightArrowInner').animate({left : '0px'}, 200);
}

function hideRightArrow() {
	jQuery('.galleryRightArrowInner').animate({left : '-28px'}, 200);
}
