var defaultQuestion = 'e.g. What are taxes like? How new is the carpet?';
var defaultEmail = 'e.g. jimmy@yahoo.com';

$(document).ready(function() {
	ProAdMap.load();

	if(typeof(ProAdEmailAFriendLightbox) != 'undefined') {
		var emailFriendLightbox = new ProAdEmailAFriendLightbox();
	}

	if(typeof(ProAdAskQuestionStandard) != 'undefined') {
		var askQuestionStandard = new ProAdAskQuestionStandard();
	}

	$(window).resize(function() {
		if(typeof(ProAdMap.map) != 'undefined') {
			ProAdMap.map.checkResize();
			ProAdMap.recenterMap();
		}
	});
	
	//Email a Friend Lightbox
	$(".emailToAFriend").click(function(){
			emailFriendLightbox.resetForm();
			$("#lightboxBg").addClass('show');
		  $("#lightboxEmail").addClass('show');
			return false;
	});
	
	$(".lightboxCloseEmailAFriend").click(function(){
			$("#lightboxBg").removeClass('show');
		  $("#lightboxEmail").removeClass('show');
			return false;
	});
	
	$("#lightboxEmailAFriendForm").submit(function(){
			//small hack to remove old error message (reset it basically)
			var targetElement = $("div.lightboxEmailFriendResultBox").removeClass();
			targetElement.addClass("lightboxEmailFriendResultBox");  //Why I wanted to us ID's and not classes.  die ie6
			var userEmail   = $("input[name=eafEmail]").val();
			var userName    = $("input[name=eafName]").val();
			var friendEmail = $("input[name=eafFriendEmail]").val();
			var friendName  = $("input[name=eafFriendName]").val();
			var message     = $("#eafMessage").val();
			var proAdID   = $("input[name=proAdID]").val();
			var error = false;
			//error checks

			if(!isEmail(userEmail)){emailFriendLightbox.toggleErrorBox('Show', 'InvalidEmail'); error=true;}
			if(!isEmail(friendEmail)){emailFriendLightbox.toggleErrorBox('Show', 'InvalidEmail'); error=true;}
			if(userName == ''){emailFriendLightbox.toggleErrorBox('Show', 'MissingField'); error=true;}
			if(friendName == ''){emailFriendLightbox.toggleErrorBox('Show', 'MissingField'); error=true;}
			if(error){ return false; } //kill submit if error occurs
			
			emailFriendLightbox.sendMessage(userEmail, userName, friendEmail, friendName, message, proAdID);

			return false;
	});

	// Ask Question standard
	$("#askQuestionStandardForm").submit(function(){
		//remove all past error messages
		var targetElement = $("ul.askQuestionStandardResultBox").removeClass();
		targetElement.addClass("askQuestionStandardResultBox");  //Why I wanted to us IDs and not classes.  die ie6
		var proAdID = $("input[name=proAdID]").val();  //requires listingID to be hidden field, bad structural - nonModular
		var email = $("#askQuestionStandardEmail").val();
		validEmail = isEmail(email);
		var charCount = $("#askQuestionStandardTextArea").val().length;
		var enteredQuestion = $("#askQuestionStandardTextArea").val();
		var errorFound = false;

		if(!validEmail){ askQuestionStandard.toggleErrorBox('Show', 'InvalidEmail'); errorFound = true;}
		if(charCount == 0 || enteredQuestion == defaultQuestion){ askQuestionStandard.toggleErrorBox('Show', 'InvalidQuestion'); errorFound = true; }
		if(charCount > 500){ askQuestionStandard.toggleErrorBox('Show', 'InvalidQuestionLength'); errorFound = true; }
		if(errorFound){return false; } //if error return false to stop form submission
		//if no error send mail
		var success = askQuestionStandard.sendMessage(enteredQuestion, email, proAdID);
		return false; //false stops the submit because form was sent via ajax
	});

	if($("#askQuestionStandardEmail").val() != defaultEmail) {
		$("#askQuestionStandardEmail").addClass("active");
	}

	$("#askQuestionStandardEmail").focus(function() {
		if($(this).val() == defaultEmail) {
			$(this).addClass("active");
			$(this).val("");
		}
	});

	$("#askQuestionStandardEmail").blur(function() {
		if($(this).val() == "") {
			$(this).removeClass("active")
			$(this).val(defaultEmail);
		}
	});

	if($("#askQuestionStandardTextArea").val() != defaultQuestion) {
		$("#askQuestionStandardTextArea").addClass("active");
	}

	$("#askQuestionStandardTextArea").focus(function() {
		if($(this).val() == defaultQuestion) { 
			$(this).addClass("active");
			$(this).val('');
		}
	});

	$("#askQuestionStandardTextArea").blur(function() {
		if($(this).val() == '') {
			$(this).removeClass("active");
			$(this).val(defaultQuestion);
		}
	});

	$("#askQuestionStandardTextArea").keyup(function(){
			updateWordCount('#askQuestionStandardTextArea','#askQuestionStandardCharCount');
	});

	if(typeof(proAdID) != 'undefined' && proAdID != '') {
		$.ajax({
			url: '/proAd/proAdHits.php',
			type: 'POST',
			data: 'page=profile&type=impression&proAdIDs='+proAdID,
			dataType: 'html',
			success: function() {
			},
			error: function(data, textStatus) {
			}
		});
		if (typeof(hitSource) != 'undefined' && hitSource != '') {
			$.ajax({
				url: '/proAd/proAdHits.php',
				type: 'POST',
				data: 'page='+hitSource+'&type=hit&proAdIDs='+proAdID,
				dataType: 'html',
				success: function() {
				},
				error: function(data, textStatus) {
				}
			});
		}
	}

	$('a.proAdExtLink').click(function() {
		if(typeof(proAdID) != 'undefined' && proAdID != '') {
			$.ajax({
				url: '/proAd/proAdHits.php',
				type: 'POST',
				data: 'page=profile&type=urlclick&proAdIDs='+proAdID,
				dataType: 'html',
				success: function() {
					return true;
				},
				error: function(data, textStatus) {
					return true;
				}
			});
		}
		return false;
	});
});

function updateWordCount(textArea, textCounter){
	var maxChars = 500;
	//var count = maxChars - $("#lightBoxAskQuestionTextArea").val().length;  //calculate # chars left
	var count = maxChars - $(textArea).val().length;  //calculate # chars left
	//Check word count and [do]Error messages
  
	if(count < 0  ){
		//$("#askQuestionCharCount").addClass("OverLimit");
		$(textCounter).addClass("OverLimit");
		$(textCounter).removeClass("UnderLimit");
		//toggleErrorBox('Show', 'InvalidQuestionLength');
	} else if (count >= 0 ){
		$(textCounter).addClass("UnderLimit");
		$(textCounter).removeClass("OverLimit");
		//toggleErrorBox('Hide', 'InvalidQuestionLength');
	}
	//Update character message with each keyUp
	$(textCounter).text(count.toString());
}