
var fnCreatePassword = null;
var fnRedirectToSignIn = null;

isNumeric = function(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

var fnCheckEmail = function(strEmail) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(strEmail)){
		return true;
	}
	return false;
}

var strCurrentURL = document.URL;
if (strCurrentURL.indexOf('#') > -1) {
	strCurrentURL = strCurrentURL.substring(0, strCurrentURL.indexOf('#'));
}

$(function()
{
	// MAIN NAVIGATION
	
	$("li.WhatsNew a,.NavBarSubWhatsNew").hover (
		function(){
			$(".NavBarSubWhatsNew").show();
			$("li.WhatsNew a").css("background-position","0 -704px");
			$(".NavBarSubWhatsNew").css("box-shadow","-2px 2px 1px #cccccc");
			$(".NavBarSubWhatsNew").css("-moz-box-shadow","-2px 2px 1px #cccccc");
			$(".NavBarSubWhatsNew").css("-webkit-box-shadow","-2px 2px 1px #cccccc");
		},
		function(){
			$(".NavBarSubWhatsNew").hide();
			if (!$("li.WhatsNew a").hasClass("current")) {
				$("li.WhatsNew a").css("background-position","0 -792px");
			}
			$(".NavBarSubWhatsNew").css("box-shadow","-2px 2px 1px #cccccc");
			$(".NavBarSubWhatsNew").css("-moz-box-shadow","-2px 2px 1px #cccccc");
			$(".NavBarSubWhatsNew").css("-webkit-box-shadow","-2px 2px 1px #cccccc");
		}
	);
	$("li.HowTos a,.NavBarSubHowTos").hover (
		function(){
			$(".NavBarSubHowTos").show();
			$("li.HowTos a").css("background-position","0 -176px");
			$(".NavBarSubHowTos").css("box-shadow","-2px 2px 1px #cccccc");
			$(".NavBarSubHowTos").css("-moz-box-shadow","-2px 2px 1px #cccccc");
			$(".NavBarSubHowTos").css("-webkit-box-shadow","-2px 2px 1px #cccccc");
		},
		function(){
			$(".NavBarSubHowTos").hide();
			if (!$("li.HowTos a").hasClass("current")) {
				$("li.HowTos a").css("background-position","0 -264px");
			}
			$(".NavBarSubHowTos").css("box-shadow","-2px 2px 1px #cccccc");
			$(".NavBarSubHowTos").css("-moz-box-shadow","-2px 2px 1px #cccccc");
			$(".NavBarSubHowTos").css("-webkit-box-shadow","-2px 2px 1px #cccccc");
		}
	);
	$("li.SignedIn,.NavBarSubSignIn").hover (
		function(){
			$(".NavBarSubSignIn").show();
			$(".NavBarSubSignIn").css("box-shadow","-2px 2px 1px #cccccc");
			$(".NavBarSubSignIn").css("-moz-box-shadow","-2px 2px 1px #cccccc");
			$(".NavBarSubSignIn").css("-webkit-box-shadow","-2px 2px 1px #cccccc");
		},
		function(){
			$(".NavBarSubSignIn").hide();
			$(".NavBarSubSignIn").css("box-shadow","-2px 2px 1px #cccccc");
			$(".NavBarSubSignIn").css("-moz-box-shadow","-2px 2px 1px #cccccc");
			$(".NavBarSubSignIn").css("-webkit-box-shadow","-2px 2px 1px #cccccc");
		}
	);
	
	// don't process the rest of this if it's a 3rd party site
	if (window.location.hostname.indexOf("www.cookingwithkraft.com") != -1 || window.location.hostname.indexOf("upshotstaging.net") != -1) {
		
		// CONTACT US FORM
		
		function displayContactUs() {
			$.ajax({
				url: '/ajax/contact-us-form',
				success: function (data) {
					Shadowbox.open({
					content: data,
					player: 'html',
					height: 375,
					width: 270,
					options: {
						onFinish: function() {
							
							// submit contact us form
							
							$('#frmContactUs #SubmitButton').click(function() {
							
									var arrErrors = new Array();
									
									var strName = $('#sb-container #Name').val();
									strName = jQuery.trim(strName);
									var strEmail = $('#sb-container #Email').val();
									strEmail = jQuery.trim(strEmail);
									var strQuestion = $('#sb-container #Question').val();
									strQuestion = jQuery.trim(strQuestion);
									
									if (strName.length < 1) {
										arrErrors.push('You must provide your Name.');
									}
									var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
									if (strEmail.length < 1) {
										arrErrors.push('You must provide your Email Address.');
									} else if(!emailReg.test(strEmail)) {
										arrErrors.push('You must provide a valid Email Address.');
									}
									if (strQuestion.length < 4) {
										arrErrors.push('You must provide a Question.');
									}
									if (arrErrors.length > 0) {
										var strErrors = "Please correct the following error";
										if (arrErrors.length > 1) { strErrors += "s"; }
										strErrors += ":\n";
										for (var i = 0; i < arrErrors.length; i++) {
											strErrors += "\n" + arrErrors[i];
										}
										alert(strErrors);
									} else {
										$.ajax({
											url: '/ajax/contact-us-send',
											type: 'post',
											data: 'Name=' + strName + '&Email=' + strEmail + '&Question=' + strQuestion,
											success: function(data) {
												var arrData = data.split('||');
												if (arrData[0].toLowerCase() == 'success') {
													// SUCCESS - show confirmation page
													$('.modalHeader .sub').hide();
													$('#sb-player > div').height(170);
													$('#divContactUsForm').html("<div style=\"margin:12px; text-align:center;\">Thank you! Your question has been submitted.</div>");
													Shadowbox.skin.dynamicResize(270, 170);
												} else {
													var arrErrors = arrData[1].split('--');
													var strOutput = "Please correct the following error";
													if (arrErrors.length > 1) {
														strOutput += "s";
													}
													strOutput += ":\n";
													for (var i = 0; i < arrErrors.length; i++) {
														strOutput += "\n" + arrErrors[i];
													}
													alert(strOutput);
												}
											}
										});
									}
								return false;
							});
							
						}
					}
					});
				}
			});
		}
		
		// WRITE REVIEW FORM
		
		function displayReviewForm() {
			$.ajax({
				url: '/ajax/logged-in',
				success: function(data) {
					if (parseInt(data) > 0) {
						if ($('#review').html().length < 1) {
							$('#review').append('<div class="WriteReviewForm"></div>');
							var strURL = '/ajax/review-form/' + intEntryID;
							$.ajax({
								url: strURL,
								success: function(data) {
									$('#review .WriteReviewForm').html(data);
									$('#review').toggle(400);
									$('#review .WriteReviewForm .star').rating();
									$('#new_rating').attr('action', strCurrentURL);
									$('#comment').keyup(function(){
										var strComment = $(this).val();
										strComment = jQuery.trim(strComment);
										var intFieldLength = strComment.length;
										var strNotes = 'You have '+ (250 - intFieldLength) +' characters left.';
										if (intFieldLength > 250) {
											strNotes = 'You cannot write more then 250 characters!';
											$(this).val(strComment.substr(0,250));
											return false;
										}
										$('#comment-notes').html(strNotes);
									});
									$('#comment_form input[name=submit]').click(function() {
										var arrErrors = new Array();
										
										if (typeof $("#comment_form input[name='rating[default]']:checked").val() == 'undefined') {
											arrErrors.push('You must provide a Rating.');
										}
										
										var strComment = $('#review #comment').val();
										strComment = jQuery.trim(strComment);
										
										if (arrErrors.length > 0) {
											var strErrors = "Please correct the following error";
											if (arrErrors.length > 1) { strErrors += "s"; }
											strErrors += ":\n";
											for (var i = 0; i < arrErrors.length; i++) {
												strErrors += "\n" + arrErrors[i];
											}
											alert(strErrors);
										} else {
											if (strComment.length < 1) {
												// plain rating submit
												var intN = $("#comment_form input[name='rating[default]']:checked").val();
												intN = jQuery.trim(intN);
												$('form[name="default"]').append('<input type="hidden" name="rating[default]" value="' + intN + '" />');
												$('form[name="default"]').attr('action', strCurrentURL);
												$('form[name="default"]').submit();
											} else {
												// comment+rating submit
												if ($('#cbAnonymous').is(':checked')) {
													// save anonymous value
													$.ajax({
														url: '/ajax/anonymous-comment',
														data: 'entry_id=' + intEntryID + '&member_id=' + intMemberID,
														type: 'post',
														success: function(data) {
															var arrData = data.split('||');
															if (arrData[0].toLowerCase() == 'success') {
																$('#comment_form input[name=RET]').val(strCurrentURL+'#WriteReview');
																$('#comment_form').submit();
															} else {
																var arrErrors = arrData[1].split('--');
																var strOutput = "Please correct the following error";
																if (arrErrors.length > 1) {
																	strOutput += "s";
																}
																strOutput += ":\n";
																for (var i = 0; i < arrErrors.length; i++) {
																	strOutput += "\n" + arrErrors[i];
																}
																alert(strOutput);
															}
														}
													});
												} else {
													$('#comment_form input[name=RET]').val(strCurrentURL+'#WriteReview');
													$('#comment_form').submit();
												}
											}
										}
															
										return false;
									});
								}
							});
						} else {
							$('#review').toggle(400);
						}
					} else {
						fnRedirectToSignIn();;
					}
				}
			});
		}
		
		// CREATE PASSWORD FORM
		
		fnCreatePassword = function() {
			$.ajax({
				url: '/ajax/logged-in',
				success: function(data) {
					if (parseInt(data) < 1) {
						$.ajax({
							url: '/ajax/create-password-form',
							success: function (data) {
								Shadowbox.open({
									content: data,
									player: 'html',
									height: 185,
									width: 270
								});
							}
						});
					} else {
						document.location = '/your-account';
					}
				}
			});
		}
		
		fnRedirectToSignIn = function() {
			$.ajax({
				url: '/ajax/set-previous-page',
				success: function() {
					document.location = '/sign-in';
				}
			});
		}
		
		// Contact Us
		$('.contact-us').click(function() {
			displayContactUs();
			return false;
		});
		if (document.URL.indexOf("#ContactUs") > -1) {
			displayContactUs();
		}
		
		// Sign In / Sign Up Redirects
		
		$('.sign-in, .sign-up').click(function() {
			fnRedirectToSignIn();
			return false;
		});
		if (document.URL.indexOf("#SignIn") > -1 || document.URL.indexOf("#SignUp") > -1) {
			fnRedirectToSignIn();
		}
		
		// Reviews
		
		$('.rate-this').click(function() {
			var p = $('#divReview').offset();
			window.scrollTo(p.left,p.top);
			displayReviewForm();
			return false;
		});
		
		$(".review-toggle").click(function() {
			displayReviewForm();
			return false;
		});
	
		// THUMBNAIL ROLLOVERS
		
		$('.recipe-thumb').hover(
			function() { var el = $(this).find('.recipe-info'); el.animate({'top': ('-=' + el.height()) }, 'fast'); },
			function() { var el = $(this).find('.recipe-info'); el.animate({'top': ('+=' + el.height()) }, 'fast'); }
		);
		
		$('.video-thumb').hover(
			function() { var el = $(this).find('.video-info'); el.animate({'top': ('-=' + el.height()) }, 'fast'); },
			function() { var el = $(this).find('.video-info'); el.animate({'top': ('+=' + el.height()) }, 'fast'); }
		);
		
		// CSS "HACKS"
		
		// coupon thumbnails - vertical align
		
		$.each($('.CouponPicture img, .CouponThumbnail img'), function() {
			intNewMargin = (($(this).closest('div').height() - $(this).height()) / 2) - 10;
			if (intNewMargin > 0) {
				$(this).css('margin-top', intNewMargin);
			}
		});
	}
	
});

if (window.location.hostname.indexOf("www.cookingwithkraft.com") != -1 || window.location.hostname.indexOf("upshotstaging.net") != -1) {
	
	Shadowbox.init({
		language: 'en',
		players: ['img', 'html', 'iframe', 'qt', 'wmp', 'swf', 'flv'],
		enableKeys: false,
		modal: false
	});

}
