var theInt = null;
var $crosslink, $navthumb;
var curclicked = 0;
var loadedImagesCount = 0;
var imageSliderIsStarted = false;
var imagesCounter = 0 ;
// TODO Magic number 7 HERE!
var imagesIds = new Array(7);
  
theInterval = function(cur) {
    clearInterval(theInt);

    if( typeof cur != 'undefined' )
        curclicked = cur;

    $crosslink.removeClass("active-thumb");
    $navthumb.eq(curclicked).parent().addClass("active-thumb");
    $(".stripNav ul li a").eq(curclicked).trigger('click');

    theInt = setInterval(function() {
        $crosslink.removeClass("active-thumb");
        $navthumb.eq(curclicked).parent().addClass("active-thumb");
        $(".stripNav ul li a").eq(curclicked).trigger('click');
        curclicked++;
        if( 7 == curclicked )
            curclicked = 0;
    }, 3000);
};

function startThumbNavigation() {
    imageSliderIsStarted = true;
    $("#main-photo-slider").codaSlider();
    $(".panel").css("display", "block");
   
    $navthumb = $(".nav-thumb");
    $crosslink = $(".cross-link");
   
    $navthumb.click(function() {
        var $this = $(this);
        theInterval($this.parent().attr('href').slice(1) - 1);
        return false;
    });

    theInterval();
}

function startThumbNavigatorWhenAllImagesAreLoaded() {
    // TODO Magic number 10 HERE!
    if (loadedImagesCount > 10 && imageSliderIsStarted==false) {
        startThumbNavigation();
    }
}

function addImageId(id) {
    imagesIds[imagesCounter] = id;
    imagesCounter++;
}

function loadFullscreenImages() {
    for (var i = 0; i < 7; i++) {
        var img1 = $('#fullscreenimage' + i);
        $(img1).load(function() {
            //$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
            $(this).hide();
            $(this).fadeIn();
            loadedImagesCount++;
            startThumbNavigatorWhenAllImagesAreLoaded();
        }).error(function() {
            // notify the user that the image could not be loaded
        }).attr('src', 'GetImage.ashx?PhotoId=' + imagesIds[i]);
    }
}

$(document).ready(function () {
    startThumbNavigation();
});

