/************************************************
*   mooquee v.1.1                               *
*   Http: WwW.developer.ps/moo/mooquee          *
*   Dirar Abu Kteish dirar@zanstudio.com        *
*   2009-01-30                                  *
*************************************************
*   Extend By www.Sod.hu                        *
*   new directions: top, bottom                 *
*   2008-04-30                                  *
/***********************************************/
/* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details. */ 
/*
!important!
has bug! current version support left/top marquee only!!!
daijj[jjyyis@126.com]
*/
var mooquee = new Class({
    initialize: function(element, options) {
		this.setOptions({
			marHeight: 40,
			marWidth: 550,
			steps: 1,
			speed: 40,
			sleep: 2000,
			direction: 'left',
      jump: 1,
			pauseOnOver: true,
			pauseOnContainerOver: true
	    }, options);
      this.mm = 0;
	    this.timer = null;
	    this.textElement = null;
	    this.textElement2 = null;
	    this.mooqueeElement = element;
	    this.constructMooquee();
	},
	constructMooquee: function() {
		var el = this.mooqueeElement;
		el.setStyles({
		    'overflow' : 'hidden'
		});
    this.textElement = new Element('div',{
		    'class' : 'mooquee'
        ,styles:{'display': 'block','position': 'relative','z-index':888}
		}).set('html', el.innerHTML);
    this.textElement2 = new Element('div',{
		    'class' : 'mooquee'
        ,styles:{'display': 'block','position': 'relative','z-index':888}
		}).set('html', el.innerHTML);
		el.set('html', '');//clear mooqueeElement inner html
		this.textElement.inject(el);
		this.textElement2.inject(el);
    //this.textElement = $('mooquee-text');
    if(!this.setStartPos()){return;}
    if(this.options.pauseOnOver){this.addMouseEvents();}
    
    //alert(el.id + '=' + this.textElement.getCoordinates().top + ':' + this.textElement2.getCoordinates().top);
    //alert(el.id + '=' + this.textElement.getCoordinates().left + ':' + this.textElement2.getCoordinates().left);
		
    //start marquee
		this.timer = this.startMooquee.delay(this.options.speed, this);
	},
	setStartPos: function(){
	    /* sod.hu Ext */
		if( this.options.direction == 'bottom' ){
        this.textElement.setStyle('bottom', ( -1 * this.textElement.getCoordinates().height.toInt()));
    }else if( this.options.direction == 'top' ){
        this.textElement.setStyle( 'top', 0);
        this.textElement2.setStyle( 'top', 0);
    }else if( this.options.direction == 'left' ){
        this.textElement.setStyle('left', 0);
        this.textElement2.setStyle('left', this.textElement.getCoordinates().width);
        if (!Browser.Engine.trident) {
          this.textElement2.setStyle('padding-left', 120);
          this.textElement2.setStyle('width', this.textElement2.getCoordinates().width + 120);
        }
        this.textElement2.setStyle('top', (-1*this.textElement2.getCoordinates().height));
    }else if( this.options.direction == 'right' ){
        this.textElement.setStyle( 'left', this.options.marWidth );
        this.textElement2.setStyle('top', (-1*this.textElement2.getCoordinates().height));
    }else{
        alert( 'direction config error: ' + this.options.direction );
        return false;
    }
    return true;
	},
	addMouseEvents : function(){
	    if(!this.options.pauseOnContainerOver){
	        this.textElement.addEvents({
	            'mouseenter' : function(me){
	                this.clearTimer();
	            }.bind(this),
	            'mouseleave' : function(me){
	                this.timer = this.startMooquee.delay(this.options.speed, this);
	            }.bind(this)
	        });
	    }else{
	        this.mooqueeElement.addEvents({
	            'mouseenter' : function(me){
	                this.clearTimer();
	            }.bind(this),
	            'mouseleave' : function(me){
	                this.timer = this.startMooquee.delay(this.options.speed, this);
	            }.bind(this)
	        });
	    }
	},
    startMooquee: function(){
        /* sod.hu Ext */
        if(this.options.direction == 'bottom' || this.options.direction == 'top'){
            var pos = this.textElement.getStyle('top').toInt();
            var pos2 = this.textElement2.getStyle('top').toInt();
        }else if(this.options.direction == 'left' || this.options.direction == 'right'){
            var pos = this.textElement.getStyle('left').toInt();
            var pos2 = this.textElement2.getStyle('left').toInt();
        }
        if(this.options.direction == 'bottom'){
          pos = pos - this.options.steps;
            this.textElement.setStyle( 'bottom', ( pos ) + 'px' );
        }else if(this.options.direction == 'top'){
            pos = pos - this.options.steps;
            pos2 = pos2 - this.options.steps;
            this.textElement.setStyle( 'top', ( pos ) + 'px' );
            this.textElement2.setStyle( 'top', ( pos2 ) + 'px' );
        }else if(this.options.direction == 'left'){
          pos = pos - this.options.steps;
          pos2 = pos2 - this.options.steps;
            this.textElement.setStyle( 'left', ( pos ) + 'px' );
            this.textElement2.setStyle( 'left', ( pos2 ) + 'px' );
        }else if(this.options.direction == 'right'){
            this.textElement.setStyle( 'left', ( pos + this.steps ) + 'px' );
        }
        /* sod.hu Ext end */
        this.checkEnd(pos);
        this.checkEnd2(pos2);
        
        if (this.options.jump > 1 && this.mm >= this.options.jump) {
          this.mm = 0;
          this.timer = this.startMooquee.delay(this.options.sleep, this);
        } else {
          this.timer = this.startMooquee.delay(this.options.speed, this);
        }
        this.mm++;
    },
    resumeMooquee: function(){
        this.stopMooquee();
        if(this.options.pauseOnOver){this.addMouseEvents();}
        this.timer = this.startMooquee.delay(this.options.speed, this);
    },
    stopMooquee: function(){
        this.clearTimer();
        this.textElement.removeEvents();
    },
    clearTimer: function(){
        $clear(this.timer);
    },
    checkEnd: function(pos){
        /* sod.hu Ext */
        if(this.options.direction == 'bottom'){
            if(pos < -1 * (this.textElement.getCoordinates().height.toInt()))
                this.textElement.setStyle('bottom', this.options.marHeight);
        } else if(this.options.direction == 'top'){
            if(pos <= -1*this.textElement.getCoordinates().height.toInt())
                this.textElement.setStyle('top', this.textElement.getCoordinates().height.toInt());
        } else if(this.options.direction == 'left'){
            if(pos <= -1*(this.textElement.getCoordinates().width.toInt()))
                this.textElement.setStyle('left', this.textElement2.getCoordinates().width.toInt());
        } else if(this.options.direction == 'right'){
            if(pos > this.options.marWidth.toInt())
                this.textElement.setStyle('left', -1 * (this.textElement.getCoordinates().width.toInt()) );
        }
        /* sod.hu Ext end */
    },
    checkEnd2: function(pos){
        /* sod.hu Ext */
        if(this.options.direction == 'bottom'){
            if(pos < -1 * (this.textElement2.getCoordinates().height.toInt()))
                this.textElement2.setStyle('bottom', this.options.marHeight);
        } else if(this.options.direction == 'top'){
            if(pos <= -1*this.textElement.getCoordinates().height.toInt()*2)
                this.textElement2.setStyle('top', 0 );
        } else if(this.options.direction == 'left'){
            if(pos <= -1*this.textElement2.getCoordinates().width.toInt())
                this.textElement2.setStyle('left', this.textElement.getCoordinates().width.toInt());
        } else if(this.options.direction == 'right'){
            if(pos > this.options.marWidth.toInt())
                this.textElement2.setStyle('left', -1 * (this.textElement2.getCoordinates().width.toInt()) );
        }
        /* sod.hu Ext end */
    },
    setDirection: function(dir){
        this.options.direction = dir;
        this.setStartPos();
    }
});
mooquee.implement(new Options);