var imagePreload = new Array();

$(document).ready(function () {
	var defaultQuestion = 'e.g. What are taxes like? How new is the carpet?';
	var defaultEmail = 'e.g. jimmy@yahoo.com';
	/* BEGIN GWO CONTACT OWNER */
	var altDefaultQuestion = 'Your Question (ie. How much are taxes? Is the carpet new?)';
	var altDefaultEmail = 'Your E-mail Address';
	/* END GWO CONTACT OWNER */
	if(typeof(AskQuestionLightBox) != 'undefined') {
		var askQuestionLightBox = new AskQuestionLightBox();
	}
	if(typeof(AskQuestionStandard) != 'undefined') {
		var askQuestionStandard = new AskQuestionStandard();
	}
	if(typeof(EmailAFriendLightbox) != 'undefined') {
		var emailFriendLightbox = new EmailAFriendLightbox();
	}
	if(typeof(FlagListingLightbox) != 'undefined') {
		var flagListingLightbox = new FlagListingLightbox();
	}

	if (photoArray.length>1) for (var i=0;i<photoArray.length;i++){
		imagePreload[i]=new Image();
		imagePreload[i].src=photoArray[i];
	}

	$("#mediaTabs .photos").click(function(){
			//we refuse focus on photos if there are no photos AND there is a map
			if($(this).hasClass('grey') && !$("#mediaTabs .map").hasClass('grey')){
				return false;
			}

			$('#mediaWidgetWrap').css('background-image', 'url(/images/fsboListing/bgShadMedia.gif)');

			//close open house lightbox, if it is open
			$("#openHouseLightboxClose").click();

			//handle content display
			$("#listingMainImageContainer").addClass('show');
			$("#mapMode").removeClass('show');
			$(".videoMode").removeClass('show');
			//toggle nav selection
			$("#mediaTabs .active").removeClass('active');  //remove active nav item
			$(this).toggleClass('active');  //activate new nav item
			return false;
	});
	
	$("#mediaTabs .map").click(function(){
			if((!ListingMap.lat && !ListingMap.lng) || ListingMap.accuracy < ListingMap.minAccuracy) {
				return;
			}

			if(!$('#mapMode').hasClass('noPhotos')) { 
				$('#mediaWidgetWrap').css('background-image', 'url(/images/fsboListing/bgShadMedia381.gif)'); 
			} 
			else { 
				$('#mediaWidgetWrap').css('background-image', 'url(/images/fsboListing/bgShadMedia.gif)'); 
			}
			//$('#mediaWidgetWrap').css('background-image', 'url(/images/fsboListing/bgShadMedia285.gif)');

			//close open house lightbox, if it is open
			$("#openHouseLightboxClose").click();

			//handle content display
			$("#mapMode").addClass('show');
			$("#listingMainImageContainer").removeClass('show');
			$(".videoMode").removeClass('show');
			//toggle nav selection
			$("#mediaTabs .active").removeClass('active');  //remove active nav item
			$(this).toggleClass('active');  //activate new nav item
			ListingMap.mapListing();
			return false;
	});
	
	$('#mapLink').click(function() {
			$('#mediaTabs .map').click();
	});
		
	$("#mediaTabs .video").click(function(){
		if($(this).hasClass('grey')){ 
			return false;
		}

		$('#mediaWidgetWrap').css('background-image', 'url(/images/fsboListing/bgShadVideo.gif)');

		//close open house lightbox, if it is open
		$("#openHouseLightboxClose").click();

		//handle content display
		$(".videoMode").addClass('show');
		$("#listingMainImageContainer").removeClass('show');
		$("#mapMode").removeClass('show');
		//toggle nav selection
		$("#mediaTabs .active").removeClass('active');  //remove active nav item
		$(this).toggleClass('active');  //activate new nav item
		return false;
	});
	
	$("#mediaTabs .slide").click(function(){
		if($(this).hasClass('grey')){ 
			return false;
		}

		//close open house lightbox, if it is open
		$("#openHouseLightboxClose").click();

		window.open($('#mediaTabs .slide a').attr("href"), 'SlideShow','left=0,top=0,toolbar=no,menubar=no,scrollbars=yes,resizable=no,width=450,height=465');
		return false;
	});
	
	$("#mediaTabs .virtual").click(function(){
		if($(this).hasClass('grey')){ 
			return false;
		}

		//close open house lightbox, if it is open
		$("#openHouseLightboxClose").click();

		//handle content display
		window.open($('#mediaTabs .virtual a').attr("href"), 'VirtualTour','left=0,top=0,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,width=450,height=465');
		return false;
	});

	$("#mediaWidgetWrap .thumb").click(function() {
		var id = $(this).attr('id');
		var imageNum = id.substring(id.search(/thumblink/)+9);

		$('#listingPhoto').attr("src", photoArray[imageNum-1]);
		$('.thumb').removeClass("on");
		$('#thumblink' + imageNum).addClass("on");
		return false;
	});

	if($('#mediaTabs .photos').hasClass('grey') && $("#mediaTabs .video").hasClass('grey')) {
		$('#infoWrap').addClass('noPhotos');
		$('#mapMode').addClass('noPhotos');
	}

	if((!ListingMap.lat && !ListingMap.lng) || ListingMap.accuracy < ListingMap.minAccuracy) {
		$('#mapLinks').html('');

		//If the map tab is active, this means that there were no photos
		if($('#mediaTabs .map').hasClass("active")) {
			//So, if there is a video default to it, else just default to the photo tab
			//which should be displaying a Photo Pending image
			if(!$('#mediaTabs .video').hasClass('grey')) {
				$('#mediaTabs .video').click();
			} else {
				$('#mediaTabs .photos').click();
			}
		}
	} else {
		ListingMap.load();
		$('#mediaTabs .map').removeClass("grey");
		if($('#mediaTabs .map').hasClass("active")) {
			$('#mediaTabs .map').click();
		}
	}

	$("#soldZip").click(function () {
		$(this).attr('value', '');
	});
	
	$('#syndEmbedCode').focus(function() {
		$(this).select();
	});

	$("#openHouseLightboxClose").click(function(){
			$(".overlay").addClass('hide');
			$(".floater").addClass('hide');
			return false;
	});

	//////////////////////////////
	//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 listingUrl  = $("input[name=url]").val();
			var listingID   = $("input[name=iLID]").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, listingUrl, listingID)
			
			return false;
	});

	//////////////////////////////
	// Flag Listing Lightbox
	//////////////////////////////
	$(".flagListing").click(function(){
			$("#lightboxBg").addClass('show');
		  $("#lightboxFlag").addClass('show');
			return false;
	});
	
	$(".lightboxCloseFlagListing").click(function(){
			$("#lightboxBg").removeClass('show');
		  $("#lightboxFlag").removeClass('show');
			flagListingLightbox.resetForm();
			return false;
	});
	
	$("#lightboxFlagListingForm").submit(function(){
			//small hack to remove old error message (reset it basically)
			var targetElement = $("div.lightboxFlagListingResultBox").removeClass();
			targetElement.addClass("lightboxFlagListingResultBox");  //Why I wanted to us ID's and not classes.  die ie6
			var flagReason  = $("input[name=flReason]:checked").val();
			var flagComment = $("#flComment").val();
			var listingUrl  = $("input[name=url]").val();
			var listingID   = $("input[name=iLID]").val();
			var error = false;
			//error checks
			if(!flagReason){
				flagListingLightbox.toggleErrorBox('Show', 'MissingReason'); error=true;
			}
			if(flagReason == 5 && !jQuery.trim(flagComment)){
				flagListingLightbox.toggleErrorBox('Show', 'CommentRequired'); error=true;
			}
			if(error){ return false; } //kill submit if error occurs
			flagListingLightbox.flagListing(listingID, flagReason, flagComment);			
			return false;
	});

	//////////////////////////////
	//Ask a question (LIGHTBOX)
	//////////////////////////////
	if($("#askQuestionTextArea").val() != defaultQuestion && $("#askQuestionTextArea").val() != altDefaultQuestion) {
		$("#askQuestionTextArea").addClass("active");
	}
	
	$("#askQuestionTextArea").focus(function() { 
		if($(this).val() == defaultQuestion) {
			$(this).addClass("active");
			$(this).val('');
		}
	});

	$("#askQuestionTextArea").blur(function() {
		if($(this).val() == '') {
			$(this).removeClass("active");
			$(this).val(defaultQuestion);
		}
	});
	
	$(".quickAskQuestion").click(function(){
		gaTrackPageView('/listing/' + listingURL + '/email1/form');
		var params = new Object();
		params.page = 'listing';
		params.action = 'email1';
		params.status = 'form';
		$("#lightboxBg").addClass('show');
		$("#lightboxAsk").addClass('show');
		//load input[value] from textarea
		var userQuestion = $("#askQuestionTextArea").val();
		if(userQuestion != defaultQuestion) {
			$("#lightBoxAskQuestionTextArea").val(userQuestion);
		}
		else {
			$("#lightBoxAskQuestionTextArea").val("");
		}
		updateWordCount('#lightBoxAskQuestionTextArea','#askQuestionCharCount');
		if($("#lightBoxAskQuestionTextArea").val() != "") {
			$("#askEmail").focus();
		}
		else {
			$("#lightBoxAskQuestionTextArea").focus();
		}
	});
	
	$("#lightBoxAskQuestionTextArea").keyup(function(){
			updateWordCount('#lightBoxAskQuestionTextArea','#askQuestionCharCount');
	});
	
	$("#lightBoxAskQuestionForm").submit(function(){
			//small hack to remove old error message (reset it basically)
			var targetElement = $("div.lightboxAskQuestionResultBox").removeClass();
			targetElement.addClass("lightboxAskQuestionResultBox");  //Why I wanted to us ID's and not classes.  die ie6
			//Build parameters
			var enteredQuestion = $("#lightBoxAskQuestionTextArea").val();
			var charCount = $("#lightBoxAskQuestionTextArea").val().length;
			var email = $("#askEmail").val();
			var validEmail = isEmail(email);
			var listingID = $("input[name=iLID]").val();
			var errorFound = false;
			
			if(!validEmail){askQuestionLightBox.toggleErrorBox('Show', 'InvalidEmail'); errorFound = true;}
			if(charCount == 0 || enteredQuestion == defaultQuestion){ askQuestionLightBox.toggleErrorBox('Show', 'InvalidQuestion'); errorFound = true; }
			if(charCount > 500){ askQuestionLightBox.toggleErrorBox('Show', 'InvalidQuestionLength'); errorFound = true; }
			if(errorFound){ return false; } //if error return false to stop form submission
			var SuccessMsg = askQuestionLightBox.sendMessage(enteredQuestion, email, listingID);
		return false; //false stops the submit because form was sent via ajax
		
	});

	$(".lightboxCloseAskQuestion").click(function(){
		var enteredQuestion = $("#lightBoxAskQuestionTextArea").val();
		askQuestionLightBox.resetForm();
		$("#lightboxBg").removeClass('show');
		$("#lightboxAsk").removeClass('show');
		if(enteredQuestion != '') {
			$("#askQuestionTextArea").addClass("active");
			$("#askQuestionTextArea").val(enteredQuestion);
		}
		else {
			$("#askQuestionTextArea").removeClass("active");
			$("#askQuestionTextArea").val(defaultQuestion);
		}
		return false;
	});

	/////////////////////////////
	// Ask Question non-lightbox
	/////////////////////////////
	$("#askQuestionStandardForm").submit(function(){
		//remove all past error messages
		var targetElement = $("ul.askQuestionStandardResultBox").removeClass();
		targetElement.addClass("askQuestionStandardResultBox");  //Why I wanted to us ID's and not classes.  die ie6
		var listingID = $("input[name=iLID]").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, listingID);
		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');
	});

	////////////////////
	// Calendar lightbox
	////////////////////
	$(".lightboxCloseAddCal").click(function(){
		$("#lightboxBg").removeClass('show');
		$(".lightboxCal").removeClass('show');
		return false;
	});

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

	$('a.proAdExtLink').click(function() {
		var proAdID = $(this).attr('rel');
		$.ajax({
			url: '/proAd/proAdHits.php',
			type: 'POST',
			data: 'page=listingSearch&type=urlclick&proAdIDs='+proAdID,
			dataType: 'html',
			success: function() {
				return true;
			},
			error: function(data, textStatus) {
				return true;
			}
		});
		return false;
	});

});

