/*
Created By: Chris Campbell
Website: http://particletree.com
Date: 2/1/2006

Adapted By: Simon de Haan
Website: http://blog.eight.nl
Date: 21/2/2006

Inspired by the lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
And the lightbox gone wild by ParticleTree at http://particletree.com/features/lightbox-gone-wild/

*/

/*-------------------------------GLOBAL VARIABLES------------------------------------*/

var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;

/*-----------------------------------------------------------------------------------------------*/

//Browser detect script origionally created by Peter Paul Koch at http://www.quirksmode.org/

function getBrowserInfo() {
	if (checkIt('konqueror')) {
		browser = "Konqueror";
		OS = "Linux";
	}
	else if (checkIt('safari')) browser 	= "Safari"
	else if (checkIt('omniweb')) browser 	= "OmniWeb"
	else if (checkIt('opera')) browser 		= "Opera"
	else if (checkIt('webtv')) browser 		= "WebTV";
	else if (checkIt('icab')) browser 		= "iCab"
	else if (checkIt('msie')) browser 		= "Internet Explorer"
	else if (!checkIt('compatible')) {
		browser = "Netscape Navigator"
		version = detect.charAt(8);
	}
	else browser = "An unknown browser";

	if (!version) version = detect.charAt(place + thestring.length);

	if (!OS) {
		if (checkIt('linux')) OS 		= "Linux";
		else if (checkIt('x11')) OS 	= "Unix";
		else if (checkIt('mac')) OS 	= "Mac"
		else if (checkIt('win')) OS 	= "Windows"
		else OS 						= "an unknown operating system";
	}
}

function checkIt(string) {
	place = detect.indexOf(string) + 1;
	thestring = string;
//	alert(place);
	return place;
}

/*-----------------------------------------------------------------------------------------------*/

Event.observe(window, 'load', initialize, false);
Event.observe(window, 'load', getBrowserInfo, false);
Event.observe(window, 'unload', Event.unloadCache, false);

var lightbox = Class.create();

lightbox.prototype = {

	yPos : 0,
	xPos : 0,

	initialize: function(ctrl) {
		this.content = ctrl.rel;
		Event.observe(ctrl, 'click', this.activate.bindAsEventListener(this), false);
		ctrl.onclick = function(){return false;};
	},

	// Turn everything on - mainly the IE fixes
	activate: function(){
		if (browser == 'Internet Explorer'){
			this.getScroll();
			this.prepareIE('100%', 'hidden');
			this.setScroll(0,0);
			this.hideSelects('hidden');
		}
		this.displayLightbox("block");
	},

	// Ie requires height to 100% and overflow hidden or else you can scroll down past the lightbox
	prepareIE: function(height, overflow){
		bod = document.getElementsByTagName('body')[0];
		bod.style.height = height;
		bod.style.overflow = overflow;

		htm = document.getElementsByTagName('html')[0];
		htm.style.height = height;
		htm.style.overflow = overflow;
	},

	// In IE, select elements hover on top of the lightbox
	hideSelects: function(visibility){
		selects = document.getElementsByTagName('select');
		for(i = 0; i < selects.length; i++) {
			selects[i].style.visibility = visibility;
		}
	},

	// Taken from lightbox implementation found at http://www.huddletogether.com/projects/lightbox/
	getScroll: function(){
		if (self.pageYOffset) {
			this.yPos = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){
			this.yPos = document.documentElement.scrollTop;
		} else if (document.body) {
			this.yPos = document.body.scrollTop;
		}
	},

	setScroll: function(x, y){
		window.scrollTo(x, y);
	},

	displayLightbox: function(display){
		$('overlay').style.display = display;
		$(this.content).style.display = display;
		if(display != 'none') this.actions();
	},

	// Search through new links within the lightbox, and attach click event
	actions: function(){
		lbActions = document.getElementsByClassName('lbAction');

		for(i = 0; i < lbActions.length; i++) {
			Event.observe(lbActions[i], 'click', this[lbActions[i].rel].bindAsEventListener(this), false);
			lbActions[i].onclick = function(){return false;};
		}

	},

	// Example of creating your own functionality once lightbox is initiated
	deactivate: function(){
		if (browser == "Internet Explorer"){
			this.setScroll(0,this.yPos);
			this.prepareIE("auto", "auto");
			this.hideSelects("visible");
		}

		this.displayLightbox("none");
	}
}

/*-----------------------------------------------------------------------------------------------*/

// Onload, make all links that need to trigger a lightbox active
function initialize(){
	addLightboxMarkup();
	lbox = document.getElementsByClassName('lbOn');
	for(i = 0; i < lbox.length; i++) {
		valid = new lightbox(lbox[i]);
	}
}

// Add in markup necessary to make this work. Basically two divs:
// Overlay holds the shadow
// Lightbox is the centered square that the content is put into.
function addLightboxMarkup() {

	bod 				= document.getElementsByTagName('body')[0];

	overlay 			= document.createElement('div');
	overlay.id			= 'overlay';
	bod.appendChild(overlay);
}

/* Below added by Randy N. */

