
/**
 * scroll document to the given Element
 * @param $e
 * @param speed
 * @return $e
 */
function scrollTo($e, speed) {
    $e = $($e);
    speed   = speed || 'slow';
    if ($e.length == 1) {
        var offset = $e.offset();
        $('html,body').stop().animate({
            scrollTop: offset.top
        }, speed);
    }
    return $e;
};



var scrollTopLink = {
    $link:null,
    top: 50,
    $window : $(window),
    scroll_timer:0,
    displayed : false,
    init: function() {
        var self = scrollTopLink;
        self.$link = $('<a class="link-top" href="#top" id="link-top"><span class="nocss">Revenir en haut</span></a>');
        $('body').append(self.$link);
        self.$link = $('.link-top, #box-fast-nav');


        
        self.$window.scroll(function () {
            window.clearTimeout(self.scroll_timer);
            self.scroll_timer = window.setTimeout(function () {
                if(self.$window.scrollTop() <= self.top)
                {
                    self.displayed = false;
                    self.$link.fadeOut('normal');
                }
                else if(self.displayed == false)
                {
                    self.displayed = true;
                    self.$link.stop(true, true).fadeIn('normal').click(function () {
                        self.$link.fadeOut('normal');
                    });
                }
            }, 100);
        });
        self.$link.click(function(event) {
            scrollTo($('#top'), 'slow');
            event.stopPropagation();
            return false;
        })

    }
};

//
// On load
$(function() {

    //
    // ----- HOME -----
    if($("#home-tweets-content").length == 1) {
        myTweetHome.start($("#home-tweets-content"));
    } else if($("#page-tweets-content").length == 1) {
        myTweetHome.start($("#page-tweets-content"));
    }

    // links
    $('a[href^="#"]').click(function(event) {
        var $target = $($(this).attr('href'));
        if($target.length === 1) {
            scrollTo($target,'slow');
            event.stopPropagation();
            return false;
        } else {
            return true;
        }
    });

    // heights
    //    $('.home-page,#box-home-container').height($(window).height());
    $('.home-page,#box-home-container').css('min-height', $(window).height());

    $('#box-home-container').css('min-height',$(window).height() - $('#home-box-tweets').outerHeight() );




    //    scrollTopLink.top = $(window).height() / 2;
    scrollTopLink.init();
});




var myTweetHome = {
    $e: null,

    currentLiNumber : 0,

    interval: null,

    speed: 4000,

    start: function($container) {
        myTweetHome.$e = $container;

        myTweetHome.$e.tweet({
            avatar_size: 32,
            count: 7,
            username: "flaveaConseils",
            template: "{text}",
            loading_text:"Chargement des tweets",
            auto_join_text_default: "j'ai dit,",
            auto_join_text_ed: "Je",
            auto_join_text_ing: "Je suis",
            auto_join_text_reply: "J'ai répondu à"
        }).bind('loaded', function() {
            myTweetHome.onUpdate();
            if(myTweetHome.interval) {
                clearInterval(myTweetHome.interval);
            }
            myTweetHome.interval = setInterval(myTweetHome.showNextTweet, myTweetHome.speed);
        });
    },

    onUpdate: function() {
        var $lis = $('li', myTweetHome.$e);
        var $liToShow = myTweetHome.getCurrentTweet();
        $lis.hide(0);
        $liToShow.fadeIn('slow');
    },

    showNextTweet: function() {
        myTweetHome._incrementCounter();
        myTweetHome.onUpdate();
    },

    getCurrentTweet: function() {
        return $('li:eq(' + myTweetHome.currentLiNumber+')', myTweetHome.$e);
    },

    _incrementCounter: function() {
        myTweetHome.currentLiNumber++;
        var $lis = $('li', myTweetHome.$e);
        if(myTweetHome.currentLiNumber >= $lis.length) {
            myTweetHome.currentLiNumber = 0;
        }
    },

    getNextTweet: function() {
        myTweetHome._incrementCounter();
        return myTweetHome.getCurrentTweet();
    }
};


//
// Menu ------------------------------------------------------------------------
var menuEffect = function() {
    this.html = {
        $container:$('#menu'),
        $element : $('#nav-hover'),
        $current : $('#menu .current a')
    };
    this.speed = 'normal';

    this.goTo = function($to) {
        var $e = this.html.$element;
        this._animTo($to);
    };

    this.backToCurrent = function() {
        var $e = this.html.$element;
        var $curr = this.html.$current;
        this._animTo($curr);
    };

    this._animTo = function($to) {
        var $e = this.html.$element;
        var posLeft = $to.offset().left - this.html.$container.offset().left;
        $e.stop().animate({
            width: $to.width(),
            marginLeft: posLeft
        }, this.speed);
    };

    this._appliesEvents = function() {

        $('a', this.html.$container).bind('mouseenter', function(instance) {
            return function(event) {
                var $link = $(this);
                if($link.parent('li').is('.current')) {
                    instance.backToCurrent();
                } else {
                    instance.goTo($link);
                    $('a', instance.html.$menu).not($link).removeClass('hovered');
                    $link.addClass('hovered');
                }
                event.stopPropagation();
            };
        }(this));

        $(this.html.$container).bind('mouseleave', function(instance) {
            return function(event) {
                $('a', instance.html.$menu).removeClass('hovered');
                instance.backToCurrent();
            };
        }(this));

    };

    this.start = function() {
        this._animTo(this.html.$current, 0);
        this.html.$element.show(0);
        this._appliesEvents();
    };

};


menuEffect.singleton = {
    instance : null,
    getInstance: function() {
        if(menuEffect.singleton.instance === null) {
            menuEffect.singleton.instance = new menuEffect();
        }
        return menuEffect.singleton.instance;
    }
};

$(function() {
    menuEffect.singleton.getInstance().start();
});


//
// Formation : filters ---------------------------------------------------------
function onFilterSelect($e) {
    $('#filters a').removeClass('active');
    $e.addClass('active');
};

$(function() {
    onFilterSelect($('#filters a:first'));
    $('#filters a').bind('click', function() {
        onFilterSelect($(this));
        return true;
    });
});
