//-----------------------------------------------------------------------
// Module name   : Marquee
// Author        : Paul Battersby
// Creation Date : Jan 12/11
/*
	Marquee using mootools
	Abdelkader Elkalidi contact[at]updel.com
	http://updel.com
*/
//
//  This will take whatever is in the marqee section and scroll it from
//  right to left
//
//  Usage:
/*
    window.addEvent('domready', function(){
      new Marquee({
        marqueeIn : 'marqueeCell',
        marqueeMe : 'marquee',
        speed     : 20,
        width     : 500,
        height    : 20
      });
    });

    <div id="marqueeCell">
      <div id="marquee">
        This is a sentence. This is another sentence. This is yet another sentence
      </div>
    </div>

*/
//
//  $Log:$
//------------------------------------------------------------------------

//---------------------------- INCLUDE FILES -----------------------------

var Marquee = new Class({
    initialize: function(options) {
		this.setOptions({
			marqueeIn	: 'marqueeIn',
			marqueeMe	: 'marqueeMe',
      width  : 500,
      height : 20,
			speed		: 10,
			//direction	: 'left', Not yet
			hoverpause	: true
	    }, options);
	    this._construct();
	},
	_construct: function() {
    $(this.options.marqueeIn).setStyles({
      position : "relative",
      width :  this.options.width,
      height : this.options.height,
      overflow : "hidden",
      margin : "0 auto"
    });

    this.elTomarquee  = $(this.options.marqueeMe);

    this.elTomarquee.setStyles({
       position : "absolute",
       "white-space" : "nowrap",
       top : 0
    });

    this.startFrom    = $(this.options.marqueeMe).getStyle('width').toInt();
    this.elTomarquee.setStyle("right",-this.startFrom);

    this.restartLimit = $(this.options.marqueeIn).getStyle('width').toInt();

    this.marquee(); //start marquee
		this.mouseEvents(); //start marquee
	},

  // this resets the width of the marquee. Useful if the browser window has
  // been resized
  setWidth : function(width) {
    $(this.options.marqueeIn).setStyle("width",width);
    this.startFrom    = $(this.options.marqueeMe).getSize().x;
    this.elTomarquee.setStyle("right",-this.startFrom);
    this.restartLimit = width;
  },

  marquee: function() {
		var addPix = this.elTomarquee.getStyle('right').toInt();
		this.elTomarquee.setStyle('right',(addPix+1)+'px');
		if(addPix > this.restartLimit){
			this.elTomarquee.setStyle('right',-this.startFrom+'px');
		}
		this.timer = this.marquee.delay(this.options.speed, this);
	},
	mouseEvents : function(){
	    this.elTomarquee.addEvents({
	        'mouseover' : function(){
	            $clear(this.timer);
	        }.bind(this),
	        'mouseout' : function(){
	            this.timer = this.marquee.delay(this.options.speed, this);
	        }.bind(this)
	    });
	}
});
Marquee.implement(new Options);


