/* ===========================================================================
 * SCHEDULE THE BEHAVIOURS
 * =========================================================================== 
 */

 
attachEventListener(window, "resize", resizePage, false);
attachEventListener(window, "load", initPage, false);
attachEventListener(window, "unload", unloadPage, false);

function initPage() 
{	
	initLinks();
	initTabs();
	tidyPage();
	if(typeof loadMap==='function')
		loadMap();
}


function resizePage()
{
	if(typeof loadMap==='function')
		loadMap();
}

function unloadPage() 
{
	if(typeof GUnload==='function')
		GUnload();
}


function tidyPage()
{
	var tabs = document.getElementById("tabs");
	if(tabs != null)
	{
		var tab1 = document.getElementById("tab1");
		var tab2 = document.getElementById("tab2");
		
		var height1 = tab1 ? tab1.offsetHeight : 0;
		var height2 = tab2 ? tab2.offsetHeight : 0;
	
		
		/* The "minus 10" accounts for the top padding on the tab divs */
		if(height1 > height2)
		{
			tab1.style.height=(height1-10)+'px';
			tab2.style.height=(height1-10)+'px';
		}
		else if(height2 > height1)
		{
			tab1.style.height=(height2-10)+'px';
			tab2.style.height=(height2-10)+'px';
		}	
	}
}


function initTabs()
{
	var tabs = document.getElementById("tabs");
	if(tabs != null)
	{
		var tab1link = document.getElementById("tab1link");
		var tab2link = document.getElementById("tab2link");
		
		if(tab1link != null && tab2link != null) 
		{
			tab1link.onclick = function() 
        	{
          		activateTab1(this);
          		return false;
        	};
        	
        	tab2link.onclick = function() 
        	{
          		activateTab2(this);
          		return false;
        	};
		}
	}	
}


function activateTab1(el)
{
	var tab2link = document.getElementById("tab2link");
	var tab1 = document.getElementById("tab1");
	var tab2 = document.getElementById("tab2");
	
	setClass(el.parentNode.parentNode,"current");
	removeClass(tab2link.parentNode.parentNode,"current");
	
	setClass(tab2,"hidden");
	removeClass(tab1,"hidden");
	
	return false;
}

function activateTab2(el)
{
	var tab1link = document.getElementById("tab1link");
	var tab1 = document.getElementById("tab1");
	var tab2 = document.getElementById("tab2");
	
	setClass(el.parentNode.parentNode,"current");
	removeClass(tab1link.parentNode.parentNode,"current");
	
	setClass(tab1,"hidden");
	removeClass(tab2,"hidden");
	
	return false;
}


function setClass(el, str)
{
	if(el)
	{
		if (el.className == "")
			el.className = str;
		else
			el.className += " "+str;	
	}	
	return true;
}

function removeClass(el, str)
{
	if(el)
	{
		el.className=el.className.replace(new RegExp(" "+str+"\\b"), "");
		el.className=el.className.replace(new RegExp(str+"\\b"), "");	
	}
}

function initLinks()
{
	if (!document.getElementsByTagName) 
 		return;
 		

 	if(document.body.className.match("new-window"))
 	{
	 	is_popup=true;
 		window.focus();
	}
 
 	var anchors = document.getElementsByTagName("a");
 	for (var i=0; i<anchors.length; i++) 
 	{
   		var anchor = anchors[i];
   		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
   		{
     		anchor.target = "_blank";
 		}	
     	else if (anchor.className.match("print")) 
     	{
        	anchor.onclick = function() 
        	{
          		printPage();
          		return false;
        	};
    	}
    	else if (anchor.className.match("new-window")) 
     	{
        	anchor.onclick = function() 
        	{
          		popUp(this.getAttribute("href"));
          		return false;
        	};
    	}
    	else if (anchor.className.match("file")) 
     	{
        	anchor.onclick = function() 
        	{
          		window.open(this.getAttribute("href"));
          		return false;
        	};
    	}
    	else if (anchor.className.match("close")) 
     	{
	     	anchor.style.display = 'inline';
	     	anchor.style.visibility = 'visible';
        	anchor.onclick = function() 
        	{
          		window.close();
          		return false;
        	};
    	}
 	}
}


function popUp(URL)
{
	eval("window.open('" + URL + "','windowName', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width=600,height=600');");
}

/* Author: Rebecca Skeers rebecca@webmistress.com.au 2005 */
function printPage() 
{
  	if (window.print)
		window.print()
	else
		alert("Sorry, your browser doesn't support the print feature. Use the File menu on your browser to select Print.");
	return false;
}


function attachEventListener(node, eventType, functionRef) 
{
    if (node.addEventListener) 
    {
        node.addEventListener(eventType, functionRef, false); 
    } 
    else if (node.attachEvent) 
    {       
        node.attachEvent("on" + eventType, functionRef); 
    } 
    else 
    {
        node["on" + eventType] = functionRef;
    } 
}



/* The JavaScript Anthology - James Edwards & Cameron Adams 
function attachEventListener(target, eventType, functionRef, capture)
{
	if (typeof target.addEventListener != "undefined")
	{ 
		target.addEventListener(eventType, functionRef, capture);
	}
	else if (typeof target.attachEvent != "undefined")
	{
		var functionString = eventType + functionRef;
		target["e" + functionString] = functionRef;
		target[functionString] = function(event)
		{
			if(typeof event == "undefined")
			{
				event = window.event
			};

			target["e" + functionString](event);
        };
		target.attachEvent("on" + eventType, target[functionString]);
	}
	else
	{
		eventType = "on" + eventType;

		if (typeof target[eventType] == "function")
		{
			var oldListener = target[eventType];
			target[eventType] = function()
			{
				oldListener();
				return functionRef();
			}
		}
		else
		{
			target[eventType] = functionRef;
		}
	}

	return true;
};
*/
