/*
-----------------------------------------------
KidsFlySafe.com
Script: vdwUtil.js
Author: Ben Glassman
Organization: Vermont Design Works
Created: 23 Apr 2009
----------------------------------------------- */

vdwUtil = {
	init:function() {
		vdwUtil.mailtoFix('REMOVETHISBEFORESENDING');
		vdwUtil.preparePopups();
		vdwPngRollover.init({ containerIds : ['btn-facebook', 'btn-twitter'], preloadImgs : ['/assets/templates/main/images/facebook-on.png', '/assets/templates/main/images/twitter-on.png' ]});
		// Demo video
		$jq('#btn-watch-demo a').click(function(e) {
			e.preventDefault();
			if (!vdwUtil.videoDemoLoaded) {
				$jq('#video-slideshow-placeholder').remove();
				flowplayer("installation-video", { src : "/assets/flash/flowplayer-3.1.1.swf", wmode : 'transparent' }); 
				vdwUtil.videoDemoLoaded = true;
			}
		});
		// Media images
		$jq('#asseenbox .img img').each(function() {
			if ($jq(this).height() >= 141) { return; }
			$jq(this).css({ marginTop : Math.ceil((141 - $jq(this).height()) / 2) + 'px' });
		});
	},
	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,'');
	}
}

$jq(document).ready(vdwUtil.init);
startList = function() {
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("menuElem");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" jsover";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" jsover", "");
				}
			}
		}
	}
}
window.onload=startList;

$jq.preloadImages = function(images) {
	for(var i = 0; i < images.length; i++) {
		$jq("<img>").attr("src", images[i]);
	}
}