/**
* @PARAMS: textArea amd textCounter need to be qualified jQuery selectors
*/
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());
}


/*
 Show open house calendar links in lightbox, given open house ID
*/
function showAddCal(openHouseID) {
	$("#lightboxBg").addClass('show');
	$("#lightboxCal"+openHouseID).addClass('show');
}

/* BEGIN GWO CONTACT OWNER */
function bindQuickAskQuestion() {
	var defaultQuestion = 'e.g. What are taxes like? How new is the carpet?';
	$(".quickAskQuestion").click(function(){
		gaTrackPageView('/listing/' + listingURL + '/email1/form');
		var params = new Object();
		params.page = 'listing';
		params.action = 'email1';
		params.status = 'form';
		$("#lightboxBg").addClass('show');
		$("#lightboxAsk").addClass('show');
		//load input[value] from textarea
		var userQuestion = $("#askQuestionTextArea").val();
		if(userQuestion != defaultQuestion) {
			$("#lightBoxAskQuestionTextArea").val(userQuestion);
		}
		else {
			$("#lightBoxAskQuestionTextArea").val("");
		}
		updateWordCount('#lightBoxAskQuestionTextArea','#askQuestionCharCount');
		if($("#lightBoxAskQuestionTextArea").val() != "") {
			$("#askEmail").focus();
		}
		else {
			$("#lightBoxAskQuestionTextArea").focus();
		}
	});
}

