function js_domLoad () {
	$('container').set ('destino', false);
	$('fadeWrap').set ({
		tween: {
			duration: 500,
			onComplete: function () { if ($('container').destino) window.location = $('container').destino; }
		}
	});
	$('container').setStyle ('display', 'block');
	$('container').ocultar = function (destino) { this.destino = destino; $('fadeWrap').fade ('in'); };
	$$('a[href!=#]').forEach (function (oa) {
		if (!oa.getAttribute ('href').contains ('mailto:') && !oa.getParent ('#obras')) {
			oa.addEvent ('click', function (eve) { $('container').ocultar (this.href); return false; });
		}
	});

	Element.implement ({
		highlight: function(start, end){
			var tween = this.get('tween');
			tween.start('background-color', start || '#ffff88', end).chain(function(){ tween.callChain(); }.bind(this));
			return this;
		}
	});
	
	js_menu ();
	js_contenido ();
	js_seccion ();
	if ($('mapa')) js_mapa ();
	$('fadeWrap').fade ('out');
}

function js_allLoad () {
	js_menuHeight ();
	$$('.pCol img').forEach (function (cImg) {
		if (cImg.scrollWidth < cImg.getParent ().scrollWidth) {
			wDif = (cImg.getParent ().scrollWidth - cImg.scrollWidth) / 2;
			cImg.setStyle ('marginLeft', wDif);
		}
	});
	$('fadeWrap').fade ('out');
}

function js_menu () {
	oImg = new Element ('img', { 'src': 'img/menu-1.gif', 'styles': { 'position': 'absolute', 'top': 0, 'left': 0 } });
	nMLis = $$('#menu>li').length;
	if (nMLis < 7) $('menu').getFirst ().setStyles ({display: 'inline', marginLeft: (137*(7-nMLis)/2)});
	$$('#menu>li').forEach (function (oLi) {
		$(oImg).clone ().inject (oLi, 'top');
		oLi.getFirst ().fade ('hide');
		oLi.getFirst ().set ('tween', {'link': 'chain'});
		oLi.set ('events', {
			'mouseenter': function () { this.getFirst ().fade ('in'); this.getFirst ().getNext ().setStyle ('color', '#ffffff'); },
			'mouseleave': function () { this.getFirst ().fade ('out'); this.getFirst ().getNext ().setStyle ('color', '#000000'); }
		});
	});
	$$('#menu ul').forEach (function (oUl) {
		oUl.set ('tween', {'link': 'cancel'});
		oUl.getParent ().set ('events', {
			'mouseenter': function () { this.getLast ().setStyle ('display', 'block'); this.getLast ().tween ('height', [this.getLast ().getStyle ('height'), this.getLast ().scrollHeight]);},
			'mouseleave': function () { this.getLast ().tween ('height', [this.getLast ().scrollHeight, 0]); }
		});
	});
	$$('#menu ul a').set ({
		'events': {
			'mouseenter': function () { this.tween ('backgroundColor', '#971B06', '#000000'); },
			'mouseleave': function () { this.tween ('backgroundColor', '#000000', '#971B06'); }
		}
	});
	$$('#submenu li').set ({
		'events': {
			'mouseenter': function () { this.highlight ('#EBE8E8', '#CDCDCD'); this.getFirst ().setStyle ('color', '#000'); },
			'mouseleave': function () { this.highlight ('#CDCDCD', '#EBE8E8'); this.getFirst ().setStyle ('color', '#971B06'); }
		}
	});
}
function js_menuHeight () {
	var alto = 0;
	$('submenu').getElements ('li').forEach (function (l) {
		if (Browser.Engine.trident) alto += l.scrollHeight;
		else alto += l.clientHeight;
	});
	if ($('body').getSize ().y < alto) {
		if (Browser.Engine.trident4) $('body').setStyle ('height', alto);
		else $('body').setStyle ('min-height', alto);
	}
	else if (!Browser.Engine.trident) $('body').setStyle ('paddingBottom', 20);
	if (Browser.Engine.trident4 && $('submenu').getPosition ().x != 0) $('submenu').setStyle ('left', 0);
	
}
function js_seccion () {
	//bodyId = document.body.id.toLowerCase ();
	bodyId = $('submenu').get ('class').toLowerCase ();
	$$('#submenu li').forEach (function (oLi, n) { oLi.setStyle ('background', '#EBE8E8 url(img-menu/'+ bodyId +'-'+ (n+1) +'.jpg) no-repeat bottom'); });
}

function js_contenido () {
	maxH = 0;
	$$('.pCol img').forEach (function (im) { im.setStyle ('marginTop', 0); });
	$$('.pCol').forEach (function (col) {
		maxH = maxH >= col.scrollHeight ? maxH : col.scrollHeight;
	});
	$$('.pCol').forEach (function (col) {
		if (col.scrollHeight < maxH) {
			heightDif = maxH - col.scrollHeight;
			if (col == $$('.pCol').getLast () && heightDif > 0 && col.getLast ().get ('tag') == 'img') {
				col.getLast ().setStyle ('marginTop', heightDif/2 - col.getPrevious ().getLast ().getStyle ('margin-bottom').toInt ());
			}
			else if (heightDif > 0 && col.getLast ().get ('tag') == 'img') {
				col.getLast ().setStyle ('marginTop', heightDif);
			}
			//col.setStyle ('height', maxH);
		}
	});
}

function js_mapa () {
	var obj = new Swiff ('mapa.swf', { id: 'mapa', width: 650, height: 243 });
	$('mapa').adopt (obj);
}

window.addEvent ('domready', js_domLoad);
window.addEvent ('load', js_allLoad);