/*
-----------------------------------------------
sitename
Script: vdwUtil.js
Author: Ben Glassman
Organization: Vermont Design Works
Created: 
----------------------------------------------- */

vdwUtil = {
	init:function() {
		vdwUtil.mailtoFix('REMOVETHISBEFORESENDING');
		vdwUtil.preparePopups();
		vdwUtil.tempRedirectOverlay();
	},
	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;
				}
			}
		}
	},
	tempRedirectOverlay:function() {
		var visitorURL = window.location.href; 
		var oldURL = /vtpcrc.org/gi;
		if (visitorURL.match(oldURL)) {
			$jq("<div id='temp-redirect-overlay'><div class='content'><p>Vermont Palliative and End-of-Life Care Resource Connections (www.vtpcrc.org) is now the website for <strong>Start the Conversation</strong>, a public education initiative by Vermont's non-profit Visiting Nurse Associations, Home Health, and Hospice Agencies.</p><p>You will be redirected to the new website at <a href='http://www.starttheconversationvt.org'>www.starttheconversationvt.org</a><br /> in the next 10 seconds, or click <a href='http://www.starttheconversationvt.org'>here</a> now.</p></div>").insertBefore('body'); 
			$jq('#temp-redirect-overlay').dialog({
				modal : true,
				autoOpen : true,
				closeOnEscape : false,
				width : 500,
				height: 250
			});
			setTimeout("vdwUtil.closeTempRedirectOverlay()", 10000);
			$jq('.ui-dialog-overlay').css( { 'background' : '#000', 'opacity': '.80',  'filter' : 'Alpha(Opacity=80)', 'z-index' : 1000 } );
		}
	}, 
	closeTempRedirectOverlay:function() {
		$jq('#temp-redirect-overlay').remove();
		var visitorURL = window.location.href; 
		var newURL = visitorURL.replace('vtpcrc', 'starttheconversationvt');
		//vdwUtil.createCookie('popUpViewed',1,365);
		window.location.href= newURL;		
	},
	createCookie:function(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	},
	readCookie:function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	},
	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);
	},
	createCookie:function(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	},
	readCookie:function(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}
}

/*
jQuery Form Validation Error Container Plugin */

jQuery.fn.prepareFormVal = function() {
    return this.each(function(){
        jQuery('<div id="error-container"><h2>The following errors occured</h2><ul></ul></div>').prependTo(jQuery(this)).hide();
    });
};

$jq(document).ready(vdwUtil.init);
