/*
  # ticker.js                               Version 0.4
  # Copyright (c) 2002 Luca Borrione        write(#)tiscali|it
  # Created 31 October 2002                 Last Modified 02 July 2004
  # _________________
  # COPYRIGHT NOTICE:
  # Copyright (c) 2002 Luca Borrione  All Rights Reserved.
  # Permission granted to use and modify free of charge this script so long as this
  # copyright notice above is maintained, modifications are documented,
  # and credit is given for any use of the script.
  # By using this code you agree to indemnify Luca Borrione from any liability that
  # might arise from its use.
  # Selling the code contained in this script without prior written consent is
  # expressly forbidden.
  # Obtain permission before redistributing this script over the Internet or
  # in any other medium.
  # In all cases copyright and header must remain intact.
  #
  # ____________
  # SCRIPT CODE:
*/

var d = document;

function ticker (Id) {
  var d = document;
  if (!d.ticker || !d.ticker[Id]) {init_ticker (Id);}
  if (d.ticker[Id]) {return d.ticker[Id];}
  else return null;
}
window.ticker = ticker;

function init_ticker (Id) {
  // alert ("init "+Id);
  if (!d.ticker) d.ticker = [];
  d.ticker[Id] = new Ticker (Id);
  /*
  if (!d.ticker[Id].object)
  {
    if (debugging)
      alert ("DYNAMIC LAYER Error:\n"+
             "Impossibile risalire al layer "+ Id +".");
    delete d.layer[Id];
  }
  */
}

