/*
 * JavaScript Application
 * http://www.volgorate.ru/
 *
 * Copyright (c) 2009 Internet-Agency Volgorate
 *
 * Date: 15-12-2009
 */

var topLine = {
    node: {
        detail: null,
        activeText: null,
        textTpl: null,
        link: null
    },
    options: {
        aminateTime: 500,
        timeToHide: 1000,
        timeToShow: 300,
        timer: null,
        runingLineSpeed: 30000,
        hiddenPosition: -200,
        visiblePosition: 22,
        state: 0
    },
    init: function()
    {
        var self = this;
        self.node.detail = $('#top-line > .detail-wrap');
        self.node.link = $('#top-line .line > .running-line');
        self.node.activeText = self.node.link.children('.active').eq(0);
        self.node.textTpl = '<div>' + self.node.activeText.html() + '</div>';
        
        self.runLine(self);
        
        self.node.link.hover(
            function() {
                clearTimeout(self.options.timer);
                if (!self.options.state)
                    self.options.timer = setTimeout(function() { self.show(self); }, self.options.timeToShow);
            },
            function() {
                clearTimeout(self.options.timer);
                if (self.options.state)
                    self.options.timer = setTimeout(function() { self.hide(self); }, self.options.timeToHide);
            }
        ).click(function(){
            clearTimeout(self.options.timer);
            self.show(self);
            return false;
        });
        self.node.detail.children('div').hover(
            function() {
                clearTimeout(self.options.timer);
            },
            function() {
                clearTimeout(self.options.timer);
                if (self.options.state)
                    self.options.timer = setTimeout(function() { self.hide(self); }, self.options.timeToHide);
            }
        );
    },
    hide: function(self)
    {
        if (!self.options.state)
            return;
        self.node.detail.animate({top: self.options.hiddenPosition}, self.options.aminateTime, 'swing', function() {self.node.detail.css('display', 'none'); self.options.state = 0});
        clearTimeout(self.options.timer);
    },
    show: function(self)
    {
        if (self.options.state)
            return;
        self.node.detail.css('display', 'block').animate({top: self.options.visiblePosition},self.options.aminateTime, 'swing', function() {self.options.state = 1});
        clearTimeout(self.options.timer);
    },
    runLine: function(self)
    {
        self.node.activeText.animate({width: 0}, self.options.runingLineSpeed, 'linear', function() {
            self.node.activeText.remove();
            self.node.link.append(self.node.textTpl);
            self.node.activeText = self.node.link.children('div').eq(0).addClass('active');
            self.runLine(self);
        });
    }
}
