/*Fotogalerija v1.0 light
development by Marko Žabčić*/
function f(){this.blur(); return false; }
function $(id){return document.getElementById(id);}

function addEvent(elm, evType, fn, useCapture) {
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else {
		elm['on' + evType] = fn;
	}
}



function fotogalerija(type){	


	var speed = 32;
	var easeout = 7;
	var offsetLeft = 20;
	var loadtxt = 'Slika se učitava';
	var loaddonemsg = 'Slika je učitana';
	var loaderrormsg = 'Slika je nemoguće učitati molimo Vas da odaberete drugu!';
	var page_items = 4;
	
	var box = $('box');
	var next = $('next');
	var prev = $('prev');
	var thumbs = $('thumbs');
	var preload = $('preload');
	var msg = $('msg');
	var pic = $('pic');
	var slika = $('slika');
	var alpha = $('alpha');
	var btn_close = $('close');
	preload.style.display = 'none';
	thumbs.style.display = 'block';
	prev.onclick = f;
	next.onclick = f;
	var athumbs = thumbs.getElementsByTagName('div');	

	for(var i=0; i<athumbs.length; i++){
		//athumbs[i].onclick = showPic;
		}
		//btn_close.onclick = hide;
		
		function hide(){
			/*slika.style.display = 'none';*/
			sizeit(slika.offsetHeight,0,slika);
			return false;
			}
		function loadmsg(){
				slika.style.display = 'block';
				msg.style.display = 'none';		
			}
		
		function showPic(){
			/*alert(this.href);*/
			msg.style.display = 'block';
			msg.className = '';
			msg.innerHTML = loadtxt;
			pic.onload = function(){
			/*alpha.style.height = pic.height;	*/
			  setTimeout(loadmsg,200);
			msg.innerHTML = loaddonemsg;	
			}
			
			pic.onerror = function(){
				msg.innerHTML = loaderrormsg;
				msg.className = 'error';
				slika.style.display = 'none';
				}
			pic.src = this.href;
			return false;
			}
	
	var timer;
	var x=offsetLeft;
	var posx=0;
	var thewidth = 0;
	var real_thumbs_num = 0;

	for (var i = 0;i<athumbs.length ;i++ )
	{
		thewidth += athumbs[i].offsetWidth;
		if( athumbs[i].offsetWidth>1){real_thumbs_num++; }
		else{athumbs[i].style.display = 'none';}
		//console.log(athumbs[i].offsetWidth)
	}
//console.log('Pravi broj thumbnailova:'+real_thumbs_num);
//console.log('broj thumbnailova:'+ athumbs.length);


	var b_width = box.offsetWidth;
	var width = (athumbs.length*40 + athumbs.length*15);
	if(type == 'big')width = (athumbs.length*168 + athumbs.length*15);
	//console.log(thewidth);
	width = thewidth+real_thumbs_num*15;
	var page = (page_items*40 +page_items*15);
	thumbs.style.width = width +'px';
	
	function moveby(y,elem,dir,speedup){
		timer = setInterval(resize,speed);	
		var a=0;
		function resize(){
		a=a+speedup;
				x+=y+a; //ease out
				elem.style.left =x+'px';
				posx = elem.style.left;
				posx = posx.substring(0,posx.indexOf('px'));
				
				goleft=true;
				if(dir=='left' && posx<b_width-width){
					elem.style.left = (b_width-width) + 'px';
					clearInterval(timer);
					
				}
				if(dir=='right' && posx>20){
					elem.style.left = 20 + 'px';
					clearInterval(timer);	
					
				}
		}		
	}


var gl;

function slideby(y,elem,dir,speedup){
		timer = setInterval(resize,speed);	
		var a=0;
		function resize(){
		a=a+speedup;
				x-=y; //ease out
				elem.style.left =x+'px';

				posx = elem.style.left;
				posx = posx.substring(0,posx.indexOf('px'));
				
			
				
				
				
				if( posx<b_width-width){
						y = -y;
					//elem.style.left = (b_width-width) + 'px';
					clearInterval(timer);
					//console.info('End left')
					
						timer= setInterval(resize,speed);
						gl= true;
					
				}
				if(posx>20){			
				y = -y;
					//elem.style.left = (b_width-width) + 'px';
					clearInterval(timer);
					//console.info('End left')
					
						timer= setInterval(resize,speed);
						gl = false;
						
					
					
				}	
				

				
					
					
				
		}		
	}








	
	function sizeit(x,y,elem){	
		var timer = setInterval(resize,speed);
	 	var ex = y;
		b = (((x-y)>0));
		function resize(){
			x-=(x-ex)/easeout; //ease out smanji se
			x = -x;
			elem.style.height = x+'px';		
			if(b) a = (x<=ex+0.5);
			if(!b)a = (x>=ex-0.5);
			
			if(a){
				clearInterval(timer);
			if(b) {
				elem.style.height = 'auto';
				elem.style.display = 'none';
				}
			}	
		}		
	}


if(width-35>b_width){
prev.onmouseover = function(){
clearInterval(timer);
		moveby(15,thumbs,'right',0.1)
		}
		prev.onmouseout = function(){
		clearInterval(timer);
		
		}
		
		next.onmouseover = function(){
		clearInterval(timer);
		moveby(-15,thumbs,'left',-0.1)
		}
		next.onmouseout = function(){
		clearInterval(timer);
	
		}


		if(type == 'big'){		
		slideby(2,thumbs,'right',0.1)
		thumbs.onmouseover = function(){
			clearInterval(timer);		
		}	

		thumbs.onmouseout = function(){
		if(gl == true) {	slideby(-2,thumbs,'right',0.1)	}
		else{	slideby(2,thumbs,'right',0.1)	}

		}	

}

		}




}


function init(){
if($('product-scroll')) fotogalerija();
if($('product-scroll').className == 'bigone') fotogalerija('big');
}


addEvent(window, 'load', init)