$(function() {  
	
		initLinks();
				
	// initialize the mailing list tooltip 
	_mailinglisttooltip();
	
	// Initialize the tracking functionality
	_trackEvents();

});

var inProduction = (window.location.host === 'www.jeanchatzky.com') ? true : false;

var initLinks = function () {

		$('body').delegate('a', 'click', function(e) {
			var win;
			$link = $(this);
	
		switch (this.rel) {
				case 'external':
					e.preventDefault();
						window.open(this.href);
						break;
				case 'popup':
							e.preventDefault();
						if ($link.hasClass('profile')) {
								_launchPop(this.href,510,840);
					} else if ($link.hasClass('video')){
									_launchPop(this.href,302,520);
					} else {
								_launchPop(this.href,510,660);
					}
						break;
			}
 
		});
		
		
		var _launchPop = function (theURL, iHeight, iWidth) {
		   leftPos = (screen.width) ? (screen.width - iWidth) / 2 : 0;
		   topPos = (screen.height) ? (screen.height - iHeight) / 2 : 0;
		   newWindow = window.open(theURL, 'subWindowCustom', 'toolbar=no,location=no,scrollbars=yes,status=no,menubar=no,resizable=yes,left=' + leftPos + ',top=' + topPos + ',height=' + iHeight + ',width=' + iWidth + ',fullscreen=no');
		   newWindow.focus();
 }

		
};


/* Tooltip functionality for mailing list checkbox */
var _mailinglisttooltip = function () {
	// hide tooltip elements and add close and arrow elements.
	$('.tooltip').hide().append('<a class="close" href="#">Close</a><span class="arrow"></span>');
	var $triggers = $('.tooltip-trigger');
	
	$triggers.bind('click', function (e) {
		var $this = $(this);
		var $tooltip = $this.siblings('.tooltip');
		
		// Only show the tooltip if the checkbox is unchecked
		if (this.checked === false) {
			$tooltip.show();
			$('#user_email_pref').val('no');
		} else {
			$('#user_email_pref').val('yes');
		}
		
		// bind close button event functionality
		$tooltip.find('a.close').bind('click', function (e) {
			e.preventDefault();
			// Close tooltip
			$tooltip.hide();
			// Unbind event from the close button
			$(this).unbind();
		});
	});
};

var _trackEvents = function () {
	$('body.partner-registration').delegate('a.signin, a.video, a.signup, a.overlay, #wp-submit-reg', 'click', function(e){
		var $this = $(this);
		var opt_label;
		
		// Set the appropriate opt_label
		if ($this.hasClass('signin')) {
			opt_label = 'Signin Link';
		};
		
		if ($this.hasClass('signup')) {
			opt_label = 'Signup Link';
		};
		
		if ($this.hasClass('overlay')) {
			opt_label = 'Video Overlay';
		};
		
		if ($this.hasClass('video')) {
			opt_label = 'Video - ('+$this.attr('href')+')';
		};
		
		
		
		
		if (this.id === 'wp-submit-reg') {
			opt_label = 'Registration Submit Button';
		};
		
		if (inProduction){
				_gaTrackEvent(ORG_NAME, 'click', opt_label);
		}
	});
	
	var _gaTrackEvent = function (category, action, opt_label) {
		_gaq.push(['_trackEvent', category, action, opt_label]);
	};
};


