
$(document).ready(function(){

	var cssUrl = cfg["tinymceCssUrl"];
	var filesUrl = cfg["tinymceFilesUrl"];
	$(".editorTinymce").each(function(i){
		var elementId = $(this).attr("id");
		var elementClass = $(this).attr("class");
		initTinyMce(elementId, elementClass, cfg["tinymceCssUrl"], cfg["tinymceFilesUrl"], cfg["lang"]);
	});
	
});
//TODO
function lightListItems()
{
	$(".list input[type='checkbox']").one("change", function(){
		flag = $(this).attr("checked");
		parentTr = $(this).parent().parent();
		if (flag) {
			parentTr.css("");
		}
	});
}

function selectListItems(flag)
{
	$(".list input[type='checkbox']").each(function(){
		$(this).attr("checked", flag);
	});
}

function deleteSelectedListItems()
{
	var arr = new Array();
	var i = 0;
	$(".list input[type='checkbox']:checked").each(function(){
		var parentId = $(this).parent().parent();
		arr[i++] = parentId.attr("id");
	});
	
	if (cfg["lang"] == "en") {
		var msgSelectedZero = "Select no less than one item";
		var msgConfirm1 = "Are you delete ";
		var msgConfirm2 = " items?";
	} else {
		var msgSelectedZero = "Aby usunąć zaznacz przynajmniej jedną pozycję z listy";
		var msgConfirm1 = "Jesteś pewnien że chcesz usunąć ";
		var msgConfirm2 = " pozycji?";
	}
	
	if (arr.length > 0 ) {
		if (!confirm(msgConfirm1 + arr.length + msgConfirm2)) {
			return;
		}
	} else {
		alert(msgSelectedZero);
		return;
	}
	
	var serialized = serializeIds(arr);
	var url = cfg["deleteUrl"] + "?" + serialized;
	document.location.href = url;
}

function serializeIds(arr)
{
	var len = arr.length;
	var serialized = "";

	for (var i=0; i<len; i++) {
		var str = arr[i];
		serialized += "id[]=" + str.substring(4, str.length) + "&";
	}
	
	// usuwamy ostatni znak &
	if (serialized.length > 0) {
		serialized = serialized.substring(0, (serialized.length-1));
	}
	
	return serialized;
}

function typeTinyMce(classes)
{
	var allowType = ["fullOptions", "professionalOptions", "primaryOptions"];
	for(i=0; i<allowType.length; i++) {
		var type = allowType[i];
		var index = classes.indexOf(type);
		if (index > -1) {
			return type;
		}
	}
	return null;
}

function initTinyMce(elementId, elementClass, cssUrl, filesUrl, lang)
{
	var upImagesPath = "../../../tiny/index/tiny_images/"+lang;
	var upLinksPath = "../../../tiny/index/tiny_links/"+lang;
	var upMediaPath = "../../../tiny/index/tiny_media/"+lang;
	var upMediaImagesPath = "../../../tiny/index/tiny_media_images/"+lang;
	//var upMediaImagesPath = "../../../../adm/uploader/index/type/media-images/lang/"+lang;
        
	tinyMCE.addI18n('pl.my_uploader',{
		images_name: "Wybierz", images_path: upImagesPath,
		links_name: "Wybierz", links_path: upLinksPath,
		media_name: "Wybierz", media_path: upMediaPath,
		media_images_name: "Zdjęcie", media_images_path: upMediaImagesPath
	});
	tinyMCE.addI18n('en.my_uploader',{
		images_name: "Select", images_path: upImagesPath,
		links_name: "Select", links_path: upLinksPath,
		media_name: "Select", media_path: upMediaPath,
		media_images_name: "Select photo", media_images_path: upMediaImagesPath
	});

	if (elementClass.indexOf('fullOptions') > -1) 
	{
		tinyMCE.init({
		
			// General options
			mode : "exact",
			language : lang,
			elements : elementId,
			theme : "advanced",
			plugins : "safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template",

			//URL
			relative_urls : false,
			//document_base_url: filesUrl,
			
			// Theme options
			theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
			theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
			theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
			theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_resizing : true,
		
			// Example content CSS (should be your site CSS)
			content_css : cssUrl,
		
			// Drop lists for link/image/media/template dialogs
			template_external_list_url : "lists/template_list.js",
			external_link_list_url : "lists/link_list.js",
			external_image_list_url : "lists/image_list.js",
			media_external_list_url : "lists/media_list.js",
		
			// Replace values for the template plugin
			template_replace_values : {
				username : "Some User",
				staffid : "991234"
			}
		});
	}
	else if (elementClass.indexOf('professionalOptions') > -1) 
	{
		tinyMCE.init({
			// General options
			theme : "advanced",
			plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount,advlist",
			mode : "exact",
			elements : elementId,
			language : lang,
			
			relative_urls : false,
			//document_base_url: filesUrl,

			// Theme options
			theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,fontselect,fontsizeselect,|,undo,redo,|,link,unlink,image,media,code,|,forecolor,backcolor,|,fullscreen",
			theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,bullist,numlist,|,outdent,indent,blockquote,|,tablecontrols,|,removeformat,visualaid,|,ltr,rtl",
			theme_advanced_buttons3 : "",
			theme_advanced_buttons4 : "",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_resizing : true,

			// Example content CSS (should be your site CSS)
			content_css : cssUrl,

			// Drop lists for link/image/media/template dialogs
			template_external_list_url : "lists/template_list.js",
			external_link_list_url : "lists/link_list.js",
			external_image_list_url : "lists/image_list.js",
			media_external_list_url : "lists/media_list.js",
			
			// Style formats
			style_formats : [
				{title : 'Bold text', inline : 'b'},
				{title : 'Red text', inline : 'span', styles : {color : '#ff0000'}},
				{title : 'Red header', block : 'h1', styles : {color : '#ff0000'}},
				{title : 'Example 1', inline : 'span', classes : 'example1'},
				{title : 'Example 2', inline : 'span', classes : 'example2'},
				{title : 'Table styles'},
				{title : 'Table row 1', selector : 'tr', classes : 'tablerow1'}
			],

			// Replace values for the template plugin
			template_replace_values : {
				username : "Some User",
				staffid : "991234"
			}
		});
	}
	else if (elementClass.indexOf('primaryOptions') > -1) 
	{
		tinyMCE.init({
			
			// General options
			theme : "advanced",
			plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount,advlist",
			mode : "exact",
			elements : elementId,
			language : lang,

			//URL
			relative_urls : false,
			//document_base_url: filesUrl,
			
			// Theme options
			theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,image",
			theme_advanced_buttons2 : "",
			theme_advanced_buttons3 : "",
			theme_advanced_buttons4 : "",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "bottom",
			theme_advanced_resizing : true,
		
			// Example content CSS (should be your site CSS)
			content_css : cssUrl,
		
			// Drop lists for link/image/media/template dialogs
			template_external_list_url : "lists/template_list.js",
			external_link_list_url : "lists/link_list.js",
			external_image_list_url : "lists/image_list.js",
			media_external_list_url : "lists/media_list.js",
		
			// Replace values for the template plugin
			template_replace_values : {
				username : "Some User",
				staffid : "991234"
			}
			
		});
	} else {
		alert("Nieznany typ edytora przy inicjalizacji: " + elementClass);
	}

	
}

function menuInit()
{/*
	$("#menu ul ul li:not(:first)").each(function(){
		$(this).attr("class", "topBorder");
	});
*/
}

function unselectDisabled(it) {
	if( it.options[it.selectedIndex].disabled ) {
		it.selectedIndex++;
	}
} 

