window.addEvent('domready', function(){ 
	
	/* Page Links */
	$$('a[rel=external]').each(function(element) {
			element.addEvent('click', function() { 
				window.open(this.href);
				return false;
			});
	});
	
	
	/* Activate tab JS on article pages */
	var tabBlock = $$("body.article .tab_block");
	if(tabBlock != null) {
		var tabLinks = $$(".tab_block .nav_tabs li");
		tabLinks.each(function(item) {
			item.getElement("a").removeProperty("href");
		});
		tabLinks.addEvents({
			'click': function(){
				var tabIndex = tabLinks.indexOf(this);

				tabBlock.removeClass("a1");
				tabBlock.removeClass("a2");
				tabBlock.removeClass("a3");
				tabBlock.removeClass("a4");
				tabBlock.removeClass("a5");

				tabBlock.addClass("a"+ (tabIndex+1));

			}
		});

	}
	/* */
	
	/* News & Views Accordion */
	if ($$("body.news")) {
		new Accordion($$("body.news .accordion_block .grozza"), $$("body.news .accordion_block .accordion_content"), {alwaysHide: false, show: 0, opacity: false});
	}
	/* */

	/* Image Gallery */
	var galleryBlock = $("gallery_block");
	if(galleryBlock != null) {
		
		var galleryLinks = $$("#gallery_block ul li");
		var imageArray = galleryBlock.getElements("img");
		
		// Previous link
		galleryLinks[0].getElement("a").removeProperty('href');
		galleryLinks[0].addEvent('click', function() {
			if (galleryBlock.hasClass("a1") == false) {
				for (var i=imageArray.length; i>0; i--) {
					if (galleryBlock.hasClass("a"+i) == true) {
						galleryBlock.removeClass("a"+i);
						galleryBlock.addClass("a"+(i-1));
						if (imageArray.length == i) {
							galleryLinks[1].setStyle("display", "block");
						}
						return true;
					}
				}
			}
		});
				
		// Next link
		galleryLinks[1].getElement("a").removeProperty('href');
		galleryLinks[1].addEvent('click', function() {
			if (galleryBlock.hasClass("a" + imageArray.length) == false) {
				for (var i=1; i<imageArray.length; i++) {
					if (galleryBlock.hasClass("a"+i) == true) {
						galleryBlock.removeClass("a"+i);
						galleryBlock.addClass("a"+(i+1));
						if (imageArray.length == i+1) {
							this.setStyle("display", "none");
						}
						return true;
					}
				}
			}
		});
		
		// Hide if only one image
		if (imageArray.length == 1) { 
			galleryLinks[1].setStyle("display", "none");
		}
		
	}
	
	
	// Project page navigation
	if($$("#nav_secondary li li") != null) {
		
		// Auto-select a category marked as 'active' in the HTML
		var categories = $$("#nav_secondary li li span");
		var selectedCategory = -1;
		categories.each(function(element, index) { if (element.hasClass('active') == true) { selectedCategory = index; return false; }});
		
		new Accordion($$("#nav_secondary li li span"), 
						 $$("#nav_secondary li li ul"), 
						 { 
						 	alwaysHide: true, 
							show: selectedCategory, opacity: false, 
							onActive: function(toggler){ toggler.addClass("active"); }, 
							onBackground: function(toggler){ toggler.removeClass("active"); 
						}});
	}
	
});