/*
---

script: Loop.js

description: Runs a class method on a periodical

license: MIT-style license.

authors: Ryan Florence <http://ryanflorence.com>

docs: http://moodocs.net/rpflo/mootools-rpflo/Loop

requires:
- core:1.2.4/'*'

provides: [Loop]

...
*/

var Loop = new Class({

	loopCount: 0,
	isStopped: true,
	isLooping: false,
	loopMethod: $empty,

	setLoop: function(fn,delay){
		if(this.isLooping) {
			this.stopLoop();
			var wasLooping = true;
		} else {
			var wasLooping = false;
		}
		this.loopMethod = fn;
		this.loopDelay = delay || 3000;
		if(wasLooping) this.startLoop();
		return this;
	},

	stopLoop: function() {
		this.isStopped = true;
		this.isLooping = false;
		$clear(this.periodical);
		return this;
	},

	startLoop: function(delay) {
		if(this.isStopped){
			var delay = (delay) ? delay : this.loopDelay;
			this.isStopped = false;
			this.isLooping = true;
			this.periodical = this.looper.periodical(delay,this);
		};
		return this;
	},

	resetLoop: function(){
		this.loopCount = 0;
		return this;
	},

	looper: function(){
		this.loopCount++;
		this.loopMethod(this.loopCount);
		return this;
	}

});


/*
---

script: SlideShow.js

description: Easily extendable, class-based, slideshow widget. Use any element, not just images. Comes with packaged transitions but is easy to extend and create your own transitions.  The class is built to handle the basics of a slideshow, extend it to implement your own navigation piece and custom transitions.

license: MIT-style license.

authors: Ryan Florence <http://ryanflorence.com>

requires:
- /Loop
- more:1.2.4.4:Fx.Elements

provides: [SlideShow, Element.playSlideShow, Element.pauseSlideShow]

...
*/


var SlideShow = new Class({
	
	Implements: [Options, Events, Loop],
		
		options: {
			/*
			onShow: $empty,
			onShowComplete: $empty,
			onReverse: $empty,
			onPlay: $empty,
			onPause: $empty,
			*/
			delay: 7000,
			transition: 'crossFade',
			duration: '500',
			autoplay: false
		},
	
	initialize: function(element, options){
		this.setOptions(options);
		this.setLoop(this.showNext, this.options.delay);
		this.element = document.id(element);
		this.slides = this.element.getChildren();
		this.current = this.slides[0];
		this.transitioning = false;
		this.setup();
		if (this.options.autoplay) this.play();
	},
	
	setup: function(){
	  this.setupElement().setupSlides(true);
		return this;
	},
	
	setupElement: function(){
		var el = this.element;
		if (el.getStyle('position') != 'absolute' && el != document.body) el.setStyle('position','relative');
		return this;
	},
	
	setupSlides: function(hideFirst){
		this.slides.each(function(slide, index){
			this.storeTransition(slide).reset(slide);
			if (hideFirst && index != 0) slide.setStyle('display','none');
		}, this);
		return this;
	},
	
	storeTransition: function(slide){
		var classes = slide.get('class');
		var transitionRegex = /transition:[a-zA-Z]+/;
		var durationRegex = /duration:[0-9]+/;
		var transition = (classes.match(transitionRegex)) ? classes.match(transitionRegex)[0].split(':')[1] : this.options.transition;
		var duration = (classes.match(durationRegex)) ? classes.match(durationRegex)[0].split(':')[1] : this.options.duration;
		slide.store('ssTransition', transition).store('ssDuration', duration);
		return this;
	},
	
	resetOptions: function(options){
		this.options = $merge(this.options, options);
		this.setupSlides(false);
		return this;
	},
	
	getTransition: function(slide){
		return slide.retrieve('ssTransition');
	},
	
	getDuration: function(slide){
		return slide.retrieve('ssDuration');
	},
	
	show: function(slide, options){
		slide = (typeof slide == 'number') ? this.slides[slide] : slide;
		if (slide != this.current && !this.transitioning){
			this.transitioning = true;
			var transition = (options && options.transition) ? options.transition: this.getTransition(slide),
				duration = (options && options.duration) ? options.duration: this.getDuration(slide),
				previous = this.current.setStyle('z-index', 1),
				next = this.reset(slide).setStyle("opacity", 0);
			var slideData = {
				previous: {
					element: previous,
					index: this.slides.indexOf(previous)
				}, 
				next: {
					element: next,
					index: this.slides.indexOf(next)
				}
			};
			this.fireEvent('show', slideData);
			this.transitions[transition](previous, next, duration, this);
			(function() { 
				previous.setStyle('display','none');
				this.fireEvent('showComplete', slideData);
				this.transitioning = false;
			}).bind(this).delay(duration);
			this.current = next;
		}
		return this;
	},
	
	reset: function(slide){
		return slide.setStyles({
			'position': 'absolute',
			'z-index': 0,
			'display': 'block',
			'left': 0,
			'top': 0
		}).fade('show');
	},
	
	nextSlide: function(){
		var next = this.current.getNext();
		return (next) ? next : this.slides[0];
	},

	previousSlide: function(){
		var previous = this.current.getPrevious();
		return (previous) ? previous : this.slides.getLast();
	},
	
	showNext: function(options){
		this.show(this.nextSlide(), options);
		return this;
	},
	
	showPrevious: function(options){
		this.show(this.previousSlide(), options);
		return this;
	},
	
	play: function(){
		this.startLoop();
		this.fireEvent('play');
		return this;
	},
	
	pause: function(){
		this.stopLoop();
		this.fireEvent('pause');
		return this;
	},
	
	reverse: function(){
		var fn = (this.loopMethod == this.showNext) ? this.showPrevious : this.showNext;
		this.setLoop(fn, this.options.delay);
		this.fireEvent('reverse');
		return this;
	},
	
	toElement: function(){
		return this.element;
	}
	
});

