﻿/*
Based on Mootools js framework

input parameters : 
    frames :        array['src.frame1.png', 'src.frame2.png', 'src.frame3.png',]
    imageObjId : string container ID
*/

var CubeRotator = new Class({
    // constructor
    initialize: function (options) {
        this.setOptions(options);
        this.currentFrame = this.options.startFrame;
        this.initEventHandlers();
    },
    
    initEventHandlers: function() {
        this.rotaterHandler = this.rotate.bind(this);
        this.mouseOverHandler = this.onMouse_Over.bind(this);
        this.mouseOutHandler = this.onMouse_Out.bind(this);

        var obj = $(this.options.imageObjId);
        if (obj) {
            if (obj.addEvent) {
                obj.addEvent('mouseover', this.mouseOverHandler);
                obj.addEvent('mouseout', this.mouseOutHandler);
            }
        }
    },
    
    dispose: function() {
        var obj = $(this.options.imageObjId);
        if (obj) {
            if (obj.removeEvent) {
                obj.removeEvent('mouseover', this.mouseOverHandler);
                obj.removeEvent('mouseout', this.mouseOutHandler);
            }
        }
    },

    setOptions: function(options) {
        // default options
        this.options = {
            frames: new Array(),
            startFrame: 0,
            latency: 1000,
            delay: 300,
            imageObjId: null
        }
        Object.extend(this.options, options || {});
    },
    
    rotate: function() {
        if (this.options.imageObjId) {
            $(this.options.imageObjId).setStyle('backgroundImage', this.options.frames[this.currentFrame]);
            
            this.currentFrame = this.currentFrame + 1 < this.options.frames.length ? this.currentFrame + 1 : 0;

            if (!this.delayPointer) {
                this.delayPointer = window.setInterval(this.rotaterHandler, this.options.delay);
            }
        }
    },
    
    onMouse_Over: function(sender, evtObj) {
        if (this.options.imageObjId) {
            this.latencyPointer = window.setTimeout(this.rotaterHandler, this.options.latency);
        }
    },
    
    onMouse_Out: function(sender, evtObj) {
        if (this.latencyPointer) {
            window.clearTimeout(this.latencyPointer);
            this.latencyPointer = null;
        }
        if (this.delayPointer) {
            window.clearInterval(this.delayPointer);
            this.delayPointer = null;
        }
    }
});

/*
var logoRotator;
window.addEvent('load', function() {
  logoRotator = new CubeRotator({frames:['url(assets/templates/sweetsoft/images/cube1.png)','url(assets/templates/sweetsoft/images/cube2.png)','url(assets/templates/sweetsoft/images/cube3.png)'], imageObjId: 'big-logo'});
    window.addEvent('unload', function() {logoRotator.dispose;});
});
*/