﻿martinco.namespace('martinco.history');

martinco.history = {
    year: 0,
    id: null,
    text: null,
    img: null,
    caption: null,

    yearObject: null,

    timer: null,
    interval: 50,
    opacity: 1,
    isMinOpacity: false,

    init: function(containerId, headingId, textId, imageId, captionId) {
        this.id = headingId;
        this.text = textId;
        this.img = imageId;
        this.caption = captionId;

        var dates = $(containerId).getElementsByTagName('A');

        for (var i = 0; i < yearDetails.year.length; i++) {
            window['yearObject_' + yearDetails.year[i].Id] = yearDetails.year[i];
        }

        for (var i = 0; i < dates.length; i++) {

            dates[i].onclick = function() {
                for (var j = 0; j < dates.length; j++)
                    Dom.removeClass(dates[j].parentNode, "selected");

                martinco.history.year = this.lastChild.nodeValue;
                Dom.addClass(this.parentNode, "selected");

                martinco.history.swapImages();

                return false;
            }
        }
    },

    getYear: function() {
        var object = window['yearObject_' + arguments[0]];

        martinco.history.id.innerHTML = object.Id;
        martinco.history.text.innerHTML = object.Text;
        martinco.history.img.src = object.ImgUrl;
        martinco.history.img.setAttribute('alt', object.ImgAlt);
        martinco.history.caption.innerHTML = object.ImgAlt;

        return false;
    },

    fade: function() {
        Dom.setStyle(martinco.history.id, 'opacity', martinco.history.opacity);
        Dom.setStyle(martinco.history.text, 'opacity', martinco.history.opacity);
        Dom.setStyle(martinco.history.img, 'opacity', martinco.history.opacity);
        Dom.setStyle(martinco.history.caption, 'opacity', martinco.history.opacity);
    },

    reduceOpacity: function() {
        if (martinco.history.timer == null) {
            martinco.history.timer = setInterval(function() {
                if (martinco.history.opacity > 0.2) {
                    martinco.history.opacity = parseFloat(martinco.history.opacity - 0.2).toFixed(1);
                    martinco.history.fade();
                } else {
                    window.clearInterval(martinco.history.timer);
                    martinco.history.timer = null;
                    martinco.history.isMinOpacity = true;
                }
            }, martinco.history.interval);
        }
    },

    increaseOpacity: function() {
        if (martinco.history.timer == null) {
            martinco.history.timer = setInterval(function() {
                if (martinco.history.opacity < 1) {
                    martinco.history.opacity = parseFloat(martinco.history.opacity + 0.2);
                    martinco.history.fade();
                } else {
                    window.clearInterval(martinco.history.timer);
                    martinco.history.timer = null;
                }
            }, martinco.history.interval);
        }
    },

    swapImages: function() {
        var swapping = setInterval(function() {
            if (martinco.history.isMinOpacity == false) {
                // Reduce opacity
                martinco.history.reduceOpacity();
            } else {
                // Swap images
                martinco.history.getYear(martinco.history.year);

                // Increase opacity
                if (martinco.history.opacity >= 1) {
                    window.clearInterval(swapping);
                    martinco.history.isMinOpacity = false;
                } else {
                    martinco.history.increaseOpacity();
                }
            }
        }, 10);
    }
}