Element.Properties.slideshow = {

	set: function(options){
		var slideshow = this.retrieve('slideshow');
		if (slideshow) slideshow.pause();
		return this.eliminate('slideshow').store('slideshow:options', options);
	},

	get: function(options){
		if (options || !this.retrieve('slideshow')){
			if (options || !this.retrieve('slideshow:options')) this.set('slideshow', options);
			this.store('slideshow', new SlideShow(this, this.retrieve('slideshow:options')));
		}
		return this.retrieve('slideshow');
	}

};


Element.implement({
	
	playSlideShow: function(options){
		this.get('slideshow', options).play();
		return this;
	},
	
	pauseSlideShow: function(options){
		this.get('slideshow', options).pause();
		return this;
	}
	
});

SlideShow.adders = {
	
	transitions:{},
	
	add: function(className, fn){
		this.transitions[className] = fn;
		this.implement({
			transitions: this.transitions
		});
	},
	
	addAllThese : function(transitions){
		$A(transitions).each(function(transition){
			this.add(transition[0], transition[1]);
		}, this);
	}
	
}

$extend(SlideShow, SlideShow.adders);
SlideShow.implement(SlideShow.adders);

SlideShow.add('fade', function(previous, next, duration, instance){
	previous.set('tween',{duration: duration}).fade('out');
	return this;
});

SlideShow.addAllThese([

	['none', function(previous, next, duration, instance){
		previous.setStyle('display','none');
		return this;
	}],

	['crossFade', function(previous, next, duration, instance){
		previous.set('tween',{duration: duration}).fade('out');
		next.set('tween',{duration: duration}).fade('in');
		return this;
	}],

	['fadeThroughBackground', function(previous, next, duration, instance){
		var half = duration/2;
		next.set('tween',{ duration: half	}).fade('hide');
		previous.set('tween',{
			duration: half,
			onComplete: function(){
				next.fade('in');
			}
		}).fade('out');
	}],
	
	['pushLeft', function(previous, next, duration, instance){
		var distance = instance.element.getSize().x;
		next.setStyle('left', distance);
		new Fx.Elements([previous,next],{duration: duration}).start({
			0: { left: [-distance] },
			1: { left: [0] }
		});
		return this;
	}],

	['pushRight', function(p,n,d,i){
		var distance = i.element.getSize().x;
		n.setStyle('left', -distance);
		new Fx.Elements([p,n],{duration: d}).start({
			0: { left: [distance] },
			1: { left: [0] }
		});
		return this;
	}],

	['pushUp', function(p,n,d,i){
		var distance = i.element.getSize().y;
		n.setStyle('top', distance);
		new Fx.Elements([p,n],{duration: d}).start({
			0: { top: [-distance] },
			1: { top: [0] }
		});
		return this;
	}],

	['pushDown', function(p,n,d,i){
		var distance = i.element.getSize().y;
		n.setStyle('top', -distance);
		new Fx.Elements([p,n],{duration: d}).start({
			0: { top: [distance] },
			1: { top: [0] }
		});
		return this;
	}],

	['blindRight', function(p,n,d,i){
		var distance = i.element.getSize().x;
		n.setStyles({
			left: -distance,
			'z-index': 2
		}).set('tween',{duration: d}).tween('left',0);
		return this;
	}],

	['blindLeft', function(p,n,d,i){
		var distance = i.element.getSize().x;
		n.setStyles({
			left: distance,
			'z-index': 2
		}).set('tween',{duration: d}).tween('left',0);
		return this;
	}],

	['blindUp', function(p,n,d,i){
		var distance = i.element.getSize().y;
		n.setStyles({
			top: distance,
			'z-index': 2
		}).set('tween',{duration: d}).tween('top',0);
		return this;
	}],

	['blindDown', function(p,n,d,i){
		var distance = i.element.getSize().y;
		n.setStyles({
			top: -distance,
			'z-index': 2
		}).set('tween',{duration: d}).tween('top',0);
		return this;
	}],

	['blindDownFade', function(p,n,d,i){
		this.blindDown(p,n,d,i).fade(p,n,d,i);
	}],

	['blindUpFade', function(p,n,d,i){
		this.blindUp(p,n,d,i).fade(p,n,d,i);
	}],

	['blindLeftFade', function(p,n,d,i){
		this.blindLeft(p,n,d,i).fade(p,n,d,i);
	}],

	['blindRightFade', function(p,n,d,i){
		this.blindRight(p,n,d,i).fade(p,n,d,i);
	}]

]);

