/*
-----------------------------------------------
HVACMASTER
Script: vdwUtil.js
Author: 
Organization: 
Created: 
----------------------------------------------- */

vdwUtil = {
	init:function() {
		vdwUtil.mailtoFix('REMOVETHISBEFORESENDING');
		vdwUtil.preparePopups();
		vdwUtil.prepareImages();
		//vdwUtil.downloadTracker();
		if ( $jq('#secondary-navigation ul li.parent ul li.current').length) { 
			$jq('#secondary-navigation ul li.parent > a').addClass('open').css('color', '#0086c8'); 
			$jq('#secondary-navigation ul li.parent > a').mouseover( 
			  function() { $jq(this).css('color', '#f37507'); }
			);
			$jq('#secondary-navigation ul li.parent > a').mouseout( 
			  function() { $jq(this).css('color', '#0086c8'); }
			);
		}
		if ($jq('table.tableinfo_cms').length) {
			$jq('table.tableinfo_cms tr:even').addClass('even');
			$jq('table.tableinfo_cms tr:odd').addClass('odd');
		}
		$jq('#nav-explore').expando({
			loadCallback : function($trigger, $targets, opts) {
				if (!$targets.length) {
					$trigger.hide();
					return false;
				}
				var height = 0;
				$targets.each(function() {
					var pHeight = $jq(this).parent().height();
					if (pHeight > height) { height = pHeight; }
				});
				$trigger.data('targetHeight', height);
			},
			toggleCallback : function($trigger, $targets, $this, opts) {
				var targetHeight = $trigger.hasClass(opts.openClass) ? $trigger.data('targetHeight') : 'auto';
				$jq('#nav-explore-content > li').height(targetHeight);
			}
		});
		//When you click on a link with class of poplight and the href starts with a # 
		$jq('a.poplight[href^=#]').click(function() {
			var popID = $jq(this).attr('rel'); //Get Popup Name
			var popURL = $jq(this).attr('href'); //Get Popup href to define size
			//Pull Query & Variables from href URL
			var query= popURL.split('?');
			var dim= query[1].split('&');
			var popWidth = dim[0].split('=')[1]; //Gets the first query string value
			//Fade in the Popup and add close button
			$jq('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"><img src="/assets/templates/main/images/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');
			//Define margin for center alignment (vertical + horizontal) - we add 80 to the height/width to accomodate for the padding + border width defined in the css
			var popMargTop = ($jq('#' + popID).height() + 80) / 2;
			var popMargLeft = ($jq('#' + popID).width() + 80) / 2;
			//Apply Margin to Popup
			$jq('#' + popID).css({ 
				'margin-top' : -popMargTop,
				'margin-left' : -popMargLeft
			});
			//Fade in Background
			$jq('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
			$jq('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer 
			return false;
		});
		//Close Popups and Fade Layer
		$jq('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
			$jq('#fade , .popup_block').fadeOut(function() {
				$jq('#fade, a.close').remove();  
		}); //fade them both out
			return false;
		});
		$jq('.expando').expando();
		vdwUtil.autoPopulate('input.populate');
	},
	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);
	},
	/*downloadTracker : function() {
		$jq('.download-links li a').click(function(event) {
			event.preventDefault();
			var queryString = {};
			var anchorLink = $jq(this).attr('href');
			$jq(this).attr('href').replace(
				new RegExp("([^?=&]+)(=([^&]*))?", "g"),
				function($0, $1, $2, $3) { queryString[$1] = $3; }
			);
			console.log(queryString);
		});
	},*/
	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.href, "popup", "width=480,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.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);
	},
	prepareImages : function() {
		$jq('.image-right, .image-left').wrapInner('<div class="image_wrapper"></div>');
		$jq('.caption').each(function() {
			$jq(this).width($jq(this).parent().find('img').width());
		});
	},
	autoPopulate:function(input_sel) {	
		$jq(input_sel).each(function() {
			var populate_text = $jq('label[for="' + $jq(this).attr('id') + '"]').text();
			if (populate_text) {
				$jq(this).val(populate_text).data('populate_text', populate_text);				
				$jq(this).focus(function() {
					if ($jq(this).val() == $jq(this).data('populate_text')) {
						$jq(this).val('');
					}
				});
				$jq(this).blur(function() {
					if ($jq(this).val() == '') {
						$jq(this).val($jq(this).data('populate_text'));
					}
				});
			}
		});
	}
};


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


sfHover = function() {
	if (document.getElementById('navigation')) {
		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);


(function($) {
	// Expando Plugin by Ben Glassman
	$.fn.expando = function(options) {
		var opts = $.extend({}, $.fn.expando.defaults, options);
		return this.each(function() {
			$this = $(this);
			var o = opts;
			var $targets = $('.' + o.targetClass, $this);
			var $status = $('.' + o.statusClass, $this);
			var $trigger = $('.' + o.triggerClass, $this).bind('click', function(e) {
				e.preventDefault();
				if ($trigger.hasClass(o.openClass)) {
					$status.text(o.closedStatus);
					$targets.hide();
					$trigger.removeClass(o.openClass).addClass(o.closedClass);
				} else {
					$status.text(o.openStatus);
					$targets.show();
					$trigger.removeClass(o.closedClass).addClass(o.openClass);
				}
				if (o.toggleCallback) { o.toggleCallback($trigger, $targets, $jq(this), o); }
			});
			if (o.loadCallback) { o.loadCallback($trigger, $targets, o); }
			if (o.autoClose) { $trigger.trigger('click'); }
		});
	};
	$.fn.expando.defaults = {
		triggerClass : 'trigger',
		targetClass : 'target',
		statusClass : 'status',
		openClass : 'expanded',
		closedClass : 'collapsed',
		openStatus : 'Collapse',
		closedStatus : 'Expand',
		autoClose : 1,
		loadCallback : null,
		toggleCallback : null
	};
})(jQuery);

var RecaptchaOptions = { theme : 'clean' };

$jq(document).ready(function() {

	var i;
	for (i in document.images) {
		if (document.images[i].src) {
			var imgSrc = document.images[i].src;
			if (imgSrc.substr(imgSrc.length-4) === '.png' || imgSrc.substr(imgSrc.length-4) === '.PNG') {
				document.images[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='crop',src='" + imgSrc + "')";
			}
		}
	}

	vdwUtil.init();// set up slideshow on front page
	//var video_box_height = $jq('#youtube-video .video').height();
	//$jq('#youtube-video').css('height', video_box_height);
	if ($jq('#youtube-video .video').length > 1) {
		$jq('<p id="cycle-controls"><a id="control-next" href="#">Next Video</a><a id="control-prev" href="#">Prev Video</a></p>').prependTo('#youtube-video-wrapper');
		$jq('#youtube-video').cycle({
			fx : 'fade',
			speed: 0,
			timeout : 0,
			prev : '#control-prev',
			next : '#control-next'
		});
	}

	if ($jq('#youtube-main .video').length > 1) {
		$jq('<p id="cycle-controls"><a id="control-next" href="#">Next Video</a><a id="control-prev" href="#">Prev Video</a></p>').prependTo('#youtube-video-main-wrapper');
		$jq('#youtube-main').cycle({
			fx : 'fade',
			speed: 0,
			timeout : 0,
			prev : '#control-prev',
			next : '#control-next'
		});
	}
	
});
