jQuery(function(){
	jQuery('div.post-filter li a').click(function(){
//		jQuery('div.post-filter li').removeClass('act');
//		jQuery(this).parent().addClass('act');
		
		$.cookie('show_posts', jQuery(this).attr('href').split('/')[1], { expires: 1, path: '/' });
		if (jQuery(this).attr('href').indexOf("new-post") > 0) {}
		else {
			document.location.hash = jQuery(this).attr('hash');
			document.location.reload();
		}
	});
	
	var clck = false;
	var entr = jQuery("input[class=enter]").parent();
	jQuery('#newpostform').submit(function(){
		if (!clck) {
			jQuery.post(jQuery(this).attr('action'), jQuery(this).serialize(), function(data){
				jQuery('body').append('<div id="hidden_res" style="display:none"></div>');
				jQuery('#hidden_res').html(data);
				clck = false;
				entr.removeClass("d_submit");
				if (jQuery('#hidden_res div.message p a:first').length > 0) {
					var href = jQuery('#hidden_res div.message p a:first').attr('href');
					var post_id = href.match(/\d+/img)[0];
					
					// Сохраняем meta...
					if (jQuery('input[name^=corporate_ip_only]').eq(0).attr('checked') && parseInt(post_id, 10) > 0) {
						jQuery.get('/profile/edit-post/' + post_id + '/', {save_meta:1, corporate_ip_only:1}, function(){
							document.location.href = '/profile/edit-post/' + post_id + '/';
						});
					} else {
						document.location.href = href;
					}
					
				}
			});
		}
		entr.addClass("d_submit");
		clck = true;
		return false;
	});
	
	$('.subj a[target!=_blank] img').parent().each(function(){ 
		var obj = $(this);
		if (obj.attr('href').match(/(jpg|jpeg|png|gif)$/img)) {
			obj.lightBox();
		}
	});
	
	jQuery('form#registration').submit(function(){
		var ul = jQuery('#r_user_login');
		var ue = jQuery('#r_user_email');
		var msg = '';
		
		jQuery(".required").removeClass("required");	
		if (jQuery('#reg_error_msg').length > 0) {
			jQuery('#reg_error_msg').slideUp('slow').removeAttr('id');
		}
	
		if (trim(ul.val()).length == 0) {
			msg += 'Не заполнено поле "логин"' + "<br />";
			ul.focus();
			ul.parent().addClass("required");
		}
		
		if (trim(ue.val()).length == 0) {
			msg += 'Не заполнено поле "e-mail"' + "<br />";
			ue.focus();
			ue.parent().addClass("required");
		} else if (/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/.test(trim(ue.val())) == false) {
			msg += 'Поле "e-mail" заполнено неверно' + "<br />";
			ue.focus();
			ue.parent().addClass("required");
		}
		
		if (msg.length > 0) {
			jQuery('form#registration div').eq(0).before('<div id="reg_error_msg" class="row error" style="display:none; color: red">' + msg + '</div>');
			jQuery('#reg_error_msg').slideDown('slow');
			return false;
		}
				
		jQuery.post(jQuery(this).attr('action'), jQuery(this).serialize(), function(data){
			var error = data.replace(/\n/gim, "").match(/login_error.*?<\/div/);
			if (error != null && typeof error == "object" && typeof error[0] == "string") {
				var error_msg = error[0].replace(/^[^>]*>\s*/img, "").replace("</div", "").replace(/<strong>.*?<\/strong>:\s+/img, '');
				error_msg = trim(error_msg).replace(/<br\s?\/>$/img, '')
				
				if (error_msg.match(/логин/img)) {
					ul.parent().addClass("required");
				}
				if (error_msg.match(/mail/img)) {
					ue.parent().addClass("required");
				}
				
				jQuery('form#registration div').eq(0).before('<div id="reg_error_msg" class="row error" style="display:none; color: red">' + error_msg + '</div>');
				jQuery('#reg_error_msg').slideDown('slow');
			} else {
				var success = data.replace(/\n/gim, '').match(/message.*?<\/p/);
				if (success != null && typeof success == "object" && typeof success[0] == "string") {
					var success_msg = success[0].replace(/^[^>]*>\s*/img, "").replace("</p", "");
					success_msg = trim(success_msg).replace(/<br\s?\/>$/img, '')
					// Success
					jQuery('form#registration div').eq(0).before('<div id="reg_error_msg" class="row error" style="display:none; color: green">' + success_msg + '</div>');
					jQuery('#reg_error_msg').slideDown('slow');
				}
			}
		});
	
		return false;
	});

});

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}