
if(typeof(window['MooTools']) == 'undefined'){

    
    var mstkRedirect = 'http:/www.mootools.net/';

    if(confirm('Moostick requires the MooTools JavaScript framework.\n'
               + 'Be sure to include it before Moostick.\n\n'
               + 'Click OK for more info, or Cancel to load the page without it.')){
        top.location.href = redirect;
    }
}
else{

    var MstkHelpers = {
        
        qsVar: function(key){
            var val = false;
            if(key.length > 0){
                $ES('script').each(function(s){
                    if(s.src.match('moostick.js?')){
                        var uriParts = s.src.split('?');
                        if(uriParts[1]){
                            uriParts[1].split('&').each(function(keyPair){
                                var tmp = keyPair.split('=');
                                if(tmp[1] && tmp[0] == key)
                                    val = tmp[1];
                            });
                        }
                    }
                });
            }
            return val;
        },
        
        checkArray: function(val){
            if(!val[0])
                return [val];
            else
                return val;
        }
    
    } // end MstkHelpers


    // See: <Procedural code>
    
    if(MstkHelpers.qsVar('init') !== 'false'){
        window.addEvent('domready', function() {
            MstkInit.go(false, true);
        });
    }


   
    var MstkInit = {
        
        lists        : false,
        
        autoStart    : false,
        interval     : 3500,
        fxOptions    : {duration:1000},
        trust4Speed  : false,     
        trustOpacity : false,
        
        go: function(lists, autoStart, interval, fxOptions, trust4Speed, trustOpacity) {
            this.lists        = (!lists)
                              ? this.lists
                              : lists;
            this.autoStart    = (autoStart === true)
                              ? autoStart
                              : this.autoStart;
            
            this.interval     = (!interval) || (interval.toInt() < 500)
                              ? this.interval
                              : interval.toInt();
            
            this.fxOptions    = ((!fxOptions) || (typeof(fxOptions) != 'object'))
                              ? this.fxOptions
                              : fxOptions;
            
            this.trust4Speed  = (trust4Speed === true)
                              ? trust4Speed
                              : this.trust4Speed;
            
            this.trustOpacity = (trustOpacity === true)
                              ? trustOpacity
                              : this.trustOpacity;
    
            if(this.autoStart === true){
                if(this.trust4Speed === true){
                    // Kamikaze mode!
                    this._fastMode();
                }
                else{
                    // Cautious mode
                    this._safeMode();
                }
            }
        },
        
        
        _fastMode: function(){
            this.lists = MstkHelpers.checkArray(this.lists);
            this.lists.each(function(list){
                new Moostick(
                    list,
                    this.autoStart,
                    this.interval,
                    this.fxOptions,
                    this.trust4Speed,
                    this.trustOpacity
                )
            }, this);
        },
        
        _safeMode: function(){
          
            if(this.lists === false){
                var defaultLists = false;
                
                // Scan for a list with the recommended default id
                if($('moostick'))
                    defaultLists = [$('moostick')];
    
                // Or can for (a) list(s) with the recommended default class
                else if($$('.moostick'))
                    defaultLists = $$('.moostick');
                
                this.lists = (!defaultLists) ? false : defaultLists;
            }
     
            if(this.lists){
                this.lists = MstkHelpers.checkArray(this.lists);
                this.lists.each(function(list){
                    new Moostick(
                        list,
                        this.autoStart,
                        this.interval,
                        this.fxOptions,
                        this.trust4Speed,
                        this.trustOpacity
                    )
                }, this);
            }
        }
    
    } // end MstkInit


    /*
    Class: Moostick
        Modifies the presentation and behaviour of li-containing elements to
        act as pseudo news tickers, and extends their DOM with itself
		(eg: myUL.moostick) for easy manipulation.
    
    Example: Programming
        (start code)
            // Auto-discovery mode, looking for li-containing elements with
			// an id (single) or class of "moostick", then auto-starts.
            new Moostick();
            $('moostick').moostick.stopTick();

            // Instantiate Moostick for the "my-custom-id" element. Do
			// not auto-start. Fade every 5 seconds, for 1/2 second.
			// Trust the list I sent, but handle the opacity assignment.
            var myList = new Moostick(
            	$('my-custom-id'),
				false,
				5000,
				{duration: 500},
				true,
				false
            );
            myList.startTick();
        (end)
    */
    var Moostick = Class({
        version         : '1.0',
        list         : {},
        autoStart    : false,
        interval     : 6500,
        fxOptions    : {duration:8000},
        trust4Speed  : false,
        trustOpacity : false,
        _firstRun    : true,
        _elSched     : null,
        _fx: false,
        
        initialize: function(list, autoStart, interval, fxOptions, trust4Speed, trustOpacity) {
            this.list         = ((!list) || (typeof(list) != 'object'))
                              ? this.list
                              : list;
    
            this.autoStart    = (autoStart === true)
                              ? autoStart
                              : this.autoStart;
            
            this.interval     = (!interval) || (interval.toInt() < 500)
                              ? this.interval
                              : interval.toInt();
//alert(interval);
this.interval=7500;
//alert(this.interval);
            this.fxOptions    = ((!fxOptions) || (typeof(fxOptions) != 'object'))
                              ? this.fxOptions
                              : fxOptions;
            
            this.trust4Speed  = (trust4Speed === true)
                              ? trust4Speed
                              : this.trust4Speed;
            
            this.trustOpacity = (trustOpacity === true)
                              ? trustOpacity
                              : this.trustOpacity;
            
            this.list.moostick = this;
            
            if(this.autoStart === true){
                if(this.trust4Speed === true){
                    // Kamikaze mode!
                    this._fastMode();
                }
                else{
                    // Cautious mode
                    this._safeMode();
                }
            }
        },
        
        /*
        Method: startTick
            Calls <Moostick.initialize> on-demand, always in auto-start mode.
        
        Arguments:
         	lists        - See: <Moostick.list>
        	interval     - See: <Moostick.interval>
        	fxOptions    - See: <Moostick.fxOptions>
        	trust4Speed  - See: <Moostick.trust4Speed>
        	trustOpacity - See: <Moostick.trustOpacity>
        
        See also:
        	<Moostick.stopTick>, <Moostick.pauseTick>
        	and <Moostick.resumeTick>.
        */
        startTick: function(list, interval, fxOptions, trust4Speed, trustOpacity){
            this.initialize(list, true, interval, fxOptions, trust4Speed, trustOpacity);
        },
        
        /*
        Method: stopTick
            Stops the ticker completely, including the attached mouse events.
        
        See also:
        	<Moostick.startTick>.
        */
        stopTick: function(){
            // TODO: figure out why removeEvent isn't working here
            this.list.$events.mouseenter = false;
            this.list.$events.mouseleave = false;
            this.pauseTick();
        },
        
        pauseTick: function(){
            this._elSched = $clear(this._elSched);
        },    
        
        resumeTick: function(){
            this._schedule();
        },
        
         _fastMode: function(){
            this._liHandler();
            this._schedule();
        },
        
        _safeMode: function(){
            if(this.list){
                if($ES('li', this.list)){
                    var items = $ES('li', this.list);
                    // Assign proprietary CSS class if not already assigned
                    if(!this.list.hasClass('moostick')) this.list.addClass('moostick');
                   
                    // Test for an indication of existing style assignements,
                    var noStyle = false;
                    if( this.list.getStyle('overflow') != 'hidden'
                        || items[0].getStyle('display') != 'block'
                        || items[0].getStyle('list-style-type') != 'none'){
                        // we'll assume (!) no compatible style has been applied
                        noStyle = true;
                    }

                    if(noStyle){                
                        this.list.setStyles({
                            'display' : 'block',
                            'margin'  : 0,
                            'overflow': 'hidden'
                        });
                        this._liHandler({
                            'display'        : 'block',
                            'list-style-type': 'none',
                            'margin'         : '0',
                            'padding'        : 0
                        });
                    }
                    else{
                        this._liHandler();
                    }
                    // We should now have a *more* trustworthy environment
                    this._schedule();
                }
            }
        },
        
         _setMouseEvents: function(){  
            this.list.addEvents({
                'mouseenter': function(){
                    this.moostick.pauseTick();
                },
                'mouseleave': function(){
                    this.moostick.resumeTick();
                }
            });
        },
        
        _liHandler: function(styles){
            // Verify styles value and format
            if((!styles) || (typeof(styles) != 'object')) styles = false;
            
            // no need to even loop if no styles were sent and trusting opacity
            if((this.trustOpacity !== true) || (styles !== false)){
                
                $ES('li', this.list).each(function(li){
                    // Apply styles before forcing opacity
                    if(styles !== false) li.setStyles(styles);
                    
                    // Turn off all li but first
                    if(this.trustOpacity !== true)
                        li.setOpacity(0);
                });
            }
        },
        
        /*
        Method: _schedule
        	Prepares for and schedules <Moostick._run>.
        	Also sets up <Moostick._setMouseEvents>. 
        */
        _schedule: function(){
            if(!this._elSched){
                // Define if we should fade the first item in or not.
                var firstItem = $E('li', this.list);
                if(firstItem.getStyle('opacity') != 1)
                    this._fadeIn(firstItem);
                else if(this._firstRun === true)
                    this._fadeIn(firstItem);
                
                // Schedule <Moostick._run>
                this._elSched = this._run.periodical(
                    this.interval,
                    this,
                    this.list
                );
                
                // Set <Moostick._setMouseEvents>
                listEvents = this.list.$events;
                if(!listEvents)
                    this._setMouseEvents();
                else if((!listEvents.mouseenter) && (!listEvents.mouseleave))
                    this._setMouseEvents();
            }
        },
        
        /*
        Method: _run
        	Moostick transition: move first to last, fade new first in
        	then set opacity on new last to 0.
        */
        _run: function(){
            var items = $ES('li', this.list);
            
            // If we have at least 2
            if(items[1]){
                // Move first to last
                items[0].injectAfter(items.getLast());
                
                // Fade new first in
                this._fadeIn(items[1]);
                
                // Set new last off, for future fade in
                $ES('li', this.list).getLast().setOpacity(0);
                //$ES('li', this.list).getLast().effect('opacity', {duration:2000}).start(1,0);
            }
        },
        
        /*
        Method: _fadeIn
        	Handles the MooTools opacity effect, from 0 to 1
        
        Arguments:
        	item - li element to apply the effect to. 
        */
        _fadeIn: function(item){
            this._fx = item.effect('opacity', {duration:2000}).start(0,1);
            this._firstRun = false;
        }
    
    }); // end Moostick
    
}
