/**************************************************************
Functions relating to:
1. text area with custom scrollbar
2. add events
3. pop-up windows
***************************************************************/

var scroller  = null;
var scrollbar = null;
window.onload = function () {
  scroller  = new jsScroller(document.getElementById("Scroller-1"), 400, 300);
  scrollbar = new jsScrollbar (document.getElementById("Scrollbar-Container"), scroller, false);
}


function popupContact() 
{
 	newWindow = window.open ("popups/contact.html","Contact","menubar=1,resizable=0,width=300,height=500");
	newWindow.focus();
}
 

//#add event functionality e.g. can call multiple functions on page load
//#credit: http://www.accessify.com/features/tutorials/the-perfect-popup/
function addEvent(elm, evType, fn, useCapture)
{
	if(elm.addEventListener)
	{
		elm.addEventListener(evType, fn, useCapture);
		return true;
	}
	else if (elm.attachEvent)
	{
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	}
	else
	{
		elm['on' + evType] = fn;
	}
}

//#pop-up window functionality
//#credit: http://www.accessify.com/features/tutorials/the-perfect-popup/
//#edited by tidy.ie: centering of pop-ups added, icon functionality removed

//#give pop-up links a rel attribute of popup
//#add additional rel attributes to customise window if needed
//#if not given, default values will be used
//#rel attributes (order matters!):
//#1)type: standard, console or fullScreen. default is console (no toolbars).
//#2)width: e.g. 400. default is 380.
//#3)height: e.g. 200. default is 280.
var newWindow = null;

function closeWin(){
	if (newWindow != null){
		if(!newWindow.closed)
			newWindow.close();
	}
}

function popUpWin(url, type, strWidth, strHeight){
	
	//closeWin();
	
	//#060208 tidy: centre pop-ups
	//#minimum screen width/height assumed in case unable to detect true size
	var screenWidth = 500;
	var screenHeight = 300;
	//#get screen dimensions if possible
	if (screen.availWidth) 
	{
   		screenWidth = screen.availWidth;
   		screenHeight = screen.availHeight;
	}
	//#calculate top and left co-ordinates of window that will centre it
	var posLeft = (screenWidth - strWidth)/2;
	var posTop = (screenHeight - strHeight)/2;
	//#end tidy edits
	
	type = type.toLowerCase();
	if (type == "fullscreen"){
		strWidth = screen.availWidth;
		strHeight = screen.availHeight;
	}
	var tools="";
	//#060208 tidy: switched top and left positions from 0 to variables to facilitate centering of window
	if (type == "standard") tools = "resizable,toolbar=yes,location=yes,scrollbars=yes,menubar=yes,width="+strWidth+",height="+strHeight+",top="+posTop+",left="+posLeft;
	if (type == "console" || type == "fullscreen") tools = "toolbar=no,location=no,scrollbars=no,width="+strWidth+",height="+strHeight+",left="+posLeft+",top="+posTop;
	//newWindow = window.open(url, "newWin" + Math.floor(Math.random()*1000), tools);
	newWindow = window.open(url, "newWin", tools);
	newWindow.focus();
}

function doPopUp(e)
{
//set defaults - if nothing in rel attrib, these will be used
var t = "console";
var w = "473";
var h = "700";
//look for parameters
attribs = this.rel.split(" ");
if (attribs[1]!=null) {t = attribs[1];}
if (attribs[2]!=null) {w = attribs[2];}
if (attribs[3]!=null) {h = attribs[3];}

//call the popup script
popUpWin(this.href,t,w,h);
//cancel the default link action if pop-up activated
if (window.event) 
	{
	window.event.returnValue = false;
	window.event.cancelBubble = true;
	} 
else if (e) 
	{
	e.stopPropagation();
	e.preventDefault();
	}
}

function findPopUps()
{
var popups = document.getElementsByTagName("a");
for (i=0;i<popups.length;i++)
	{
	if (popups[i].rel.indexOf("popup")!=-1)
		{
		// attach popup behaviour
		popups[i].onclick = doPopUp;
		// add info to title attribute to alert fact that it's a pop-up window
		//popups[i].title = popups[i].title + " [Opens in pop-up window]";
		}
	}
}

addEvent(window, 'load', findPopUps, false);