function bindQuickAskQuestionInPage() {
	$(document).ready(function () {
		var altDefaultQuestion = 'Your Question (ie. How much are taxes? Is the carpet new?)';
		var altDefaultEmail = 'Your E-mail Address';
		if(typeof(QuickAskQuestionInPage) != 'undefined') {
			var quickAskQuestionInPage = new QuickAskQuestionInPage();
		}
		$("#askQuestionEmailArea").focus(function() { 
			if($(this).val() == altDefaultEmail) {
				$(this).addClass("active");
				$(this).val('');
			}
		});
		$("#askQuestionEmailArea").blur(function() {
			if($(this).val() == '') {
				$(this).removeClass("active");
				$(this).val(altDefaultEmail);
			}
		});
		$("#askQuestionTextArea").focus(function() { 
			if($(this).val() == altDefaultQuestion) {
				$(this).addClass("active");
				$(this).val('');
			}
		});
		$("#askQuestionTextArea").blur(function() {
			if($(this).val() == '') {
				$(this).removeClass("active");
				$(this).val(altDefaultQuestion);
			}
		});
		var listingID = $("input[name=iLID]").val();
		$("#emailListingID").val(listingID);
		$("#quickAskQuestionInPage").submit(function(){
			//remove all past error messages
			var targetElement = $("ul.quickAskQuestionResultBox").removeClass();
			targetElement.addClass("quickAskQuestionResultBox");  //Why I wanted to us ID's and not classes.  die ie6
			var listingID = $("#emailListingID").val();  //requires listingID to be hidden field, bad structural - nonModular
			var email = $("#askQuestionEmailArea").val();
			validEmail = isEmail(email);
			var charCount = $("#askQuestionTextArea").val().length;
			var enteredQuestion = $("#askQuestionTextArea").val();
			var errorFound = false;
			if(!validEmail){ quickAskQuestionInPage.toggleErrorBox('Show', 'InvalidEmail'); errorFound = true;}
			if(charCount == 0 || enteredQuestion == altDefaultQuestion){ quickAskQuestionInPage.toggleErrorBox('Show', 'InvalidQuestion'); errorFound = true; }
			if(charCount > 500){ quickAskQuestionInPage.toggleErrorBox('Show', 'InvalidQuestionLength'); errorFound = true; }
			if(errorFound){return false; } //if error return false to stop form submission
			//if no error send mail
			var success = quickAskQuestionInPage.sendMessage(enteredQuestion, email, listingID);
			return false; //false stops the submit because form was sent via ajax
		});
	});
}