function validateLbEmailForm(formName) {
	var errormsg = "";
	var thisForm = document.forms[formName];
	var emailAddr = thisForm["sendersEmail"].value;
	var sendersFullName = thisForm["sendersName"].value;

 	if(thisForm["sendersName"].value.length < 5) errormsg += "Please fill in your name (at least 5 characters required).\n"
	if(thisForm["sendersEmail"].value.length < 8) errormsg += "Please fill in your email address (at least 8 characters required).\n"
	if(thisForm["subject"].value.length < 5) errormsg += "Please fill in the subject of your message (at least 5 characters required).\n"
	if(thisForm["messageBody"].value.length < 10) errormsg += "Please fill in the body (content) of your email (at least 10 characters required).\n"
	if(errormsg.length < 10) {
		return true;
	}
	else {
		alert(errormsg);
		return false;
	}
}

 	/*
 	The <span style="display: none;"> below was added as a workaround to solve
 	the problem of the <div class="leightbox" (which is inserted in web pages
 	where ever the email form link is added) causing a line break in the
 	displayed web page. This occurred despite the <div> being set to display:
 	none, because the display none pertains to the contents of the div, not the
 	div itself. I tried using a span instead, and that did not work (it broke
 	the lightbox display). I also considered using a DOM manipulation to simply
 	add the div at the appropriate place once the link is clicked that cauuses
 	the lightbox to display. On balance, it seemed easier to just wrap the div
 	in a span tag that was set by default to display: none. This has the effect
 	of hiding the div itself. I then added some code to the generateLightbox()
 	function to make the span visible.
 	--Randy N. 7/10/2008
 	 */

function printLightboxEmailLink( acctName, emailHash, phoneNum ) {
	var lightboxEmailLink = acctName + "&nbsp;<a class=\"lbOn\" title=\"Click to send a message to " + acctName + "\" rel=\"lightbox" + emailHash + "\"><img alt=\"Click to send a message to " + acctName + "\" src=\"http://www.ttu.edu/shared/shared_ttumain/images/icons/email.gif\" border=\"0\"  onClick=\"generateLightbox('" + acctName + "','" + emailHash + "','" + phoneNum + "')\"></a><span style=\"display: none;\" id=\"lbse" + emailHash + "\"><div class=\"leightbox\" id=\"lightbox" + emailHash + "\" style=\"display: none;\"></div></span>";
 	document.write(lightboxEmailLink);
}

function printLightboxNonEmailLink(id, fileName, linkText) {
	var lightboxNonEmailLink = "&nbsp;<a class=\"lbOn\" title=\"Click to view video\" rel=\"lightbox" + id + "\"><span onClick=\"generateNonEmailLightbox('" + id + "','" + fileName + "')\">" + linkText + "</span></a><span style=\"display: none;\" id=\"lbse" + id + "\"><div class=\"leightbox\" id=\"lightbox" + id + "\" style=\"display: none;\"></div></span>";

//	var lightboxNonEmailLink = "&nbsp;<a class=\"lbOn\" title=\"Click to view video\" rel=\"lightbox" + id + "\"><img alt=\"Click to view video\" src=\"/images/mediadotlaw.png\" border=\"0\"  onClick=\"generateNonEmailLightbox('" + id + "','" + fileName + "')\"></a><span style=\"display: none;\" id=\"lbse" + id + "\"><div class=\"leightbox\" id=\"lightbox" + id + "\" style=\"display: none;\"></div></span>";

 	document.write(lightboxNonEmailLink);
}

function generateLightbox( acctName, emailHash, phoneNum ) {
 	var lightboxEmailForm = "<FORM id=\"lbform" + emailHash + "\" name=\"lbform" + emailHash + "\" onsubmit=\"return validateLbEmailForm(this.name);\" action=\"/contact/email/sendEmail.aspx\" method=\"post\">";
	lightboxEmailForm += "<TABLE class=\"lbform\" width=\"100%\" border=\"0\">";
	lightboxEmailForm += "<TR vAlign=\"top\"><TD style=\"PADDING-BOTTOM: 5px;\">Email To:</TD><TD style=\"PADDING-BOTTOM: 5px;\">" + acctName + " &nbsp; &nbsp; " + phoneNum + "<INPUT type=\"hidden\" value=\"" + emailHash + "\" name=\"id\"></TD><TD rowSpan=\"4\"><A class=\"lbAction\" href=\"#\" rel=\"deactivate\">Close</A></TD></TR>";
	lightboxEmailForm += "<TR><TD>Your name</TD><TD><INPUT size=\"25\" name=\"sendersName\"></TD></TR>";
	lightboxEmailForm += "<TR><TD>Your Email Address</TD><TD><INPUT size=\"25\" name=\"sendersEmail\"></TD></TR>";
	lightboxEmailForm += "<TR><TD>Subject of Message</TD><TD><INPUT size=\"35\" name=\"subject\"></TD></TR>";
	lightboxEmailForm += "<TR><TD>Body of Message</TD><TD><TEXTAREA name=\"messageBody\" rows=\"8\" cols=\"40\"></TEXTAREA></TD><TD></TD></TR>";
	lightboxEmailForm += "</TABLE><INPUT type=\"submit\" value=\"Send\"></FORM></div><p>Phone numbers begin with <b>(806) 74</b>2-XXXX unless otherwise stated.</p>";
	var lbId = "lightbox" + emailHash;
	var lbSpanEnclosure = "lbse" + emailHash;
	var lightboxSpanEnclosure = document.getElementById(lbSpanEnclosure);
	lightboxSpanEnclosure.style.display = 'block';
    var lbEmailForm = document.getElementById(lbId);
    lbEmailForm.innerHTML = lightboxEmailForm;
}

function generateNonEmailLightbox(id,fileName) {
    var lightboxContent = '<A class=\"lbAction\" href=\"#\" rel=\"deactivate\">Close</A><br /><br />' +
        '<embed src="' + fileName + '" width="640" height="480" autostart="false" />';
	var lbId = "lightbox" + id;
	var lbSpanEnclosure = "lbse" + id;
	document.getElementById(lbSpanEnclosure).style.display = 'block';
    var lbNonEmailContentArea = document.getElementById(lbId);
    lbNonEmailContentArea.style.width='670px';
    lbNonEmailContentArea.style.height='520px';
    lbNonEmailContentArea.style.margin='0px auto';
    lbNonEmailContentArea.innerHTML = lightboxContent;
}

