/*
-----------------------------------------------
FranklinFoods.com
Script: vdwUtil.js
Author: Ben Glassman
Organization: Vermont Design Works
Created: 03 June 2009
----------------------------------------------- */

vdwUtil = {
	init:function() {
		vdwUtil.mailtoFix('REMOVETHISBEFORESENDING');
		vdwUtil.preparePopups();
		vdwPngRollover.init({ containerIds : ['btn-facebook', 'btn-twitter', 'btn-youtube'], preloadImgs : ['/assets/templates/main/images/btn-facebook-on.png', '/assets/templates/main/images/btn-twitte-on.png', '/assets/templates/main/images/btn-youtube-on.png' ]});
	},
	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=752,height=620,resizable=yes,scrollbars=no,toolbar=no,location=no,menubar=no,status=no");
					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);

/*
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.preloadImages = function(images) {
	for(var i = 0; i < images.length; i++) {
		$jq("<img>").attr("src", images[i]);
	}
}
