
var broj=0;
var elemPosition = [0,0];
var liWidth = 0;
var currElemPosition = [0,0];
var galleryHeight = 200;
var clickBrojac=0;
var totalItems = 0;

function easeInOut(minValue,maxValue,totalSteps,actualStep,powr) {
	
	//Generic Animation Step Value Generator By www.hesido.com
	var delta = maxValue - minValue;
	var stepp = minValue+(Math.pow(((1 / totalSteps)*actualStep),powr)*delta);
	return Math.ceil(stepp)
}

function doPosChangeMem(elem,startPos,endPos,steps,intervals,powr) {
//Position changer with Memory by www.hesido.com
	if (elem.posChangeMemInt) window.clearInterval(elem.posChangeMemInt);
	var actStep = 0;
	elemPosition = [startPos,endPos];
	
	elem.posChangeMemInt = window.setInterval(
		function() {
			elemPosition = [
				easeInOut(startPos[0],endPos[0],steps,actStep,powr),
				//easeInOut(startPos[1],endPos[1],steps,actStep,powr)
				];
			//document.write(elem.currentPos[0]);
			//alert(elemPosition[0]);
			elem.css('left', elemPosition[0] + 'px');
			//elem.style.left = elem.currentPos[0]+"px";
			//elem.style.top = elem.currentPos[1]+"px";
			actStep++;
			currElemPosition[0] = elemPosition[0];
			if (actStep > steps) {
				//currElemPosition = elemPosition;
				window.clearInterval(elem.posChangeMemInt);
			}
		}
		,intervals)

}

//	Init motion animation
//var moveIt = document.getElementById('moveit');
//if (moveIt != null) moveIt.onclick = moveToBottom;

function moveLeft(elem) {
	if (clickBrojac>0) {
		if (!this.currentPos) this.currentPos = [currElemPosition[0],0]; //if no mem is set, set it first;
		//alert(currElemPosition[0]);
		doPosChangeMem(elem,[currElemPosition[0],0],[currElemPosition[0]+1*liWidth,0],20,20,0.5);
		clickBrojac--;
		//move to a random position for demonstration
	}
}

function moveRight(elem) {
	//alert(totalItems);
	if (clickBrojac<(totalItems-4)) {
		if (!this.currentPos) this.currentPos = [currElemPosition[0],0]; //if no mem is set, set it first;
		//alert(currElemPosition[0]);
		doPosChangeMem(elem,[currElemPosition[0],0],[currElemPosition[0]-1*liWidth,0],20,20,0.5);
		//move to a random position for demonstration
		clickBrojac++;
	}
}

function initGallery() {
	
	$('div#galleryItems').each(function () {
		
		var ul = $('ul', this);
    	var productWidth = ul.innerWidth() - $(this).outerWidth();
    	
    	//liWidth
		var li = $('li', ul);
		
		//u odnosu na najveci namesti visinu galerije
		($('li',ul)).each(function() {
			totalItems++;
			if ($(this).outerHeight()>galleryHeight) {
				galleryHeight = $(this).outerHeight();
			}
		});
		//sada treba svi da budu iste visine
		($('li',ul)).each(function() {
			$(this).css('height',galleryHeight+'px');
		});
		
		liWidth = li.outerWidth()+65;
		
		//alert(this.style.height);
		this.style.height = galleryHeight+'px';
		//alert(galleryHeight);
    	
    	$('div#leftArrow', document).click(function () {
    		//ul.css('left', '-' + 20 + 'px');
    		moveLeft(ul);
    	});
    	
    	$('div#rightArrow', document).click(function () {
    		//ul.css('left', '-' + 20 + 'px');
    		moveRight(ul);
    	});
    	
    
  });
}


