﻿// $cmignore
var wireWhyLinks = function () {
	var fieldNo = 0;
	var fieldTitles = new Array();
	$('.field a.why').remove();
	
	$('.field[title]').each(function () {
		var question = $(this).attr('title').replace(/^((.*?\?)|(Help)) .*$/, "$1");
		$(this).find('label').after('<a href="#" class="why">' + question + '</a>');
		$(this).append('<span id="field-'+fieldNo+'" class="fieldNo" style="display:none;"></span>');
		fieldTitles.push($(this).attr('title').replace(/^(.*?\?)|(Help) (.*)$/, "$3"));
		$(this).removeAttr('title');
		fieldNo++;
	});

	var pointerOffset = 3;

	$('.field a.why')
		.mouseenter(function (e) {
			var field = $(this).closest('.field');
			var fNumber = field.find('.fieldNo').attr('id');
			var fieldInd = fNumber.split('field-');
			var tooltip = fieldTitles[fieldInd[1]];
		
			$('body')
				.append('<div id="why-we-ask"><div class="content"><h3/><p/></div></div>')
				.find('#why-we-ask')
				.find('h3').html((field.find('label').html()=='Text in the box') ? 'Captcha' : field.find('label').html()).end()
				.find('p').html(tooltip).end()
				.css('top', e.pageY + pointerOffset + 'px').css('left', e.pageX + pointerOffset + 'px')
			.end();
		
			// Fixes IE6 issue where popups are rendered below select elements
			if ($.browser.msie && $.browser.version < 7) {
				$('#why-we-ask')
					.prepend('<iframe src="javascript:false" style="border: 0; z-index: -1; filter: mask(); position: absolute; top:0; left:0; width: 280px; height: 100px; display: block; background-color: transparent;"></iframe>')
			}
		
		})
		.mousemove(function (e) {
			var theForm = $('#content form');
		
			var popupWidth = $('#why-we-ask').outerWidth();
			var popupHeight = $('#why-we-ask').outerHeight();
		
			var boxRightEdge = theForm.outerWidth() + theForm.position().left;
			var boxBottomEdge = theForm.outerHeight() + theForm.position().top;

		        var popupCutOff= ((e.pageY + pointerOffset) - (popupHeight + 2 * pointerOffset)) > 0;

			var clippingRight = (e.pageX + pointerOffset + popupWidth) > boxRightEdge;
			var clippingBottom = (e.pageY + pointerOffset + popupHeight) > boxBottomEdge;
			$('#why-we-ask')
				.css('left', (((e.pageX + pointerOffset) - (clippingRight ? (popupWidth + 2 * pointerOffset) : 0)) + 'px'))
				.css('top', (((e.pageY + pointerOffset) - ((clippingBottom && popupCutOff) ? (popupHeight + 2 * pointerOffset) : 0)) + 'px'));
		})
		.mouseleave(function () {
			$('#why-we-ask').remove();
		})
		.click(function () { return false; });
};


$(document).ready(function () {
	// Add the question links and a hover popup rendering the title attribute of
	// the its field. This split the value of title into the question link and the
	// content of the box that comes up on the first '?' symbol.
		
		wireWhyLinks();
		
		$('input[title]').each(function () {
			$(this)
				.addClass('default')
				.val($(this).attr('title'))
				.bind('focus', function () {
					if ($(this).hasClass('default')) {
						$(this)
							.val('')
							.removeClass('default');
					}
				})
				.bind('blur', function () {
					if ($(this).val() == '') {
						$(this)
							.addClass('default')
							.val($(this).attr('title'));
					}
				});
		});
});
// $/cmignore


