// General Javascript library

// Construct e-mail address as awkwardedly as possible!
function contactme(tag) {

var d1 = "&#109;ailto";
var d2 = "&#58;";
var d3 = "des";
var d4 = "&#64;frommars&#46;";
var d5 = "org";

document.write("<a href='" + d1 + d2 + d3 + d4 + d5 + "'>" + tag + "</a>");

}

// Toggle reviews visible/invisible
//   - takes parameters "show" or "hide"
function toggleVisibility(toggle) {
// working
//  if (document.getElementById('bs1').style.display != "none") {
//	document.getElementById('bs1').style.display = "none";
//	alert("display set to none");
//  }
//  else {
//	document.getElementById('bs1').style.display = "block";
//	alert("display set to block");
//  }


if (toggle == "hide") {
	setStyleByClass('div','review','display','none');
	setStyleById('hide', 'display', 'none');
	setStyleById('show', 'display', 'block');
  }
  else {
	setStyleByClass('div','review','display','block');
	setStyleById('hide', 'display', 'block');
	setStyleById('show', 'display', 'none');
  }

}

// *****************************************************************
// From Apple free JS sample
// *****************************************************************
// setStyleById: given an element id, style property and 
// value, apply the style.
// args:
//  i - element id
//  p - property
//  v - value
//
function setStyleById(i, p, v) {
	var n = document.getElementById(i);
	n.style[p] = v;
}

// setStyleByClass: given an element type and a class selector,
// style property and value, apply the style.
// args:
//  t - type of tag to check for (e.g., SPAN)
//  c - class name
//  p - CSS property
//  v - value
//
// NB: setting tag to '*' breaks IE 6, to be investigated further
//
var ie = (document.all) ? true : false;

function setStyleByClass(t,c,p,v){
	var elements;
	if(t == '*') {
		// '*' not supported by IE/Win 5.5 and below
		elements = (ie) ? document.all : document.getElementsByTagName('*');
	} else {
		elements = document.getElementsByTagName(t);
	}
	for(var i = 0; i < elements.length; i++){
		var node = elements.item(i);
		for(var j = 0; j < node.attributes.length; j++) {
			if(node.attributes.item(j).nodeName == 'class') {
				if(node.attributes.item(j).nodeValue == c) {
					eval('node.style.' + p + " = '" +v + "'");
				}
			}
		}
	}
}


// setStyleByTag: given an element type, style property and 
// value, and whether the property should override inline styles or
// just global stylesheet preferences, apply the style.
// args:
//  e - element type or id
//  p - property
//  v - value
//  g - boolean 0: modify global only; 1: modify all elements in document

function setStyleByTag(e, p, v, g) {
	if(g) {
		var elements = document.getElementsByTagName(e);
		for(var i = 0; i < elements.length; i++) {
			elements.item(i).style[p] = v;
		}
	} else {
		var sheets = document.styleSheets;
		if(sheets.length > 0) {
			for(var i = 0; i < sheets.length; i++) {
				var rules = sheets[i].cssRules;
				if(rules.length > 0) {
					for(var j = 0; j < rules.length; j++) {
						var s = rules[j].style;
						// selectorText broken in NS 6/Mozilla: see
						// http://bugzilla.mozilla.org/show_bug.cgi?id=51944
						ugly_selectorText_workaround();
						if(allStyleRules) {
							if(allStyleRules[j] == e) {
								s[p] = v;
							}			
						} else {
							// use the native selectorText and style stuff
							if(((s[p] != "") && (s[p] != null)) &&
							   (rules[j].selectorText == e)) {
								s[p] = v;
							}
						}

					}
				}
			}
		}
	}
}

// from http://newlinks.blogspot.com/2004/06/adding-to-blogger.html
function WikiOnThisDay(dateToMangle) {
  var strMonthNames=new Array(
   "January","February","March","April","May","June",
   "July","August","September","October","November","December"
   );
  var DTM = new Date(dateToMangle);
  
  var nMonth = DTM.getMonth(); // 0 - 11
  var nDate = DTM.getDate(); //1-31
  var strMonthNameProper = strMonthNames[nMonth];
  var strMonthNameLower = strMonthNameProper.toLowerCase();

 var BBC =
  "<a href=\"http://news.bbc.co.uk/onthisday/hi/dates/stories/" +
  strMonthNameLower + "/" +
  nDate + "/default.stm\">BBC</a>";
 var Wikipedia =
  "<a href=\"http://en.wikipedia.org/wiki/" +
  strMonthNameProper + "_" +
  nDate + "\">Wikipedia</a>";
 var HistoryChannel =
   "<a href=\"http://www.historychannel.com/tdih/tdih.jsp?month=" +
   (10272953+nMonth) +
   "&day=" +
   (10272965+nDate) +
   "&cat=10272946" +
  "\">History Channel</a>";
 var IMDB = 
  "<a href=\"http://www.imdb.com/OnThisDay?day=" +
  nDate + "&month=" +
  strMonthNameProper +
  "\">IMDB</a>";
 var HowTo =
  "<a href=\"http://newlinks.blogspot.com/2004/06/" +
  "adding-to-blogger.html\">How To</a>";
 // return BBC + " " + Wikipedia + " " + 
 //  HistoryChannel + " " + IMDB;
 return BBC + " | " + Wikipedia + " | " + IMDB;
}

// function to switch CSS class
function change(id, newClass) {
  myid = document.getElementById(id);
  myid.className=newClass;
}

// function to check for high resolution screen and change CSS classes
//   appropriately
function check_resolution() {
  if ((self.screen.width >= 1024) && (self.screen.height >= 768))
  {
    change('content', 'contentwide');
    change('main', 'mainwide');
    change('main2', 'main2wide');
    change('header', 'headerwide');
    change('header2', 'header2wide');
    change('footer', 'footerwide');
  }
}
