// JavaScript Document

$(document).ready(function() {
	
	// FAQ interaction
	$(".q a").click(function() {
		var pWrap = $(this).parent();		   
		var ques = $(this).text();
		var ans = $(pWrap).next().text();
		$(".q").show();
		$(pWrap).hide();
		$("#currentQA").remove();
		$(document.createElement("div")).attr("id","currentQA").insertAfter(pWrap); 
		$(document.createElement("p")).attr("id","question").text(ques).appendTo("#currentQA"); 
		$(document.createElement("p")).attr("id","answer").text(ans).appendTo("#currentQA");
		$("#currentQA").fadeIn("slow");
		return false;
	});
	
	//check if the listing rate is set, and if so, if it's for an MLS package
	//the following code will then grab the list of counties and inject it after the states list
	//for use in determining if the address will be covered in an MLS region
	if($("#ListingRateId").length && ($("#ListingRateId").val() == 3 || $("#ListingRateId").val() == 6)) {
		$("#ListingStateId").change(function() {
			url = $.cake.http_root+'counties/list_by_state?state_id='+$("#ListingStateId").val();
			$.get(url, function(data) {
				if($("#ListingCountyId").length) {
					var county_div = $("#ListingCountyId").parent().parent();
					county_div.replaceWith(data);
				} else {
					var state_div = $("#ListingStateId").parent().parent();
					state_div.after(data);
				}
			});
		});
	}
	
	
	//striped table from "choose plan"
	$(".stripeMe tr").mouseover(function(){$(this).addClass("over");}).mouseout(function(){$(this).removeClass("over");});
    $(".stripeMe tr:even").addClass("alt");
	
    //forces a submit from the fake button on the top of the page, since it's outside of the form
	$("#butSubmitTop_add").click(function() {
		$("#ListingAddForm").submit();
	});
	
	//code for handling available characters in textbox fields
	$("#ListingHeadline").keyup(function() {
		limitChars('ListingHeadline', 60, 'headlineCount');
	});
	$("#ListingGeneralDetails").keyup(function() {
		limitChars('ListingGeneralDetails', 300, 'generalDetailsCount');
	});
	$("#ListingKitchenDetails").keyup(function() {
		limitChars('ListingKitchenDetails', 300, 'kitchenDetailsCount');
	});
	$("#ListingFamilyRoomDetails").keyup(function() {
		limitChars('ListingFamilyRoomDetails', 300, 'familyRoomDetailsCount');
	});
	$("#ListingDiningRoomDetails").keyup(function() {
		limitChars('ListingDiningRoomDetails', 300, 'diningRoomDetailsCount');
	});
	$("#ListingBedroomDetails").keyup(function() {
		limitChars('ListingBedroomDetails', 300, 'bedroomDetailsCount');
	});
	$("#ListingBasementDetails").keyup(function() {
		limitChars('ListingBasementDetails', 300, 'basementDetailsCount');
	});

});

//helper function for calculating remaining characters in the textbox fields
function limitChars(textid, limit, infodiv) {
	var text = $('#'+textid).val(); 
	var textlength = text.length;
	if(textlength > limit) {
		$('#'+infodiv).html((limit - textlength) +' characters left.');
		$('#'+textid).val(text.substr(0,limit));
		return false;
	}
	else {
		$('#' + infodiv).html((limit - textlength) +' characters left.');
		return true;
	}
}