/**
 * Author: Darren Labey
 * Company: Graphtek Interactive
 **/

google.load('dojo', '1.6');

function rotatorInit(args) {
    dojo.declare('Rotator', null, {
        element: 'rotator',
        current: 0,
        fadeSpeed: 1000,
        image: null,
        imageContainer: null,
        speed: 3000,
        timer: null,
        unorderedListItems: null,
        unorderedListItemsCount: 0,
        unorderedListItemsIndex: 0,
        
        constructor: function(args) {
            var self = this;
            
            dojo.safeMixin(this, args);
            
            if (dojo.byId('rotator')) {
                this.fadeSpeed = this.speed / 3;
                this.image = dojo.query('#rotator .image img')[0];
                this.imageContainer = dojo.query('#rotator .image')[0];
                this.unorderedListItems = dojo.query('ul li', this.element);
                this.unorderedListItemsCount = this.unorderedListItems.length - 1;
                this.timer = new dojox.timing.Timer(this.speed);
                this.timer.onStart = function() {
                    self.rotate();
                }
                this.timer.onTick = function() {
                    self.rotate();
                }
                
                this.start(this.unorderedListItems);
                
                dojo.connect(dojo.query('#rotator .previous')[0], 'onclick', function(event) {
                    self.previous(event);
                });
                dojo.connect(dojo.query('#rotator .next')[0], 'onclick', function(event) {
                    self.next(event);
                });
            }
        },
        isInt: function(input) {
            return typeof(input) == 'number' && parseInt(input) == input;
        },
        next: function() {
            var nextIndex = this.unorderedListItemsIndex + 1;
            
            if (nextIndex <= this.unorderedListItemsCount) {
                this.unorderedListItemsIndex = nextIndex;
            } else {
                this.unorderedListItemsIndex = 0;
            }
            
            this.rotate(event, nextIndex);

        },
        previous: function() {
            var previousIndex = this.unorderedListItemsIndex - 1;
            
            if (previousIndex >= 0) {
                this.unorderedListItemsIndex = previousIndex;
            } else {
                this.unorderedListItemsIndex = this.unorderedListItemsCount;
            }
            
            this.rotate(event, previousIndex);
        },
        rotate: function(event, nextIndex) {            
            var target = ((event == undefined) ? undefined : ((event.currentTarget) ? event.currentTarget : event.srcElement.parentNode.parentNode));
            var self = this;
            
            if (nextIndex == undefined || !this.isInt(nextIndex)) {
                var control = false;
                var nextIndex = 0;
            } else {
                var control = true;
            }
            
            if (target == undefined) {
                var unorderedListItemsLength = this.unorderedListItemsCount - 1;
                var unorderedListItemsNextIndex = this.unorderedListItemsIndex + 1;
                
                dojo.removeClass(this.unorderedListItems[this.unorderedListItemsIndex], 'current');
                
                if (unorderedListItemsNextIndex > this.unorderedListItemsCount) {
                    this.unorderedListItemsIndex = 0;
                } else {
                    this.unorderedListItemsIndex = unorderedListItemsNextIndex;
                }
                
                var image = dojo.query('.next_image', this.unorderedListItems[this.unorderedListItemsIndex])[0];
                
                dojo.attr(dojo.query('img', image)[0], 'src', dojo.attr(dojo.query('img', image)[0], 'longdesc'));
                
                dojo.addClass(this.unorderedListItems[this.unorderedListItemsIndex], 'current');
                
                try {
                    dojo.fx.chain([
                        dojo.fadeOut({
                            duration: this.fadeSpeed,
                            node: this.image,
                            onEnd: function() {
                                self.imageContainer.innerHTML = image.innerHTML;
                                self.image = dojo.query('#rotator .image img')[0];
                            }
                        }),
                        dojo.fadeIn({
                            duration: this.fadeSpeed,
                            node: self.image
                        })
                    ]).play();
                } catch(error) {
                    console.log(error);
                }
            } else {
                self.timer.stop();
                
                dojo.forEach(this.unorderedListItems, function(item, index) {
                    if (dojo.hasClass(item, 'current')) {
                        dojo.removeClass(item, 'current');
                    }
                });
                
                if (control) {
                    var image = dojo.query('.next_image', self.unorderedListItems[nextIndex])[0];
                } else {
                    var image = dojo.query('.next_image', target)[0];
                    var unorderedListItemsNextIndex = dojo.indexOf(this.unorderedListItems, target);
                    
                    this.unorderedListItemsIndex = unorderedListItemsNextIndex;
                }
                
                dojo.attr(dojo.query('img', image)[0], 'src', dojo.attr(dojo.query('img', image)[0], 'longdesc'));
                
                dojo.addClass(target, 'current');
                
                dojo.fx.chain([
                    dojo.fadeOut({
                        duration: this.fadeSpeed,
                        node: this.image,
                        onEnd: function() {
                            self.imageContainer.innerHTML = image.innerHTML;
                            self.image = dojo.query('#rotator .image img')[0];
                        }
                    }),
                    dojo.fadeIn({
                        duration: this.fadeSpeed,
                        node: self.image
                    })
                ]).play();
            }
           
        },
        start: function() {
            var self = this;
            
            this.timer.start();
            
            this.unorderedListItems.forEach(function(item, index) {
                dojo.connect(item, 'onclick', function() {
                    self.rotate(event);
                    self.stop(self.timer);
                });
            });
        },
        stop: function(timer) {
            timer.stop();
        }
    });
    
    new Rotator(args);
}

google.setOnLoadCallback(function() {
    dojo.require('dojox.timing');
    dojo.require('dojo.fx');
    
    setTimeout(function() {
        rotatorInit({
            speed: 5000
        });
    }, 1000);
});
