
$(document).ready(function() {


	//code mirror fix

	if ($("#seo_conv_head").length > 0){
		if ($("#seo_conv_head").is("textarea")) {
			CodeMirror.fromTextArea(document.getElementById("seo_conv_head"), {
				lineNumbers: true
			});
		}
	}

	if ($("#seo_conv_body").length > 0){

		if ($("#seo_conv_body").is("textarea"))
			CodeMirror.fromTextArea(document.getElementById("seo_conv_body"), {
				lineNumbers: true
			});
	}


	if ($("#text_title").length && $("#seo_title").length){
		$("#text_title").blur(function(){
			if (!$("#seo_title").val().length){
				$("#seo_title").val($(this).val());
				$("#seo_title").trigger("change");
			}			
		});	
	}

	if ($("#text_title_page").length && $("#seo_title_page").length){
		$("#text_title_page").blur(function(){
			if (!$("#seo_title_page").val().length){
				$("#seo_title_page").val($(this).val());
				$("#seo_title_page").trigger("change");
			}			
		});	
	}


	//character counter
	$('#seo_title_page,#seo_title').on('input change', function(){
		chars = $(this).val().length;
		$(this).next("span").html(chars);

		if (chars == 0){
			$(this).next("span").removeClass("green orange").addClass("red");
		} else {

			if ((chars > 0 ) && (chars < 55)){
				$(this).next("span").removeClass("red orange").addClass("green");
			} else {

				if ((chars >= 55 ) && (chars < 65)){
					$(this).next("span").removeClass("green red").addClass("orange");
				} else {
					$(this).next("span").removeClass("green orange").addClass("red");
				}
			}
		}
	});


	$('#seo_desc_page,#seo_desc').on('input change', function(){
		chars = $(this).val().length;
		$(this).next("span").html(chars);

		if (chars == 0){
			$(this).next("span").removeClass("green").addClass("red");
		} else {

			if ((chars > 0 ) && (chars <= 156)){
				$(this).next("span").removeClass("red").addClass("green");
			} else {
				$(this).next("span").removeClass("green").addClass("red");
			}
		}
	});


	$('#seo_title_page , #seo_desc_page, #seo_title, #seo_desc').each(function() {
		if (!$(this).next("span").length){
			chars = $(this).val().length;
			$(this).after("<span class='counter'>" + chars + "</span>");

			$(this).trigger("input");
		}
	});



});