/*
-----------------------------------------------
DiscoverJazz.com
Script: vdwUtil.js
Author: Ben Glassman
Organization: Vermont Design Works
Created: 30 Mar 2010
----------------------------------------------- */

// popup Plugin
(function($)
{
	$.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)'
	}
})(jQuery);

// parentButton Plugin
(function($)
{
	$.fn.parentButton = function()
	{
		return this.each(function() {
			$this = $(this);
			$a = $this.find('a');
			if ($a.length) {
				$this.data('href', $a.eq(0).attr('href'));
				$this.click(function(e) {
					window.location.href = $jq(this).data('href');
				}).hover(function() {
					$jq(this).addClass('hover');
				}, function() {
					$jq(this).removeClass('hover');
				});			
			}
		});
	}
})(jQuery);

vdwUtil = {
	init:function() {
		vdwUtil.mailtoFix('REMOVETHISBEFORESENDING');
		vdwUtil.preparePopups();
		$jq('.parent-button').parentButton();
	},
	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.external').popup({ 'winName' : 'external', 'winParams' : '' });
	}
}

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