// Javascript for http://www.worshipradio.com/





/* -------------------------------------- */
/* Email functions                        */
/* -------------------------------------- */

/* Samples:  "email:com!domain$sample*jones?bob*junk" */
/* Samples:  "email:com)(domain#sample*jones$$$$$$bob*junk!and more#junk" */
/* --> mailto: bob.jones@sample.domain.com */
function unscrambleEmail(email) {
  var parts = email.split("*");
  var p1 = parts[0].split(/\W+/);
  var p2 = parts[1].split(/\W+/);

  return p2.reverse().join(".") + "@" + p1.reverse().join(".");
}



/* Unravel email addresses into proper "mailto:" tags */
/* Email addresses are in the HTML in a coded fashion to avoid spambots */
/* This should be called on startup */

function fixAllEmails() {
  var refs;
  if (document.getElementsByTagName)
    refs = [document.links, document.getElementsByTagName("area")];
  else
    refs = [document.links];

  for (var refidx in refs) {
    var collection = refs[refidx];
    for (var j=0;j<collection.length;j++) {
      var item = collection[j]
      var addr = item.getAttribute("href");
      if (/^email\:/i.test(addr)) {
        /* It's one of ours.  Decode it */

        var emailaddr = unscrambleEmail(unescape(addr.substr(6)));
        item.href = "mailto:" + emailaddr;
        /* If text inside anchor tags is empty, set it to the email address */
        if (item.innerHTML=="") item.innerHTML = emailaddr
      }
    }
  }
}




// Set all external anchors or anchors with "rel=external" or "rel=newpage" to open in a new window
// Improved version based on code at these sites:
//    See Kevin Yank's article at http://www.sitepoint.com/article/1041/1
//    http://www.sitepointforums.com/showpost.php?postid=819586&postcount=19
function externalLinks() {
  if (!document.links) return;
  //var anchors = document.getElementsByTagName("a");
  var anchors = document.links;
  for (var i=0; i<anchors.length; i++) {
    var anchor = anchors[i];
    var href   = anchor.getAttribute("href");
    if (href) {
      /* Check for hrefs starting with "http:" or "https:" outside our domain */
      if (/^https?\:/i.test(href) && !/worshipradio\.(com|org|net|tv)/i.test(href) &&
          !/freefind.com/i.test(href))
        anchor.target = "_blank";

      var relval = anchor.getAttribute("rel");
      if (relval == "external" || relval == "newpage")
        anchor.target = "_blank";
      else if (relval == "noframe")
        anchor.target = "_top";
      else if (relval == "internal")
        anchor.target = "";
    }
  }
}




/* -------------------------------------- */
/* Table stripe functions                 */
/* -------------------------------------- */

// See http://alistapart.com/articles/zebratables/


// this function is needed to work around 
// a bug in IE related to element attributes
function hasClass(obj) {
	var result = false;
	if (obj.getAttributeNode("class") != null)
		result = obj.getAttributeNode("class").value;
	return result;
}   


function stripeTable(table) {
	// the flag we'll use to keep track of 
	// whether the current row is odd or even
	var even = false;
	
	// if arguments are provided to specify the colours
	// of the even & odd rows, then use the them;
	// otherwise use the following defaults:
	var evenColor = arguments[1] ? arguments[1] : "#fff";
	var oddColor  = arguments[2] ? arguments[2] : "#eee";
	
	// by definition, tables can have more than one tbody
	// element, so we'll have to get the list of child
	// <tbody>s 
	var tbodies = table.getElementsByTagName("tbody");
	
	// and iterate through them...
	for (var h = 0; h < tbodies.length; h++) {

		// find all the <tr> elements... 
		var trs = tbodies[h].getElementsByTagName("tr");
		
		// ... and iterate through them
		for (var i = 0; i < trs.length; i++) {
			
			// avoid rows that have a class attribute
			// or backgroundColor style
			if (/*!hasClass(trs[i]) &&*/ !trs[i].style.backgroundColor) {
				
				// get all the cells in this row...
				var tds = trs[i].getElementsByTagName("td");
				
				// and iterate through them...
				for (var j = 0; j < tds.length; j++) {
					var mytd = tds[j];
					
					// avoid cells that have a class attribute
					// or backgroundColor style
					if (/*!hasClass(mytd) &&*/ !mytd.style.backgroundColor)
						mytd.style.backgroundColor = even ? evenColor : oddColor;
				}
			}
			
			// flip from odd to even, or vice-versa
			even =  !even;
		}
	}
}


function stripeTableById(id, evenColor, oddColor) {
	// obtain a reference to the desired table
	// if no such table exists, abort
	var table = document.getElementById(id);
	if (!table) return;
	
	stripeTable(table, evenColor, oddColor);
}


function stripeTableInDiv(divClass, evenColor, oddColor) {
	// Find all tables
	var divs = document.getElementsByTagName("div");
	
	// and iterate through them...
	for (var d = 0; d < divs.length; d++) {
		var c = hasClass(divs[d]);
		if (c == divClass) {
			var tables = divs[d].getElementsByTagName("table");
	
			// and iterate through them...
			for (var t = 0; t < tables.length; t++) {
				stripeTable(tables[t], evenColor, oddColor);
			}
		}
	}
}

 
function stripeTableAll(evenColor, oddColor) {
	// Find all tables
	var tables = document.getElementsByTagName("table");
	
	// and iterate through them...
	for (var t = 0; t < tables.length; t++) {
		stripeTable(tables[t], evenColor, oddColor);
	}
}





/* -------------------------------------- */
/* Initialize/uninitialize helpers        */
/* -------------------------------------- */

// From http://www.stewartspeak.com/dtr/demo/replacement.js
// See  http://www.alistapart.com/articles/dynatext/
function addLoadHandler(handler) {
  if (window.addEventListener)
    window.addEventListener("load", handler, false);
  else if (window.attachEvent)
    window.attachEvent("onload", handler);
  else if (window.onload) {
    var oldHandler = window.onload;
    window.onload = function piggyback() {
      oldHandler();
      handler();
    }
  }
  else window.onload = handler;
}





/* --------------------------------- */
/* Directory-related functions       */
/* --------------------------------- */
/*
function openLetter(setname,ind) { /////////////////////////////////////
  for (i=1; i <= 26; i++) {
    s = document.getElementById(setname + i);
    if (s) s.style.display = 'none';
    }
   s = document.getElementById(ind);
   s.style.display = 'block';
} // openLetter

function openAll(setname) { ///////////////////////////////////////////
  for (i=1; i <= 26; i++) {
    s = document.getElementById(setname + i);
    if (s) s.style.display = 'block';
    }
} // openAll

function closeAll(setname) { /////////////////////////////////////////
  for (i=1; i <= 26; i++) {
    s = document.getElementById(setname + i);
    if (s) s.style.display = 'none';
    }
} // closeAll
*/



/* -------------------------------------- */
/* Initialize/uninitialize                */
/* -------------------------------------- */


// Add initializer functions in this file
//addLoadHandler(externalLinks);  // Force all external links to open in a new window
addLoadHandler(fixAllEmails);   // Unscramble scrambled email addresses for spam protection
