/*
Copyright (c) 2010, [COMPANY]. All rights reserved.
Code licensed under the MIT License:
http://www.opensource.org/licenses/mit-license.php

Author:			Draftfcb Chicago
Development:	nick.kraska@draftfcb.com
					dennis.kim@draftfcb.com
*/

function validateExistEmail(){
	// Get the value of the email field
	existemail = $("#existemail").val();
	
	if(
		existemail == "e-mail" || existemail == ""
		){
		return true;
	}else{
		return false;
	}
}

function validateAddress(){
	// Set regex
	myregexp = /\b[P|p]*(OST|ost)*\.*\s*[O|o|0]*(ffice|FFICE)*\.*\s*[B|b][O|o|0][X|x]\b/

	// Get the value of the address field
	address = $("#address").val();
	
	// Compare against our regex. Does it find PO Box or is it null?
	if(
		myregexp.test(address) == true || address == ""
		){
		return true;
	}else{
		return false;
	}
}


/*
This class applies tooltips to the prizeCarousel list element images.

Tooltip copy is semantically inserted into the list element in the span .tipBlurb. HTML is allowed in the tooltip copy.

It must play nice with the jCarouselLite plugin, since they are both applied to the prize Carousel.
Hovering over a prize image shows a tooltip dynamically placed above the prize.
*/
jQuery.fn.prizeToolTips = function() {
	var container = this;	
	var parentOffsetLeft = 220;
	var parentOffsetTop = 0;
	if ($("#prizeCarouselContainer").length) {
		parentOffsetTop = $('#prizeCarouselContainer').position().top + 12;
	}
	var listEls = container.find('li');
	var toolTip = $('#prizeTip');
	var toolTipCopy = $('#prizeTipCopy');
	listEls.each( function(e) {
		$(this).first('img').hover(
			function() {
				// populate tool tip with tipBlurb contents
				var tipBlurb = $(this).find('.tipBlurb').html();
				toolTipCopy.html('<p>'+tipBlurb+'</p>');
				// compute x-position
				var xOffset = $(this).first('img').position().left + $(this).parent('ul').position().left + parentOffsetLeft;
				toolTip.css('left', xOffset);
				// compute y-position
				var yOffset = parentOffsetTop - toolTip.outerHeight();
				toolTip.css('top', Math.round(yOffset));
				toolTip.css('display', 'block');
			},
			function() {
				toolTip.css('display', 'none');
			}
		);
	});
}


$(document).ready( function() {

	//prize page methods
	$("#prizeCarousel").jCarouselLite({
		btnNext: ".carouselRight",
		btnPrev: ".carouselLeft"
	});
	
	$("#prizeCarousel").prizeToolTips();

	// validation
	$("#form-newuser").validationEngine();
	$("#form-refer").validationEngine();
	$("#form-donate").validationEngine();
	$("#form-existinguser").validationEngine();
	
	// Grab title attribute as field label for existing user
	$('input[title]').each(function() {
		if($(this).val() === '') {
			$(this).val($(this).attr('title'));
		}

		$(this).focus(function() {
			if($(this).val() === $(this).attr('title')) {
				$(this).val('').addClass('focused');
			}
		});

		$(this).blur(function() {
			if($(this).val() === '') {
				$(this).val($(this).attr('title')).removeClass('focused');
			}
		});
	});
	
});

