/* 
 * British Council - Venice Biennale
 *
 * @package		BCVB
 * @author		Oskar Krawczyk (o.krawczyk@keepthinking.it)
 * @version		1.2
 * @dependecies	MooTools 1.2+
 * @copyright	Copyright (c) 2009-2010, Oskar Krawczyk (Keepthinking Ltd.)		
 * @link		http://keepthinking.it
 * 
 ======================================================================= */

// future compat layer (for Mootools 2.0)
if(document.id === undefined) Document.implement({
	id: function(el, nocash) {
		return $(el, nocash);
	}
});

Window.implement({
	$log: function() {
		if(typeof console!=='undefined' && typeof console.log !== 'undefined') {
			console.log(arguments.length <= 1 ? arguments[0] : arguments);
		}
	}
});

Element.implement({
	tip: function(text, e) {
		this.timer = $clear(this.timer);
		this.set({
			'morph': 	{duration: 250},
			'html': 	text,
			'styles': 	{opacity: 0.9999, left: 0}
		});

		if(e.type.test(/out/)) {
			this.timer = function() {
				this.morph({opacity: 0, left: 30});
			}.delay(280, this);
		}
	},
	
	getNearestParent: function(a, b){
		var a = this.getParent(a), b = this.getParent(b);
		
		return (!a || !b) ? a || b : (a.hasChild(b) ? b : a);
	}
});

Array.implement({
	eachSync: function(d,c,e) {
		if(this.length==0) {
			return this;
		}
		
		var c=c || 10;
		var a=this.concat();
		var b=0;
		(function(){
			d.run([a.shift(),b,this],e);
			b+=1;
			if(a.length>0){
				setTimeout(arguments.callee.bind(this),c);
			}
		}).delay(c,this);
		return this;
	},
	
	recalculateWidth: function() {
		return (maxHeight.length * 158);
	},
	
	equalize: function(dir, mode, offset) {
		var mode 	= mode 	|| 'max';
		var dir 	= dir 	|| 'height';
		var axis 	= dir == 'height' ? 'y' : 'x';
		var offset 	= offset || 0;
		maxHeight 	= [];
		
		if(this.getElement('img') && offset > 0) this.getElement('img').setStyle('display', 'none');
		
		this.each(function(el) {
			maxHeight.push(el.getSize()[axis]);
		});
		
		this.setStyle(dir, Math[mode].apply(Math, maxHeight)+offset);
		
		if(this.getElement('img') && offset > 0) this.getElement('img').setStyle('display', 'block');
	}
});

// $extend(Selectors.Pseudo, {
// 	external: function(domain) {
// 		// if(!$chk($(this).get('href'))) return false;
// 		
// 		try{
// 			domain = $pick(domain, window.location.host.toString());
// 			return (!$(this).get('href').contains(domain) && !$(this).get('href').contains('#'));
// 		}catch(e){}
// 	}
// });
