/*
-----------------------------------------------
vermontcommons.org
Script: vdwUtil.js
Author: Ben Glassman
Organization: Vermont Design Works
Created: 25 Oct 2007
----------------------------------------------- */

vdwUtil = {
	init:function() {
		vdwUtil.mailtoFix('REMOVETHISBEFORESENDING');
		vdwUtil.preparePopups();
		vdwDOM.fixNodeTypes();
	},
	mailtoFix:function(stringToRemove) {
		var links = document.getElementsByTagName('a');
		var removeText = new RegExp(stringToRemove);
		for (var i = 0; i < links.length; i++) {
			if (links[i].href.indexOf('mailto:') != -1) {
				links[i].href = links[i].href.replace(removeText, '');
				links[i].firstChild.nodeValue = links[i].firstChild.nodeValue.replace(removeText, '');
				links[i].firstChild.nodeValue = links[i].firstChild.nodeValue.replace(/mailto:/, '');
			}
		}
	},
	popUp:function(winURL, name, parameters) {
		window.open(winURL, name, parameters);
	},
	preparePopups:function() {
		if (!document.getElementsByTagName) return false;
		var lnks = document.getElementsByTagName("a");
		for (var i=0; i<lnks.length; i++) {
			if (lnks[i].className == "popup") {
				lnks[i].title+= " (opens in a new window)";
				lnks[i].onclick = function() {
					vdwUtil.popUp(this.getAttribute("href"), "popup", "width=320,height=480");
					return false;
				}
			}
			else if (lnks[i].className == "external") {
				lnks[i].title+= " (opens in a new window)";
				lnks[i].onclick = function() {
					vdwUtil.popUp(this.getAttribute("href"), "external", "");
					return false;
				}
			}
			else if (lnks[i].href != null && lnks[i].href.indexOf('.pdf') != -1) {
				lnks[i].title += " (opens in a new window)";
				lnks[i].onclick = function() {
					vdwUtil.popUp(this.getAttribute("href"), "pdf", "");
					return false;
				}
			}
		}
	},
	trimString:function(str) {
		return str.replace(/^\s*\n*\r*|\s*\n*\r*$/g,'');
	},
	fadeUp:function(element, red, green, blue) {
		if (element.fade) {
			clearTimeout(element.fade);
		}
		element.style.backgroundColor = 'rgb('+red+','+green+','+blue+')';
		if (red == 255 && green == 255 && blue == 255) {
			return;
		}
		var newred = red + Math.ceil((255-red)/10);
		var newgreen = green + Math.ceil((255-green)/10);
		var newblue = blue + Math.ceil((255-blue)/10);
		var repeat = function() {
			vdwUtil.fadeUp(element, newred, newgreen, newblue);
		}
		element.fade = setTimeout(repeat, 100);
	},
	preloadImgs:function(imgsSrc) {
		var tmpArr = new Array();
		for (var i = 0; i < imgsSrc.length; i++) {
			tmpArr[i] = new Image();
			tmpArr[i].src = imgsSrc[i];
		}
	}
}

