/*
  general functions
*/
var aPopups = new Array();
var aModalPopups = new Array();

window.onfocus = function()
{
	if(aModalPopups.length != 0)
	{
		aModalPopups[0].focus();
	}
}


var sDefaultFeatures = 'channelmode=0, fullscreen=0, location=0, menubar=0, resizable=0, scrollbars=0, status=0, titlebar=0, toolbar=0';

// create popup
function createPopup(sURL, name, sFeatures, iWidth, iHeight){
	var iLeft	= (screen.availWidth - iWidth) / 2;
	var iTop	= (screen.availHeight - iHeight) / 2;
	
	return window.open(sURL, name, sFeatures + ', width=' + iWidth + ', height=' + iHeight + ', left=' + iLeft + ', top=' + iTop);
}


// getcookie
function getCookie(name){
	var aCookie = document.cookie.split("; ");
	
	for (var i=0; i < aCookie.length; i++){
		var aCrumb = aCookie[i].split("=");
		if (name == aCrumb[0]){
			return unescape(aCrumb[1]);
		}
	}
	
	return null;
} /*
  handleBodyLoad
*/
function handleBodyLoad(){
  markLinks();
  initPopupPainting();
  initShadowOverlay();
} /*
  thumbnails
*/
function initThumbnails(){} /*
   mark links
*/
function markLinks(){
	var links = document.getElementsByTagName('a');
	var link, href, mark;
	
	for(var i = 0; i < links.length; i++){
		link = links[i];
		
			
		switch(link.getAttribute('rel')){
			case 'ext': case 'external':
				link.className += ' external';
				link.target = '_blank';
				
				
				if(link.getElementsByTagName('img').length == 0){
					href = link.getAttribute('href').toLowerCase();
					href = href.substring((href.length - 4), (href.length));
					switch(href){
						case '.pdf':
							link.className += ' pdf';
							mark = document.createElement('img');
							mark.src = '/data/link-icons/pdf.png';
							link.insertBefore(mark , (link.firstChild));
							break;
						
						case '.xls':
							link.className += ' pdf';
							mark = document.createElement('img');
							mark.src = '/data/link-icons/xls.png';
							link.insertBefore(mark , (link.firstChild));
							break;
							
						case '.doc':
							link.className += ' pdf';
							mark = document.createElement('img');
							mark.src = '/data/link-icons/doc.png';
							link.insertBefore(mark , (link.firstChild));
							break;
							
						case '.ppt':
							link.className += ' pdf';
							mark = document.createElement('img');
							mark.src = '/data/link-icons/ppt.png';
							link.insertBefore(mark , (link.firstChild));
							break;
							
						default:
							mark = document.createElement('img');
							mark.src = '/data/link-icons/external.png';
							link.appendChild(mark);
							break;
				}
			}
		}
	}
	
	links = document.getElementsByTagName('a');
	
	for(var i = 0; i < links.length; i++){
		if(links[i].getAttribute('href')){
			href = document.createElement('span');
				href.className = 'print';
				href.appendChild((document.createTextNode(' [' + links[i].href + ']')));
			
			links[i].appendChild(href);
		}
	}
	
	mark = null;
	href = null;
	link = null;
	links = null;
} /*
  popupPainting
*/
function createPopupPainting(src){
	var popup;
	var popupHeader;
	var unescapedSRC;
	
	var titleText;
	
	// first replace until last /
	unescapedSRC = unescape(src);
	titleText = (unescapedSRC.substr(unescapedSRC.lastIndexOf('/')+1)).replace('.jpg','');
	
	if(document.getElementById('popup')){
		popup = document.getElementById('popup');
		popupHeaderText = document.getElementById('popupHeader').getElementsByTagName('div')[0];
		
		popupHeaderText.removeChild(popupHeaderText.firstChild);
		popupHeaderText.appendChild(document.createTextNode(titleText));
		
	} else {
		popup = document.createElement('div');
		
		popup.setAttribute('id', 'popup');
		
		var popupHeader = document.createElement('div');
		popupHeader.setAttribute('id', 'popupHeader');
		
		var popupCloseLink = document.createElement('a');
		popupCloseLink.appendChild(document.createTextNode('x'));
		popupCloseLink.href = '#';
		popupCloseLink.onclick = function(){ 
			switchShadowOverlay('off'); 
			document.getElementById('popup').style.display = 'none'; 
			return false; 
		}
		
		popupHeader.appendChild(popupCloseLink);
		popup.appendChild(popupHeader);
		
		var popupImageContainer = document.createElement('div');
		popupImageContainer.setAttribute('id', 'popupImageContainer');
		popup.appendChild(popupImageContainer);
		
		popupCaptionTitle = document.createElement('div');
		popupCaptionTitle.setAttribute('class', 'popupCaptionTitle');
		popupCaptionTitle.appendChild(document.createTextNode(titleText));
		
		popupHeader.appendChild(popupCaptionTitle);
		
		// append popup
		document.body.appendChild(popup);
		
	}
	
	document.getElementById('popupImageContainer').style.backgroundImage = "url('"+unescape(src)+ "')";
	popup.style.display = 'block'; 
	
}
	
function initPopupPainting(){
	var listItems = null;
	
	if(document.getElementById('di2130')) var listItems = document.getElementById('di2130').getElementsByTagName('li');
	if(document.getElementById('di2132')) var listItems = document.getElementById('di2132').getElementsByTagName('li');
	if(document.getElementById('di2133')) var listItems = document.getElementById('di2133').getElementsByTagName('li');
	if(document.getElementById('di2134')) var listItems = document.getElementById('di2134').getElementsByTagName('li');
	if(document.getElementById('di2135')) var listItems = document.getElementById('di2135').getElementsByTagName('li');
	
	if(!listItems) return;
	
	// for every list item, get anchor and specifiy the onclick event to be createpopup (a.src) 
	
	for(var i=0; i<listItems.length; i++) {
		listItems[i].getElementsByTagName('a')[0].onclick = function(){ 
			switchShadowOverlay('on');
			createPopupPainting(this.href); 
			return false;
		}
	}
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


function switchShadowOverlay(onoff) {
var objOverlay = document.getElementById('overlay');
	var arrayPageSize =getPageSize(); 
	switch(onoff) {
		case 'on':
			objOverlay.style.height = (arrayPageSize[1] + 'px');
			objOverlay.style.display = 'block';
			break;
		default:
			objOverlay.style.display = 'none';	
	}
}

function initShadowOverlay() {
var objBody = document.getElementsByTagName("body").item(0);
	
	// create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlay');
	objOverlay.onclick = function () {document.getElementById('popup').style.display = 'none';switchShadowOverlay('off'); return false;}
	objOverlay.style.display = 'none';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '90';
 	objOverlay.style.width = '100%';
	objBody.insertBefore(objOverlay, objBody.firstChild);
}


