/**
 * Used by two pages:
 * 	/cmsadmin/editseosub.php
 * 	/cmsadmin/editpagessub.php
 */

$(function() {
	
	var fields = $("#metaDescr, #metaKeywords, #city, #state, #seo_title, #seo_description, #seo_keywords, #blogtitle, #seo_keywords");
	
	var updatePageTitleField = function() {
		var buf = [];
		buf.push($("#sitename").val());
		buf.push(" - ");
		buf.push($("#city").val());
		buf.push(", ");
		buf.push($("#state").val());
		
		var txt = buf.join("");
		var len = txt.length;
		
		$("#dynamicSEO").text(txt);
		$("#dynamicSEO").parent().find("span.charcount").text(String(len));
	};
	
	fields.on("keyup keydown change paste", function(ev) {
		var target = $(this);
		
		var words = String(target.val()).split(/,\s{0,}/).filter(function(s) {
			if (s.replace(/[,\s]+/g, "") == "") 
				return false;
			return true;
		});
		
		target.parent().find("span.charcount").text(String(target.val().length));
		target.parent().find("span.keywordcount").text(String(words.length));
		
		if (!target.is("#seo_title, #seo_description, #seo_keywords")) {
			updatePageTitleField();
		}
	});
	
	fields.change();
});

