function MM_swapImgRestore() { //v3.0
	var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
	var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
	var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

Prototype.Browser.IE6 = Prototype.Browser.IE 
	&& parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 6;

var Session = Class.create(PeriodicalExecuter, {
	initialize: function($super, element) {
		this.element = $(element);
		this.options = Object.extend({
			validity: 3600,
			pollInterval: .5
		}, arguments[2] || { });
		this.internalTimer = this.options.validity;
		$super(this.update, this.options.pollInterval);
	},
	update: function() {
		if(this.internalTimer > 0) {
			this.internalTimer -= this.options.pollInterval;
			var length1 = Math.ceil(this.internalTimer / (this.options.validity / 100));
			var length2 = 100 - length1;
			this.element.update(
				"<div style=\"float:left;  height:2px; width:" + length1 + "%; background-color:#002c57;\"></div>" + 
				"<div style=\"float:right; height:2px; width:" + length2 + "%; background-color:none;\"></div>"
			);
		}
		else {
			this._stop();
		}
	},
	_stop: function() {
		this.element.update("<div style=\"height:2px; width:100%; background-color: #e2001a\"></div>");
		new Effect.Pulsate(this.element.down(), { // pulsate for one week
			pulses: 60 * 60 * 24 * 7 * 1.5,
			duration: 60 * 60 * 24 * 7
		});
		this.stop();
	}
});

Event.observe(window, 'load', function() {
	// Enable bluring for all links
	$$('a').each(function(element) {
		element.observe('click', function() {
			element.blur();
		});
	});

	// show current session validity
	if($('session')) {
		new Session('session');
	}

	// Adjust menu entry width
	var menuEntryWidth = Math.floor(($('menu').getWidth() / $('menu_top_level').childElements().size()) - 10);
	if(menuEntryWidth > 189) {
		menuEntryWidth = 189;
	}
	$$('div#menu > ul > li').each(function(element) {
		element.setStyle({
			width: menuEntryWidth + 'px'
		});
	});
	$$('div#menu > ul > li > div').each(function(element) {
		element.setStyle({
			width: (menuEntryWidth - 2) + 'px'
		});
	});
	$$('div.submenu').each(function(element) {
		element.setStyle({
			left: (menuEntryWidth - 12) + 'px'
		});
	});
	
	// Enable auto scaling for content
	if($('content')) {
		var scaleContent = function() {
			var vpWidth = document.viewport.getWidth();
			$('content').setStyle({width: (vpWidth >= 797 ? vpWidth-30 : 767) + 'px'});
		}
		scaleContent();
		Event.observe(document.resize ? document : window, 'resize', scaleContent);
	}
	
	// Fix css min-height for IE
	if($('contentpane').getHeight() < 500) {
		$('contentpane').setStyle({ height: '500px' });
	}
	
	// Fix css attribute selectors for IE 6 and set styles
	if(Prototype.Browser.IE6) {
		$$('#contentpane a[href], a.decorated').each(function(element) {
			element.setStyle({
				background: 'url(/images/hpa/link.jpg) no-repeat left center',
				paddingLeft: '12px'
			});
		});
		$$('#contentpane a[href=""], #contentpane a.showcalendar, #contentpane div.overview-column a[href], #contentpane td.tablehead a, #contentpane td.tab a').each(function(element) {
			element.setStyle({
				background: 'none',
				padding: '0'
			});
		});
		$$('input[type=checkbox], input[type=radio]').each(function(element) {
			element.setStyle({
				border: 'none'
			});
		});
	}
	
	// Add functionality to calendar links
	$$('a.showcalendar').each(function(element) {
		element.observe('click', function(event) {
			showCalendar(Event.pointerX(event)+15, Event.pointerY(event)-10);
		});
	});
});