﻿var _currentIndex = 1; //start on 1 as the inital image will be the same as index 0
var _flag = false; //flag for timer, false = go, true = halt
var _timeDelay = 5000;
var _timer = null;
var _playImg = "/images/interface/photo-gallery-navi/play.gif";
var _pauseImg = "/images/interface/photo-gallery-navi/pause.gif";

function init()
{
    //bind click events - thumbnail
    $("#photo-gallery .thumbnail").each(
	    function (i)
	    {
		    $(this).bind("click", function(e){  
		         _currentIndex = i;
		         setImage();
		         _flag = true;
		         _timer = clearTimeout(_timer);
		         $("#toggle").attr("src", _playImg);
		         $("#toggle").attr("alt", "Play");
		         $("#toggle").attr("title", "Play");
		     });
	    }
    );
    
    $("#prev").bind("click", function(e){
             _currentIndex --;
             setImage();
             _flag = true;
             _timer = clearTimeout(_timer);
             $("#toggle").attr("src", _playImg);
             $("#toggle").attr("alt", "Play");
             $("#toggle").attr("title", "Play");
        }
    );
    
    $("#next").bind("click", function(e){
             _currentIndex ++;
             setImage();
             _flag = true;
             _timer = clearTimeout(_timer);
             $("#toggle").attr("src", _playImg);
             $("#toggle").attr("alt", "Play");
             $("#toggle").attr("title", "Play");
        }
    );
    
    $("#toggle").bind("click", function(e){
            toggle();
            _timer = setTimeout("rotate(" + (_currentIndex + 1) +")", _timeDelay);
        }
    );
    
    //start the ball rolling
    _timer = setTimeout("rotate(" + _currentIndex + ")", _timeDelay);
}

function toggle()
{
    _flag = !(_flag);
    if(_flag)
    {
        _timer = clearTimeout(_timer);
        $("#toggle").attr("src", _playImg);
        $("#toggle").attr("alt", "Play");
        $("#toggle").attr("title", "Play");
    }
    else{
        $("#toggle").attr("src", _pauseImg);
        $("#toggle").attr("alt", "Pause");
        $("#toggle").attr("title", "Pause");
    }
}

function rotate(index)
{
    if(!_flag)
    {
        _currentIndex = index;
        setImage();
        _timer = setTimeout("rotate(" + (_currentIndex + 1) + ")", _timeDelay);
    }
}

function setImage()
{
    if(_currentIndex == -1)
    {
        _currentIndex = $("#photo-gallery .thumbnail").length - 1;
    }
    
    var url = $("#photo-gallery .thumbnail").eq(_currentIndex % $("#photo-gallery .thumbnail").length).attr("src");
    
    if(url.length > 0 && url.indexOf("/gallery_image.aspx?image=") > -1)
    {
        //strip current imagegen params and add in the main image params
        url = url.substring(0, url.lastIndexOf("&width=")) + "&width=423&height=423";
        $("#mainImg").attr("src", url);
        
        $("#mainImg").attr("alt", $("#photo-gallery .thumbnail").eq(_currentIndex % $("#photo-gallery .thumbnail").length).attr("alt"));
    }
}
             
$(document).ready(init);