$(document).ready(function() {
	
	
	
});

function kfm_for_tiny_mce(field_name, url, type, win){
  window.SetUrl=function(url,width,height,caption){
   win.document.forms[0].elements[field_name].value = url;
   if(caption){
    win.document.forms[0].elements["alt"].value=caption;
    win.document.forms[0].elements["title"].value=caption;
   }
  }
  window.open('../../../kfm/index.php?mode=selector&type='+type,'kfm','modal,width=800,height=600');
}

function extimgchooser(field_name, url, type, win){
	win.SetUrl=function(url,width,height,caption){
   win.document.forms[0].elements[field_name].value = url;
   if(caption){
    win.document.forms[0].elements["alt"].value=caption;
    win.document.forms[0].elements["title"].value=caption;
   }
  }
		ich = new ImageChooser({
				width:950,
				height:400,
				field_name:field_name,
				win:win,
				basefold: win.tinyMCE.settings.baselocation,
				url:win.tinyMCE.settings.baselocation+'fd.php?act=files',
				dirurl:win.tinyMCE.settings.baselocation+'fd.php?act=dirlist',
				uploadurl:win.tinyMCE.settings.baselocation+'fd.php?act=uploadfile',
				deleteurl:win.tinyMCE.settings.baselocation+'fd.php?act=deletefile',
				deldirurl:win.tinyMCE.settings.baselocation+'fd.php?act=deldir',
				newdirurl:win.tinyMCE.settings.baselocation+'fd.php?act=newdir',
				renamedirurl:win.tinyMCE.settings.baselocation+'fd.php?act=renamedir',
				caller:this		
			});
			ich.show(win.tinyMCE.activeEditor.container,function(data,rootel,iswin){iswin.win.SetUrl('../'+data,0,0,'');console.log(data);console.log(rootel);console.log(iswin);});	
}
function initMce(adr,conf1,poid,height,window,baselocation){
	tinyMCE.init({element_format : "html",
								mode: "exact",
								elements: "tinymce_"+poid,
								theme: "advanced",
								language: "sk",
								skin: "o2k7",
								theme_advanced_toolbar_location: "top",
								theme_advanced_toolbar_align: "left",
								valid_elements : "@[id|class|style|title|dir<ltr?rtl|lang|xml::lang|onclick|ondblclick|"
+ "onmousedown|onmouseup|onmouseover|onmousemove|onmouseout|onkeypress|"
+ "onkeydown|onkeyup],a[rel|rev|charset|hreflang|tabindex|accesskey|type|"
+ "name|href|target|title|class|onfocus|onblur],b/strong,i/em,strike,u,"
+ "#p[align],-ol[type|compact],-ul[type|compact],-li,br,img[longdesc|usemap|"
+ "src|border|alt=|title|hspace|vspace|width|height|align|style],-sub,-sup,"
+ "-blockquote,-table[border=0|cellspacing|cellpadding|width|frame|rules|"
+ "height|align|summary|bgcolor|background|bordercolor],-tr[rowspan|width|"
+ "height|align|valign|bgcolor|background|bordercolor],tbody,thead,tfoot,"
+ "#td[colspan|rowspan|width|height|align|valign|bgcolor|background|bordercolor"
+ "|scope],#th[colspan|rowspan|width|height|align|valign|scope],caption,-div,"
+ "-span,-code,-pre,address,-h1,-h2,-h3,-h4,-h5,-h6,hr[size|noshade],-font[face"
+ "|size|color],dd,dl,dt,cite,abbr,acronym,del[datetime|cite],ins[datetime|cite],"
+ "object[classid|width|height|codebase|*],param[name|value|_value],embed[type|width"
+ "|height|src|*],script[src|type],map[name],area[shape|coords|href|alt|target],bdo,"
+ "button,col[align|char|charoff|span|valign|width],colgroup[align|char|charoff|span|"
+ "valign|width],dfn,fieldset,form[action|accept|accept-charset|enctype|method],"
+ "input[accept|alt|checked|disabled|maxlength|name|readonly|size|src|type|value],"
+ "kbd,label[for],legend,noscript,optgroup[label|disabled],option[disabled|label|selected|value],"
+ "q[cite],samp,select[disabled|multiple|name|size],small,"
+ "textarea[cols|rows|disabled|name|readonly],tt,var,big",
								extended_valid_elements : "a[name|href|target|title|onclick|class],img[class|src|alt|title|hspace|vspace|width|height|align|style|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style],div[class|id|style|name]",
								force_p_newlines : false,
								force_br_newlines : true,
								forced_root_block : '',
								convert_newlines_to_brs : false,
								baselocation: baselocation,
								content_css : baselocation+"userdata/layout/style.css",
								class_filter : function(cls,rule){
									if(cls.substring(0,2)!='h_') {
										return false;
									} else return cls;
								},
	//							bramus_cssextras_classesstring : "li::ul[smallList,bigList];img::self[floatLeft,floatRight]",
		//						bramus_cssextras_idsstring : "p::self[introText]",
								plugins : "bramus_cssextras,advhr,advimage,advlink,autosave,contextmenu,inlinepopups,insertdatetime,layer,media,nonbreaking,pagebreak,paste,print,safari,save,searchreplace,style,table,template,visualchars",
								theme_advanced_buttons1_add_before : "save,separator,print,separator,pastetext,pasteword,selectall,separator,search,replace",
								theme_advanced_buttons3_add_before : "template,styleprops,separator,tablecontrols,separator",
								theme_advanced_buttons3_add : "bramus_cssextras_classes,bramus_cssextras_ids",
								theme_advanced_buttons2_add : "advhr,separator,insertdate,inserttime,separator,insertlayer,moveforward,movebackward,absolute,separator,media,separator,nonbreaking,separator,visualchars,separator,cancel,",
								plugin_insertdate_dateFormat : "%d.%m.%Y",
								plugin_insertdate_timeFormat : "%H:%M:%S",
								file_browser_callback : "extimgchooser",
								pagebreak_separator : "<!-- my page break -->",
								paste_create_paragraphs : false,
								paste_create_linebreaks : false,
								paste_use_dialog : true,
								height : height,
								
								media_use_script: false,
								
								paste_auto_cleanup_on_paste : false,
								paste_convert_middot_lists : false,
								paste_unindented_list_class : "unindentedList",
								paste_convert_headers_to_strong : true,
								paste_insert_word_content_callback : "convertWord",
								save_enablewhendirty : false, 
								file_browser_callback : "extimgchooser",
								save_onsavecallback : function(){
									var options={
													url: adr,
													type: "POST",
													data: {conf: conf1, stpoid: poid},
													clearForm: false,
													resetForm: false,
													success: function(responseText,statusText){
														//tinyMCE.execCommand("mceRemoveControl",false,"tinymce_"+poid);	
														//$('#tiny_edit_icon_'+poid).removeClass().addClass('tinyEditIcon');
														var newhtml=$("html",responseText).text().replace(/\\\"/g,"\"");
														var newname=$("name",responseText).text().replace(/\\\"/g,"\"");
														$('#htmlcontent_'+poid).html(newhtml);
														$('#htmlhead_'+poid).html(newname);
																											}
												};
									$("#frm_tinymce_"+poid).ajaxSubmit(options);
								}
						});
}
function draweditor(adr,conf1,conf2,poid,height,baselocation){
	var w = new Ext.Window({
		 autoShow: false,
		 width: 700,
		 autoHeight:true,
		 animateTarget: 'htmlcontent_'+poid,
		 title: "HTML Editor",
     autoScroll:true,
     modal:true,
     resizable:false,
     listeners:{beforeclose:function(){tinyMCE.execCommand("mceRemoveControl",false,"tinymce_"+poid);}}
	});
	w.show("body");
	w.setPagePosition((screen.width/2-350),50);
	w.load({url:adr,params:{"conf":conf1},scripts:true, callback:function(w){initMce(adr,conf2,poid,height,w,baselocation);}});
}