vdwDOM = {
	addEvent:function(elm, evType, fn, useCapture){
		if (elm.addEventListener){
			elm.addEventListener(evType, fn, useCapture);
			return true;
		} else if (elm.attachEvent) {
			var r = elm.attachEvent('on' + evType, fn);
			return r;
		} else {
			elm['on' + evType] = fn;
		}
	},
	getTarget:function(e){
		var target = window.event ? window.event.srcElement : e ? e.target : null;
		if (!target){return false;}
		while(target.nodeType!=1 && target.nodeName.toLowerCase()!='body'){
			target=target.parentNode;
		}
		return target;
	},
	cancelClick:function(e){
		if (window.event){
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		}
		if (e && e.stopPropagation && e.preventDefault){
			e.stopPropagation();
			e.preventDefault();
		}
	},
    safariClickFix:function(){
      return false;
    },
	addClass:function(element, value) {
		if (!element.className) {
			element.className = value;
		} else {
			newClassName = element.className;
			newClassName+= " ";
			newClassName+= value;
			element.className = newClassName;
		}
	},
	removeClass:function(element, value) {
		var rep = element.className.match(' '+value)?' '+value:value;
		element.className = element.className.replace(rep,'');
	},
	importNode:function(node, allChildren) {
		/* find the node type to import */
		switch (node.nodeType) {
			case document.ELEMENT_NODE:
				/* create a new element */
				var newNode = document.createElement(node.nodeName);
				/* does the node have any attributes to add? */
				if (node.attributes && node.attributes.length > 0)
					/* add all of the attributes */
					for (var i = 0, il = node.attributes.length; i < il;)
						newNode.setAttribute(node.attributes[i].nodeName, node.getAttribute(node.attributes[i++].nodeName));
				/* are we going after children too, and does the node have any? */
				if (allChildren && node.childNodes && node.childNodes.length > 0)
					/* recursively get all of the child nodes */
					for (var i = 0, il = node.childNodes.length; i < il;)
						newNode.appendChild(vdwDOM.importNode(node.childNodes[i++], allChildren));
				return newNode;
				break;
			case document.TEXT_NODE:
			case document.CDATA_SECTION_NODE:
			case document.COMMENT_NODE:
				return document.createTextNode(node.nodeValue);
				break;
		}
	},
	/*
    getElementsByClassName
    Written by Jonathan Snook, http://www.snook.ca/jonathan
    Add-ons by Robert Nyman, http://www.robertnyman.com
	*/
	getElementsByClassName:function(oElm, strTagName, oClassNames){
		var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
		var arrReturnElements = new Array();
		var arrRegExpClassNames = new Array();
		if(typeof oClassNames == "object"){
			for(var i=0; i<oClassNames.length; i++){
				arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames[i].replace(/\-/g, "\\-") + "(\\s|$)"));
			}
		}
		else{
			arrRegExpClassNames.push(new RegExp("(^|\\s)" + oClassNames.replace(/\-/g, "\\-") + "(\\s|$)"));
		}
		var oElement;
		var bMatchesAll;
		for(var j=0; j<arrElements.length; j++){
			oElement = arrElements[j];
			bMatchesAll = true;
			for(var k=0; k<arrRegExpClassNames.length; k++){
				if(!arrRegExpClassNames[k].test(oElement.className)){
					bMatchesAll = false;
					break;
				}
			}
			if(bMatchesAll){
				arrReturnElements.push(oElement);
			}
		}
		return (arrReturnElements)
	},
	fixNodeTypes:function() {
		/* Make sure that all the necessary node types for vdwDOM.importNode are defined */
		if (!document.ELEMENT_NODE) {
			document.ELEMENT_NODE = 1;
			document.ATTRIBUTE_NODE = 2;
			document.TEXT_NODE = 3;
			document.CDATA_SECTION_NODE = 4;
			document.ENTITY_REFERENCE_NODE = 5;
			document.ENTITY_NODE = 6;
			document.PROCESSING_INSTRUCTION_NODE = 7;
			document.COMMENT_NODE = 8;
			document.DOCUMENT_NODE = 9;
			document.DOCUMENT_TYPE_NODE = 10;
			document.DOCUMENT_FRAGMENT_NODE = 11;
			document.NOTATION_NODE = 12;
		}	
	}
}

// ---
// Array support for the push method in IE 5
if(typeof Array.prototype.push != "function"){
	Array.prototype.push = ArrayPush;
	function ArrayPush(value){
		this[this.length] = value;
	}
}
// ---
/*
	Examples of how to call the function:
	
	To get all a elements in the document with a "info-links" class:
    getElementsByClassName(document, "a", "info-links");
    
	To get all div elements within the element named "container", with a "col" and a "left" class:
    getElementsByClassName(document.getElementById("container"), "div", ["col", "left"]);
*/
// ---

