var variable=0;
var sirina=382;
var sirina_holderja=0;
var permision=1;

function refroll()
{
	$$('img.refroll').each(function(el){
		//INICIALIZACIJA - begin
		var myTween = new Fx.Tween(el,{
			duration: 0,
			link: 'chain'
		});
		myTween.start('opacity', 1);	
		//INICIALIZACIJA - end
		/*
		el.addEvent('mouseenter',function(){
			el.fade(0.1);
		});
		el.addEvent('mouseout',function(){
			el.fade(1);
		});
		*/
	});
}

function refclick()
{
	if($('reference_left') && $('reference_right') && $('reference_container'))
		{
		var levi=$('reference_left');
		var desni=$('reference_right');
		var fastScroll = new Fx.Scroll($('reference_container'), {
			wait: false,
			duration: 0,
			offset: {'x': 0, 'y': 0}
			//transition: Fx.Transitions.Bounce.easeOut
		});
		var scroll = new Fx.Scroll($('reference_container'), {
			wait: false,
			duration: 1200,
			offset: {'x': 0, 'y': 0},
			onComplete: function(){
				//POTREBNO ZA SKROLANJE LEVO
				var div_otrok=$$('div.reference_repeat')[0];
				var stevilo_otrok=div_otrok.getChildren('img').length;
				//alert(stevilo_otrok);
				//alert(variable);
				//alert(stevilo_otrok*3*sirina-sirina);
							
				if(variable==0)
				{
					//IZRAČUN SCROLLA ZA NAZAJ V PRIMERU KONCA
					variable=sirina*stevilo_otrok;
					fastScroll.start(variable, 0);
				}
				else if(variable==(stevilo_otrok*3*sirina-sirina))
				{
					//IZRAČUN SCROLLA ZA NAPREJ V PRIMERU KONCA
					variable=stevilo_otrok*2*sirina-(sirina);
					fastScroll.start(variable, 0);
				}
				//NASTAVIM PRAVICE DA SE LAHKO NAPREJ SCROLL-a
				permision=1;
			}
			//transition: Fx.Transitions.Bounce.easeOut
		});
		
		
		levi.addEvent('click',function(){
			//scroll dol
			if(permision!=0)
			{
				//PREGLEDAM KOLKO JIH JE
				var li=$$('div#gallery_intro_hol');
				if(variable+sirina>sirina)
				{
					variable-=sirina;
				}
				permision=0;
				scroll.start(variable , 0);
			}		
		});
		desni.addEvent('click',function(){
			//scroll gor
			if(permision!=0)
			{
				//PREGLEDAM KOLKO JIH JE
				var li=$$('div#gallery_intro_hol');
				if(variable<(sirina_holderja-(sirina)))
				{	
					variable+=sirina;
				}
				permision=0;
				scroll.start(variable, 0);
			}
		});
	}
}

function setWidth()
{
	//NASTAVI VELIKOST DIV-a KI VSEBUJE SLIKE GALERIJE
	if($('reference_container_inner'))
		{
		var div_otrok=$$('div.reference_repeat')[0];
		var stevilo_otrok=div_otrok.getChildren('img').length;
		var stevilo_divov=$$('div.reference_repeat').length;
		//alert(stevilo_divov);
		var cont=$('reference_container_inner');
		var st=stevilo_divov*stevilo_otrok;
		if(cont && st)
		{
			sirina_holderja=sirina*st;
			//alert(sirina_holderja);
			cont.setStyle('width',sirina_holderja);
		}
	}
	//SKROLANJE NA SREDINO
	if($('reference_container')){
		var fastScroll = new Fx.Scroll($('reference_container'), {
			wait: false,
			duration: 0,
			offset: {'x': 0, 'y': 0}
			//transition: Fx.Transitions.Bounce.easeOut
		});
		var div_otrok=$$('div.reference_repeat')[0];
		var stevilo_otrok=div_otrok.getChildren('img').length;
		//alert(stevilo_otrok);
		//variable=stevilo_otrok*sirina+sirina;		//TO JE BILO NAŠTIMANO DA SE SKROLA NA SREDI, SEDAJ PA SE SKROLA NA PRVI SREDNJI ELEMENT
		variable=stevilo_otrok*sirina;
		//alert(variable);
		fastScroll.start(variable,0);
	}
}

window.addEvent('domready',function(){
    if(count_img>1){
		refroll();
		setWidth();
		refclick();
	}
});
