/* utils.js */

$(document).ready(function() {
	$("span.email").each(emailLinkReplace);
});

// source markup looks like this: <span class='email robmcdonald'>Rob&nbsp;McDonald</span>
function emailLinkReplace()
{
	var emailinx = {webmaster: 		'32767.w:sympatico.ca',
					stats:			'gkssa.stats:gmail.com',
					registrar: 		'msakell:kos.net',
					regsecweb:		'msakell:kos.net,dschneid:cogeco.ca,32767.w:sympatico.ca',
					secretary: 		'dschneid:cogeco.ca',
					president: 		'robmcdonald:cogeco.ca',
					joanie: 		'jsagriff:soccer.on.ca',
					krista: 		'kswack:kingston.net',
					robmcdonald:	'robmcdonald:cogeco.ca',
					mikeakai:		'mikeakai:gmail.com',
					abel:			'abel:kos.net,abel:sousareadymix.ca'
					};

	$(this).removeClass('email');

	var sKey = $(this).attr('class');
	if (sKey) {
		var sText = $(this).text();
		var sAddr = emailinx[sKey];
		if (sAddr != null) {
			sAddr = sAddr.replace(/:/g, "@");
			var sTitle = $(this).attr('title');
			if (sTitle == '') {			// provide a default title if there wasn't one
				sTitle = 'send an email to ' + sAddr;
			}
			
			var sSubject = $(this).attr('subject') || '';
			if (sSubject) {	// if undefined, this evaluates to false, as will the empty string
				sSubject = '?subject=' + sSubject.replace(/ /g, "%20");
			}
			$(this).html("<a class='ml' title='" + sTitle + "' href='mailto:" +  sAddr + sSubject + "'>" + sText + "</a>");
		}
	}
}

/********************************************************************
 sample use:
<script language="JavaScript" type="text/javascript">
<!--
	document.write("GKSSA Webmaster: ");
	writeMLink('addr', 1, "32767.w", "sympatico", "ca");
-->
</script>
********************************************************************/
function writeMLink()	
{
	//from noSpam() : from Chris Sansom at <http://macintouch.com/spam.html>
	
    var link = arguments[0]; // text for the link 
    		// ''      - just glues the pieces; does not form an A tag
    		// 'addr'  - email address in HREF used as displayed text for the A tag
    		// other   - first parameter used as displayed text
    var pre = arguments[1];  // number of segments before the @ 

    var myat = String.fromCharCode(64);   // @ 
    var mydot = String.fromCharCode(46);  // . 
    var addr = ''; 

    // starting from the third argument, 
    // read the segments and build the address: 
    var i; 
    for (i = 2; i < arguments.length; i++)
    { 
        if (addr)
        { 
            // check whether this element (any but the first) 
            // comes after the @ or a dot: 
            addr += (i == pre + 2) ? myat : mydot; 
        } 
        addr += arguments[i]; 
    } 

    var str = ''; 
    if (link)
    { 
        // you could break this down further so that 
        // the 'mailto' is less apparent: 
        str = '<a class="ml" href="mailto:' + addr + '">'; 

        // if the first argument was literally 'addr', the text for the link 
        // is the address itself, otherwise it's the text of the argument: 
        str += (link == 'addr') ? addr : link; 

        str += '<\/a>';  // close the tag 
    } 
    else
    { 
        // if the first argument is '' just print the address 
        // without making it a link at all: 
        str = addr; 
    } 

    document.write(str); 
}	// writeMLink()


function announcement(nIt, el)
{
	var $bg = $(el).css('backgroundColor');
	//alert('annnouncement\n' + nIt + '\n' + el + '\n' + $bg);
	$(el).animate({ backgroundColor: "#fc6" }, 2000)
		.animate({ backgroundColor: $bg }, 2000);
}

function throb1(nIt, el)
{
	var $bg = $('#content').css('backgroundColor');

	// unless this is explicitly assigned, or available directly through the DOM
	// Safari doesn't find it and jquery.color has undefined in its rgb() calculation
	$(el).css('backgroundColor', $bg)
		.animate({ backgroundColor: "#ff9" }, 1000 * (nIt + 1))
		.animate({ backgroundColor: $bg }, 5000 * (nIt + 1));
}


function isOlderBrowser()
{
	//  inherits var browser

	bIsOlder = false;
	
	// ugh!  look in userAgent or appName?
	// let's try appName (more dependable?)
	
	// TODO? what about other browsers that don't claim to be even Mozilla/4.x?
	var strAppName = navigator.appName.toLowerCase();
	
	if (browser.nn || browser.ie)
	{
		if (browser.major < 5)
		{
			if (strAppName.indexOf('netscape') == 0)
			{
				bIsOlder = true;
			}
			else
			{
				if (strAppName.indexOf('microsoft') == 0)
				{
					// don't be fooled by userAgent like:
					// 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2)'
					var ua = navigator.userAgent.toLowerCase();
					
					if (ua.indexOf('msie 4') != -1)
					{
						bIsOlderBrowser = true;
					}
				}
			}
		}
	}	
	return bIsOlder;
}


function toggleMatchesDisclosure(sDivId, sImgId)
{
	var elContainer = document.getElementById(sDivId);
	if (elContainer)
	{
		elContainer.style.display = (elContainer.style.display == 'none' ? 'block' : 'none');

		var elDisclosure = document.getElementById(sImgId);
		if (elDisclosure)
		{
			elDisclosure.src = (elContainer.style.display == 'none' ? 'images/disclosure_closed.gif' : 'images/disclosure_open.gif');
		}
		//else alert('eek!');
	}
	//else alert("oh, oh");
}



function getWindowScrollXY()
{
	var scrOfX = 0, scrOfY = 0;
	if (typeof(window.pageYOffset) == 'number')
	{
		//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	}
	else
	{
		if (document.body && (document.body.scrollLeft || document.body.scrollTop))
		{
			//DOM compliant
			scrOfY = document.body.scrollTop;
			scrOfX = document.body.scrollLeft;
		}
		else
		{
			if (document.documentElement &&
				(document.documentElement.scrollLeft || document.documentElement.scrollTop))
			{
				//IE6 standards compliant mode
				scrOfY = document.documentElement.scrollTop;
				scrOfX = document.documentElement.scrollLeft;
			}
		}
	}
	
	return [scrOfX, scrOfY];
}


function TrimString(sInString)
{
	// thanks to: http://www.pbdr.com/jscript/trimstr.htm
  sInString = sInString.replace(/^\s+/g, "");	// strip leading
  return sInString.replace(/\s+$/g, "");		// strip trailing
}
