﻿var nsie5 = (document.documentElement) ? true : false;
var ua = navigator.userAgent.toLowerCase();
var ie = (document.all && ua.indexOf("msie")>1) ? true : false; 
var mac = ( ie && (ua.indexOf("mac") > 1) ) ? true : false;
var opera = (ua.indexOf("opera") != -1);
var ieWin = (ie && !mac && !opera);
var moz = (ua.indexOf("gecko") != -1); //true for safari (navigator.product == "Gecko");
var start, end, timer, x=1, count=0, test=true;
var hidden = new Array();

var wwStyleMenu = {	combos : [
      		{ label: "Style:",
        	options: { "Default"      : "",
                    "Hidden"          : "hidden",
                    "'More' Link"     : "a.moreLink",
                    "Don't Print"     : "dontPrint",
                    "Double Spaced"   : "spacedOut",
                    "Fine Print"      : "fineprint",
                    "Data Table"      : "table.data",
                    "Sub-Header Cell"  : "td.subHeader",
                    "Fade In Heading" : "h1.top",
                    "Fade In Smaller" : "h1.topSmaller",
                    "Tagline Heading" : "h2.tagLine",
                    "Layout Table"    : "table.layout",
                    "Top Images Cell" : "td.imageRow",
                    "Content Cell"    : "td.content",
                    "Sidebar Cell"    : "td.sidebar"
                 },
        	updatecontextclass : true
        	} ]
    	};


window.onload = function() {
    if (!nsie5) return;
	if (location.href.indexOf("editor/editor.html")!=-1) { /*
  		document.getElementById("main").style.width = (ie) ? "797px" : "787px";  
    	document.getElementById("editable").style.width = (ie) ? "612px" : "602px";*/
		document.getElementById("nav").style.width = "160px"; 
		
		if (document.getElementById("footerRow")) {
		
			document.getElementById("footerRow").style.display = "none";
			document.getElementById("footerRow").style.height = "0";
			//document.getElementById("footer").style.margin = "0";
			//document.getElementById("footer").style.display = "none";
			//if (ie) document.getElementById("editable").height = "100%"; 
			//if (ie) document.getElementById("main").height = "100%"; 
			
		}
  	} else { 
    	//if (location.href.indexOf("directory")!=-1) {
       	findEmails(document.getElementById("editable"));
    	//}
    	if (moz || ieWin) {
        	headings = document.getElementsByTagName("H1");
        	for (var i=0; i<headings.length; i++) {
            	if (headings[i].className.indexOf("top")!=-1) {
                	heading = headings[i];
                	start = new Date().getTime();
                	timer = setInterval( function () { fadeIn(heading); }, 50);
                	break;
            	}
        	} //end for
    	}
    	//findEmails(document.getElementById("editable"));
		brFixTables();
    	fixLinks();
	} // end else 
} 

function fadeIn(heading) {
    if (test) {
        count++;
    } else {
        x += 4; 
    }
    if (ieWin) {
        heading.style.filter="alpha(opacity=" + x + ")";
    } else {
      heading.style.opacity = x/100;
    }
    if (x > 95 || count > 9) {
        clearInterval(timer);
        end = new Date().getTime();
        dur = end - start;
        if (test) {
            intv = Math.round(25000/dur);
            if (intv < 25) {
				if (ieWin) {
        			heading.style.filter="alpha(opacity=100)";
    			} else {
             heading.style.opacity = 1; 
    			}
			} else {
				test = false; count=0;
	            start = new Date().getTime();
    	        timer = setInterval( function () { fadeIn(heading); }, intv);
			}
        } //else {
            //document.title = dur + " milliseconds "+intv;
    }
}

function show(lnk) {
    var start = lnk.parentNode;
    var p, count = 0;
    var span = start.getElementsByTagName("SPAN");
    if (span.length) {
        if (span[0].className == "hidden") {
            span[0].className = "";
            hidden.push(span[0]);
            count = 1;
        }
    }
    p = start.nextSibling;
    while (p) {
        if (p.nodeType==1) {
			if (p.className == "hidden") {
            	p.className = "";
            	hidden.push(p);
				count++;
			} else {
				break;
			}
        }
		p = p.nextSibling;
    }
    if (count > 0) {
        lnk.className = "hidden";
        hidden.push(lnk);
		p = hidden[hidden.length-2];
        if (p.innerHTML.indexOf("dontPrint")==-1) {
            p.innerHTML += ' <a class="dontPrint" href="#" onclick="hide(this); return false">Hide text</a>';
        }
    }
}

