// 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(".galleryItems").children().length;
	
	if (numberOfItems==2) {
		jQuery(".smallProductImages").addClass("twoImgs");
	} else if (numberOfItems==1) {
		jQuery(".smallProductImages").addClass("oneImg");
	}
	
	if (numberOfItems>0) {
		jQuery('.smallProductImages').fadeIn('slow', function(){
			div = jQuery('.galleryWrapper');
			leftArrow = jQuery('.galleryLeftArrowInner');
			rightArrow = jQuery('.galleryRightArrowInner');			
			wrapperWidth = div.width();
			
			if(numberOfItems>3) {
				itemWidth = jQuery(".galleryItems").children(":first-child").outerWidth();
				jQuery(".galleryItems").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);
				});
			}
	
			jQuery('.galleryItems a').lightBox({fixedNavigation:false,txtImage: 'Image', txtOf: 'of'});
		});
	}
}

function mouseEvent(e) {
	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 : '23px'}, 200);
}


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

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