/**
 *	Mod Ciul News Module Site Script
 *
 *	@package		mod_ciul_news
 *	@subpackage		modules
 *	@author			Ciul
 */

window.addEvent('domready', function() {
	
	(function(){
		
		var mod_ciul_news_holder = new Class({
			Implements: [Options],
			
			// Options
			options: {
				
			},
			
			// Properties
			id:			null,
			title:		null,
			el:			null,
			holders:	{},
			text:		null,
			images:		null,
			elements:	{},
			
			initialize: function(el, article, options) {
				this.setOptions(options);
				
				this.id		= article.id;
				this.title	= article.title;
				this.link	= article.link;
				this.el		= el;
				this.text	= article.text;
				this.images	= article.images;
				
				if(typeOf(this.options.show_title.toInt()) == 'number'? this.options.show_title.toInt() : 1) {
					this.createTitle();
				}
				this.options.delay = typeOf(this.options.delay.toInt()) == 'number'? this.options.delay.toInt() : 4000 ;
				
				this.createImagery();
				this.createText();
				this.createEvents();
			},
			
			createTitle: function() {
				this.elements.title = new Element('div', {
					'class'	: 'title',
					html	: '<h3><a href="'.concat(this.link, '">', this.title, '</a></h3>')
				}).inject(this.el);
			},
			
			createText: function() {
				this.elements.text = new Element('div', {
					'class'	: 'text'
				}).inject(this.el);
				
				var inner_text = new Element('div', {
					'class'	: 'inner_text',
					html	: this.text
				}).inject(this.elements.text);
			},
			
			createImagery: function() {
				var length = this.images.length;
				if(length == 0) {return null;}
				
				this.elements.images = new Element('div', {
					'class'	: 'images'
				}).inject(this.el);
				
				Array.each(this.images, function(image, index) {
					this.images[index] = new Element('img', {
						src		: image,
						opacity	: 1
					}).inject(this.elements.images);
				}.bind(this));
				
				if(length > 1) {
					this.animateImg(this.images[length-1], length-1, 'out');
				}
				
			},
			
			animateImg: function(image, index, mode) {
				var that = this;
				var fx = new Fx.Morph(image, {
					duration: this.options.delay
				});
				
				if(mode == 'out') {
					
					if(index > 0) {
						fx.start({
							'opacity'	: 0
						}).chain(function() {
							that.animateImg(that.images[index-1], index-1, 'out');
						});
					} else {
						Array.each(that.images, function(image, index) {
							var fx = new Fx.Morph(image, {
								duration: that.options.delay
							});
							fx.start({
								'opacity':	1
							});
						}, that);
						
						fx.start({
							'opacity'	: 0
						}).chain(function() {
							var length = that.images.length;
							that.animateImg(that.images[length-1], length-1, 'out');
						});
					}
					
				}
				
			},
			
			createEvents: function() {
				if(typeOf(this.options.show_title.toInt()) == 'number'? this.options.show_title.toInt() : 1) {
					// Title events
					this.elements.title.addEvents({
						mouseenter: function() {
							
						}.bind(this),
						mouseleave: function() {
							
						}.bind(this)
					});
				}
				
				// Text events
				this.elements.text.addEvents({
					mouseenter: function() {
						
					}.bind(this),
					mouseleave: function() {
						
						this.elements.text.setStyle('display', 'none');
					}.bind(this)
				});
				
				// Images events
				this.elements.images.addEvents({
					mouseenter: function() {
						
						this.elements.text.setStyle('display', 'block');
					}.bind(this),
					mouseleave: function() {
						
					}.bind(this)
				});
			}
			
		});
		
		var mod_ciul_news_els = $$('.mod_ciul_news_el');
		
		Array.each(mod_ciul_news_els, function(el, index) {
			
			var mod = mod_ciul_news[el.id];
			
			Object.each(mod.articles, function(article, key) {
				var article_div = el.getElement('div.item_'.concat(article.id));
				new mod_ciul_news_holder(article_div, article, mod.options);
			});
			
		});
		
		
	})();
	
});
