var d=document;
var state = new Array ("none", "block");
var imagewidth, imageheight, img, newImg, maxWidth, maxHeight;

function zoom(targetImage) {
	newImg = targetImage; 
	img=d.getElementById(targetImage);
	//img.src=newImg;
	imagewidth =img.getAttribute('width');
	imageheight =img.getAttribute('height');
	maxWidth = imagewidth * 2.5;
	maxHeight = imageheight * 3;
	if (img.parentNode.tagName=="TD"){
		//alert(img.parentNode.tagName);
		var parent = img.parentNode.parentNode;
		var cells = parent.getElementsByTagName("td");
		 for (i=0;i<cells.length;i++) {
			 var cellId =cells[i].childNodes[0].id;
			 //alert(cellId.tagName);
			 //if (cellId != targetImage) alert("You clicked on "+targetImage+" "+cellId+" is about to get shrunk");
		}
		//alert("there are "+parent.childNodes.length+" "+cells.length+" "+cells[0].tagName);
	}
	
	zoomX();
	zoomY();	
	//newImg.onmouseout = alert("victory");
}

function zoomX() {
	if (imagewidth >= maxWidth) { 
		 return; 
	}
	imagewidth = parseInt(imagewidth)+20;
	img.style.width=imagewidth+"px";
	the_timeout = setTimeout("zoomX();", 1);
}

function zoomY() {
	if (imageheight >= maxHeight) { return; }
	imageheight = parseInt(imageheight)+11;
	img.style.height=imageheight+"px";
	the_timeout = setTimeout("zoomY();", 1);
}


/***********   find the element clicked on *********/
/******  the document.onclick event registers any click on the document 
and checks to find the id (if any) of the element clicked on

if the element has the class name "zoomable" AND is an image then it initiates the zoom function.

***/


var e = window.event;
var shrinkThis, imageSmall = new Array();
var imageStatus = new Object;
document.onclick= getId;
window.onload = init;

function init() {
  var x = d.images;
       for (var i=0;i<x.length;i++) {
          var theName = x[i].id;
          imageStatus[theName] = 'normal';				
  		 }	
}

function getId(e){
	var targ;
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	 if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;
	var elementId = targ.id; //alert(targ.className);
	if (targ.parentNode.className=="zoomable" && targ.tagName =="IMG") {
		 
		     
		 if(imageStatus[elementId]=='normal'){
	 	   imageStatus[elementId]='expanded';
  		 moveImage(elementId);
  		 zoom(elementId);		 
    	 shrinkThis = targ;
    	 //targ.setAttribute('onMouseOut','shrink(shrinkThis)');
		 }
		 else if(imageStatus[elementId]=='expanded'){
		   imageStatus[elementId]='normal';
     	 shrinkThis = targ;
			 shrink(shrinkThis); 
		 }
		 return false;
	}
    else if (elementId=="sideTop"){switcher()}
	else {return} ;
	
	return elementId;
}

function shrink(shrinkThis){
 
	shrinkX();
	shrinkY();
	moveBack(shrinkThis);
	//shrinkThis.style.height=130+"px";
	//shrinkThis.style.width=217+"px";
}

function shrinkX() {
	if (imagewidth <= 270) { return; }
	imagewidth = parseInt(imagewidth)-20;
	img.style.width=imagewidth+"px";
	the_timeout = setTimeout("shrinkX();", 1);
}

function shrinkY() {
	if (imageheight <= 130) { return; }
	imageheight = parseInt(imageheight)-11;
	img.style.height=imageheight+"px";
	the_timeout = setTimeout("shrinkY();", 1);
}

//window.onload = stripe;




/*************   the add element to the page function ***************/

function psuedoWindow(author){
var author = author;
if (d.getElementById("psuedo"))  {closePsuedoWindow(); }
var quoteWindow = d.body.appendChild(d.createElement("div"));
quoteWindow.id = "psuedo";
quoteWindow.className = "control";
//alert("it works");

var cssLink = quoteWindow.appendChild(d.createElement("link"));
cssLink.href="positioning.css";
cssLink.rel="stylesheet";
cssLink.type="text/css";

var content = '<link type="text/css" href="positioning.css" rel="stylesheet">'; 
content +='<h2>'+author+'</h2>';
content += '<a href="#" onClick="closePsuedoWindow(event);return false;" class="edbuttons">close</a>';
var quote = d.getElementById(author);
content += quote.innerHTML;
//if (d.styleSheets[0].cssRules)content += '<a href="#" onClick="elequoteWindow(event);return false;" class="edbuttons">element</a><a href="#" onClick="rules(event);return false;" class="edbuttons">class</a>';
//else if (d.styleSheets[0].rules) content += '<a href="#" onClick="ieElequoteWindow(event);return false;" class="edbuttons">element</a><a href="#" onClick="quoteWindowIe(event);return false;" class="edbuttons">class</a>';
quoteWindow.innerHTML=content;
}

function closePsuedoWindow(){
d.body.removeChild(d.getElementById("psuedo"));
}


