/*
-----------------------------------------------
FlowerPowerFundraising.com
Script: vdwUtil.js
Author: Ben Glassman
Organization: Vermont Design Works
Created: Jan 26 2009
----------------------------------------------- */

vdwUtil = {
	init:function() {
		vdwUtil.mailtoFix('REMOVETHISBEFORESENDING');
		vdwUtil.preparePopups();
		vdwDOM.fixNodeTypes();
		// Logo animation
		if ($jq('#logo').length) {
			if ($jq('body').attr('id') == 'index') {
				var so = new SWFObject("/assets/flash/logo-flower-power-fundraising-home.swf", "flash-logo", "350", "110", "8", "#FFF");
			} else {
				var so = new SWFObject("/assets/flash/logo-flower-power-fundraising.swf", "flash-logo", "350", "110", "8", "#FFF");	
			}
			so.addParam('wmode','transparent');
			so.write("logo");			
			$jq('#logo').addClass('flash');
			$jq('#masthead').prepend('<div id="print-logo"><img height="80" width="350" alt="Flower Power Fundraising" src="/assets/templates/main/images/logo-flower-power-fundraising.png" /></div>');
		}
// 		if ($jq('body').attr('id') == 'campaign-email-preview') {
// 			window.resizeTo(600, 400);
// 		}
		$jq('.close').click(function(e) {
			e.preventDefault();
			window.close();
		});
	},
	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.indexOf('popup') != -1) {
				lnks[i].title+= " (opens in a new window)";
				lnks[i].onclick = function() {
					vdwUtil.popUp(this.href, "popup", "width=320,height=480,scrollbars=1");
					return false;
				}
			}
			else if (lnks[i].className == "external") {
				lnks[i].title+= " (opens in a new window)";
				lnks[i].onclick = function() {
					vdwUtil.popUp(this.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.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);
	},
	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;
	}
}

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;
		}
	},
	firstChild:function(parent, nodeType) {
		var child = parent.firstChild;
		while (child.nodeType != nodeType) {
			child = child.nextSibling;
		}
		return child;		
	},
	/*
    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("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);

$jq(document).ready(function(e) {
	vdwUtil.init();
	/*
	Close window link */
	if ($jq('body').attr('id') == 'campaign-landing-preview') {
		var campaignName = $jq('h1').text();
		$jq('<p class="preview-message">This is a Preview of ' + campaignName + ' Home Page, not an actual web site. <a href="#">Click Here To Close Window</a></p>').prependTo('body').clone().appendTo('body');
		$jq('.preview-message a').click(function(e) {
			e.preventDefault();
			window.close();
		});
		$jq('a').not('.preview-message a').click(function(e) {
			e.preventDefault();
		});
	}
	/*
	Close email preview link */
	if ($jq('body').attr('id') == 'campaign-email-preview') {
		$jq('<p class="preview-message">This is a Preview of a campaign email. <a href="#">Click Here To Close Window</a></p>').prependTo('body').clone().appendTo('body');
		$jq('.preview-message a').click(function(e) {
			e.preventDefault();
			window.close();
		});
		$jq('a').not('.preview-message a').click(function(e) {
			e.preventDefault();
		});
	}
	/*
	Newsletter Signup */
	if ($jq('#newsletter-signup').length) {
		$jq('#newsletter-email').parent().prepend('<div class="message hide"></div>');
		$jq('<div id="dialog-box"><p>You have successfully signed up the email address <span class="dialog-confirm"></span> for the Flower Power newsletter</p></div>').insertAfter('#newsletter-signup');
		$jq('#dialog-box').hide();
		$jq('#newsletter-signup').submit(function(e) {
			e.preventDefault();
			$jq.post('/assets/ajax/newsletter_signup.php', { newsletter_email : $jq('#newsletter-email').val() }, function(data) { newsletterSignupCallbank(data); }, 'json'); 
				$jq('#dialog-box').dialog({
					autoOpen : false,
					width : 425, 
					modal : true,
					resizable : false, 
					buttons : {
						"OK" : function() {
							$jq(this).dialog("close");
						}
					}
				});
				$jq('span.dialog-confirm').html($jq('input#newsletter-email').val());
				$jq('#dialog-box').dialog('open');
				$jq('#dialog-box').show();
				return false;
		});
		function newsletterSignupCallbank(data) {
			$jq('#newsletter-signup .message').html(data.message_html).slideDown('fast');
		}	
	}
});