$(function(){

	if($('#form-newsletter').length){
		$('#form-newsletter').bind('submit', function(){
			if(! isEmail($('#email').val()))  {
				alert('Enter valid E-mail');
			}
			else
				return true ;
			return false;
		});
	}

	if($('#form-tell-a-friend').length){ // check emails
		$('#form-tell-a-friend').bind('submit', function(){
			if(!isEmail($('#email1').val()) ){
								alert('We’re sorry. The email address that you provided was incomplete. Please check the address and try again.');
								return false;
			}
			var em = new Array($('#email1').val(), $('#email2').val(), $('#email3').val(), $('#email4').val(), $('#email5').val());
			var emu = new Array();
			for(i=0;i<em.length;i++){
				if(em[i].length>0){
					if(!isEmail(em[i])){ // Check email
						alert('We’re sorry. The email address that you provided was incomplete. Please check the address and try again.');
						return false;
					}
					for(j=0;j<emu.length;j++){  // find dublicate
						if(em[i]==emu[j]){
							alert('We’re sorry, you’ve entered your friend’s email address twice. Please try again.');
							return false;
						}
					}
					emu[i]=em[i];
				}
			}

			return  true;
		});
	}

	if ($('#promo-data-ok').length) { /* promo-enter.php */
		var promoVal = $('#promo-data-ok').val();
		if (promoVal == 1) {
			alert( 'You have been successfully entered in the Win-a-Chef sweepstakes.' );
		}
		else if (promoVal == 0) {
			alert( 'Thank you for entering the Sea Cuisine™ Win-a-Chef Sweepstakes. We’re sorry but you already entered our sweepstakes. You can earn additional Sweepstakes entries by recommending Sea Cuisine to a friend. For each valid email address you submit, you will automatically receive one (1) Sweepstakes entry.' );
		}
		else if (promoVal == -1) {
			alert( 'Thank you for entering the Sea Cuisine™ Win-a-Chef Sweepstakes. We’re sorry but you’ve already reached the maximum of (50) entries. But, please feel free to tell as many more friends as you would like. Good luck!' );
		}
	}


	if ($('#tell-a-friend').length) { /* promo-enter.php tell-a-friend form  */
		//alert( $('#tell-a-friend').val() );
		switch($('#tell-a-friend').val()){
			case 'err1':
				alert( 'We’re sorry. The email address that you provided was incomplete. Please check the address and try again.' );
				break;

			case 'errD':
				alert( 'We’re sorry. The email address that you provided doesn’t match.' );
				break;

			case 'errF':
				alert( 'We’re sorry, you’ve entered your friend’s email address twice. Please try again!' );
				break;

			case 'errE':
				alert( 'Thank you for telling your friends about the Sea Cuisine™ Win-a-Chef Sweepstakes. We’re sorry but you’ve already reached the maximum of (50) entries. But, please feel free to tell as many more friends as you would like. Good luck!' );
				break;

			case 'mes2':
				alert( 'Thank you for telling your friends about the Sea Cuisine™ Win-a-Chef Sweepstakes. You have received one additional entry for each friend you’ve told. Good luck!' );
				break;
		}
	}

	if ($('#contact-data-ok').length) { /* contact-us.php */
		alert( 'Your message was successfully sent.' );
	}
	if ($('#save_newsletter_db_ok').length) { /* newsletter.php set for home.php, talk.php, promotions.php  */
		if($('#save_newsletter_db_ok').val()==1)
			alert( 'You have successfully registered to receive the Talk to the Chef Newsletter.' );
		else
			alert( 'You are already registered to receive the Talk to the Chef newsletter' );

	}

	if ($('#pmtr-variants').length) {
		var currentRate = $('#pmtr-variants input:checked').val();
		for (var i = 1, newRate = ''; i <= 5; i++) {
			newRate += '<span class="fish' + (currentRate >= i ? ' selected' : '') + '" id="fish' + i + '"></span>';
		}
		$('#pmtr-variants')
			.html('<input type="hidden" name="rate" id="rateAll" value="' + ( currentRate != undefined ? currentRate : 0) + '" />' + newRate)
			.find('span').click(function(){
				var i = $(this).attr('id').replace(/fish/, '');
				$('#rateAll').val(i);
				for (var j = 1; j <= 5; j++) {
					with ($('#fish' + j)) (j <= i) ? addClass('selected') : removeClass('selected');
				}
			})
		;
	}

	var maxWidth = 0;
	$('.top-links ul ul li').each(function(){
		if ($(this).width() > maxWidth) {
			maxWidth = $(this).width();
		}
	});
	$('.top-links ul ul').css({display: "none"}); // Opera Fix
	$('.top-links ul ul').width(maxWidth);
	$('.top-links ul ul li').width(maxWidth);
	$('.top-links ul li').hover(function(){
			$(this).find('ul:first').css({visibility: "visible", display: "none"}).show(200);
			$(this).addClass('selected');
		},
		function(){
			$(this).find('ul:first').css({visibility: "hidden"});
			$(this).removeClass('selected');
		}
	);

	if ($('#rc-recipes').length) {
		$('.rtFishes').each(function(){
			var currentRate = $(this).children('input:checked').val();
			for (var i = 1, newRate = ''; i <= 5; i++) {
				newRate += '<span class="fish' + (currentRate >= i ? ' selected' : '') + '">' + i + '</span>';
			}
			$(this)
				.html('<input type="hidden" name="rate" value="' + (currentRate != undefined ? currentRate : 0) + '" />' + newRate)
				.find('span').click(function(){
					var rate = $(this).text();
					$(this).parent().children('input').val(rate);
					for (var j = 1; j <= 5; j++) {
						with($(this).parent().children('span:eq(' + (j - 1) + ')')) {
							(j <= rate) ? addClass('selected') : removeClass('selected');
						}
					}
				})
			;
		});
		$('#rc-recipes a.selectRecipe').click(function(){
			$('.recipeHidden').removeClass('recipeVisible');
			$(this).next().addClass('recipeVisible');
			return false;
		});
	}

	if ($('#rc-ingradients').length) {
		$('#rc-ingradients li a.ingr').click(function(){
			var action = $('#ingrForm').attr('action');
			var id = $(this).attr('id').replace(/ingr/, '');
			if ($(this).hasClass('selected')) {
				$(this).removeClass('selected');
				var needle = '&ingrs[]=' + id;
				var length = needle.length;
				var start = action.indexOf(needle);
				var firstPart = action.substr(0, start);
				var secondPart = action.substr(start + length);
				action = firstPart + secondPart;
			}
			else {
				$(this).addClass('selected');
				action += '&ingrs[]=' + id;
			}
			$('#ingrForm').attr('action', action);
			return false;
		});
	}

	$('a[rel=box]').each(function(){
		$(this).fancybox();
	});

	$('a[rel=recipe]').each(function(){
		$(this).fancybox({
			frameWidth : 850,
			frameHeight : 475
		});
	});

	$('a[rel=locator]').each(function(){
		$(this).fancybox({
			frameWidth : 659,
			frameHeight: 429
		});
	});

	$('a[rel=boxFriend]').each(function(){
		$(this).fancybox({
			frameWidth: 354,
			frameHeight: 396,
			hideOnContentClick: false
		});
	});

	$('a[rel=boxTalk]').each(function(){
		$(this).fancybox({
			frameWidth: 310 ,
			frameHeight: 420,
			hideOnContentClick: false
		});
	});

	$('area.topRecipeBox').each(function(){
		$(this).fancybox({
			frameWidth: 850,
			frameHeight: 475
		});
	});

	if ($('#friends_email_sent').length) {
		alert( 'Your email was successfully sent.' );
	}

	if ($('#contact_feedback_sent').length) {
		alert( 'Your message was successfully sent.' );
	}

	if ($('#chefEmail').length) {
		$('#chefEmail').bind('submit', function(){
			if (!isEmail($('#textfield2').val())) {
				alert('Incorrect E-mail, please check it.');
				return false;
			}
		});
	}

	if ($('#promoEnter').length) {
		$('#promoEnter').bind('submit', function(){
			if (!isEmail($('#email').val())) {
				alert('Incorrect E-mail, please check it.');
				return false;
			}
		});
	}

	if ($('#selectInputId').length) {
		$('#item' + $('#selectInputId').val())[0].scrollIntoView(document.documentElement);
	}

	if ($('#upcHelp').length) {
		$('#upcHelp').click(function(){
			if ($('span', this).length) {
				$('#upcHelp span').remove();
			}
			else {
				$('#upcHelp').append('<span>Here are the UPC numbers from a 24 oz (48 count carton):<br />4160066930</span>');
			}
		});
	}

});

function isEmail($addr){
	return $addr.match(/^[a-z\d_.-]+@(?:[a-z\d][a-z\d-]*[a-z\d]\.|[a-z\d]\.)+[a-z]{2,6}$/);
}
