﻿
i = new Object();
iRotator = {		
		// settings
		crossfade : false,
		speed : 3000,					// 5000ms = 5s	
		transitionSpeed : 0.8, 			//fade in/out	
		layerPos : 100, 				// z-index of top element
		currentImageNumber : null,		//firstImage to show		
		nextImg : null,
		nextImageNumber : 0,
		timer : null,
		first : true,
		img1 : null,
		img2 : null,
		imageList : [],
		
		// debug mode
		debug : false,
		preloaded : null,		
		
		
		init : function (rotatorContainer, list, options){	
			iR_Cont = $(rotatorContainer)
			if (iR_Cont == null){
				if(iRotator.debug) console.error('Container element doesn\'t exist: '+ rotatorContainer )				
				return;
			}
			
			if(list == null) {
				if(iRotator.debug) console.error('Sorry but your image list is empty. Name of your image list is:' + list);
				return;
			}
				
			
				options.sec > iRotator.speed/1000 ? iRotator.speed = options.sec * 1000 : iRotator.speed //set custom initial speed if seconds > 5
				iRotator.crossfade = options.crossfade || iRotator.crosfade
				iRotator.layerPos = options.layerPos || iRotator.layerPos
				iRotator.rnd = options.rnd || false
                iRotator.lightbox = options.lightbox || false
				
				iRotator.imageList = list || [];
				if(iRotator.rnd) iRotator.nextImageNumber = Math.floor(Math.random()*iRotator.imageList.length)
				if(iRotator.debug) console.log('Nasumična slika:'+ iRotator.nextImageNumber)
				
				
			
					iRotator.link = document.createElement('a')
					iRotator.link.setAttribute('title', iRotator.imageList[iRotator.nextImageNumber].title.stripTags())
					iRotator.link.setAttribute('href', iRotator.imageList[iRotator.nextImageNumber].link)
					iR_Cont.appendChild(iRotator.link)
				
                
                
                 
				
				
				//Set img elements inside container element (	
					iRotator.img1 = document.createElement('img')
					iRotator.img1.setAttribute('id', 'img1_' + iRotator.rndNum())
                   
					iRotator.img1.setAttribute('src', iRotator.imageList[iRotator.nextImageNumber].path)
					iRotator.link.appendChild(iRotator.img1);
					
					if(iRotator.crossfade){
						iRotator.img2 = document.createElement('img')
						iRotator.img2.setAttribute('id', 'img2_' + iRotator.rndNum())
						iRotator.img2.setAttribute('src', iRotator.imageList[iRotator.nextImageNumber].path)
						iRotator.link.appendChild(iRotator.img2);
					}
                    
                       
                    iRotator.graf_title = document.createElement('div')					
                    iRotator.graf_title.setAttribute('id', 'ir_title')
					iRotator.graf_title.innerHTML = iRotator.imageList[iRotator.nextImageNumber].title
					iR_Cont.appendChild(iRotator.graf_title)
				
                  
					
					
					
				iRotator.getNextImage();
			
		},
		
		rndNum : function(){
			var num = Number(new Date())
			return num.toString();;
		},
		
		
		
		getNextImage: function (){
			
			
			function loaded(){				
				if(iRotator.debug) console.info('Image was loaded:  - ' + iRotator.loadImg.src)
				if(iRotator.debug)  console.info('-----------------------------------------------------------------------------')				
				iRotator.nextImg = iRotator.loadImg;
				
				iRotator.nextImageNumber = iRotator.currentImageNumber+1;
				if(iRotator.nextImageNumber > iRotator.imageList.length-1) iRotator.nextImageNumber = 0;
				iRotator.currentImageNumber = iRotator.nextImageNumber;
				iRotator.nextImage = new Image();
				iRotator.nextImage.src =  iRotator.imageList[iRotator.nextImageNumber].path								
				if(iRotator.debug) console.log('Preloading next image:' + iRotator.nextImage.src)
				if(iRotator.checkNextImage) iRotator.startTimer();				
			}
			
			
			
			//if(first){nextImage = imgNum+1; imgNum = nextImage; first = false;}
			
			
			
			iRotator.loadImg = new Image();
			iRotator.loadImg.onload = loaded			
			iRotator.loadImg.src = iRotator.imageList[iRotator.nextImageNumber].path			
			iRotator.link.setAttribute('href', iRotator.imageList[iRotator.nextImageNumber].link || 'javascript:void(0)')			
			iRotator.link.setAttribute('title',iRotator.imageList[iRotator.nextImageNumber].title.stripTags())
            iRotator.graf_title.innerHTML = iRotator.imageList[iRotator.nextImageNumber].title
            if(iRotator.lightbox) iRotator.link.setAttribute('rel', 'lightbox['+iR_Cont.id+']') 
			if(iRotator.debug) console.info('New image is loading:  ' + iRotator.nextImageNumber + '. - ' + iRotator.imageList[iRotator.nextImageNumber].path)			
			
			
		},
		
		
		
		checkNextImage : function (){
			if(iRotator.img1){						
				return iRotator.img1.src != iRotator.nextImg.src
			}
		},
		
		
		
		// Show next image
		showNextImage : function(){
			if(iRotator.crossfade) iRotator.img1.style.zIndex = iRotator.layerPos;
			new Effect.Appear(iRotator.img1,{
					duration : iRotator.transitionSpeed,
					beforeStart : iRotator.setImage,
					afterFinish : iRotator.setGhostImage
					})
		},
		
		hideCurrentImage : function(){			
			iRotator.getNextImage();
			new Effect.Fade(iRotator.img1,{
					duration : iRotator.transitionSpeed,
					afterFinish : iRotator.showNextImage
				})
		},
		
		
		setImage : function(){				
				iRotator.img1.src = iRotator.nextImg.src
		},
		
		setGhostImage : function(){
			if(iRotator.crossfade) iRotator.img2.src = iRotator.img1.src
		},
		
		startTimer : function(){
			 timer = setTimeout(iRotator.rotate,iRotator.speed);
			if(iRotator.debug) console.info('Timer start')
		},
		
		
		rotate : function (){
			
			if(iRotator.checkNextImage) iRotator.hideCurrentImage();		
		}
		
	}
	
	function rot(){		
		iRotator.init("iR",slike, {sec:5, rnd: false, crossfade: false, layerPos: 800, lightbox:false})
	}
	
	var slike = new Array(
		{title: 'Kretanje prihoda od prodaje <span>u milijunima kuna 1995. - 2010.</span>', path: 'images/grafovi/naslovna/kretanje-prihoda.gif', link: 'o_tdr/financije/index.htm'},
		{title: 'Ukupna prodaja <span>u milijunima cigareta 1995. - 2010.</span>', path: 'images/grafovi/naslovna/ukupna-prodaja.gif',link: 'o_tdr/financije/index.htm'},
		{title: 'Udjel izvoza TDR-a u ukupnoj prodaji <span>u postocima 1995.-2010.</span>', path: 'images/grafovi/naslovna/udjel-izvoza.gif',link: 'o_tdr/financije/index.htm'}
		
	)	
	
	
	Event.observe(window, 'load', rot, false);
	
	
	
