/**
 * StripTweet framework
 *
 * @fileOverview    StripTweet_Navigation_Carousel
 * @author          Matej Keglevic, Copyright (c) 2009, ThirdFrameStudios
 */

/**
 * Description.
 *
 * @name    StripTweet_Navigation_Carousel
 * @class   Short Description.
 * @author  Matej Keglevic, Copyright (c) 2009, <a href="http://www.thirdframestudios.com">ThirdFrameStudios</a>
 * @version 1.0
 */
var StripTweet_Navigation_Carousel = new Class({
    Extends: StripTweet_Core,
    wrapper: '',
    moveLeft: '',
    moveRight: '',
    slides: 1,
    offset: 940,
    currentslide: 1,
    pos: 0,
    initialize: function(wrapper, moveLeft, moveRight, options) {
        if ($(wrapper)) {
            this.setOptions(options);
            this.wrapper = $(wrapper);

            this.offset = 940;
            this.parent = this.wrapper.getParent();

            this.scroll = new Fx.Scroll(this.wrapper, {
                offset: {
                    'x': 0,
                    'y': 0
                }
            });

            var req = new Request.JSON({
                method: 'get',
                url: this.add_ajax_to_url('/gallery/data.json'),
                onComplete: function(response) {
                    if ($defined(response) && response.data.length > 0) {
                        $('carousel_wrapper').empty();

                        this.data = response.data;
                        
                        var html = '';
                        var j = 4, k = 14;
                        
                        $each(response.data, function(el, i) {
                            html += '<li' + ((i > 0) && (i === j) ? ' class="l"' : '') + '><a href="' + el.url + '"><img src="' + el.img.thumb + '" alt="' + el.tweet + '" /></a></li>';
                            if (i === j) j += 5;
                                
                            if (i === k) {
                                this.slides++;
                                
                                new Element('ul', {
                                    html: html
                                }).injectInside($('carousel_wrapper'));
                                
                                html = '';
                                
                                k += 15;
                            }
                        }, this);
                        
                        if (html !== '') {
                            new Element('ul', {
                                html: html
                            }).injectInside($('carousel_wrapper'));
                        }
                        
                        if (this.slides > 1) {                            
                            if (!$(moveRight)) {
                                new Element('a', {
                                    href: '#',
                                    id: 'next',
                                    html: 'Next'
                                }).injectAfter(this.wrapper);
                            }
                            
                            if (!$(moveLeft)) {
                                new Element('a', {
                                    href: '#',
                                    id: 'prev',
                                    html: 'Prev'
                                }).injectBefore($(moveRight));
                            }
                            
                            this.moveLeft = $(moveLeft);
                            this.moveRight = $(moveRight);
                            this.moveLeft.addEvent('click', this.cMoveLeft.bind(this));
                            this.moveRight.addEvent('click', this.cMoveRight.bind(this));
                        }
                        
                        this.overlay = new StripTweet_Overlay_Image(response.data);
                        this.reset();
                    }
                }.bind(this)
            }, this).send();

            this.wrapper.addEvent('click', this.cOverlay.bind(this));
        }
    },
    cOverlay: function(event) {
        event = new Event(event).stop();

        var search = "";

        switch (event.target.nodeName) {
            case "A":
                search = event.target.href;
            break;
            case "IMG":
                search = event.target.src;
            break;
                search = $(event.target).getChildren('a').href;
        }

        if (search) {
            $each(this.data, function(data, index) {
                if (data.url === search || data.img.thumb === search) {
                    this.overlay.loadImage(data);

                    return false;
                }
            }, this);
        }

        return false;
    },
    cMoveLeft: function(event) {
        event = new Event(event).stop();

        $(this.moveRight).removeClass('d');

        if (this.currentslide == 1) return;

        this.currentslide--;

        if (this.currentslide == 1) {
            $(this.moveLeft).addClass('d');
        }

        this.pos += -(this.offset);
        this.scroll.start(this.pos);
        this.scroll.toLeft();
    },
    cMoveRight: function(event) {
        event = new Event(event).stop();

        $(this.moveLeft).removeClass('d');

        if (this.currentslide >= this.slides) return;

        this.currentslide++;

        if (this.currentslide >= this.slides) {
            $(this.moveRight).addClass('d');
        }

        this.pos += this.offset;
        this.scroll.start(this.pos);
        this.scroll.toLeft();
    },
    reset: function() {
        $(this.moveLeft).addClass('d');
        $(this.moveRight).addClass('d');
        
        var id = (this.get_id_from_url(location.href) > 0 ? this.get_id_from_url(location.href) : 0)
        
        this.currentslide = (id > 0 && id <= this.slides ? id : 1);
        this.pos = (id > 0 ? this.offset * (id - 1) : 0);
        this.scroll.start(this.pos);
        this.scroll.toLeft();
        
        if (this.currentslide > 1) $(this.moveLeft).removeClass('d');
        if (this.currentslide < this.slides) $(this.moveRight).removeClass('d');
        
    }
});