function QuickAskQuestionInPage(){
	this.disableFormButtons = disableFormButtons;
	this.enableFormButtons = enableFormButtons;
	this.sendMessage = sendMessage;
	this.toggleErrorBox = toggleErrorBox;
	this.toggleSuccessBox = toggleSuccessBox;
	function sendMessage(emailBody, email, listingID){
		gaTrackPageView('/listing/' + listingURL + '/email1/send');
		var params = new Object();
		params.page = 'listing';
		params.action = 'email1';
		params.status = 'send';
		$.ajax({
				url: '/email-seller-process.php',
				type: 'POST',
				dataType: 'html',
				data: 'ajaxRequest=1&sendemail=1&body_email='+escape(emailBody)+'&szEmailAddress='+email+'&iLID='+listingID,
				timeout: 10000,
				error: function(){
						alert('A connection error occured... please try again.');
				},
				beforeSend: disableFormButtons(), 
				success:  function(data, textStatus){
					if(data == 1){
						gaTrackPageView('/listing/' + listingURL + '/email1/success');
						if (typeof window.gwoEmailSellerSuccess == 'function') {
							gwoEmailSellerSuccess();
						}
						if (typeof window.gwoContactOwnerSuccess == 'function') {
							gwoContactOwnerSuccess();
						}
						var params = new Object();
						params.page = 'listing';
						params.action = 'email1';
						params.status = 'success';
						toggleSuccessBox('Show', 'EmailSuccess');
						enableFormButtons();
					} else if(data == -1){
						gaTrackPageView('/listing/' + listingURL + '/email1/error');
						var params = new Object();
						params.page = 'listing';
						params.action = 'email1';
						params.status = 'error';
						toggleErrorBox('Show', 'EmailFailureSecurity');
						enableFormButtons();
					} else {
						gaTrackPageView('/listing/' + listingURL + '/email1/error');
						var params = new Object();
						params.page = 'listing';
						params.action = 'email1';
						params.status = 'error';
						toggleErrorBox('Show', 'EmailFailure');
						enableFormButtons();
					}
				}
		});  //end ajax handler
	}
	function disableFormButtons(){
		$("#askQuestionEmailArea").attr("disabled", "disabled");
		$("#askQuestionTextArea").attr("disabled", "disabled");
		$("#askQuestionSubmit").attr("disabled", "disabled");
	}
	function enableFormButtons(){
		$("#askQuestionEmailArea").attr("disabled", "");
		$("#askQuestionTextArea").attr("disabled", "");
		$("#askQuestionSubmit").attr("disabled", "");
	}
	function toggleErrorBox(toggleBox, msgHandler){
		if(toggleBox == 'Show'){
			 $("ul.quickAskQuestionResultBox").addClass('ShowError '+msgHandler);
		} else if(toggleBox == 'Hide'){
			 $("ul.quickAskQuestionResultBox").removeClass('ShowError '+msgHandler);
		}
	}
	function toggleSuccessBox(toggleBox, msgHandler){
		if(toggleBox == 'Show'){
			 $("ul.quickAskQuestionResultBox").addClass('ShowSuccess '+msgHandler);
		} else if(toggleBox == 'Hide'){
			 $("ul.quickAskQuestionResultBox").removeClass('ShowSuccess '+msgHandler);
		}
	}
}
/* END GWO CONTACT OWNER */