/************************************************
*   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. */ 

var mooquee = new Class({
    initialize: function(element, options) {
		this.setOptions({
			marHeight: 40,
			marWidth: 550,
			steps: 1,
			speed: 40,
			direction: 'bottom',
			pauseOnOver: true,
			pauseOnContainerOver: true
	    }, options);
	    this.timer = null;
	    this.textElement = null;
	    this.fx = null;
	    this.mooqueeElement = element;
	    this.constructMooquee();
	},
	constructMooquee: function() {
		var el = this.mooqueeElement;
		el.setStyles({
		    'width' : this.options.marWidth
		    ,'height' : this.options.marHeight		    
		});
        this.textElement = new Element('div',{
		    'class' : 'mooquee-text'
		    ,'id' : 'mooquee-text'
		}).set('html', el.innerHTML).setStyle('position', 'absolute');
		el.set('html', '');//clear mooqueeElement inner html
		el.setStyles({
			position: 'absolute',
			overflow: 'hidden',
			'white-space': 'nowrap'
		});
		this.textElement.inject(el);
		//this.textElement = $('mooquee-text');
		
		if((this.textElement.getCoordinates().width.toInt()+20) <= this.textElement.getParent().getCoordinates().width.toInt()) {
			this.textElement.setStyle("padding-left", "20px");
			return;
		}
        if(!this.setStartPos()){return;}
        if(this.options.pauseOnOver){this.addMouseEvents();}
		//start marquee
        
		//this.timer = this.startMooquee.delay(this.options.speed, this);
        var mq = this;
        this.fx = new Fx.Tween(this.textElement, {
        	"onComplete": function() {
        		this.start("left", mq.textElement.getParent().getCoordinates().width.toInt(), -1 * mq.textElement.getCoordinates().width.toInt());
        	},
        	"duration": this.options.speed,
        	transition: "linear"
        });
        
        this.fx.start("left", this.textElement.getParent().getCoordinates().width.toInt(), this.textElement.getCoordinates().width.toInt() * -1);
	},
	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( 'bottom', this.options.marHeight );
        else if( this.options.direction == 'left' )
            this.textElement.setStyle('left', ( -1 * this.textElement.getCoordinates().width.toInt()));
        else if( this.options.direction == 'right' )
            this.textElement.setStyle( 'left', this.options.marWidth );
        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();
	        		this.fx.pause();
	            }.bind(this),
	            'mouseleave' : function(me){
	               // this.timer = this.startMooquee.delay(this.options.speed, this);
	            	this.fx.resume();
	            }.bind(this)
	        });
	    }else{
	        this.mooqueeElement.addEvents({
	            'mouseenter' : function(me){
	                //this.clearTimer();
	        		this.fx.pause();
	            }.bind(this),
	            'mouseleave' : function(me){
	                //this.timer = this.startMooquee.delay(this.options.speed, this);
	            	this.fx.resume();
	            }.bind(this)
	        });
	    }
	},
    startMooquee: function(){
        /* sod.hu Ext */
        if(this.options.direction == 'bottom' || this.options.direction == 'top')
            var pos = this.textElement.getStyle('bottom').toInt();
        else if(this.options.direction == 'left' || this.options.direction == 'right')
            var pos = this.textElement.getStyle('left').toInt();
        if(this.options.direction == 'bottom')
            this.textElement.setStyle( 'bottom', ( pos + -1 ) + 'px' );
        else if(this.options.direction == 'top')
            this.textElement.setStyle( 'bottom', ( pos + 1 ) + 'px' );
        else if(this.options.direction == 'left'){
            this.textElement.setStyle( 'left', ( pos + -1 ) + 'px' );
        }
        else if(this.options.direction == 'right')
            this.textElement.setStyle( 'left', ( pos + 1 ) + 'px' );
        /* sod.hu Ext end */
        this.checkEnd(pos);
        //this.timer = this.startMooquee.delay(this.options.speed, this);
    },
    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 > this.options.marHeight.toInt())
                this.textElement.setStyle('bottom', -1 * (this.textElement.getCoordinates().height.toInt()) );
        } else if(this.options.direction == 'left'){
            if(pos < -1 * (this.textElement.getCoordinates().width.toInt()))
                this.textElement.setStyle('left', this.options.marWidth);
        } 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 */
    },
    setDirection: function(dir){
        this.options.direction = dir;
        this.setStartPos();
    }
});
mooquee.implement(new Options);


$(document).addEvent("domready", function() {
	if($('start_news')) {
		var newsContainer = $('start_news').getElement("div");
		var speed = newsContainer.getElements("span").length * 10000;
		var speed = newsContainer.get("html").stripTags().length*50 + 5000;
		obj = new mooquee(newsContainer, {marHeight: newsContainer.getSize().y+"px", marWidth: newsContainer.getSize().x+"px", direction:"left", "speed":speed});
	}
});

