﻿/* 
 *  Fabrizio Calderan @ h-art 
 *  rev. 2008.04.16
 */

function newsTicker() {

    var pg          = null;
    var pgcontainer = null;
    var newsWindow    = 0;
    
    var pgnews      = [];
    var currnews = 1;
    var height      = 0;
    
    var nextLink, prevLink = null;
    var slideFx     = null;
    var pgMutex     = true;    
    
    this.initNt = function() {
        pgnews      = pg.getElements('li');
    };

    this.setClasses = function() {
        pg.addClass('js');    
        pgcontainer         = pg.getElements('div ul')[0];
        pgcontainer.setStyle('height', (pgnews.length * height) + 'em');
        slideFx = new Fx.Morph(pgcontainer, {'unit': 'em', duration: '400', transition: Fx.Transitions.Quart.easeOut});
        
    };
    
    this.createLinks = function() {
        
        prevLink = new Element('a');
        prevLink.addClass('prevlink');
        prevLink.href = '#';
        prevLink.innerHTML = 'previous news';
        prevLink.setStyle('opacity', '0.3');        
        prevLink.injectBefore(pg);
        
        nextLink = new Element('a');
        nextLink.addClass('nextlink');
        nextLink.href = '#';
        nextLink.innerHTML = 'next news';
        nextLink.injectAfter(pg);
        

        prevLink.onclick = this.prevClick;
        nextLink.onclick = this.nextClick;
    }
    
    
    this.nextClick = function() {
        if (pgMutex) {
            prevLink.setStyle('opacity', '1');
            
            if (currnews <= pgnews.length - newsWindow) {
                pgMutex = false;
                var offset = -(currnews * (height));
                if ((document.all) && (!window.opera)) offset += 0.12;
                
                slideFx.start({'margin-top': offset}).chain(function() {
                    pgMutex = true;
                    ++currnews;
                });
            }
        }
        
        if ((currnews) == pgnews.length - newsWindow) { 
            this.setStyle('opacity', '0.3'); 
        }
            
        return false;
    };

    this.prevClick = function() {
    
        if (pgMutex) {
            nextLink.setStyle('opacity', '1');
            
            if (currnews > 1) {
                pgMutex = false;
                var offset = -((--currnews - 1) * (height));
                if ((document.all) && (!window.opera)) offset += 0.12;
                
                slideFx.start({'margin-top': offset}).chain(function() {
                    pgMutex = true;
                });
            }
        };
        
        
        if (currnews == 1) { 
            this.setStyle('opacity', '0.3'); 
        };
        
        return false;
    };



    this.init = function(idpg, h) {
        if ($(idpg)) {
            pg = $(idpg);
            
            height  = h;
            this.initNt();
            this.setClasses();
            this.createLinks();
        }
    };
    
    this.setWindow = function(window) {
        newsWindow        = window;
    };
}
