$(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');
}

jQuery.fn.outerHTML = function() {
	return $('<div>').append( this.eq(0).clone() ).html();
};

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 newsup(poid,nid,nurl){
	$.ajax({
				type: "POST",
				timeout: 7000,
				data: {stpoid: poid, newsid:nid},
				url: nurl,
				dataType: 'xml',
				success: function(result){
					if($("before",result).text()!="-1") {
						html = $("#newsContainer_"+poid+"_"+nid).outerHTML();
						$("#newsContainer_"+poid+"_"+nid).remove();
						$("#newsContainer_"+poid+"_"+$("before",result).text()).before(html);
					}
				}
	});	
	return true;
}

function newsdown(poid,nid,nurl){
	$.ajax({
				type: "POST",
				timeout: 7000,
				data: {stpoid: poid, newsid:nid},
				url: nurl,
				dataType: 'xml',
				success: function(result){
					if($("after",result).text()!="-1") {
						html = $("#newsContainer_"+poid+"_"+nid).outerHTML();
						$("#newsContainer_"+poid+"_"+nid).remove();
						$("#newsContainer_"+poid+"_"+$("after",result).text()).after(html);
					}
					}
	});	
	return true;
}


function newsdelete(poid,nid,otazka,nurl){
		Ext.Msg.show({
			title: "Otazka",
			msg: otazka,
			buttons: Ext.Msg.YESNO,
			fn: function(btn,text){
					if(btn=="yes"){
						$.ajax({
							type: "POST",
							timeout: 7000,
							data: {stpoid: poid, newsid:nid},
							url: nurl,
							dataType: 'xml',
							success: function(result){
								$("#newsContainer_"+poid+"_"+nid).remove();
								$("#sysmsgs").html($("html",result).text()).fadeIn(500).fadeOut(3000);
							}
						});
					}
			}
		});
		return true;
	
}

function newsfoto(poid,nid,cesta,nurl){
	window.SetUrl=function(val){
		$.ajax({
				type: "POST",
				timeout: 7000,
				data: {stpoid: poid, newsid:nid, imgpath: val},
				url: nurl,
				dataType: 'xml',
				success: function(result){
					$('#newsimage_'+poid+'_'+nid).html($("html",result).text());
					}
			});	
		
	}
	window.open('../system/modules/kfm/index.php?mode=selector','kfm','modal,width=800,height=600');
}

function drawnewseditor2(poid,nid,height,baselocation,nurl,nsaveurl){
	var w = new Ext.Window({
		 autoShow: false,
		 width: 700,
		 mihHeight:500,
		 autoHeight: true,
		 animateTarget: 'newscnt_'+poid+'_'+nid,
		 title: "News Editor",
     autoScroll:true,
     modal:true,
     listeners:{beforeclose:function(){tinyMCE.execCommand("mceRemoveControl",false,"newsmce_"+poid);}}
	});
	w.show("body");
	w.setPagePosition((screen.width/2-350),50);
	w.load({url:nurl,params:{"newsid":nid,"stpoid":poid},scripts:true, callback:function(w){initNewsMce(poid,height,w,baselocation,nid,nsaveurl);}});
}

function initNewsMce(poid,height,window,baselocation,nid,nsaveurl){
		tinyMCE.init({
								element_format : "html",
								mode: "exact",
								elements: "newsmce_"+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|border|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,5)!='html_') {
										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: nsaveurl,
													type: "POST",
													data: {'stpoid': poid, 'nid':nid},
													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,"\"");
														$('#newsContainer_'+poid+'_'+nid).html(newhtml);
														//$('#htmlhead_'+poid).html(newname);
																											}
												};
									$("#frm_newsmce_"+poid).ajaxSubmit(options);
								}
						});
}

function newsadd(poid, height,nurl,cesta,nediturl,nsaveurl){
	$.ajax({
				type: "POST",
				timeout: 7000,
				data: {stpoid: poid},
				url: nurl,
				dataType: 'xml',
				success: function(result){
					$('.newsAddIcon').after($("html",result).text());
					drawnewseditor2(poid,$("nid",result).text(),height,cesta,nediturl,nsaveurl);
				}
});	
}

function confirm(message, callback) {
	$('#confirm').modal({
		close:false, 
		overlayId:'confirmModalOverlay',
		containerId:'confirmModalContainer', 
		onShow: function (dialog) {
			dialog.data.find('.message').append(message);

			// if the user clicks "yes"
			dialog.data.find('.yes').click(function () {
				// call the callback
				if ($.isFunction(callback)) {
					callback.apply();
				}
				// close the dialog
				$.modal.close();
			});
		}
	});
}
