function trace(s) {
  try { console.log(s) } catch (e) { alert(s) }
};


//From prototype.js
var PeriodicalExecuter = new Class({
	initialize: function(callback, frequency) {
		
		this.callback = callback;
		this.frequency = frequency;
		this.currentlyExecuting = false;
		
		this.registerCallback();
	},
	
	registerCallback: function() {
		this.stop();
		this.timer = setInterval(this.onTimerEvent.bind(this), this.frequency * 1000);
		return this;
	},
	
	execute: function() {
		this.callback(this);
		return this;
	},
	
	stop: function() {
		if (!this.timer) return this;
		clearInterval(this.timer);
		this.timer = null;
		return this;
	},
	
	onTimerEvent: function() {
		if (!this.currentlyExecuting) {
			try {
				this.currentlyExecuting = true;
				this.execute();
			} finally {
				this.currentlyExecuting = false;
			}
		}
	return this;
	}
});




// 
var RoolOverLink = new Class({
	
	initialize: function( id, noImg, roImg ) {
		this.id=id;
		this.noImg=noImg;
		this.roImg=roImg;
		this.image=$(id);
		this.link=$($(id).parentNode);
		this.setHandlers();
	},
	
	setHandlers: function() {
		/*
		Event.observe(this.link, 'mouseover', RoolOverLink.over.bind(this));
		Event.observe(this.link, 'mouseout',  RoolOverLink.out.bind(this));
		*/
		
		this.link.addEvent('mouseover',RoolOverLink.over.bind(this));
		this.link.addEvent('mouseout' ,RoolOverLink.out.bind(this));
		
	}
});
RoolOverLink.over = function() {
	this.image.src=this.roImg;
}
RoolOverLink.out = function() {
	this.image.src=this.noImg;
}

// 
var SubMenusManager = new Class({
	
	initialize: function( sMenu ) {
		this.linkDiv=$(sMenu.parentNode);
		this.submenuDiv=sMenu.getFirst();
		this.setHandlers();
	},
	
	setHandlers: function() {
		/*
		Event.observe(this.linkDiv, 'mouseover', SubMenusManager.over.bind(this));
		Event.observe(this.linkDiv, 'mouseout',  SubMenusManager.out.bind(this));
		*/
		
		this.linkDiv.addEvent('mouseover',SubMenusManager.over.bind(this));
		this.linkDiv.addEvent('mouseout' ,SubMenusManager.out.bind(this));
	}
});
SubMenusManager.over = function() {
	this.submenuDiv.setStyle('display','block');
}
SubMenusManager.out = function() {
	this.submenuDiv.setStyle('display','none');
}


// 
var MenuManager = new Class({
	
	initialize: function() {
		this.setupRoolOvers();
		this.setupSubmenus();
	},
	
	setupRoolOvers: function() {
		this.preload_array=[];
		this.links=[];
		
		
		var images=$$('img');
		var roImages = images.filter(function(s) {
			if (s.className == '') return false;
			return s.className.substring(0,6) == 'roIMG_';
		})
		roImages.each(function(img){
			var menusDir='typo3temp/menu/';
			var noImage=menusDir+img.className.split('_')[1];
			var roImage=menusDir+img.className.split('_')[2];
			this.preloadImage(roImage);
			var link = new RoolOverLink( img.id, noImage, roImage);
			this.links.push(link);
		}.bind(this));
	},
	
	setupSubmenus: function() {
		this.subMenus=[];
		var sMenus = $$('div.smenu');
		
		sMenus.each(function(sMenu){
				var subMenu = new SubMenusManager(sMenu);
				this.subMenus.push(subMenu);
			}, this);
	},
	
	preloadImage: function(rute) {
		var tmpImg=new Image();
		tmpImg.src=rute;
		this.preload_array.push(tmpImg);
	}
	
	
});





function manageComboBox() {
	if ( $('prem_years') == null ) return;
	
	$('prem_years').addEvent("change", function() {
		var val = $('prem_years').value;
		if (val!=0) document.location = $('prem_years').value;
	});
}


function openSendAFriendWin (url) {
	window.open(url,'tipAFriend','channelmode=0,directories=0,fullscreen=0,location=0,menubar=0,resizable=0,scrollbars=1,status=1,toolbar=0,width=590,height=700');
}



function openMedia( _href, _width, _height, _title, _author, _caption )
{
	console.log( arguments );
}


/*
function openMedia( _href, _width, _height, _title, _author, _caption )
{
	if ( myLightWindow ) 
	{
		myLightWindow.activateWindow({
			href:    _href, 
			width:   _width,
			height:  _height,
			title:   '', 
			author:  null, 
			caption: null
		});
	}
	else 
	{
		window.setTimeout( function () { openMedia( _href, _width, _height, _title, _author, _caption ) }, 1000);
	}
}
*/

/*
function setupLinks () {
	$$('a[rel="external"]').each(function(link){
		if(link.readAttribute('href') != '' && link.readAttribute('href') != '#'){
			link.writeAttribute('target','_blank');
		}
	});
}
*/



function openWindow(theURL,w,h) {
	var dim = 'width='+w+',height='+h;
	window.open(theURL,'',dim);
}

// MM_openBrWindow('gr/mapa16.htm','','width=640,height=460')





