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.getAttribute ('href').contains ('.pdf')
			&& !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 ();
	if ($('obras')) js_obras ();
	$('fadeWrap').fade ('out');
}

function js_allLoad () { $('fadeWrap').fade ('out'); }

function js_menu () {
	oImg = new Element ('img', { 'src': 'img/menu-1.gif', 'styles': { 'position': 'absolute', 'top': 0, 'left': 0 } });
	if ($$('#menu>li').length < 7) $('menu').getFirst ().setStyles ({display: 'inline', marginLeft: 137});
	$$('#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', '#827928', '#dedcd2'); },
			'mouseleave': function () { this.tween ('backgroundColor', '#dedcd2', '#827928'); }
		}
	});
	$$('#submenu li').set ({
		'styles': { 'opacity': 0.7 },
		'events': {
			'mouseenter': function () { this.fade (1); },
			'mouseleave': function () { this.fade (0.7); }
		}
	});

	alto = $('submenu').getElement ('li').clientHeight * $$('#submenu li').length;
	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 ();
	$$('#submenu li').forEach (function (oLi, n) { oLi.setStyle ('background', '#cccccc url(img-menu/'+ bodyId +'-'+ (n+1) +'.jpg) no-repeat bottom'); });
}

function js_contenido () {
	maxH = 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);
		}
	});
	$$('.pCol img').forEach (function (cImg) {
		if (cImg.scrollWidth < cImg.getParent ().scrollWidth) {
			wDif = (cImg.getParent ().scrollWidth - cImg.scrollWidth) / 2;
			cImg.setStyle ('marginLeft', wDif);
		}
	});
}

function js_obras () {
	$('obraDetalle').setStyles ({ 'opacity': 0, 'left': (($$('body') [0].getSize ().x/2) - 403) });
	if (Browser.Engine.trident4) $('obraDetalle').setStyles ({ 'left': -70 });
	$('obraDetalle').oTween = new Fx.Tween ($('obraDetalle'), { 'link': 'chain' });
	$('obraDetalle').adopt (new Element ('div', { 'styles': { 'overflow': 'hidden', 'position': 'absolute', 'top': 0, 'left': 0, 'width': 800, 'height': 420 }}));
	$('obraDetalle').adopt (new Element ('a', {
		'text': 'x',
		'styles': { 'background': '#aaa', 'cursor': 'pointer', 'font-size': '20px', 'position': 'absolute', 'text-align': 'center', 'top': -5, 'right': 4, 'width': 20, 'height': 29 },
		'events': { 'click': function () {
			$('obraDetalle').oTween.start ('opacity', 1, 0).chain ( function () { $('obraDetalle').getLast ().setStyles ({ 'opacity': 0, 'visibility': 'hidden' }); });
		}}
	}));
	$('obraDetalle').adopt ($('obraDetalle').getFirst().clone());
	$('obraDetalle').getFirst ().setStyles ({ 'background': '#fff', 'border': '#ccc solid 1px' });
	$('obraDetalle').getLast ().setStyles ({ 'background': '#fff url(img/loader.gif) no-repeat center center', 'opacity': 0, 'width': 782, 'z-index': 100 });
	$('obraDetalle').getLast ().oTween = new Fx.Tween ($('obraDetalle').getLast (), { 'link': 'chain' });

	$('obraDetalle').limpiar = function () { $('obraDetalle').getFirst ().set ('text', ''); }
	$('obraDetalle').loader = function () {
		$('obraDetalle').getLast ().oTween.start ('opacity', 0, 1).chain (function () { $('obraDetalle').limpiar (); });
	}
	
	$('obras').hijoNum = $$('#obras li a').length;
	$('obras').hijoWidth = 130;
	$('obras').scrollPos = 0;
	$('obras').scrollDes = 4;
	if ($('obras').hijoNum > $('obras').scrollDes) {
		$('obras').setStyle ('float', 'left');
		$('obras').getElement ('ul').setStyle ('width', $('obras').hijoNum * $('obras').hijoWidth);
		$('obras').scroller = new Fx.Scroll ($('obras'));
		new Element ('a', {
			html: '<img src="img/fdrc.gif" width="10" height="80" alt="derecha" />',
			styles: { cursor: 'pointer', float: 'left', marginTop: 30, opacity: 0.5 },
			events: {
				click: function () {
					$('obras').scrollPos += $('obras').scrollDes;
					if ($('obras').scrollPos >= $('obras').hijoNum) {
						$('obras').scrollPos = $('obras').hijoNum - $('obras').scrollDes;
						$('obras').scroller.toRight ();
					}
					$('obras').scroller.start ($('obras').scrollPos * $('obras').hijoWidth);
				},
				mouseenter: function () { this.fade ('in'); },
				mouseleave: function () { this.fade (0.5); }
			}
		}).inject ($('obras'), 'after');
		new Element ('a', {
			html: '<img src="img/fizq.gif" width="10" height="80" alt="izquierda" />',
			styles: { cursor: 'pointer', float: 'left', marginTop: 30, paddingLeft: 103, opacity: 0.5 },
			events: {
				click: function () {
					$('obras').scrollPos -= $('obras').scrollDes;
					if ($('obras').scrollPos <= 0) {
						$('obras').scrollPos = 0;
						$('obras').scroller.toLeft ();
					}
					else $('obras').scroller.start ($('obras').scrollPos * $('obras').hijoWidth);
				},
				mouseenter: function () { this.fade ('in'); },
				mouseleave: function () { this.fade (0.5); }
			}
		}).inject ($('obras'), 'before');
	} else {
		$('obras').setStyle ('margin-left', ($('obras').scrollDes - $('obras').hijoNum) * $('obras').hijoWidth /2);
	}

	$$('#obras li a').set ({
		'styles': { 'opacity': 0.3, 'cursor': 'pointer' },
		'events': {
			'mouseenter': function () { this.morph ({opacity: 1, height: this.scrollHeight}); },
			'mouseleave': function () { this.morph ({opacity: 0.3, height: 122}); },
			'click': function (eve) {
				//eve.stopPropagation ();
				if ($('obraDetalle').getStyle ('visibility') == 'hidden' ) {
					$('obraDetalle').limpiar ();
					$('obraDetalle').getLast ().setStyles ({ 'opacity': 1, 'visibility': 'visible', 'z-index': 100 });
					$('obraDetalle').oTween.start ('opacity', 0, 1);
				}
				else $('obraDetalle').loader ();

				var obraRequest = new Request.HTML ({
						url: this.href +'&ajax=1',
						onSuccess: function (html) {
							$('obraDetalle').getFirst ().className = 'w'+ html [1].width;
							$('obraDetalle').getFirst ().adopt (html);
							$('obraDetalle').getLast ().oTween.start ('opacity', 1, 0).chain (function () {
								$('obraDetalle').getLast ().setStyles ({ 'opacity': 0, 'visibility': 'hidden' });
								$('obraDetalle').getFirst ().getLast ().setStyle ('width', (780 - $('obraDetalle').getFirst ().getChildren ('img').getWidth ()));
							});
						},
						onFailure: function () { $('obraDetalle').set ('text', 'Error'); }
					});
				obraRequest.send ();
				return false;
			}
		}
	});
}

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);