sfHover = function() {
	var sfEls = document.getElementById("primary_navigation").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

$(document).ready(function(){
	vdwDOM.addEvent(window, 'load', vdwUtil.init, false);
	
	function get_tallest(selector) {
		var tallest = 0;
		$(selector).each(function() {
			tallest = ($(this).height() > tallest) ? $(this).height() : tallest;
		});
		return tallest;
	}
	
	function build_prev_next_links(prev_id, next_id, prev_txt, next_txt) {
		var prev_next = document.createElement('ul');
		prev_next.className = 'prev_next';
		var prev_li = document.createElement('li');
		var prev_a = document.createElement('a');
		prev_a.href = '#';
		prev_a.id = prev_id;
		var prev_txt = document.createTextNode(prev_txt);
		prev_a.appendChild(prev_txt);
		prev_li.appendChild(prev_a);
		prev_li.className = 'prev_link';
		var next_li = document.createElement('li');
		var next_a = document.createElement('a');
		next_a.id = next_id;
		next_a.href = '#';
		var next_txt = document.createTextNode(next_txt);
		next_a.appendChild(next_txt);
		next_li.appendChild(next_a);
		next_li.className = 'next_link';
		prev_next.appendChild(prev_li);
		prev_next.appendChild(next_li);
		return prev_next;
	}
	
	// Photo Gallery Module
	if ($('#gallery') && ($('#gallery li').size() > 1)) {
		
		// Add a class for styling
		$('#gallery').addClass('hasJS');
		
		// Determine the height of the tallest list item and set the height of the module accordingly
		var tallest = get_tallest('#gallery li');
		$('#gallery_module').height((tallest / 10) + 'em');
		
		// Build the previous next navigation
		var prev_next = build_prev_next_links('gallery_prev', 'gallery_next', '< Prev', 'Next >');
		$('#gallery').append(prev_next);
		
		// Initialize the slideshow
		$('#gallery_module').cycle({ 
			fx:     'fade', 
			speed:  'fast', 
			timeout: 0, 
			next:   '#gallery_prev', 
			prev:   '#gallery_next' 
		});
		
	}
	
	// Student Profile Module
	if ($('#profiles') && ($('#profiles li').size() > 1)) {
		
		// Add a class for styling
		$('#profiles').addClass('hasJS');
		
		// Determine the height of the tallest list item and set the height of the module accordingly
		var tallest = get_tallest('#profiles li');
		$('#profiles_module').height((tallest / 10) + 'em');
		
		// Build the previous next navigation
		var prev_next = build_prev_next_links('profiles_prev', 'profiles_next', '< Prev', 'Next >');
		$('#profiles').append(prev_next);
		
		// Initialize the slideshow
		$('#profiles_module').cycle({ 
			fx:     'fade', 
			speed:  'fast', 
			timeout: 0, 
			next:   '#profiles_prev', 
			prev:   '#profiles_next' 
		});
		
	}
	
	// Equalize Home/MyVCS module row heights
	if ($('.module_row') && $('.module_row').size() != 0) {
		$('.module_row').each(function() {
			if ($(this).find('.module').size() > 1) {
				var tallest = 0;
				$(this).find('.module').each(function() {
					tallest = ($(this).height() > tallest) ? $(this).height() : tallest;
				});
				$(this).find('.module').each(function() {
					$(this).height((tallest / 10) + 'em');
				});
			}
		});
	}
	
	// Add a class of odd to ever other row in a table in the content area
	if ($('#content .stripe tbody tr')) {
		$('#content .stripe tbody tr:even').addClass('alt');
	}
	
	// Wrap the first character of the first paragraph tag in the content div for dropcaps
	/*
	var p_txt = $('#content h1 + p').html();
	if (p_txt) {
		$('#content h1 + p').html('<span class="dropcap">' + p_txt.substring(0,1) + '</span>' + p_txt.substring(1));
	}
	*/
});