﻿//=============================================================================
// Primary javascript file for the web site wwww.FraserSquadron.ca
//
// © Wild Hare Productions 2008-2009. All rights reserved.
//
// Portions of this file are copyright their respective owners.
// Copyright notice for these sections is given as appropriate.
//=============================================================================


//=============================================================================
//=============================================================================
function addLoadEvent(func) {
  if (window.addEventListener)
    window.addEventListener("load",func,false);
  else if (document.addEventListener)
    document.addEventListener("load",func,false);
  else if (window.attachEvent)
    window.attachEvent("onload",func);
  else if (document.attachEvent)
    document.attachEvent("onload",func);
}


//=============================================================================
// This function masks e-mail addresses so spam-bots can't farm them.
// This code is based on something I saw somewhere and modified.
//=============================================================================
function makemail(name,domain,subj)
{ 
	document.location.href = "mail" + "to:" + name + "@" + domain + "?subject=" + subj; 
}


//=============================================================================
//	GATEKEEPER - from www.javascriptkit.com
//=============================================================================
var nifty_little_window = null;

function gateKeeper()
{
	nifty_little_window = window.open('../gatekeeper.html', 'theKeeper', 'width=350, height=200, resizable=1');
}


//=============================================================================
//
//=============================================================================
var Class=new Array('Intro', 'Boating', 'Seaman', 'AdvPilot', 'CelNav','GPS1','GPS2','ElChart','RADAR','Sounder', 'VHF1','VHF2','Distress', 'Weather1','Weather2','ExCruise','SeaSail','MarMain', 'MarElec', 'PCOC', 'Rmd','DSS','CoPS');
var Links=new Array('CPS', 'Gov', 'Tide', 'Safe', 'Shop', 'Misc');
//'Pubs'

function SwitchClass(id){	
	for (var i=0; i < Class.length; i++) {
		HideDiv(Class[i]);
	}		  
	ShowDiv(id);
}


function SwitchLinks(id){	
	for (var i=0; i < Links.length; i++) {
		HideDiv(Links[i]);
	}		  
	ShowDiv(id);
}


function HideDiv(id) {
	// safe function to hide an element with a specified id
	if (document.getElementById) {				// DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) {					// Netscape 4
			document.id.display = 'none';
		}
		else {									// IE 4
			document.all.id.style.display = 'none';
		}
	}
}

function ShowDiv(id) {
	// safe function to show an element with a specified id
	if (document.getElementById) {				// DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	}
	else {
		if (document.layers) {					// Netscape 4
			document.id.display = 'block';
		}
		else {									// IE 4
			document.all.id.style.display = 'block';
		}
	}
}


//=============================================================================
//=============================================================================
function PopupPic(sPicURL)
{ 
	window.open( "popup.htm?"+sPicURL, "", "resizable=1,HEIGHT=200,WIDTH=100"); 
}


//=============================================================================
//	http://icant.co.uk/sandbox/footercollapsetables/
//=============================================================================
function tablecollapse()
{
	/* Variables */
	var collapseClass='footcollapse';
	var collapsePic='arrow_up.gif';
	var expandPic='arrow_down.gif';
	var initialCollapse=true;

	// loop through all tables
	var t=document.getElementsByTagName('table');
	var checktest= new RegExp("(^|\\s)" + collapseClass + "(\\s|$)");
	for (var i=0;i<t.length;i++)
	{
		// if the table has not the right class, skip it
		if(!checktest.test(t[i].className)){continue;}		

		// make the footer clickable
		t[i].getElementsByTagName('tfoot')[0].onclick=function()
		{
			// loop through all bodies of this table and show or hide 
			// them
			var tb=this.parentNode.getElementsByTagName('tbody');
			for(var i=0;i<tb.length;i++)
			{
				tb[i].style.display=tb[i].style.display=='none'?'':'none';
			}			
			// change the image accordingly
			var li=this.getElementsByTagName('img')[0];
			li.src=li.src.indexOf(collapsePic)==-1?collapsePic:expandPic;	
		}
		// if the bodies should be collapsed initially, do so
		if(initialCollapse)
		{
			var tb=t[i].getElementsByTagName('tbody');
			for(var j=0;j<tb.length;j++)
			{
				tb[j].style.display='none';
			}			
		}
		// add the image surrounded by a dummy link to allow keyboard 
		// access to the last cell in the footer
		var newa=document.createElement('a');
		newa.href='#';
		newa.onclick=function(){return false;}
		var newimg=document.createElement('img');
		newimg.src=initialCollapse?expandPic:collapsePic;
		var tf=t[i].getElementsByTagName('tfoot')[0];
		var lt=tf.getElementsByTagName('td')[tf.getElementsByTagName('td').length-1];
		newa.appendChild(newimg);
		lt.insertBefore(newa,lt.firstChild);
	}		
}

// Run tablecollapse when the page loads
addLoadEvent(tablecollapse);

