﻿/*
	Sample slideElements using mootools 
	Coder  : Abdelkader Elkalidi contact[at]updel.com
*/
var flashnews = new Class({
	Implements: Options,
    initialize: function(content, options) {
		this.setOptions({
			sleepDelay		: 	6000,
			singleTag		: 	'li.class',
			animationDelay	:	1000,
			makeNav			:	false,
			navOpacity		:	0.8,
			debug			:	true
	    }, options);
		this.content		=	$(content);
		this.elems	 		= 	Array();
		this.top	 		= 	$empty;
		this.top	 		= 	0;
		this.startFrom 		= 	0;
	    this._construct();
	},
	_construct: function() {
		if($chk(this.content)){
			if(this.options.makeNav	==	true){
				this.links			=	new Element('div',{'class':'links'}).inject(this.content, 'after');
			}
			this.news 			= 	this.content.getElements(this.options.singleTag);
			if(this.news.length>0){
				this.news.each(function(n,i){
					this.elems[i]		=	n;
					n.getElement('a').addEvent('click', function(){
						newwindow=window.open(this.get('href'),'name','height=300,width=350');
						if (window.focus) {newwindow.focus()}
						return false;
					});
				}.bind(this));
				this.slideStart();
				this.mouseEvents();
			}
		}
	},
	slideStart : function() {
		this.inProgress();
		this.animateElement.bind(this, this.startFrom).delay(this.options.sleepDelay);
		this.startFrom = (this.startFrom+1 < this.news.length) ? (this.startFrom + 1) : 0;
		this.timer = this.slideStart.delay(this.options.sleepDelay, this);
	},
	inProgress : function(){
		if(this.options.makeNav	==	true){
			var inAction	=	this.startFrom;
			this.links.getElements('img').each(function(n,i){
				i	==	inAction	?
				$('slideNav'+i).set('class','hover').setStyle('opacity',1)	:	
				//$('slideNav'+i).set('class','link').setStyles({'opacity':this.options.navOpacity});
				$('slideNav'+i).set('class','link');
			}.bind(this));
		}
	},
	goToElement : function(myId){
		this.startFrom	=	myId;
		this.inProgress();
		$clear(this.timer);
		this.animateElement(myId);
		this.timer = this.slideStart.delay(this.options.sleepDelay, this);
		this.startFrom = (this.startFrom+1 < this.news.length) ? (this.startFrom + 1) : 0;
	},
	animateElement	: function(elmId){
		this.top	=	-this.elems[elmId].getSize().y.toInt()
		this.fx	=	new Fx.Morph(
			this.content, {
				duration: this.options.animationDelay,
				onComplete: function(){
					this.content.setStyle('top',0).grab(this.elems[elmId]);
				}.bind(this)
			}
		).start({'top' : this.top+'px'});
	},
	mouseEvents : function(){
	    this.content.addEvents({
	        'mouseenter' : function(){ 
				$clear(this.timer);
				if($chk(this.fx))	this.fx.pause();
	        }.bind(this),
	        'mouseleave' : function(){
				if($chk(this.fx))	this.fx.resume();
	            this.timer = this.slideStart.delay(this.options.sleepDelay, this);
	        }.bind(this)
	    });
	}
});
