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

vdwUtil = {
	init:function() {
		var self = vdwUtil;
		self.mailtoFix('REMOVETHISBEFORESENDING');
		self.preparePopups();
		self.prepareExploreNav();
		self.validateNewsletterForm();
		// fixes tabs-related problems on clients page SVE 9/26/11
		if ($jq('#clients').length) { 
			if(window.location.href.indexOf("letter") != -1) {
				$tabs = $jq('#client-list').tabs({selected : 1 });
			} else { $tabs = $jq('#client-list').tabs(); }
			vdwUtil.waitIndicator();
			$jq('#tab-full-clients-list, #tab-full-clients-list a, #tab-recent-clients-list, #tab-recent-clients-list a').click(function() {
				$jq('#nav-secondary-current').remove();
				vdwUtil.setCurrentIndicator();
			});
		}
		else { 
			self.setCurrentIndicator();
		}
	},
	waitIndicator:function() {
		setTimeout("vdwUtil.setCurrentIndicator()", 100);
	},
	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:/, '');
			}
		}
	},
	preparePopups:function() {
		$jq('a.popup').popup();
		// Open external links (other than flynncenter.org) in a new window
		$jq('a').live('click', function(e) {
			var href = $jq(e.target).closest('a').attr('href');
			if ($jq(e.target).closest('a').attr('class') == '' || $jq(e.target).closest('a').attr('class') == 'external') {
				if (!(new RegExp(window.location.hostname).test(href)) && (/https?:/.test(href))) {
					window.open(href, 'external', '');
					return false;
				}
			}
		});
	},
	prepareExploreNav : function() {
		$jq('#content-intro').append('<div class="divider"></div>');
		$jq('#content h1.page-title').append('<div class="divider"></div>');
		$jq('#nav-explore').expando({
			loadCallback : function($trigger, $targets, opts) {
				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);
			}
		});		
	},
	validateNewsletterForm:function(emailField, errorMsg) {
		var errorMsg = "Please submit a valid email address."; 
		var emailField = document.getElementById("newsletter_email");
			$jq("#newsletter_signup_form").submit(function() {
				emailpat = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;
					if(!emailpat.test(emailField.value)) {
						alert(errorMsg);
						emailField.focus();
						emailField.select();
						return false;
					 }
					return true;
			});
	},
	/*validateNewsletterForm : function() {
		$jq('#newsletter_signup_form').validate({
		   rules : {
			newsletter_email: {
			   required : true,
			   email : true
			}
		   },
		   messages : {
			   newsletter_email : 'A valid email is required'
		   }
		});
	},*/
	setCurrentIndicator : function() {
		$navSecondary = $jq('#nav-secondary');
		$current = $navSecondary.find('.current strong').eq(0);
		if (!$current.length) { $current = $navSecondary.find('p.current').eq(0); }
		if (!$current.length) { return; }
		$currentOp = $current.offsetParent();
		var indicatorOffset = {
			top : ($current.offset().top - $currentOp.offset().top) + Math.ceil(($current.height() + parseInt($current.css('paddingTop')) + parseInt($current.css('paddingBottom'))) / 3),
			left : 0
		}
		var indicatorHeight = $jq('#main-wrapper').height() - (indicatorOffset.top - ($navSecondary.offset().top - $currentOp.offset().top));
		$jq('<div id="nav-secondary-current"><div></div></div>').css({
			width : '40px',
			height : indicatorHeight + 'px',
			position: 'absolute',
			top : indicatorOffset.top + 'px',
			left : indicatorOffset.left + 'px'
		}).appendTo($navSecondary).find('div').eq(0).css({ width : '40px', height : indicatorHeight + 'px' });		
	}
};

var RecaptchaOptions = { theme : 'white' };

(function($) {
	// popup Plugin
	$.fn.popup = function(options)
	{
		var opts = $.extend({}, $.fn.popup.defaults, options);
		return this.each(function() {
			$this = $(this);
			if (opts.titleMessage) {
				$this.attr('title', ($this.attr('title') == '') ? opts.titleMessage : $this.attr('title') + ' ' + opts.titleMessage);
			}
			$this.click(function(e) {
				e.preventDefault();
				window.open($(this).attr('href'), opts.winName, opts.winParams);
			});
		});
	};
	$.fn.popup.defaults = {
		winName : 'popup',
		winParams : 'width=320,height=480,scrollbars=yes',
		titleMessage : '(opens in a new window)'
	}
	
	// equalizeCols Plugin
	$.fn.equalizeCols = function()
	{
	  var height = 0;
	  return this.css("height","auto").each(function(){
		$this = jQuery(this);
		height = Math.max( height, ($this.outerHeight() - parseInt($this.css('padding-top')) - parseInt($this.css('padding-bottom'))));
	  }).css("height", height);
	
	};

	// 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);


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