function sbm(){
	if(document.MF.text.value == '' && document.MF.title.value == '' && document.MF.music.value == '' && document.MF.mood.value == ''){
		alert('Вы не можете добавить абсолютно пустую запись!');
		return false;
	}
	document.MF.sbm_btn.disabled=true;
	document.MF.sbm_btn.value="Идет добавление/обновление";
	return true;
}

function ch_color(color){
	document.MF.color.value="#"+color;
	expand(findObj('clr_sel'));
}
	
function test(color){
	tc = findObj('test_color');
	tc.innerHTML = "<span style='color:#"+color+"'>#"+color+"</span>";
	td = findObj('test_td');
	td.setAttribute('bgcolor', color, false);
}

function AddText(NewCode) {
document.MF.text.value+=NewCode
}

function email() {
	var txt=prompt("Введите адрес.","");
	bbfontstyle("[email="+txt+"]","[/email]");
}

function bold() {
	bbfontstyle("[b]","[/b]")
}
function italic() {
	bbfontstyle("[i]","[/i]");
}
function del() {
	bbfontstyle("[del]","[/del]");
}
function underline() {
	bbfontstyle("[u]","[/u]");
}

function acenter() {
	bbfontstyle("[center]","[/center]");
}
function aleft() {
	bbfontstyle("[left]","[/left]");
}
function aright() {
	bbfontstyle("[right]","[/right]");
}

$(function(){
	$("div.floatDialog").dialog({
		modal:true, 
		resizable:false, 
		width:340,
		height: 70,
		autoOpen:false
	});
	
	$("div.floatDialog div.floatRadio").buttonset();
	$("div.floatDialog div.floatRadio input").click(function(){
		var floatMode = $(this).val();
		if(floatMode){
			bbfontstyle("[float="+floatMode+"]","[/float]");
		}
		$("div.floatDialog").dialog("close");
		$("div.floatDialog div.floatRadio input").each(function(){
			this.checked = false;
		});
	});
});
function float() {
	$("div.floatDialog").dialog("open");
}


function flash(){
	$("div.flashDialog").dialog("open");
}

function cut() {
	bbfontstyle("[cut=cокращено]","[/cut]");
}

function showsize(size, sel, di) {
	bbfontstyle("[size="+size+"]","[/size]");
	sel.options[di].selected=true;
}

function showfont(font, sel, di) {
	bbfontstyle("[font="+font+"]","[/font]");
	sel.options[di].selected=true;
}

function showcolor(color) {
	bbfontstyle("[color="+color+"]","[/color]");
}

function showcode() {
	bbfontstyle("[code]","[/code]");
}

function hyperlink() {
	var url=prompt("Введите URL.","http://");      
	bbfontstyle("[url="+url+"]", "[/url]");
}

function image() {
	var txt=prompt("Введите URL картинки","http://");
	var alt=prompt("Введите текст, который будет показан при наведении на картинку.","");
	if(txt!=null) {            
		bbfontstyle("[img="+'"'+txt+'"'+"]"+alt+"[/img]","");
	}	
}

function video(){
	var url;
	if(url = prompt("Введите URL видео","http://")){
		bbfontstyle("[video]"+url,"[/video]");
	}
}



<!-- // cloak
var submitted = 0;
// -->

function colorSelector(){
	clr = new Array();
	clr[0] = '00';
	clr[1] = '20';
	clr[2] = '40';
	clr[3] = '60';
	clr[4] = '80';
	clr[5] = 'a0';
	clr[6] = 'c0';
	clr[7] = 'ff';
	for (var i=0;i<8;i++){
	if(i==4){
			document.write('</tr><tr><td><table cellpadding="0" cellspacing="0">');
		}else{
			document.write('<td><table cellpadding="0" cellspacing="0">');
		}
		for (var j=0;j<8;j++) {
			document.write('<tr>');
			for (var k=0;k<8;k++) {
				document.write('<td bgcolor="#'+clr[i]+clr[j]+clr[k]+'"><a onmouseover="test('+"'"+clr[i]+clr[j]+clr[k]+"'"+')" href="JavaScript: ch_color('+"'"+clr[i]+clr[j]+clr[k]+"'"+')"><img src="/gfx/shim.gif" width="20" height="10" border="0"></a></font></td>'); 
			}
			document.write('</tr>');
		}
		document.write('</table></td>');
	}
}

function photoStoreDialog(album_id){
	divDialog.setTitle('Фото архив');
	divDialog.setSize(180,400);
	setWaitDialog(true, "Открываем фото архив");
	doLoadParser(true, 
		{'album_id':album_id != null?album_id:null}, 
		'/user/photo_store/dialog_chooser.html',
		function(debug,res){ 
			setWaitDialog(false);
			if(res.ok == 1){
			    divDialog.setContent(res.html);
			    divDialog.show();
			}else{
			    alert(res.error);
			}
		}
	);
}

function addPSImage(url, title){
	bbfontstyle('[img="'+url+'"]'+title+'[/img]', "");
	//AddText('[img="'+url+'"]'+title+'[/img]');
}


function list() {
	txt=prompt("Введите '1' для нумерованого списка или оставьте поле пустым маркированного списка.","");
	while ((txt!="") && (txt!="1") && (txt!=null)) {
		txt=prompt("ERROR!\n Неверное значение, введите '1' или оставьте поле пустым.","");
	}
	if (txt!=null) {
		if (txt=="") {
			AddTxt="\r[list]\r\n";
		} else {
			AddTxt="\r[list="+txt+"]\r";
		} 
		txt="1";
		while ((txt!="") && (txt!=null)) {
			txt=prompt("Введите элемент списка\nОставьте поле пустым для окончания списка.",""); 
			if (txt!="") {             
				AddTxt+="[*]"+txt+"\r"; 
			}                   
		} 
		AddTxt+="[/list]\r\n";
		//AddText(AddTxt); 
		bbfontstyle(AddTxt,"");
	}
}