Slimbox.scanPage = function() {
	var links = $$("a").filter(function(el) { 
		return el.rel && el.rel.test(/^lightbox/i);
	});
	$$(links).slimbox({
		resizeDuration: 400, 
		resizeTransition: Fx.Transitions.Sine.easeInOut, 
		opacity: 0.8, 
		opacityDuration: 500, 
		loop: 1, 
		initialWidth: 250, 
		initialHeight: 250, 
		animateCaption: 1, 
		showCounter: 1, 
		enablePrintButton: 0, 
		enableSaveButton: 0, 
		closeKeys: [27, 88, 67],	// Esc (27), 'x' (88), 'c' (67)
		previousKeys:  [37, 80],	// Left arrow    (37), 'a' (65)
		nextKeys:      [39, 78],		// Right arrow   (39), 's' (83)
		counterText: 'Imágen {x} de {y}', 
		psScriptPath: 'typo3conf/ext/pmkslimbox/savefile.php'
	}, 
	null, 
	function(el) {
		return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
	});
};

PiezaCartograficas = {};
PiezaCartograficas.openWindow = function(theURL,w,h) {
	var dim = 'width='+w+',height='+h;
	window.open(theURL,'',dim);
};
PiezaCartograficas.scanPage = function() {
	var links = $$("a").filter(function(el) { 
		return el.rel && el.rel.test(/^cart_pieze/i);
	});
	var bar = '';
	if (Browser.Engine.name=='trident') bar = '/';
	links.each ( function(i) {
			var splitedPath = i.pathname.split('/');
			var file = splitedPath[splitedPath.length-1];
			var splitedFile = file.split('.');
			if ( splitedFile.length == 4 ) {
				i.addEvent("click", function() {
					PiezaCartograficas.openWindow(bar+i.pathname,splitedFile[1],splitedFile[2]);
					return false;
				});
			}
//			if (this.console && typeof console.log != "undefined") console.log('http://sge.comuniland.com/'+i.pathname);
//			if (this.console && typeof console.log != "undefined") console.log(document);
//			if (this.console && typeof console.log != "undefined") console.log(Browser.Engine.name);
		}
	);
	
};



// Mootools jump menu

window.addEvent('domready', function() {
	if ( $('jump-menu') ) {
		$('jump-menu').getElement('select').setStyle('display', 'block');
		$('jump-menu').getElement('select').addEvent('change', function() {
		//	alert('change');
			var jumpUrl = this.getElement(':selected').value;
			if(jumpUrl != '') {
				location.href = jumpUrl;
			}
		});
	}
}); 


window.addEvent("domready", Slimbox.scanPage);
window.addEvent("domready", PiezaCartograficas.scanPage);

var menu_mng=null;
window.addEvent("domready", function() {
	menu_mng=new MenuManager();
	// setupLinks();
	$$('a[rel=external]').set('target', '_blank');
	$$('area[rel=external]').set('target', '_blank');
	manageComboBox();
});



function tx_publicaciones_pi1_CheckFields () {
//	alert( $("tx_publicaciones_pi1-mode").value );
	switch ( $("tx_publicaciones_pi1-mode").value ) {
		case 'b':
//			alert('Biblioteca');
			$('tx_publicaciones_pi1-titulo').setStyle('display', 'table-row');
			$('tx_publicaciones_pi1-autor').setStyle('display', 'table-row');
			$('tx_publicaciones_pi1-texto').setStyle('display', 'table-row');
			$('tx_publicaciones_pi1-zona').setStyle('display', 'table-row');
			break;
		case 'c':
//			alert('Cartoteca');
			$('tx_publicaciones_pi1-titulo').setStyle('display', 'table-row');
			$('tx_publicaciones_pi1-autor').setStyle('display', 'none');
			$('tx_publicaciones_pi1-texto').setStyle('display', 'table-row');
			$('tx_publicaciones_pi1-zona').setStyle('display', 'table-row');
			break;
		case 'h':
//			alert('Hemeroteca');
			$('tx_publicaciones_pi1-titulo').setStyle('display', 'table-row');
			$('tx_publicaciones_pi1-autor').setStyle('display', 'none');
			$('tx_publicaciones_pi1-texto').setStyle('display', 'table-row');
			$('tx_publicaciones_pi1-zona').setStyle('display', 'none');
			break;
		case 'v':
//			alert('Videoteca');
			$('tx_publicaciones_pi1-titulo').setStyle('display', 'table-row');
			$('tx_publicaciones_pi1-autor').setStyle('display', 'none');
			$('tx_publicaciones_pi1-texto').setStyle('display', 'table-row');
			$('tx_publicaciones_pi1-zona').setStyle('display', 'table-row');
			break;
		default:
//			alert('Biblioteca');
			$('tx_publicaciones_pi1-titulo').setStyle('display', 'table-row');
			$('tx_publicaciones_pi1-autor').setStyle('display', 'table-row');
			$('tx_publicaciones_pi1-texto').setStyle('display', 'table-row');
			$('tx_publicaciones_pi1-zona').setStyle('display', 'table-row');
	}
}

window.addEvent("domready", function() {
	
	if ( $("tx_publicaciones_pi1-mode") ) {
		tx_publicaciones_pi1_CheckFields ();
		$("tx_publicaciones_pi1-mode").addEvent("change", function() {
			tx_publicaciones_pi1_CheckFields ();
		});
	}
	
	if ( $$("div.expe_left a img") ) {
	//	if ( console ) {
			$$("div.expe_left a img").each ( function (x) { 
				var lnk = x.parentNode.href;
				var from = lnk.length-3;
				var to = lnk.length;
				if ( lnk.substring(from,to)=='jpg' ) {
					x.parentNode.slimbox({/* Put custom options here */}, null, function(el) {
							return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
						});
				}
			} );
	//	}
	}
	
	
		
});