function draweditor2(poid, height){
	var ajaxcall = $("#ajaxcall").attr('value');
	if($('#tiny_edit_icon_'+poid).hasClass('tinyEditIcon')){ 
		$('#tiny_edit_icon_'+poid).removeClass().addClass('tinyHideIcon');
		$.ajax({
					type: "POST",
					timeout: 7000,
					data: {fnc: 'AjaxDrawEditor@' + poid},
					url: "index2.php?conf="+ajaxcall,
					dataType: 'xml',
					success: function(result){
						if($('#htmlcontent_'+poid).length == 0){
							$('#tinystick_'+poid).append('<div id="htmlcontent_'+poid+'"></div>"');
						};
						$('#htmlcontent_'+poid).html($('html',result).text());
						
						tinyMCE.init({
								element_format : "html",
								mode: "exact",
								elements: "tinymce_"+poid,
								theme: "advanced",
								language: "sk",
								skin: "o2k7",
								theme_advanced_toolbar_location: "top",
								theme_advanced_toolbar_align: "left",
								valid_elements : "@[id|class|style|title|dir<ltr?rtl|lang|xml::lang|onclick|ondblclick|"
+ "onmousedown|onmouseup|onmouseover|onmousemove|onmouseout|onkeypress|"
+ "onkeydown|onkeyup],a[rel|rev|charset|hreflang|tabindex|accesskey|type|"
+ "name|href|target|title|class|onfocus|onblur],b/strong,i/em,strike,u,"
+ "#p[align],-ol[type|compact],-ul[type|compact],-li,br,img[longdesc|usemap|"
+ "src|border|alt=|title|hspace|vspace|width|height|align|style],-sub,-sup,"
+ "-blockquote,-table[border=0|cellspacing|cellpadding|width|frame|rules|"
+ "height|align|summary|bgcolor|background|bordercolor],-tr[rowspan|width|"
+ "height|align|valign|bgcolor|background|bordercolor],tbody,thead,tfoot,"
+ "#td[colspan|rowspan|width|height|align|valign|bgcolor|background|bordercolor"
+ "|scope],#th[colspan|rowspan|width|height|align|valign|scope],caption,-div,"
+ "-span,-code,-pre,address,-h1,-h2,-h3,-h4,-h5,-h6,hr[size|noshade],-font[face"
+ "|size|color],dd,dl,dt,cite,abbr,acronym,del[datetime|cite],ins[datetime|cite],"
+ "object[classid|width|height|codebase|*],param[name|value|_value],embed[type|width"
+ "|height|src|*],script[src|type],map[name],area[shape|coords|href|alt|target],bdo,"
+ "button,col[align|char|charoff|span|valign|width],colgroup[align|char|charoff|span|"
+ "valign|width],dfn,fieldset,form[action|accept|accept-charset|enctype|method],"
+ "input[accept|alt|checked|disabled|maxlength|name|readonly|size|src|type|value],"
+ "kbd,label[for],legend,noscript,optgroup[label|disabled],option[disabled|label|selected|value],"
+ "q[cite],samp,select[disabled|multiple|name|size],small,"
+ "textarea[cols|rows|disabled|name|readonly],tt,var,big",
								extended_valid_elements : "a[name|href|target|title|onclick|class],img[class|src|alt|title|hspace|vspace|width|height|align|style|onmouseover|onmouseout|name],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style],div[class|id|style|name]",
								force_p_newlines : false,
								force_br_newlines : true,
								forced_root_block : '',
								convert_newlines_to_brs : false,
								content_css : "../userdata/layout/style.css",
								class_filter : function(cls,rule){
									if(cls.substring(0,2)!='h_') {
										return false;
									} else return cls;
								},
								plugins : "advhr,advimage,advlink,autosave,contextmenu,inlinepopups,insertdatetime,layer,media,nonbreaking,pagebreak,paste,print,safari,save,searchreplace,style,table,template,visualchars",
								theme_advanced_buttons1_add_before : "save,separator,print,separator,pastetext,pasteword,selectall,separator,search,replace",
								theme_advanced_buttons3_add_before : "template,styleprops,separator,tablecontrols,separator",
								theme_advanced_buttons3_add : "separator,visualchars,separator,cancel",
								theme_advanced_buttons2_add : "advhr,separator,insertdate,inserttime,separator,insertlayer,moveforward,movebackward,absolute,separator,media,separator,nonbreaking",
								plugin_insertdate_dateFormat : "%d.%m.%Y",
								plugin_insertdate_timeFormat : "%H:%M:%S",
								file_browser_callback : "kfm_for_tiny_mce",
								pagebreak_separator : "<!-- my page break -->",
								paste_create_paragraphs : false,
								paste_create_linebreaks : false,
								paste_use_dialog : true,
								height : height,
								
								paste_auto_cleanup_on_paste : false,
								paste_convert_middot_lists : false,
								paste_unindented_list_class : "unindentedList",
								paste_convert_headers_to_strong : true,
								paste_insert_word_content_callback : "convertWord",
								save_enablewhendirty : false, 
								file_browser_callback : "kfm_for_tiny_mce",
								template_templates : [
								{
									title : "Test 1",
									src : "/demo2/templates/test.html",
									description : "Basic table"
								}],
								save_onsavecallback : function(){
									var options={
													url: 'index2.php?conf='+ajaxcall,
													type: "POST",
													data: {fnc: 'AjaxSaveHtml@'+poid, stpoid: poid},
													clearForm: false,
													resetForm: false,
													success: function(responseText,statusText){
														tinyMCE.execCommand("mceRemoveControl",false,"tinymce_"+poid);	
														$('#tiny_edit_icon_'+poid).removeClass().addClass('tinyEditIcon');
														var newhtml=$("html",responseText).text().replace(/\\\"/g,"\"");
														var newname=$("name",responseText).text().replace(/\\\"/g,"\"");
														$('#htmlcontent_'+poid).html(newhtml);
														$('#htmlhead_'+poid).html(newname);
																											}
												};
									$("#frm_tinymce_"+poid).ajaxSubmit(options);
								}
						});
						$('#htmlcontent_'+poid).height('auto');
						//console.log(estck);
						//var stid='s_'+poid;
						//estck[stid].doLayout();
						//alert($('#htmlcontent_'+poid).html());
					}
		});		
		
	} else {
		$('#tiny_edit_icon_'+poid).removeClass().addClass('tinyEditIcon');
		tinyMCE.execCommand("mceRemoveControl",false,"tinymce_"+poid);
		var oldhtml=$("#tinyoldhtml_"+poid).html();
		var oldname=$("#tinyoldname_"+poid).html();
		$('#htmlcontent_'+poid).html(oldhtml);
		$('#htmlhead_'+poid).html(oldname);
	}
}