function arrayUnique(a) {  
	var temp = [];  
   	var temp2 = [];  
   	for (c in a)  
   	temp[a[c]] = c  
	for (c in temp)  
   	temp2.push(c);  
	return temp2;  
}  

(function($){
	$(function(){
		
		/* Preview */
		$('.preview').click(function(){
			var button = $(this);
			if(button.data('enabled')){
				$("div#preview_pane").html("").hide();
				$("#entry_form_table").show();
				
				$(window).scrollTop($("div#entry_add_block").offset().top);
				
				button.val("Предпросмотр");
				button.data('enabled', false);
			}else{
				button.val("Загрузка предпросмотра...").attr('disabled', 'disabled');
				$.post(
					"/bgscript/entry_preview.html",
					{text:$('textarea[name=text]').val()},
					function(data){
						$("#entry_form_table").hide();
						$("div#preview_pane").html(data).show();
						$(window).scrollTop($("div#entry_add_block").offset().top);
						
						button.val("Продолжить редактирование").attr('disabled', false);
						button.data('enabled', true);
					}
				);
			}
		});
		
		/* ************* Flash dialog ********************** */
		
		// Set as tabbed
		$("div.flashDialog").tabs();
		
		// Set as dialog
		$("div.flashDialog").dialog({
			modal:true, 
			resizable:false, 
			width: 360,
			height: 'auto',
			autoOpen:false,
			close: function(){ clearFlashDialog(); }
		});
		
		// SWF
		$("div.flashDialog input#flash_dialog_swf_btn").click(function(){
			var width 	= 	$("#flash_width").val();  
			var height 	= 	$("#flash_height").val();
			var url 	=	$("#flash_url").val();
			var flashVars = $("#flash_vars").val();
			if( width && height && url  ){
				var tag = "[flash="+width+","+height+","+url+(flashVars?","+flashVars:"")+"]";
				bbfontstyle(tag,"");
				
				clearFlashDialog();
				$("div.flashDialog").dialog("close");
			}else{
				alert("Вы должны указать размеры и URL");
			}
			return false;
		});
		
		// EMBED parsing
		$("div.flashDialog input#flash_dialog_embed_btn").click(parseEmbedDom);
		function parseEmbedDom(){
			try{
				var object, embed, url, width, height, flashVars;
				var embedStr = $('div.flashDialog textarea').val();
				
				embed = $(embedStr).filter('embed:first'); 
				object = $(embedStr).filter('object:first');
								
				// we try to find embed tag first and object in case of failure
				if( embed.length == 1 ){
					width 		= embed.attr('width');
					height 		= embed.attr('height');
					url 		= embed.attr('src');
					flashVars 	= embed.attr('flashvars');
				}else if(object.length == 1){
					width 		= object.attr('width');
					height 		= object.attr('height');
					url 		= object.children('param[name=movie]:first').attr('value');
					flashVars 	= object.children('param[name=flashvars]:first').attr('value');
				}
				if( width && height && url ){
					insertFlashTag(width, height, url, flashVars);
					clearFlashDialog()
					$("div.flashDialog").dialog("close");
				}else{
					throw {message: "Не удалось распознать формат тега <EMBED>/<OBJECT>"};
				}
			}catch(e){
				// if something goes wrong
				alert( e.message );
			}
		}
		
		function insertFlashTag(width, height, url, flashVars){
			var tag = "[flash="+width+","+height+","+url+(flashVars?","+flashVars:"")+"]"; 
			bbfontstyle(tag,"");
		}
		
		function clearFlashDialog(){
			$("div.flashDialog input").not('input[type=button]').val('');
			$("div.flashDialog textarea").val('');
		}
		
		
		/* *********************** Tag list container ************************************* */
		$('div#userTagsCont')
		.attr('class', 'textBlock')
		.css({
			display: 'none',
			position: 'absolute',
			width: '200px',
			height: '300px'
			
		})	
		.click(function(){return false;});
				
		/* Show tags */
		$('a#showUserTags').click(function(event){
			$('#userTagsCont')
			.css({
				left: event.pageX,
				top: event.pageY
			})		
			.fadeIn();
			return false;
		});
		
		/* Tag animation, tag clicking */		
		$('div#userTagsCont li')
		.hover(
			function(){
				$(this).addClass('textBlock');
				$(this).css('font-weight', 'bold');
			},
			function(){
				$(this).removeClass('textBlock');
				$(this).css('font-weight', 'normal');
			}
		)
		.css({cursor: 'pointer'})
		.click(function(){
			var cv = $('input[name=tag]').val();
			cv = cv+','+$(this).html();
			cv = cv.replace(/,+/g, ",");
			cv = cv.replace(/^,+/, "");
			cv = cv.replace(/,+$/, "");
			
			var utags = new Array();
			var allTags = cv.split(",");
			for(var i = 0; i < allTags.length; i++){
				allTags[i] = $.trim(allTags[i]);
			}
			allTags = arrayUnique(allTags);
			$('input[name=tag]').val(allTags.join(', '));
		});
				
		/* global body onclick */
		$('body').click(function(){
			$('#userTagsCont').fadeOut();
		});
	});
})(jQuery)

