if(typeof(console) == 'undefined' || console == null) {
	
}


function include(filename) {
	var head = document.getElementsByTagName('head')[0];
	
	script = document.createElement('script');
	script.src = filename;
	script.type = 'text/javascript';
	
	head.appendChild(script)
	
	
}


include("js/tiny_mce/jquery.tinymce.js");

var editOn = false;

$(document).ready(function() {
	

	var adminPath = "?view=admin%2F";

	$(".editIcon").click(function(event) {
		event.preventDefault();
		openEditWindow($(this).attr("target"));
	});
	
	$(".addIcon").click(function(event) {
		event.preventDefault();
		openAddWindow($(this).attr("target"));
	});
	
	$("#CMS_logout").click(function() {
		window.location.replace("?logout=1&view=index");
	})
	
	$("#CMS_clearcache").click(function() {
		var ok = confirm("Are you sure you want reset the cache of ALL PAGES?");
		if(ok) resetCache();
	});
	
	$("#CMS_blocks").click(function() {
		
		window.location.replace(adminPath+"CMS_blocks");
	});
	
	$("#CMS_pagesadmin").click(function() {
		window.location.replace(adminPath+"CMS_pages");
	});
	
	$("#CMS_newsadmin").click(function() {
		window.location.replace(adminPath+"CMS_news");
	});
	
	$("#CMS_jobsadmin").click(function() {
		window.location.replace(adminPath+"CMS_jobs");
	});
	
	$("#CMS_fileadmin").click(function() {
		window.location.replace(adminPath+"CMS_files");
	});
	
	$("#CMS_useradmin").click(function() {
		window.location.replace(adminPath+"CMS_users");
	});
	
	$("#CMS_frontpage").click(function() {
		window.location.replace("?view=index");
	});
	
	var left = ($(document).width() - $("#CMSEDITWINDOW").width()) / 2; 
	var top = ($(document).height() - $("#CMSEDITWINDOW").height()) / 2;
	
	$("#CMSEDITWINDOW").css("left", left);
	$("#CMSEDITWINDOW").css("top", top);
	
	$("#CMSEDITWINDOW").draggable();
	
	// TINYMCE SETTINGS
	
	$("#CMSEDITWINDOW .tinyEditor").css("width", "100%");
	$("#CMSEDITWINDOW .tinyEditor").css("height", "320px");
	$(".tinyEditor").tinymce({
		script_url: 'js/tiny_mce/tiny_mce.js',
		theme: 'advanced',
		css: 'css/kilo.css',
		theme_advanced_toolbar_location : 'top',
		force_root_block: 'div'
	});
	
	
	$("#showEditor").click(function(event) {
		event.preventDefault();
		$(".tinyEditor").tinymce().show();
		$("#showEditor").css("display", "none");
		$("#hideEditor").css("display", "inline");
	});
	
	$("#hideEditor").click(function(event) {
		event.preventDefault();
		$(".tinyEditor").tinymce().hide();
		$("#showEditor").css("display", "inline");
		$("#hideEditor").css("display", "none");
	});
	
});


function openWindow(title) {
	editOn = true;
	var editWin = $("#CMSEDITWINDOW");
	editWin.css("display", "block");
	
	
	
	$("#CMSEDITWINDOW .title").html(title);
	
	
	$("#CMSEDITWINDOW .close").click(function() {
		editOn = false;
		var editWin = $("#CMSEDITWINDOW");
		editWin.css("display", "none");
	});
	
	
	$("#CMSEDITWINDOW button.cancel").click(function() {
		editOn = false;
		var editWin = $("#CMSEDITWINDOW");
		editWin.css("display", "none");
	});
	
}

function openEditWindow(target) {

	openWindow("Edit block");
	
	$("#CMSEDIT_KEY").attr("disabled",  true);
	
	$.ajax({
		url: 'ajax.php?action=getBlock&key='+target,
		dataType: 'json',
		contentType: "application/json; charset=utf-8",
		success: function(json, value) {
			
			$("#CMSEDIT_ID").val(json.id);
			$("#CMSEDIT_KEY").val(json.key);
			$("#CMSEDIT_LANG").val(json.lang);
			
			$("#CMSEDIT_TEXT").val(json.text);
			
			$("#CMSEDIT_VISIBLE").val(json.visible);
			
			if(json.visible =="t")
				$("#CMSEDIT_VISIBLE").attr("checked", "checked");
			else
				$("#CMSEDIT_VISIBLE").attr("checked", "");
		}
	});
	
	
	$("#CMSEDITWINDOW button.ok").click(function() {
		
		var id = $("#CMSEDIT_ID").val();
		var key = $("#CMSEDIT_KEY").val();
		var text = $("#CMSEDIT_TEXT").val();
		
		var lang = $("#CMSEDIT_LANG").val();
		
		var visible = "N";
		if($("#CMSEDIT_VISIBLE:checked").val() != null) {
			visible = "Y";
		}
		
		
		$.ajax({
			url: 'ajax.php?action=updateBlock',
			dataType: 'json',
			type: 'post',
			data: {'id':id, 'lang': lang, 'key':key, 'visible': visible, 'text':text },
			success: function(json, value){

			}
		});	
		
		
		editOn = false;
		var editWin = $("#CMSEDITWINDOW");
		editWin.css("display", "none");
		resetCache();
		window.location.replace("?");
		
	});	
}

function openAddWindow(target){
	
	openWindow("Add new block");
	
	$("#CMSEDIT_KEY").val(target);
	$("#CMSEDIT_KEY").attr("disabled",  false);
	
	$("#CMSEDIT_LANG").val($("#currentLang").val());
	
	$("#CMSEDITWINDOW button.ok").click(function() {
			
		var key = $("#CMSEDIT_KEY").val();
		var text = $("#CMSEDIT_TEXT").val();
		
		var lang = $("#CMSEDIT_LANG").val();
		
		var visible = "N";
		if($("#CMSEDIT_VISIBLE:checked").val() != null) {
			visible = "Y";
		}
		
		var data = {'lang': lang, 'key':key, 'visible': visible, 'text':text };
		$.ajax({
			url: 'ajax.php?action=insertBlock',
			dataType: 'json',
			type: 'post',
			data: data,
			success: function(json, value){
			}
		});	
		
		
		editOn = false;
		var editWin = $("#CMSEDITWINDOW");
		editWin.css("display", "none");
		resetCache();
		window.location.replace("?");
	});
}

function resetCache() {
	$.ajax({
		url: 'ajax.php?action=clearCache',
		success: function() {
		}
	});
}

