/********************************************************
*														*
*		www.nordicpoint.net								*
*		gallery.js v1.0									*
*		author: Ivan Jukić								*
*		copy: 2009										*
*		description: script that changes images			*
*					 periodicaly						*
*														*
*********************************************************/


//jquery part, code inside will start executing when all page elements are loaded
$(document).ready( function() {
							
							
/* GLOBAL VARIABLES */

/* varijable containing relative path to galery images folder */
var path = "images/gallery_images/";
//preloader name
var preloader = "loader.gif";
/* variable with id of div that contains img tag */
var sDiv = "gallery_img_div";
/*variable with id of img tag*/
var sImg = "gallery_img";

/* global array, contains image names */
var images =  new Array(
					"img1.png",
					"img2.png",
					"snowboarding_sweden.jpg",
					"img3.png",
					"stockholm2.jpg",
					"swedish_girls.jpg",
					"up_helly.jpg",
					"iceland_hotspring.jpg",
					"raindeers.jpg",
					"reykjavik-5-.jpg",
					"island_village.jpg",
					"finland_christmas_Santa_sledge.jpg",
					"gullfoss.jpg",
					"lordi.jpg",
                                        "funny_scan.jpg", 
					"winter_norway.jpg",
					"siberian-husky-mountains.jpg");	// <- tu se dodaju nove slike

//counter when loading images
var i = 0;
//bool variable, indicates when images are downloaded
//so we can start showing them
var bDownloaded = false;



/* FUNCTIONS */

//function definition for images load
function loadImages()
{
	//load image
	var nImg = new Image;
	//obtain relative path to it
	nImg.src = path + images[i];
	
	//check if image is loaded every 50 ms
	var interval = setInterval( 
			function() 
			{	//if download completed load new image
				if( nImg.complete ) 
				{
					//alert( i + ") " + nImg.src + " (downloaded)" );
					//clear timeout
					clearTimeout( interval );
					//increase counter
					i++;
					//if all images loaded reset counter, indicate images are downloaded and return
					if( i == images.length ) {
						i = 0;
						bDownloaded = true;
						return;
					}
					//else load new image
					loadImages();
				}
			}, 50 );
	// <- function end 
}

//function for image fade in, argument new image source
function FadeIn( src )
{	
	//get image object
	var oImgDest = document.getElementById( sImg );
	
	//set opacity
	oImgDest.style.opacity = 0.0;
	oImgDest.style.filter = "alpha(opacity=0)";
	
	//add new src
	oImgDest.src = src;
	
	//current alpha chanel value
	curAlpha = 0;
	
	var fade_time = setInterval(
		function()
		{
			//set new alpha value
			oImgDest.style.opacity = curAlpha;
			oImgDest.style.filter = "alpha( opacity = " + (curAlpha*100) + ")";
			
			//check if fade complete
			if( curAlpha >= 1 ) {
				//clear interval, end fade
				clearTimeout( fade_time );
			}
			else
			{
				//increase alpha vlaue
				curAlpha += 0.03;
			}
		}
		, 50 );
	// <- function end
}



/* IMAGE GALLERY FUNCTIONALITY */

//set up preloader gif
var oDiv_pre = document.getElementById( sDiv );
var oImg_pre = document.getElementById( sImg );
//set CSS style
oImg_pre.src = path + preloader;
//move preloader on middle
oImg_pre.style.paddingLeft = 121 + "px";
oImg_pre.style.paddingTop = 47 + "px";

//call function for image download
loadImages();

//check until all images downloaded, then start changing them
var interval = setInterval( 
		function()
		{
			//if all images are downloaded then bDownloaded will be true
			if( bDownloaded )
			{
				//cancel interval
				clearInterval( interval );
				
				//reset padding
				oImg_pre.style.paddingLeft = 0 + "px";
				oImg_pre.style.paddingTop = 0 + "px";
				
				//set first image, first image is random
				i = Math.ceil(Math.random()*100)%images.length;
				FadeIn( path + images[i] );
				i++;
				if( i == images.length ) i = 0;
				
				//change images every 10 seconds
				setInterval (
					function()
					{		
						//alert( "image change" );
						//set new image source
						var src = path + images[i];
						//then fade in new image
						FadeIn( src ); 
						//increase counter to new image
						i++;
						//if number of images reached, reset
						if( i == images.length ) i = 0;
						
					} 
					, 8000 );
			}
		}
		, 50 );



/* SCRIPT END */
});