function hide(lnk) {
    lnk.parentNode.removeChild(lnk);
    if (hidden.length) {
        for (var i in hidden) {
            hidden[i].className = (hidden[i].tagName=="A") ? "" : "hidden";
        }
       hidden = new Array(); //empty it
    }
}

function makeEmail(recipient,domain) {
	if (!domain) domain = location.host;
	if (domain.indexOf("www")==0) domain = domain.substring(4);
	return (recipient + "@" + domain);
}

function emailLink(recipient,domain) {
	addr = makeEmail(recipient,domain);
	return '<a href="mailto:' + addr + '">' + addr + '</a>';
}

function findEmails(elem) {
	for (var i = 0; i < document.links.length; i++) {
		href = document.links[i].href.toLowerCase();
		if (href.indexOf("ismail:")==0) {
			document.links[i].href = "mailto:" + convertEmail(href);
		}
	} 
	if (!elem) elem = document.body;
	do { //keep checking all html until we catch all instances
		html = elem.innerHTML;
		x = html.toLowerCase().indexOf("ismail:");
		if (x !=- 1) {
			endChunk = html.substring(x+7);
			startChunk = html.substring(0,x);
			addr = "";
           // fullAddress = endChunk.match(/^[A-Z0-9._-]+%[A-Z0-9._-]+\.[A-Z]{2,4}\b/);
			fullAddress = endChunk.match(/^[\w-_]+%[\w-_]+\.\w+\b/);//^\w+%\w+\.\w+\b/);
			if (fullAddress) {
				addr = fullAddress[0].split("%");
				addr = emailLink(addr[0],addr[1]);
				endChunk = endChunk.substring(fullAddress[0].length);					
			} else {
				recip = endChunk.match(/^[\w-_]+\b/);
				if (recip) {
					addr = emailLink(recip[0]);
					endChunk = endChunk.substring(recip[0].length);
				}
			}
			elem.innerHTML = startChunk + addr + endChunk;
		}
	} while (x != -1) 
}

function convertEmail(text) {
	addr = stripSpaces(text.substring(7));
	fullAddress = addr.match(/^[\w-_]+%[\w-_]+\.\w+\b/);
	//fullAddress = addr.match(/^\w+%\w+\.\w+\b/);
	if (fullAddress) {
		addr = fullAddress[0].split("%");
		return makeEmail(addr[0],addr[1]);
	} 
	if (addr.match(/^[\w-_]+$/)) {
		return makeEmail(addr);
	}
	return "";
}

function stripSpaces(str) { 
  //remove leading and trailing spaces
  return str.replace(/^\s*/, "").replace(/\s*$/, ""); 
}

function brFixTables() {
    tblArray = document.getElementsByTagName("TABLE"); 
    for (t=0; t<tblArray.length; t++) {
	elem = tblArray[t];
	if (elem.className=="data") {
            colourRows(elem);
	}
    }
}
function colourRows(tbl) { //give even rows of table a grey stripe
  for (i=1; (i<tbl.rows.length && tbl.rows[i].className.indexOf("noStripes")==-1); i++) {
    if (tbl.rows[i].className.indexOf("subHeader")==-1 && i%2 != 0) brAddClass(tbl.rows[i],"stripe");
  }
}


function brAddClass(elem,classStr) {
	cn = elem.className;
	if (cn.indexOf(classStr) == -1) { // only add if not found
		//ie mac cant deal with leading whitespace
		elem.className = (cn != "") ? (cn + " " + classStr) : classStr;
	} 
}

function fixLinks() {
  var domain = location.host;
  var j = document.links.length - 1;
  for (i=0; i<=j; i++)   {
    objLink = document.links[i];
    if (objLink.className == "moreLink") {
        objLink.onclick = function() {
            show(this); return false;
        };
        continue;
    }
    url = objLink.href.toLowerCase();
    if (url.indexOf(domain) == -1 && objLink.target == "" && url.indexOf("javascript") == -1 && url.indexOf("mailto") == -1) {
      objLink.target = "_blank";
      objLink.title = "Opens in new window";
    } else if (url.indexOf(".pdf") != -1) {
        objLink.title = "PDF - opens in new window";
        objLink.onclick = function() {
            var p = window.open(this.href,'','menubar,scrollbars,status,resizable');
            p.focus();
            return false;
        };
    } 
  }
}