function Ticker (Id) {
  this.id           = Id;
  this.x            = 0;
  this.y            = 0;
  this.width        = 100;
  this.height       = 100;
  this.items        = [];
  this.bgColor      = null;
  
  this.visibility   = true;
  /**
   * It tells whether the slide is stopped if the mouse
   * goes over the sliding element.
   * If enabled the slide will start again on mouseout.
   * 
   * @var boolean
   */
  this.hover        = true;
  
  this.inc          = 2;
  this.fromX        = 0;
  this.fromY        = this.height;
  this.speed        = 30;
  this.pause        = 2000;
  
  this._dx          = 0;
  this._dy          = 0;
  
  this.timer        = null;  
  this.timeout      = null;
  
  this._scrollCount = 0;

  this.addItem      = addItem;
  this.build        = build;
  this.activate     = activate;
  this.start        = start;
  this.slide        = slide;
  this.show         = ticker_show;
  this.stop         = ticker_stop;
  
  this.hslide       = false;
  this.sliding      = true;
  
  this._css         = '';
  this._div         = '';
  
  this.addCSS       = ticker_addCSS;
  this.addDIV       = ticker_addDIV;
  
  this.started      = false;
  
  
  function ticker_addCSS (D){
    var doc = (D) ? D : document;
    doc.write (this._css);
    //doc.write('<textarea>'+this._css+'</textarea>')
  }
  
  function ticker_addDIV (D){
    var doc = (D) ? D : document;
    doc.write (this._div);
    //doc.write('<textarea>'+this._div+'</textarea>')
  }
  
  
  function addItem (Text) {
    var i = this.items.length;
    this.items[i] = new Object();
    this.items[i].text = Text;
  }
  
  function build (Document) {
    // overflow:hidden is used for clipping in Opera
    var visibility = (this.visibility) ? 'inherit' : 'hidden';
    var bgcolor = (this.bgColor) ? 'background-color:'+this.bgColor+'; layer-background-color:'+this.bgColor+';' : '';
  
   
    this._css = ''+
	'<style type="text/css">'+
	  /*
	    Nuovo css
	  */
	  
	  '#'+this.id+' {'+
        'position:relative;'+
        'visibility:'+ visibility +';'+
        'left:0; top:0;'+
        'width:'+ this.width +'; height:'+ this.height +'; clip:rect(0,'+ this.width +','+ this.height +',0);'+
        bgcolor +
      '}'+

     /*
	  '#'+this.id+' {'+
        'position:absolute;'+
        'visibility:'+ visibility +';'+
        'left:'+ this.x +'; top:'+ this.y +';'+
        'width:'+ this.width +'; height:'+ this.height +'; clip:rect(0,'+ this.width +','+ this.height +',0);'+
        bgcolor +
      '}'+
	
	  */
      '.'+this.id+'Item {'+
        'position:absolute;'+
        'visibility:hidden;'+
        'left:0; top:0;'+
        'width:'+ this.width +'; height:'+ this.height +'; clip:rect(0,'+ this.width +','+ this.height +',0);'+
        // 'background-color:aqua;layer-background-color:aqua;'+
        'text-align:left;'+
        ''+
      '}'+
    '</style>';
    var hover = "";
    if (this.hover) {
      hover = " onMouseOver=\"window.document.ticker['"+this.id+"'].stop();\""+
              " onMouseOut=\"window.document.ticker['"+this.id+"'].start();\"";
    }
    this._div = '<div id="'+this.id+'"'+hover+'><layer'+hover+'>';
    for (var i=0; i<this.items.length; i++) {
      newsItemA = i;
      this._div += '<div class="'+this.id+'Item" id="'+this.id+'Item'+i+'">'+this.items[i].text+'</div>'
    }
    this._div += '</layer></div>';
  }
  
  function activate () {
    this.fromX = (this.hslide) ? this.width : 0;
    this.fromY = (this.hslide) ? 0 : this.height;
    for (var i=0; i<this.items.length; i++) {
      with (layer (this.id+'Item'+i)) {
        if (i==0 || !this.sliding)
		  //moveTo(0,0, this.id);
          moveTo(0,0, this.id);
        else
          //moveTo(this.fromX,this.fromY, this.id);
		  moveTo(0,this.fromY, this.id);
        if (this.sliding || i==0)
          show();
        unset();
      }
    }
    var num = Math.sqrt(Math.pow(this.fromX,2) + Math.pow(this.fromY,2))/this.inc;
    ticker(this.id)._dx = this.fromX/num || 0;
    ticker(this.id)._dy = this.fromY/num || 0;
    ticker(this.id).items = this.items;
		if (this.sliding)
			this.timeout = setTimeout('ticker("'+this.id+'").start()', this.pause);
		else
			this.timeout = setTimeout('ticker("'+this.id+'").start()', 100);
  }
  
  function start() {
    if (!this.sliding) {
	    if (this.started) {
        var a = this._scrollCount;
        var b = (this._scrollCount==this.items.length-1)? 0 : this._scrollCount+1;

        layer(this.id+'Item'+a).hide();
        layer(this.id+'Item'+b).show();

        this._scrollCount = (this._scrollCount==this.items.length-1)? 0 : this._scrollCount+1;
	    }
	    ticker(this.id).started = true;
      clearInterval(this.timer);
      this.timer = null;
      this.timeout = setTimeout('ticker("'+this.id+'").start()', this.pause);
    } else if (!this.started) {
      this.started = true;
      var a = this._scrollCount;
      var b = (this._scrollCount==this.items.length-1)? 0 : this._scrollCount+1;
      ticker(this.id).items = this.items;
      if(this.items.length > 1)
      {
        this.timer = setInterval('ticker("'+this.id+'").slide("'+this.id+'Item'+a+'","'+this.id+'Item'+b+'")', this.speed);
      }
    }
  }
  
  function slide (a,b) {
    layer(a).moveBy (-this._dx, -this._dy);
    layer(b).moveBy (-this._dx, -this._dy);
    if ((this.hslide && layer(b).left<=0) || (!this.hslide && layer(b).top <=0)) {
      if (is.nn4) {
        layer(a).reset();
        layer(b).reset();
      }
      clearInterval(this.timer);
      this.timer = null;
	    layer(a).moveBy (this.fromX,this.fromY*2);
      layer(b).moveBy (0,0);	  
      this._scrollCount = (this._scrollCount==this.items.length-1)? 0 : this._scrollCount+1;
      this.timeout = setTimeout('with(ticker("'+this.id+'")){started=false;start();}',this.pause);
    }
  }
  
  function ticker_show () {
    layer(this.id).show();
  }
  
  function ticker_stop () {
    clearTimeout(this.timeout);
    this.timeout = null;
    clearInterval(this.timer);
    this.timer   = null;
    this.started = false;
  }
  
}