var variable=0;
var sirina=87;
var sirina_holderja=0;
var permision=1;
var stevilo_vidnih=7;
var hitrost=700;

function refroll()
{
	//FUNCKCIJA ZA GUMBE OVER EFEKT
	$$('img.refroll').each(function(el){
		var hitrost_gumbov=400;
		//INICIALIZACIJA - begin
		var myTween = new Fx.Tween(el,{
			duration: 0,
			link: 'chain'
		});
		myTween.start('opacity', 0.1);	
		//INICIALIZACIJA - end
		el.addEvent('mouseover',function(){
			el.set('morph', {duration: hitrost_gumbov});
			el.morph({opacity: 1});
		});
		el.addEvent('mouseout',function(){
			el.set('morph', {duration: hitrost_gumbov});
			el.morph({opacity: 0.1});
		});
	});
}

function refclick()
{
	if($('reference_left') && $('reference_right') && $('scroll_roller_bck'))
		{
		var levi=$('reference_left');
		var desni=$('reference_right');
		var fastScroll = new Fx.Scroll($('scroll_roller_bck'), {
			wait: false,
			duration: 0,
			offset: {'x': 0, 'y': 0}
			//transition: Fx.Transitions.Bounce.easeOut
		});
		var scroll = new Fx.Scroll($('scroll_roller_bck'), {
			wait: false,
			duration: hitrost,
			offset: {'x': 0, 'y': 0},
			onComplete: function(){
				//POTREBNO ZA SKROLANJE LEVO
				var div_otrok=$$('div.scroll_repeat')[0];
				var stevilo_otrok=div_otrok.getChildren('a').length;
				//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);
				}
				//pazi (pri tem primeru -7 vidnim)
				else if(variable==(stevilo_otrok*3*sirina-sirina-(sirina*stevilo_vidnih)))
				{
					//IZRAČUN SCROLLA ZA NAPREJ V PRIMERU KONCA (pazi - stevilo vidnih - zadnji oklepaj)
					//alert(variable);
					variable=stevilo_otrok*2*sirina-sirina-(stevilo_vidnih*sirina);
					//alert(variable);
					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
				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
				//alert(variable+" "+sirina_holderja+" "+sirina);
				var div_otrok=$$('div.scroll_repeat')[0];
				var stevilo_otrok=div_otrok.getChildren('a').length;
				var neki=stevilo_otrok*3*sirina-sirina;
				//alert(variable+"<"+neki);
				if(variable<(sirina_holderja-(sirina)))
				{	
					variable+=sirina;
				}
				permision=0;
				scroll.start(variable , 0);
			}
		});
	}
}

function setWidth()
{
	//NASTAVI VELIKOST DIV-a KI VSEBUJE SLIKE GALERIJE
	
	if($('scroll_roller') && $$('div.scroll_repeat'))
	{
		
		var div_otrok=$$('div.scroll_repeat')[0];
		var stevilo_otrok=div_otrok.getChildren('a').length;
		//var stevilo_divov=$$('div.scroll_repeat').length;
		var stevilo_divov=$('scroll_roller').getChildren('div').length; //IE8 problem
		//alert(stevilo_divov);
		var cont=$('scroll_roller');
		var st=stevilo_divov*stevilo_otrok;
		if(cont && st)
		{
			sirina_holderja=sirina*st;
			//alert(sirina_holderja);
			cont.setStyle('width',sirina_holderja);
		}
		
		//SKROLANJE NA SREDINO
		var fastScroll = new Fx.Scroll($('scroll_roller_bck'), {
			wait: false,
			duration: 0,
			offset: {'x': 0, 'y': 0}
			//transition: Fx.Transitions.Bounce.easeOut
		});
		var div_otrok=$$('div.scroll_repeat')[0];
		var stevilo_otrok=div_otrok.getChildren('a').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);		
	}
}

function setInnerWidth(){
	if($$('div.scroll_repeat')[0]){
		var div_otrok=$$('div.scroll_repeat')[0];
		var stevilo_otrok=div_otrok.getChildren('a').length;
		$$('div.scroll_repeat').each(function(el){
			var notranja_sirina=stevilo_otrok*sirina;
			el.setStyle('width',notranja_sirina);
		});
	}
}

function fog_it(){
	var hitrost_megle=600;
	var start_opacity=0.35;
	if($('scroll_roller'))
	{
		$('scroll_roller').getChildren('div').each(function(el){	//IE8 problem	
			el.getChildren('a').each(function(el2){
				//na začetku nastavi
				el2.set('morph', {duration: 0});
				el2.morph({opacity: start_opacity});
				//onmouse over se zjasni
				el2.addEvent('mouseover',function(){
					el2.set('morph', {duration: hitrost_megle});
					el2.morph({opacity: 0.99});
				})
				//onmouse out se potemni
				el2.addEvent('mouseout',function(){
					el2.set('morph', {duration: hitrost_megle});
					el2.morph({opacity: start_opacity});
				})
			});
		});
	}
}

function duplicate_it(){
	if($$('div.scroll_repeat') && $('scroll_roller'))
	{
		var what_to_clone=$$('div.scroll_repeat')[0].clone();
		var what_to_clone2=$$('div.scroll_repeat')[0].clone();
		//dvakrat kloniram, ker morajo biti 3 enake kopije, da stvar deluje (za levo in desno ciklanje)
		$('scroll_roller').grab(what_to_clone);
		$('scroll_roller').grab(what_to_clone2);
		
		//var copy = $$('div.item')[0].clone();
		//$('inner_intro_slide').grab(copy);
		
		showLarge();
	}
}

function num_photos(){
	if($$('div.scroll_repeat')[0])
	{
		var div_otrok=$$('div.scroll_repeat')[0];
		var stevilo_otrok=div_otrok.getChildren('a').length;
		if(stevilo_otrok<stevilo_vidnih)
		{
			alert("NAPAKA: Premalo naloženih slik!");
		}
	}
}
function showLarge(){
//Source velike slike
$$('#main_right div.intro_news div.right img').each(function(el){
		var p = el.getProperty('src');
	//source majhne slike, parsanje image id-ja	
	$$('#scroll_roller a img').each(function(img){
		var o = img.getProperty('src');
		var lastSlash = o.lastIndexOf('/') + 1;
		var src = '/image/medium/' + o.substring(lastSlash);
	//click event
		img.getParent('a').addEvent('click', function(event){
				event.stop();				
				el.setProperty('src', src);
				//start
				var req = new Request({
					url: 'sys/test?iid='+o.substring(lastSlash),
					onSuccess: function(){
						$('ajax_intro_23').load('sys/test?iid='+o.substring(lastSlash));
						}
				});
				req.send();
				//end
		});
	});
});
}


window.addEvent('domready',function(){
	//dupliciraj
	duplicate_it();
	refroll();
	setWidth();
	setInnerWidth();
	refclick();
	
	//zameglim vse slike
	fog_it();
	//v primeru da je premalo slik javi napako
	num_photos();
});