// Correctly handle PNG transparency in Win IE 5.5 or higher.
// http://homepage.ntlworld.com/bobosola. Updated 02-March-2004

function correctPNG() 
   {
   for(var i=0; i<document.images.length; i++)
      {
	  var img = document.images[i]
	  var imgName = img.src.toUpperCase()
	  if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
	     {
		 var imgID = (img.id) ? "id='" + img.id + "' " : ""
		 var imgClass = (img.className) ? "class='" + img.className + "' " : ""
		 var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
		 var imgStyle = "display:inline-block;" + img.style.cssText 
		 if (img.align == "left") imgStyle = "float:left;" + imgStyle
		 if (img.align == "right") imgStyle = "float:right;" + imgStyle
		 if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle		
		 var strNewHTML = "<span " + imgID + imgClass + imgTitle
		 + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
	     + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
		 + "(src=\'" + img.src + "\', sizingMethod='image');\"></span>" 
		 img.outerHTML = strNewHTML
		 i = i-1
	     }
      }
   }
window.attachEvent("onload", correctPNG);



//collapsable menus

function showMenu(subx)  {  
var submenu = d.getElementById(subx).style;
var check = d.getElementById(subx);
var holder = check.parentNode ;
var testVar ="";
//alert(holder.tagName+" "+holder.className+" "+holder.childNodes.length);
for (i=0; i<holder.childNodes.length; i++) {
		if (holder.childNodes[i] != check) {
			 if (holder.childNodes[i].id){
			 		testVar += holder.childNodes[i].id;
					holder.childNodes[i].className ="hideList";
					
			 }	 
		}
		
}
//alert(testVar);
check.className = "showList";

    /*if (check.className == "showList"){
         submenu.display = state[0];
         //i =0;
         check.className = "hideList";
    }
    else if (check.className == "hideList"){
			 submenu.display = state[1];
       //i =1;
       check.className = "showList";
    }   */
		return false;
}


/*******  and now... to fix the zoom for internet explorer **********/
function moveImage(elementId) {
		 //alert(elementId);
		 var img = d.getElementById(elementId);
		 //alert (img.name);
		 if (img.name != "enlarged") {
  		 var placeHolder = d.createElement('p');
  		 var placeHolderText = d.createTextNode('');
  		 placeHolder.appendChild(placeHolderText);
  		 d.getElementById(elementId).parentNode.appendChild(placeHolder);
  		 //placeHolder.height = 200+"px";
  		 placeHolder.className = "placeHolder";
  		 placeHolder.id = elementId+"placeHolder";
  		 img.style.position = "absolute";
  		 img.style.top = 0+"px";
  		 img.style.left = -75+"px";
			 img.name = "enlarged";
		 }
		 
}

function moveBack(elem) {
				 img.style.position = "relative";
  		   img.style.top = 0;
  		   img.style.left = 0;
				 img.name = "";
				 img.parentNode.removeChild(d.getElementById(elem.id+"placeHolder"))
				 //var placeHolder = d.getElementById(elem.id+"placeHolder");
}


/*    form validation  */

function checkEmail() {


         //alert("is this mic on??");
	/*this regex looks for a string where at least one letter is followed by an @
	which is followed by at least one letter which is followed by a . 
	*/			 
        var emailFilter=/^.+@.+\..{2,3}$/;
	
	var emailState ="", fnameState="", lnameState="",schoolState="";

	var fName = d.getElementById("fname");
	if (fName.value=="") {
	
		 d.getElementById("fnameLabel").className = "error";

	}
	else {
		d.getElementById("fnameLabel").className = "";
		fnameState = "ok";
	}

	var lName = d.getElementById("lname");
	if (lName.value=="") {
	
		 d.getElementById("lnameLabel").className = "error";
		 lnameState = "Most def NOT ok";

	}
	else {
		d.getElementById("lnameLabel").className = "";
		lnameState = "ok";
	}
	var emailAddy = d.getElementById("emailAddress").value;
	if (!emailFilter.test(emailAddy)) { 
		 //alert(emailAddy+" is invalid");
		 d.getElementById("emailLabel").className = "error";
	}
	else { 
		d.getElementById("emailLabel").className = "none";
		emailState = "ok";	
	}
	
	var school = d.getElementById("schoolName").value; 
	if (school == "") { 
		 
		 d.getElementById("schoolLabel").className = "error";
		
	}
	else { 
		d.getElementById("schoolLabel").className = "";	
		schoolState = "ok"
	}
	
	if (schoolState=="ok" && emailState=="ok" && fnameState=="ok" && lnameState=="ok") {
		//alert("everything is ok");
		return true;
	}
	else { 
		//alert("everything is not ok :( email "+emailState+" fname "+fnameState+" lname "+lnameState+" school "+schoolState);
		return false;
	}
}

document.ondblclick=switcher;
function switcher() {
	var active=getActiveStyleSheet();
  if (active=="bright") {
     setActiveStyleSheet('subdued');
  }
	if (active=="subdued") {
	   setActiveStyleSheet('bright');	
	}
}

