

	function validateNoempty(text) {
		if (!text || text.length < 10) { return false;	}
		
		return true;
	}
	
		function validateNoempty2(text) {
		if (!text || text.length < 3) { return false;	}
		
		return true;
	}
	
	function validateText(text) {
		if (!validateNoempty2(text)) { return false; }
		
		var re_text = /[^A-Za-z0-9\s]/;
		if (text.match(re_text) != null ) { return false; } 
		return true;
	}
	
	function validateEmail(email) {
		if (!validateNoempty(email)) { return false; }
		
		var splitted = email.match("^(.+)@(.+)$");
		if (splitted == null) return false;
		if (splitted[1] != null ) {
			var regexp_user=/^\"?[\w-_\.]*\"?$/;
			if (splitted[1].match(regexp_user) == null) { return false; }
		}
		
		if(splitted[2] != null) {
			var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
			if (splitted[2].match(regexp_domain) == null) {
				var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
				if (splitted[2].match(regexp_ip) == null) { return false; }
			}
			return true;
		}
		return false;
	}	



$(document).ready(function(){
						   
$('.js-submit').click( function(event) {
			event.preventDefault();
			error = false;
			
			$(this).parents('form').children('fieldset').children('p').children('input[type="text"], textarea').each( function() {
				if ($(this).hasClass('v-noempty')) {
					if (!validateNoempty($(this).val())) {
						$(this).animate( { opacity: 0.2 }, 175 ).animate( { opacity: 1 }, 150 );
						error = true;
					}
					
				} else if ($(this).hasClass('v-text')) {
					if (!validateText($(this).val())) {
						$(this).animate( { opacity: 0.2 }, 175 ).animate( { opacity: 1 }, 150 );
						error = true;
					}
				} else if ($(this).hasClass('v-mail')) {
					if (!validateEmail($(this).val())) {
						$(this).animate( { opacity: 0.2 }, 175 ).animate( { opacity: 1 }, 150 );
						error = true;
					}
				}
			});
			
			if (!error) {
				
var mesajul = $(this).parents('form').serialize();
 var node = $(this).parents('form');
 var snode = $(this).parents('form').children('fieldset').children('h1');
 
$.ajax({
  url:  ajax_path + "/add_ajax_comments.php?" + mesajul,
  cache: false,
    success: function(html){		
	  snode.html(html);
	  node.delay(2000).slideUp('slow');
	  //snode.delay(2000).html("your comment");
	  }
	  
});  

$(this).parents('form').children('fieldset').children('p').children('input[type="text"], textarea').each( function() {
this.value = this.defaultValue;
});

			}
		}
	);	

	// SCROLLBAR INIT
	if($('.opened .comments-scroll').length){
		$('.opened .comments-scroll').jScrollPane({showArrows:false});
	}

	// FONTS INIT
	if($('.post .title a').length){
		Cufon.replace('.post .title a', {
			hover: false,
			fontFamily: 'Mrs Blackfort'
		});
	}
	if($('.post .hidden p').length){
		Cufon.replace('.post .hidden p', {
			hover: false,
			fontFamily: 'Century Gothic'
		});
	}
	if($('#navigation').length){
		Cufon.replace('#navigation', {
			hover: false,
			fontFamily: 'Century Gothic'
		});
	}
	if($('#archives ul li').length){
		Cufon.replace('#archives ul li', {
			hover: false,
			fontFamily: 'Century Gothic'
		});
	}
	if($('#contact .text p').length){
		Cufon.replace('#contact .text p', {
			hover: false,
			fontFamily: 'Century Gothic'
		});
	}
	if($('#about .text p').length){
		Cufon.replace('#about .text p', {
			hover: false,
			fontFamily: 'Century Gothic'
		});
	}

	if($('#page .text p').length){
		Cufon.replace('#page .text p', {
			hover: false,
			fontFamily: 'Century Gothic'
		});
	    Cufon.replace('#page h1', {
			hover: false,
			fontFamily: 'Century Gothic',
			fontWeight:"bold"
		});
	
	}

	// POSTS OPENER-CLOSER
	$('.closed .hidden').css({'display':'none'});
	$('.opener-closer a, .post-header .comments a').click(function(){
		if($(this).parent().hasClass('comments')){
			var parent_node = $(this).parent().parent().parent().parent();
		}else{
			var parent_node = $(this).parent().parent();
		}
		var hidden_node = parent_node.find('.hidden');
		var comments_node = parent_node.find('.comments-scroll');
		if(parent_node.hasClass('opened')){
			$(window).scrollTo({ top:parseInt(parent_node.offset().top), left:'0px'}, 500, {axis:'xy'});
			hidden_node.animate({'height':'hide'},700,function(){
				parent_node.removeClass('opened');
				parent_node.addClass('closed');
			});
		}else{
			$('.opened').each(function(){
				$(this).find('.hidden').css({'display':'none'});
				$(this).addClass('closed');
				$(this).removeClass('opened');
			});
			if(!$(this).parent().hasClass('comments')){
				$(window).scrollTo({ top:parseInt(parent_node.offset().top), left:'0px'}, 500, {axis:'xy'});
			}
			var thiss = $(this);
			hidden_node.animate({'height':'show'},700,function(){
				comments_node.jScrollPane({showArrows:false});
				parent_node.addClass('opened');
				parent_node.removeClass('closed');
				if(thiss.parent().hasClass('comments')){
					$(window).scrollTo({ top:parseInt(comments_node.offset().top), left:'0px'}, 200, {axis:'xy'});
				}
			});
		}
		return false;
	});
	
	
	$('.post-header .image a').click(function(){

		var parent_node = $(this).parent().parent().parent();

		var hidden_node = parent_node.find('.hidden');
		var comments_node = parent_node.find('.comments-scroll');
		if(parent_node.hasClass('opened')){
			$(window).scrollTo({ top:parseInt(parent_node.offset().top), left:'0px'}, 500, {axis:'xy'});
			hidden_node.animate({'height':'hide'},700,function(){
				parent_node.removeClass('opened');
				parent_node.addClass('closed');
			});
		}else{
			$('.opened').each(function(){
				$(this).find('.hidden').css({'display':'none'});
				$(this).addClass('closed');
				$(this).removeClass('opened');
			});
			if(!$(this).parent().hasClass('comments')){
				$(window).scrollTo({ top:parseInt(parent_node.offset().top), left:'0px'}, 500, {axis:'xy'});
			}
			var thiss = $(this);
			hidden_node.animate({'height':'show'},700,function(){
				comments_node.jScrollPane({showArrows:false});
				parent_node.addClass('opened');
				parent_node.removeClass('closed');
				if(thiss.parent().hasClass('comments')){
					$(window).scrollTo({ top:parseInt(comments_node.offset().top), left:'0px'}, 200, {axis:'xy'});
				}
			});
		}
		return false;
	});


 function populateElement(selector, defvalue) {

		$(selector).focus(function() {
				if ($(selector).val() == 'Website') {
                    	$(selector).val('http://');
                } else if ($(selector).val() == defvalue) {
						$(selector).val('');
				}
		});
		
		$(selector).blur(function() {
				if ($.trim($(selector).val()) == '') {
						$(selector).val(defvalue);
				}
		});
	};

	$('input[type="text"], input[type="password"], textarea').each(function() {
			populateElement($(this), $(this).val())
		}
	);
	
	/* */
	
	var _char_count = 500;
	
	$('textarea').keydown( function() {
			var node = $(this).parents('form').children('.w-left').
												 children('.w-action').children('.js-count-char').children('em');

			if ($(this).val().length >= _char_count) {
				$(this).val( $(this).val().substr(0, _char_count) );
			}
			
			node.html( _char_count - $(this).val().length );

		}
	);
});