/**
 * Copyright (C) 2009 Reactor SA
 *
 * V 1.0
 * $LGTM
 */
(function ($) {
        window.reactor = {};
        reactor.slide = {
                current: -1,
                timer: null,
                slides: null,
                total: null,
       
                start: function(path, duration) {
                        var slides = jQuery(path);
                        this.slides = slides;
                        this.total = slides.length;

			this.slides.animate({opacity:0}).hide();
       
                        this.loadNavigation();
       
                        if(this.total > 1) {
                                var self = this;
                                this.timer = setInterval(function() { self.periodical(); }, duration||5000);
                        }
       
                        this.load(0, false);
                },
       
       
                loadNavigation: function() {
                  var self = this;
                        jQuery('#slideshow .arrow-left').fadeIn().click(function() { self.next(); });
                        jQuery('#slideshow .arrow-right').fadeIn().click(function() { self.previous(); });
                },
       
                periodical: function() {
                        var next = this.current +1;
                        if(next >= this.total) {
                                next = 0;
                        }
                        this.load(next, false);
                },
       
                load: function(index, pause) {
                        if(this.current >= 0) {
                                jQuery(this.slides[this.current]).animate({opacity:0}).hide();
                        }

                        jQuery(this.slides[index]).show().animate({opacity:1});
			
                        if(pause) {
                                clearTimeout(this.timer);
                        }
                        this.current = index;
                },
       
                next: function() {
                        var next = this.current + 1;
                        if(next >= this.total)
                                next = 0;
                        this.load(next, true);
                },
       
                previous: function() {
                        var previous = this.current - 1;
                        if(previous < 0)
                                previous = this.total-1;
                        this.load(previous, true);
                }
        }
        

    $(document).ready(function () {
        var a = {
            style: {
                width: 65,
                textAlign: 'center',
                background: '#333',
                padding: 0,
                color: 'white',
                tip: {
                    corner: 'bottomLeft',
                    size: {
                        x: 8,
                        y: 4
                    }
                },
                border: {
                    width: 2,
                    radius: 4,
                    color: '#333'
                }
            },
            position: {
                adjust: {
                    x: -10,
                    y: -45
                }
            }
        };
        $('.more a,a.more,.net img').qtip(a);
    })
})(jQuery);
