function addlabel(th){
          var nl=$(th).prev("input").attr("value");
          if(nl=='' || nl==null) return;
          $(th).prev("input").attr("value",'')
          $("div.to_label").append('<span><a href="http://www.blog.ukrbiznes.com/index.php?param=mitku&mitka='+nl+'" target="_blank" class="l">'+nl+'</a><a href="javascript: emty();" class="del" onclick="dellabel(this)">[x]</a><input class="add_mitku" type="hidden" name="labels[]" value="'+nl+'" /></span> ');
}
function dellabel(th){
	if(confirm("Удалить?")){
		$(th).parent("span").remove();
	}
}
function blogSave(ch){
	//------------------------------------------head
	var headtext = $.trim($("#b_head").val());
	if(headtext.length < 1 && ch != 2 ){
		alertdiverror("Добавте заголовок",$("#head_post"));
		window.location.hash = "#head_post";
		
		return false;}
	
	var res = $("div.to_editor").jseditor_get();
	//------------------------------------------text
	var text_input = res[0]['html'];
	text_input = trimFromEditor(text_input);
	if(text_input.length < 5 && ch != 2 ){
		alertdiverror("Добавте текст блога",$("#text_post"));
		window.location.hash = "#head_post";
		return false;
	}
	//-------------------------------------------is_close is_comm
	if(!$("input.insert_is_close:checked").val() && ch != 2){
		alertdiverror("Вы не указали параметры публикации",$("#choose_close"));
		return false;}
	if(!$("input.insert_is_comm:checked").val() && ch != 2){
		alertdiverror("Вы не указали параметры публикации",$("#choose_close"));
		return false;}
	//-------------------------------------------img
	var arrImg = res[0]['img'];
	var strImg = '';
	for(var i = 0; i < arrImg.length - 1; i++ ){
		strImg +=  arrImg[i] + "###";
	}
	strImg +=  arrImg[i];
	//-------------------------------------------video
	var arrVideo = res[0]['video'];
	var strVideo = '';
	for(var i = 0; i < arrVideo.length - 1; i++ ){
		strVideo +=  arrVideo[i] + "###";
	}
	strVideo +=  arrVideo[i];
	//-------------------------------------------mitku
	var str_m = '';
	for(var i = 0; i < $('.add_mitku').length; ++i){
		str_m += $('.add_mitku').eq(i).val() + ", ";	
	}
	//-------------------------------------------date
	var day = $("select#date_post_day option:selected").val();
	var manth = $("select#date_post_manth option:selected").val();
	var year = $("select#date_post_year option:selected").val();
	var hour = $("select#date_post_hour option:selected").val();
	var minute = $("select#date_post_minute option:selected").val();
	
	if(ch == 2){$("#in_ch").val("1");}
	$('#blog_date').val(day+"/"+manth+"/"+year+"/"+hour+"/"+minute);
	$('#blog_head').val(headtext);
	$('#blog_text').val(text_input);
	$('#blog_foto').val(strImg);
	$('#blog_video').val(strVideo);
	$('#blog_metka').val(str_m);
	$('#blog_save').val("b_save");	
	$("#add_form").submit();
}
function trimFromEditor(text){
	text =  text.replace(/^\s*(&nbsp;)*\s*/, "");
	text =  text.replace(/(&nbsp;)*\s*(<br>)*$/, "");
	text = $.trim(text);
	return text;
}

function isVusYear(year){
	if(year%400 == 0){return true;}
	if(year%100 == 0){return false;}
	if(year%4 == 0 ){return true;}
	return false;
}

