window.addEvent('domready', function() {
	externalLinks();
	report_links();
	
	new ZoomBox();	
	
	if ($('gmap')) new gmap($('gmap'));
	if ($('navigation')) new navigation;
});

var gmap = new Class({
	initialize: function(obj) {
		this.obj = obj;
		this.loadApi();
	},
	loadApi: function(){
		google.load("maps", "2", {"callback" : this.loadMap.bind(this)});		
	},
	loadMap: function() {
		if (GBrowserIsCompatible()) {
			this.gmapBody = new Element('div', {"class":'gbody span-24 last'}).setStyle("height", '400px').injectInside(this.obj);
			this.gmapContainer = new Element('div', {"class":'span-22 prepend-1'}).injectInside(this.gmapBody);

			new Element('div', {"class":'gbottom span-24 last'}).injectInside(this.obj);

			this.gmap = new Element('div', {"class":'map span-22'}).setStyle("height", '400px').injectInside(this.gmapContainer);	
			
			
			this.gmap = new GMap2(this.gmap);
			this.gmap.setCenter(new GLatLng(51.0971079975735, 3.63973932723773), 13);
			var mapTypeControl = new GMapTypeControl();
			var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(10,10));
			var bottomRight = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(10,10));
			this.gmap.addControl(mapTypeControl, topRight);
			GEvent.addListener(this.gmap, "dblclick", function() {
				map.removeControl(mapTypeControl);
				map.addControl(new GMapTypeControl(), bottomRight);
			});
			this.gmap.addControl(new GSmallMapControl());
			
			var latlng = new GLatLng(51.0971079975735, 3.63973932723773);
			this.gmap.addOverlay(new GMarker(latlng));
			
		}
	}
});


var navigation = new Class({	
    initialize: function() {
		this.build_ui();
	},
	build_ui: function(){
		this.topnav = $('navigation');
		if (this.topnavSelected = this.topnav.getElement('.selected')) {
			//this.topnavSelected.removeClass('selected');

			this.topnav.getElement('ul').getChildren().each(function(item, idex) {
				item.addEvent('mouseenter', function(el) {
				
					if (this.tmpSelected) this.tmpSelected.removeClass('selected');
					this.tmpSelected = item;
				
					this.topnavSelected.removeClass('selected');
					item.addClass('selected');
					item.getChildren('li').addEvent('mouseleave', function(el){
						item.removeClass('selected');
					});
				}.bind(this));
			}.bind(this));
		
			this.topnav.addEvent('mouseleave', function(el){
				if (this.tmpSelected) this.tmpSelected.removeClass('selected');
				this.topnavSelected.addClass('selected');
			}.bind(this));
		}
	}
});

function externalLinks() {
	$$('a[rel=external]').each(function(item, index) {
		item.target = "_blank";
	});
}

function report_links() {
	$$('a').each(function(item, index) {
		if (
			item.getProperty("rel") == "pdf" ||
			item.getProperty("rel") == "doc" ||
			item.getProperty("rel") == "ppt" ||
			item.getProperty("rel") == "mov" ||
			item.getProperty("rel") == "file"
			) {
				item.addEvent('click', function() {
					url = item.getProperty("href");
					if ((a = url.indexOf("://") ) < 10) url = url.substr(a + 3);
					pageTracker._trackPageview("file/" + url);
				});
		}
		if (
			item.getProperty("rel") == "external"
			) {
				item.addEvent('click', function() {
					url = item.getProperty("href");
					if ((a = url.indexOf("://") ) < 10) url = url.substr(a + 3);
					pageTracker._trackPageview("external/" + url);
				});
		}
	});
}

var ZoomBox = new Class({
	initialize: function() {
		
		// setup SqueezeBox
		SqueezeBox.initialize({
			size: {x: 800, y: 400}
		});
		
		// setup FancyZoom
		insertZoomHTML();
		zoomdiv = document.getElementById(zoomID);  
		zoomimg = document.getElementById(theID);

		
		this.presets = {
			def: {
				width:800,
				height:400
			},
			panorama:{
				width:900,
				height:450
			},
			video:{
				width:800,
				height:400
			}
		};
		
		this.els = $$('a.zoom');	
					
		this.els.each(function(el){
			if (el.get('href').search(/(.*)\.(jpg|jpeg|gif|png|bmp|tif|tiff)/gi) != -1) {
				el.addEvents({
					'click':function(e) {
						e.stop();
						zoomClick(el, e);
						SqueezeBox.close();
					},
					'mouseover':function(e) {
						zoomPreload(el); 
					}
				});
			}
			else if (!Browser.Platform.ipod) { 
				el.addEvents({
					'click':function(e) {						
						e.stop();
						zoomOut();
						SqueezeBox.fromElement(this, {handler: 'iframe'});
					}
				});
			}
		}, this);
		
	}
});

