// SiteImageNavigator
// Requires jQuery

$(document).ready(function() 
{
	if(document.getElementById('head_image_slide'))
	{
		initSlideShow();
	}
});

function allGotLoaded(domObj)
{
	initSlideShow();
}

var _slideShowInterval = 0;
var _interval = 5000;

// Prevent image display except the first one
$(function()
{
	$('#head_image_slide img').css({opacity: 0.0});
	$('#head_image_slide img:first').css({opacity: 1.0});
});

function initSlideShow()
{

	//Set the opacity of all images to 0
	$('#head_image_slide img').css({opacity: 0.0});
	
	//Get the first image and display it (set it to full opacity)
	$('#head_image_slide img:first').css({opacity: 1.0});
	
	var current = $('#head_image_slide img:first');
	if(current.next().length)
	{	
		// More then one image is present, continue
		current.addClass('show');
		
		_slideShowInterval = setInterval('siteImageGalery()', _interval);
	
		/* Set up navigation */
		$('#head_image').append('<div class="siteimage_nav"></div>');
		$('.siteimage_nav').append('<div class="scroll_left"></div>');
		$('.siteimage_nav').append('<div id="siteimage_nav_navigation"></div>');
		$('.siteimage_nav').append('<div class="scroll_right"></div>');
		
		var allimages = $('#head_image_slide img');
		var maximages = (allimages.length < 7) ? allimages.length : 6;
		
		for(i = 0; i < maximages; i++)
		{
			c_index = current.prevAll().length;
			$('#siteimage_nav_navigation').append('<div id="nav_item" onclick="navto('+c_index+')">' + (c_index+1) + '</div>');
			current = current.next();
		}
		
		setNavigation(-1, 0);
	}
}

function navto(index)
{
	clearInterval(_slideShowInterval);
	
	$('#siteimage_nav_navigation div').removeClass('nav_current');
	$('#siteimage_nav_navigation div:eq('+index+')').addClass('nav_current');
	
	var current = ($('#head_image_slide img.show')?  $('#head_image_slide img.show') : $('#head_image_slide img:first'));
	var next = $('#head_image_slide img:eq('+index+')');
	
	if(current.prevAll().length != next.prevAll().length)
		fadeImage(current, next);
	
	_slideShowInterval = setInterval('siteImageGalery()', _interval);
}

function siteImageGalery()
{
	
	//if no IMGs have the show class, grab the first image
	var current = ($('#head_image_slide img.show')?  $('#head_image_slide img.show') : $('#head_image_slide img:first'));

	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = ((current.next().length) ? current.next() : $('#head_image_slide img:first'));	
	
	if(current.equals(next))
	{
		clearInterval(_slideShowInterval);
		return;
	}
	
	// Only navigate to next image, if that is loaded
	var next_cnt = next.prevAll().length;
	if(document.getElementById('head_image_slide').childNodes[next_cnt].complete)
		fadeImage(current, next);
}

function fadeImage(current, next)
{
	//Set the fade in effect for the next image, show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
	
	/* Update navigation */
	setNavigation(current.prevAll().length, next.prevAll().length);
}

function setNavigation(last_index, current_index)
{
	if(last_index > -1)
		$('#siteimage_nav_navigation div:eq('+last_index+')').removeClass('nav_current');
	
	$('#siteimage_nav_navigation div:eq('+current_index+')').addClass('nav_current');
}

$.fn.equals = function(compareTo) 
{ 
        if (!compareTo || !compareTo.length || this.length!=compareTo.length) 
		{ 
                return false; 
        } 
        for (var i=0; i<this.length; i++) { 
                if (this[i]!==compareTo[i]) { 
                        return false; 
                } 
        } 
        return true; 



}