function changDayOfManth(){
	var year = $("select#date_post_year option:selected").val();
	if(isVusYear(year)){
		var manthMax = new Array(31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	}else{
		var manthMax = new Array(31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	}
	var top = manthMax[$("select#date_post_manth option:selected").val()];
	for(var i = 1; i <= 31; i++){
		if($("select#date_post_day option").eq(i).val() > top){
			$("select#date_post_day option").eq(i).attr("disabled", "true");
			if($("select#date_post_day option").eq(i).attr("selected")){
				$("select#date_post_day option").eq(0).attr("selected", "true");
			}
		}else{
			$("select#date_post_day option").eq(i).attr("disabled", "");
		}
	}
}
function doChangDatePost(){
	var dateForm = getDateFromForm();
	var text = dateForm['day'] + " " + dateForm['manthtext'] + " " + dateForm['year'] + ", " + dateForm['hour'] + ":" + dateForm['minute'];
	$("#date_post").empty().text(text);
	$("#date_post_input").hide();
	$("#date_post_text").show();	
}
function getDateFromForm(){
	var manthText = new Array("", "января", "февраля", "марта", "апреля", "мая", "июня", "июля", "августа", "сентября", "октября", "ноября", "декабря");
	var day = $("select#date_post_day option:selected").val();
	var manth = $("select#date_post_manth option:selected").val();
	var year = $("select#date_post_year option:selected").val();
	
	var hour = $("select#date_post_hour option:selected").val();
	var minute = $("select#date_post_minute option:selected").val();
	var res = new Array(6);
	res['day'] = day;
	res['manth'] = manth;
	res['manthtext'] = manthText[manth];
	res['year'] = year;
	res['hour'] = hour;
	res['minute'] = minute;
	return res;
}
function showChangDate(){
	$("#date_post_text").hide();
	$("#date_post_input").show();
}
function showPreview(){
	var res = $("div.to_editor").jseditor_get();
	//------------------------------------------text
	var text_input = new String();
	text_input = res[0]['html'];
	text_input = trimFromEditor(text_input);
	$("#blog_text_preview").html(text_input);
	//------------------------------------------foto
	var arrImg = new Array();
	arrImg = res[0]['img'];
	for(var i = 0; i < arrImg.length; i++){
		$(".placeforimg").eq(0).replaceWith("<img src='http://blog.ukrbiznes.com/user_foto_tmp/"+arrImg[i]+"'/>");
	}
	//------------------------------------------video
	var arrVideo = new Array();
	arrVideo = res[0]['video'];
	var arrVideoObject = new Array(arrVideo.length);
	for(var i = 0; i < arrVideo.length; i++){
		arrVideoObject[i] = getVideoObject(arrVideo[i]);
		$(".placeforvideo").eq(0).replaceWith(arrVideoObject[i]);
	}
	//-----------------------------------------date
	var dateForm = getDateFromForm();
	if($("#b_head").val() == ""){
		$("h3#zagl_preview a").text(dateForm['day']+" "+dateForm['manthtext']+" "+dateForm['year']+" года");
		$("span#subzagl-date-prev").text(", "+dateForm['hour']+":"+dateForm['minute']);
	}else{
		$("h3#zagl_preview a").text($("#b_head").val());
		$("span#subzagl-date-prev").text(", "+dateForm['day']+" "+dateForm['manthtext']+" "+dateForm['year']+" года, "+dateForm['hour']+":"+dateForm['minute']);	
	}
	//----------------------------------------is_close
	if($("input#is_closed").attr("checked")){
		$("img#imgclosed_preview").show();
	}
	//----------------------------------------mitku
	var str_m = '';
	
	for(var i = 0; i < $('.add_mitku').length - 1; ++i){
		str_m +=  "<a href='#'>" + $('.add_mitku').eq(i).val() + "</a>, ";	
	}
	str_m +=  "<a href='#'>" + $('.add_mitku').eq(i).val() + "</a>";
	if($('.add_mitku').length > 0){
		$("div.metki-blog").html("<span>Метки : </span>" + str_m).show();
	}
	$("#ubcut").hide();
	var w = $(document).width();
	var h = $(document).height();
		var wpreview = w/2-340;
		var hpreview = 150;
	window.location.hash = "#";
	$("div.blog-preview").css("left", wpreview).css("top", hpreview).show();
//	if($("div.blog-preview").height() < 300){
//		$("div.blog-preview").height(300);
//	}
	$("#blok-preview-bloker").width(w).height(h).show();
}
function getVideoObject(path){
	if(path.indexOf("youtube") != -1){
		var l = path.indexOf("v="); l += 2; 
		var video = path.substring(l, l+11);
		var res = "<object width='425' height='344'><param name='movie' value='http://www.youtube.com/v/"+video+"&hl=ru&fs=1'></param><param name='allowFullScreen' value='true'></param><param name='allowscriptaccess' value='always'></param><embed src='http://www.youtube.com/v/"+video+"&hl=ru&fs=1' type='application/x-shockwave-flash' allowscriptaccess='always' allowfullscreen='true' width='425' height='344'></embed></object>";
		return res;
	}else if(path.indexOf("rutube") != -1){
		var l = path.indexOf("v="); l += 2;
		var video = path.substring(l, l+32);
		var res = "<OBJECT width='470' height='353'><PARAM name='movie' value='http://video.rutube.ru/"+video+"></PARAM>	<PARAM name='wmode' value='window'></PARAM>	<PARAM name='allowFullScreen' value='true'></PARAM>	<EMBED src='http://video.rutube.ru/"+video+"' type='application/x-shockwave-flash' wmode='window' width='470' height='353' allowFullScreen='true' ></EMBED></OBJECT>";
		return res;
	}else if(path.indexOf("vimeo") != -1){
		var sl = path.split("/");
		var video = sl[sl.length-1];
		var res = "<object width='400' height='302'><param name='allowfullscreen' value='true' /><param name='allowscriptaccess' value='always' /><param name='movie' value='http://vimeo.com/moogaloop.swf?clip_id="+video+"&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1' /><embed src='http://vimeo.com/moogaloop.swf?clip_id="+video+"&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1' type='application/x-shockwave-flash' allowfullscreen='true' allowscriptaccess='always' width='400' height='302'></embed></object>";
		return res;
	}
}
function closePreview(){
	$("div.metki-blog").hide();
	$("div.blog-preview").height("auto");
	$("div.blog-preview").hide();
	$("#blok-preview-bloker").hide();		
}
function changeDest(){
	if($("#choos_dest_sel").val() == -1){
		$("#close_comm_text").text("Только моим контактам");
	}else{
		$("#close_comm_text").text("Только участникам группы");
	}
}
