﻿/****************************************************
* Script: ImageNavigator
* Date: (November 13th 2008)
* Author: Dimitri Troncquo
* Contact: dimi3.t@gmail.com
* Description: 
* Simple image navigation control
*
****************************************************/
var NavigationDirection = {
    Previous: -1, Next: 1
};
var ImageNavigator = {
    currentCollection: 1,
    currentImageIndex: 0,
    loadCurrentImage: function() {
        var carsouselItem = document.getElementById("car" + ImageNavigator.currentCollection);
        if (carsouselItem) {
            var div = carsouselItem.getElementsByTagName("div")[ImageNavigator.currentImageIndex];
            if (div) {
                var img = div.getElementsByTagName("img")[0];
                if (img) {
                    if (img.click)
                        img.click();
                    else if (img.onclick)
                        img.onclick();
                }
                // loadBGImage(img.src);
            }
        }
    },
    setCurrentImage: function(image) {
        if (image.index != null && image.collection != null) {
            ImageNavigator.currentImageIndex = image.index;
            ImageNavigator.currentCollection = image.collection;
        } else if (image.url) {
            var carouselItems = document.getElementById("carouselItems");
            if (carouselItems) {
                for (var i = 0; i < carouselItems.childNodes.length; i++) {
                    if (carouselItems.childNodes[i].id && carouselItems.childNodes[i].id == "car" + (i + 1)) {
                        var divs = carouselItems.childNodes[i].getElementsByTagName("div");
                        for (var j = 0; j < divs.length; j++) {
                            var img = divs[j].getElementsByTagName("img")[0];
                            if (img && img.src && img.src == image.url) {
                                ImageNavigator.currentImageIndex = j;
                                ImageNavigator.currentCollection = i + 1;
                                return;
                            }
                        }
                    }
                }
            }
        }
    },
    move: function(direction) {
        var currentIndex = ImageNavigator.currentImageIndex + direction;
        var currentCollection = ImageNavigator.currentCollection;
        if (currentIndex < 0) {
            if (--currentCollection < 1)
                currentCollection = ImageNavigator.getLastCollection();
            currentIndex = ImageNavigator.getLastImageInCollection(currentCollection);
        } else {
            if (currentIndex > ImageNavigator.getLastImageInCollection(currentCollection)) {
                currentIndex = 0;
                if (document.getElementById("car" + (currentCollection + 1)))
                    currentCollection++;
                else
                    currentCollection = 1;
            }
        }
        ImageNavigator.setCurrentImage({ index: currentIndex, collection: currentCollection });
        ImageNavigator.loadCurrentImage();
    },
    getLastImageInCollection: function(collection) {
        var carsouselItem = document.getElementById("car" + collection);
        if (carsouselItem) {
            var divs = carsouselItem.getElementsByTagName("div");
            return divs.length - 1;
        }
        return 0;
    },
    getLastCollection: function() {
        var collectionCount = 1;
        while (document.getElementById("car" + collectionCount)) {
            collectionCount++;
        }
        return --collectionCount;
    }
};
