var timer = setInterval("neo.changeHero()",7000);
var neo = {
	init: $(document).ready( function(){
		//preload main carousel images
		var carousel = new Array();
		function preload() {
			for (i = 0; i < preload.arguments.length; i++) {
				carousel[i] = new Image();
				carousel[i].src = preload.arguments[i];
			}
		}
		preload(
			"../assets/img/carousel/hero1.png",
			"../assets/img/carousel/hero2.png",
			"../assets/img/carousel/sprite_hero2_1.png",
			"../assets/img/carousel/sprite_hero2_1overlay.png",
			"../assets/img/carousel/sprite_hero2_1sub.png",
			"../assets/img/carousel/sprite_hero2_2.png",
			"../assets/img/carousel/sprite_hero2_2overlay.png",
			"../assets/img/carousel/sprite_hero2_3.png",
			"../assets/img/carousel/sprite_hero2_3overlay.png",
			"../assets/img/carousel/hero2_text.png",
			"../assets/img/carousel/hero3.png",
			"../assets/img/carousel/hero3_text1.png",
			"../assets/img/carousel/hero3_text2.png",
			"../assets/img/carousel/hero4.png",
			"../assets/img/carousel/img_couponEssentials.png",
			"../assets/img/carousel/img_couponEssentialsTrial.png"
		)
		timer;
		
		$('.onPageTabs').each( function(i,e){
			var allContents = $(this).find('.tabContent');
			var allTabs = $(this).find('.tabs a');
			allTabs.click( function(e){
				allTabs.removeClass('on');
				$(this).addClass('on');
				allContents.removeClass('on');
				$($(this).attr('href')).addClass('on');
				e.preventDefault();
			});
		});	
		
		$('a[rel=tooltip]').mouseover(function(e) {
			
			//Grab the title attribute's value and assign it to a variable
			var tip = $(this).attr('name');	
			
			//Remove the title attribute's to avoid the native tooltip from the browser
			$(this).attr('title','');
			
			//Append the tooltip template and its value
			$(this).append('<div id="simpletooltip">' + tip + '</div>');		
					
			//Show the tooltip with faceIn effect
			//$('#tooltip').fadeIn('500');
			//$('#tooltip').fadeTo('10',0.9);
			
		}).mousemove(function(e) {
		
			//Keep changing the X and Y axis for the tooltip, thus, the tooltip move along with the mouse
			//$('#simpletooltip').css('top', e.pageY -50 );
			//$('#simpletooltip').css('left', e.pageX -250 );
			
		}).mouseout(function() {
		
			//Put back the title attribute's value
			$(this).attr('title',$('#simpletooltip').html());
		
			//Remove the appended tooltip template
			$(this).children('div#simpletooltip').remove();
			
		});
		
		var wasRound = false;
		$('.hoverPriority').hover( function(){
			if($(this).parents('#navBar').hasClass('roundBtm')){ $(this).parents('#navBar').removeClass('roundBtm').addClass('flatLeft'); wasRound = true;} 
			$(this).find('ul').css({left: 0, zIndex: 10});
			$('.active ul').hide();
		}, function(){
			if(wasRound){
				$(this).parents('#navBar').removeClass('flatLeft').addClass('roundBtm'); wasRound = false;
			}
			$(this).find('ul').css({left: '-9999px', zIndex: 00});
			$('.active ul').show();
		});
		
		$('.temp #hero1 a.product').click(function(e){
			e.preventDefault();
		});
		
		$('.temp #hero1 .prodBucket').hover(function(){
			$('.temp #hero1 a.product').addClass('fade');
			$(this).children('a.product').removeClass('fade');
			$('.temp #hero1').addClass('fade');
			$('.temp #hero1 .text').hide();
			$('.temp #hero1 .cta').hide();
		},function(){
			$('.temp #hero1 a.product').removeClass('fade');
			$('.temp #hero1').removeClass('fade');
			$('.temp #hero1 .text').show();
			$('.temp #hero1 .cta').show();
		});
		
		$('#hero2 a.product').click(function(e){
			e.preventDefault();
		});
		
		$('#hero2 .prodBucket').hover(function(){
			$('#hero2 a.product').addClass('fade');
			$(this).children('a.product').removeClass('fade');
			$('#hero2').addClass('fade');
			$('#hero2 .text').hide();
			$('#hero2 .cta').hide();
		},function(){
			$('#hero2 a.product').removeClass('fade');
			$('#hero2').removeClass('fade');
			$('#hero2 .text').show();
			$('#hero2 .cta').show();
		});
		
		$('#pager a').click(function(e){
			e.preventDefault();
			clearInterval(timer);
			var pg = $(this).attr('rel');
			var id = "hero"+pg;
			$('.hero').hide().removeClass('active');
			if(brow == 'ie7' || brow == 'ie8'){
				$('#'+id).show().addClass('active');
			}else{
				$('#'+id).fadeIn(500).addClass('active');
			}
			
			$('#pager a').removeClass('active');
			$(this).addClass('active');
			timer = setInterval("neo.changeHero()",7000);
		});
		
		$('#slideInner .hero').hover(function(){
			clearInterval(timer);
			$('#slideInner').addClass('hover');
		}, function(){
			timer = setInterval("neo.changeHero()",7000);
			$('#slideInner').removeClass('hover');
		});
		
		
		$('.pops a').fancybox({titlePosition: 'inside'});
		
		//var checked = null;
		$('.fullGroup').click( function(){
			checked = $(this).attr('checked');

			$('.fullGroup').removeAttr('checked');
			if(checked){
				$(this).attr('checked', 'checked');
			}else{
				$(this).removeAttr('checked');
			}
			if($(this).attr('id') == 'target7' || $(this).attr('id') == 'target8'){
				$('.yesGroup').removeAttr('checked');
			}
		});
		
		$('.yesGroup').change( function(){
			if(!$('#target4').attr('checked') && !$('#target5').attr('checked') && !$('#target6').attr('checked')){
				$('#target4a').removeAttr('checked');
			}else if($('#target4').attr('checked') || $('#target5').attr('checked') || $('#target6').attr('checked')){
				$('#target4a').attr('checked', 'checked');
				$('#target7, #target8').removeAttr('checked');
			}
		});
		
		var href = '';
		$('.leavePop').click(function(e){ href = $(this).attr('name');}).fancybox({titlePosition: 'inside', onStart: function(e){
			$('#leavePop .contBtn').attr('href', href); 
		}});
		
		$('.send-print a.email').fancybox({titlePosition: 'inside'});
		$('.send-print a.email').live('click', function () {
			// clear errors + form inputs
			$('#ef-form .error').html('');
			$('#ef-form input[type="text"]').removeClass('errorBorder').val('').html('');
		});
		
		// Email to a friend AJAX form
		$('#submit-ef-form').live('click', function () {
			//var REGEX_EMAIL = [ "^([a-zA-Z0-9_\\.\\-\\+])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,4})+$" ];
			var emailRegEx = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
			var name_regex = /^[a-zA-Z]+[ a-zA-Z]$/g;
			var errorCount = 0;
			
			// clear errors
			$('.error').html('');
			$('input').removeClass('errorBorder');
			
			// check for names
			var name1 = $('input#ef-your-name').val();
			var name2 = $('input#ef-friend-name').val();
			var email1 = $('input#ef-your-email').val();
			var email2 = $('input#ef-friend-email').val();
			if (name_regex.test(name1) == false) {
				$('input#ef-your-name').addClass('errorBorder');
				$('input#ef-your-name').siblings('.error').text('Please enter your name');
				errorCount++;
			}
			

			if (/^[a-zA-Z]+[ a-zA-Z]$/g.test(name2) == false) {
				$('input#ef-friend-name').addClass('errorBorder');
				$('input#ef-friend-name').siblings('.error').text('Please enter your friend\'s name');
				errorCount++;
			}
			
			// validate email addresses
			
			if (!email1) {
				$('input#ef-your-email').addClass('errorBorder');
				$('input#ef-your-email').siblings('.error').text('Please enter a valid email');
				errorCount++;
			} else if (!emailRegEx.test(email1)) {
				$('input#ef-your-email').addClass('errorBorder');
				$('input#ef-your-email').siblings('.error').text('Please enter a valid email');
				errorCount++;
			}
			
			if (!email2) {
				$('input#ef-friend-email').addClass('errorBorder');
				$('input#ef-friend-email').siblings('.error').text('Please enter a valid email');
				errorCount++;
			} else if (!emailRegEx.test(email2)) {
				$('input#ef-friend-email').addClass('errorBorder');
				$('input#ef-friend-email').siblings('.error').text('Please enter a valid email');
				errorCount++;
			}
			if (errorCount == 0){
				var yourname = $('input#ef-your-name').val();
				var youremail = $('input#ef-your-email').val();
				var friendname = $('input#ef-friend-name').val();
				var friendemail = $('input#ef-friend-email').val();
				var current_url = $('input#ef-current-url').val();
				$.ajax({
					url: emailFr+"/ajax_process.php",
					type: "post",
					data: {
						"typeID": 1,
						"yourname": yourname,
						"youremail": youremail,
						"friendname": friendname,
						"friendemail": friendemail,
						"current_url": current_url
					},
					success: function (data) {
						//alert(data);
						$('.ef-form-box, .ef-thanks').slideToggle(500);
					},
					error: function (errmsg) {
					}
				});	
			}	
			return false;
		});
		
		$('#resetBtn').click( function(e){
			$('input[type="text"]').val('');
			$('input:checked').removeAttr('checked');
			$(':input').removeClass('errorBorder');
			$('p span, #purchasedError').removeClass('error');
			e.preventDefault();
		});
		
		$('.tipper').hover( function(e){
			$(this).find('.ttip').css('left', 0);
		}, function(e){
			$(this).find('.ttip').css('left', '-9999px');
		}).click( function(e){ e.preventDefault();});
		
		neo.unsubscribe();
	}),
	
	signupValidation: function () { 
		//input mask
		$('input[name$="dob"]').mask("99/99/9999");
		$('input[name$="phone"]').mask("(999) 999-9999");
		
		
		$('a#submit').click(function(e)
		{	
			var REGEX_NAME = [ ".*[\\S]+.*" ],
			REGEX_EMAIL = [ "^([a-zA-Z0-9_\\.\\-\\+])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,4})+$" ],
			REGEX_ZIPCODE = [ "(^[0-9]{5}(-[0-9]{4})?$)|(^([ABCEGHJKLMNPRSTVXY]{1}[0-9]{1}[A-Z]{1})(\\s*)([0-9]{1}[A-Z]{1}[0-9]{1})$)|(^([A-Z]{2})(\\s*)([A-Z]{2}|[0-9]{2})$)" ],
			emailRegEx = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/,
			name_regex = /^[a-zA-Z ]+[a-zA-Z]$/g,
			last_regex = /^[a-zA-Z- ]+[a-zA-Z]$/g,
			cityReg = /^[a-zA-z- ]+[a-zA-Z]$/g,
			stateString = 'alabama|alaska|arizona|arkansas|california|colorado|connecticut|delaware|florida|georgia|hawaii|idaho|illinois|indiana|iowa|kansas|kentucky|louisiana|maine|maryland|massachusetts|michigan|minnesota|mississippi|missouri|montana|nebraska|nevada|new hampshire|new jersey|new mexico|new york|north carolina|north dakota|ohio|oklahoma|oregon|pennsylvania|rhode island|south carolina|south dakota|tennessee|texas|utah|vermont|virginia|washington|west virginia|wisconsin|wyoming',
			sA = stateString.split('|');
			errorCount=0;
			
			// hide error messages
			$(':input').removeClass('errorBorder');
			$('p span, #purchasedError').removeClass('error');
			
			// validate checkboxes			
			if (!$('#target').attr('checked') && !$('#target2').attr('checked') && !$('#target3').attr('checked')) {
				$('#target2').siblings("span").addClass('error');
				errorCount++;
			}
			var firstCondition = ($('#target4a').attr('checked') && ($('#target4').attr('checked') || $('#target5').attr('checked') || 
									$('#target6').attr('checked'))) || (!$('#target7').attr('checked') && !$('#target8').attr('checked'));
			if (firstCondition == true) {
				$('#purchasedError').addClass('error');
				errorCount++;
			}
			
			// validate first name
			var fname = $.trim($('input[name$="first-name"]').val());
			$('input[name$="first-name"]').val(fname);
			if (!name_regex.test(fname)) {			
				$('input[name$="first-name"]').addClass('errorBorder');
				$('input[name$="first-name"]').siblings("span").addClass('error');
				errorCount++;
			}
			
			// validate last name
			var lname = $.trim($('input[name$="last-name"]').val());
			$('input[name$="last-name"]').val(lname);
			if (!lname || !last_regex.test(lname)) {
				$('input[name$="last-name"]').addClass('errorBorder');
				$('input[name$="last-name"]').siblings("span").addClass('error');
				errorCount++;
			}
			
			// validate city
			var city = $.trim($('input[name$="city"]').val());
			$('input[name$="city"]').val(city);
			if (!city || !cityReg.test(city)) {
				$('input[name$="city"]').addClass('errorBorder');
				$('input[name$="city"]').siblings("span").addClass('error');
				errorCount++;
			}
			
			// validate state
			var state = $.trim($('input[name$="state"]').val()), validState = false;
			$.each( sA, function(i,e){
				if(state.toLowerCase() == sA[i]){
					validState = true;
				}
			});
			$('input[name$="state"]').val(state);
			if (!validState) {
				$('input[name$="state"]').addClass('errorBorder');
				$('input[name$="state"]').siblings("span").addClass('error');
				errorCount++;
			}

			// validate dob
			if (!$('input[name$="dob"]').val()) {
				$('input[name$="dob"]').addClass('errorBorder');
				$('input[name$="dob"]').siblings("span").addClass('error');
				errorCount++;
			} else {
				var dob = new String($('input[name$="dob"]').val());				
				var year=new Date();
				year=year.getFullYear();
								
				// validate month day year
				if (parseInt(dob.slice(0,2)) > 12 || parseInt(dob.slice(3,5)) > 31 || parseInt(dob.slice(6,10)) > year) {
					$('input[name$="dob"]').addClass('errorBorder');
					$('input[name$="dob"]').siblings("span").addClass('error');
					errorCount++;
				}
				
				//Check Age
				//alert($.cookies.get('age'));
				if(!neo.checkAge(parseInt(dob.slice(0,2)), parseInt(dob.slice(3,5)), parseInt(dob.slice(6,10))) || $.cookies.get('age') == false){
					$('input[name$="dob"]').addClass('errorBorder');
					$('input[name$="dob"]').siblings("span").addClass('error').text('You must be 13 years of age or older.');
					$.cookies.set('age', 'false');
					errorCount++;
				}

			}
					
			// validate email address
			var email = $.trim($('input[name$="email"]').val());
			if (!email) {
				$('input[name$="email"]').addClass('errorBorder');
				$('input[name$="email"]').siblings("span").addClass('error')
					.text('Please enter your email address');
				errorCount++;
			} else if (!emailRegEx.test(email)) {
				$('input[name$="email"]').addClass('errorBorder');
				$('input[name$="email"]').siblings("span").addClass('error')
					.text('Incorrect email address');
				errorCount++;
			}
			
			// validate re-enter email address
			var email2 = $.trim($('input[name$="email2"]').val());
			if (!email2) {
				$('input[name$="email2"]').addClass('errorBorder');
				$('input[name$="email2"]').siblings("span").addClass('error')
					.text('Please re-enter your email address');
				errorCount++;
			} else if (!/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/.test(email)) {
				$('input[name$="email2"]').addClass('errorBorder');
				$('input[name$="email2"]').siblings("span").addClass('error')
					.text('Incorrect email address');
				errorCount++;
			}			
			
			// validate both email addresses
			if (email != email2) {
				$('input[name$="email"]').siblings("span").addClass('error');
				$('input[name$="email2"]').siblings("span").addClass('error')
					.text('Incorrect email address');
				errorCount++;
			}
			
			if (errorCount>0){	
				e.preventDefault();		
				return false;
			} else {	
				document.forms[0].submit();
				e.preventDefault();			
				//return true;
			}			 
		});
		
	},
	
	unsubscribe: function(){
		$('a#unsubSubmit').click(function(e)
		{
			var emailRegEx = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
			var errorCount=0;
			
			// validate re-enter email address
			var email = $.trim($('#unEmail').val());
			if (!email) {
				$('#unEmail').addClass('errorBorder');
				$('#unEmail').siblings("span").addClass('error')
					.text('Please enter your email address');
				errorCount++;
			} else if (!emailRegEx.test(email)) {
				$('#unEmail').addClass('errorBorder');
				$('#unEmail').siblings("span").addClass('error')
					.text('Incorrect email address');
				errorCount++;
			}
			
			if(errorCount == 0){
				document.forms[0].submit();
				e.preventDefault();
			}
			
		});
	},
	checkAge: function(month, day, year)
	{
		/* the minumum age you want to allow in */
		var min_age = 13;

		var theirDate = new Date((year + min_age), month, day);
		var today = new Date;
		//alert((today.getTime() - theirDate.getTime()));
		if ( (today.getTime() - theirDate.getTime()) < 0) {
			//alert("You are too young to enter this site!");
			return false;
		}
		else {
			return true;
		}
	},
	changeHero: function(){
		if ($('body').hasClass('home')) {
			var count = $('#slideInner .hero').length + 1;
			if ($('#slideInner').hasClass('hover')){
				
			} else {
				var id = parseInt($('.hero.active').attr('id').substring(4)) + 1;
				if (id == count) {
					id = 1;
				}	
				$('.hero').hide().removeClass('active');
				$('#pager a').removeClass('active');
				$('#pager a[rel=' + id + ']').addClass('active');
				if(brow == 'ie7' || brow == 'ie8'){
					$('#hero' + id).show().addClass('active');
				}else{
					$('#hero' + id).fadeIn(500).addClass('active');
				}
				
			}
		} 
	}
}
