/* 
 * Schiavo Mazzonis
 *
 * @package		Schiavo Mazzonis
 * @author		Oskar Krawczyk (o.krawczyk@keepthinking.it)
 * @version		1.0
 * @dependecies	MooTools 1.2.1
 * @copyright	Copyright (c) 2008-2010, Oskar Krawczyk (Keepthinking Ltd.)		
 * @link		http://keepthinking.it
 * 
 ======================================================================= */

// e  - event
// el - element

// COMPRESS BEFORE DEPLOYING
// $ java -jar yuicompressor-2.3.4.jar --nomunge --type js -o output.js input.js 

var Engine = new Class({
	
	Implements: Options,
		
	options: {},
	
	/* Initialize needed methods
	 ======================================================================= */
	initialize: function(options) {
		this.setOptions(options);
		
		this.abstract = $('abstract');
		
		this.newWindow();	
		this.infoItem();
		this.openclose();
		this.gallery();
		this.addMail();
	},
	
	/* Unobtrusive new window opener
	 ======================================================================= */
	newWindow: function() {
		$$('a.window').each(function(link) {
			link.addEvent('click', function(e) {
				var newWindow = window.open(this.getProperty('href'), '_blank').focus();
				e.stop();
			});
		});
	},
	
	addMail: function() {
		$$('*[rel=mail]').each(function(link) {
			var mail = link.get('text').replace('/dot/', '.').replace('/at/', '@');
			
			link.set({
				'text': mail,
				'href': 'mailto:'+mail
			});
		});
	},
	
	infoItem: function() {
		this.item = $$('.extItem');

		if($chk(this.item)) {
			this.item.each(function(item) {				
				var info = item.getElement('.eiInfo');
				info.get('morph', {transition: 'cubic:out', duration: 300});
				info.set('styles', {opacity: 0.7, width: item.getSize().x.toInt()-10});
				
				(function(){info.morph({left: -200, top: 40, opacity: 0})}).delay(1000);
				
				item.addEvents({
					mouseover: function() {
						info.morph({left: 0, top: 0, opacity: 0.7});
					}.bind(this),
					mouseout: function() {
						info.morph({left: -200, top: 40, opacity: 0});
					}.bind(this)	
				});
			});
		}
	},
	
	initOc: function() {
		this.ocAbstract.store('height', this.ocAbstract.getSize().y)
		this.ocFull.store('height', this.ocFull.getSize().y)
		this.ocMode.store('state', 'closed');
		
		this.ocFull.setStyles({height: 0});
	},
	
	openclose: function() {
		var ocCont = $$('.ocContent');
		
		if($chk(ocCont)) {
			ocCont.each(function(item) {

				var prop = {
					ocCont: item,
					ocAbstract: item.getElement('.ocAbstract'),
					ocFull: item.getElement('.ocFull'),
					ocMode: item.getParent().getElement('.oc a')
				};
				
				this.initOc.bind(prop).run();
				
				prop.ocMode.addEvent('click', function() {
					if(prop.ocMode.retrieve('state').test(/closed/i) == true) {
						prop.ocFull.morph({height: prop.ocFull.retrieve('height')});
						prop.ocAbstract.morph({height: 0});
						prop.ocMode.store('state', 'opened');
						prop.ocMode.set('text', Lang.less);
						prop.ocMode.set('title', Lang.showLess);
					} else {
						prop.ocFull.morph({height: 0});
						prop.ocAbstract.morph({height: prop.ocAbstract.retrieve('height')});
						prop.ocMode.store('state', 'closed');
						prop.ocMode.set('text', Lang.more);
						prop.ocMode.set('title', Lang.showMore);
					}
				}.bind(this));
			}, this);
		}
	},
	
	gallery: function() {
		var thumbs = $$('#sw-thumbs a');
		var mainImage = $('main-image');

		if($chk(thumbs)) {
			thumbs.each(function(thumb) {		
				thumb.addEvent('click', function(e) {
					mainImage.get('morph', {duration: 500});
					mainImage.morph({opacity: 0});
					
					(function() {
						new Asset.image(this.get('href'), {
							id: 'current-image', 
							title: 'myImage', 
							onload: function() {
								this.replaces($('current-image'));
								mainImage.morph({opacity: 1});
							}
						});
						$('current-caption').set('text', this.get('title'))
					}).delay(500, this);
					
					thumbs.get('morph', {duration: 500});
					thumbs.morph({opacity: 0.5, backgroundColor: '#fff'});
					this.morph({opacity: 1.0, backgroundColor: ['#fff', '#EEE']});
					e.stop();
				});
			});
		}
	}
});