function openAuthDialog(url,from)
{
    if($("#passportAuthDialog").attr("ready")=='1')
    {
        $("#passportAuthDialog").dialog('open');
    }
    else
    {
        showLoadingScreen();
        $("#passportAuthDialog > iframe").attr("from",from).attr("src",url).load(function(){showAuthDialog();});
    }
}

function showAuthDialog()
{
    if ($("#passportAuthDialog").attr("ready")=="2")
    {
        window.location.reload();
    }
    if ($("#passportAuthDialog").attr("ready")=="1")
    {
       $("#passportAuthDialog").attr("ready","2");
    }
    
    if ($("#passportAuthDialog").attr("ready")=="0")
    {
        closeLoadingScreen();
        $("#passportAuthDialog").dialog({
            draggable:false,
            width:450,
            title:'Авторизация в системе',
            open:function(a,b){$("#passportAuthDialog").attr("ready","1");},
            height:260,
            resizable:false,
            overlay: {
            opacity: 0.7,
            background: "black"
            }
        });
    }
}

function openRegDialog(url,from)
{
    if($("#passportRegDialog").attr("ready")=='1')
    {
        $("#passportRegDialog").dialog('open');
    }
    else
    {
        showLoadingScreen()
        $("#passportRegDialog > iframe").attr("src",url).load(function(){showRegDialog()});
    }
}

function showRegDialog()
{
    $("#passportRegDialog").attr("ready", "1");
    closeLoadingScreen();
    $("#passportRegDialog").dialog({
		draggable:false,
        width:520,
        title:'Регистрация в системе',
        height:520,
		resizable:false,
		overlay: {
		opacity: 0.7,
     	background: "black"
 		}
	});
}


function showLoadingScreen()
{
	//$("#loadingscreen").css("display","block");

	$("#loadingscreen").dialog({
		//bgiframe: true,
		height: 140,
		width: 270,
		draggable:false,
		resizable:false,
		//modal: true,
		overlay: {
		opacity: 0.7,
     	background: "black"
 		}
	});
}

function closeLoadingScreen()
{
	$("#loadingscreen").dialog('close');
	$("#loadingscreen").dialog('destroy');
	//$("#dialogs").append("<div style='display:none; padding:30px;' id=loadingscreen><span id=loadinghtml>Идет загрузка...<br><img src=/images/loading.gif></span></div>");
}




function admin_closeDialog()
{
    closeLoadingScreen();
	$("body").children('div').slideDown();
	$("body").children('span').slideDown();
	$("body").children('table').slideDown();
	$("#dialog").slideUp();
	$("#loadingscreen").slideUp();
	$("#system_zone").slideUp();

	$("#dialog > .dialog_html").empty();
	//$("#dialog > .dialog_html").html('clip');
	//$("#dialog").dialog("close");
}

  /*
function mDialog(url)
{
	showLoadingScreen();
	url = siteURL+url+'/?session='+session;
	$("#dialog > .dialog_html").load(url,function(){
        var top_value = document.documentElement.scrollTop;
		$("#dialog").css("top",top_value);
		$("#dialog").css("height",getClientHeight());
		//$(".dialog_html").css("height",getClientHeight());
		$("#dialog").toggle("clip");
		closeLoadingScreen();

	});
}       */


function mDialog(url)
{

	//$(document).css("overflow", "hidden");
	//$('body').css("overflow", "hidden");
    if (url.indexOf('http://')!=false)
	url = siteURL+url+'/?session='+session;


	$("body").children('div').slideUp();
	$("body").children('span').slideUp();
	$("body").children('table').slideUp();
	showLoadingScreen();
	$("#dialog").css("min-height",getClientHeight());
	$.post(
        url,{session:session},
        function(data){
            $(".dialog_html").html(data);
            $("#dialog").slideDown('fast',function()
            {
                closeLoadingScreen();
            }
        )});
   // closeLoadingScreen();

}



function getClientWidth()
{
return document.getElementsByTagName('html')[0].clientWidth;
}


function getClientHeight()
{
	return document.getElementsByTagName('html')[0].clientHeight;
}



function goTo(url)
{
	window.location = url;
}

var tinyMCEenabled = false;
function initTinyMCE()
{

/*	tinyMCE.init({
			mode : "none",
			theme : "advanced",
			language : "ru",
			plugins : "advimage,advlink,media,contextmenu",
			theme_advanced_buttons1_add_before : "newdocument,separator",
			theme_advanced_buttons1_add : "fontselect,fontsizeselect",
			theme_advanced_buttons2_add : "separator,forecolor,backcolor,liststyle",
			theme_advanced_buttons2_add_before: "cut,copy,separator,",
			theme_advanced_buttons3_add_before : "",
			theme_advanced_buttons3_add : "media",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			extended_valid_elements : "hr[class|width|size|noshade]",
			file_browser_callback : "ajaxfilemanager",
			paste_use_dialog : false,
			theme_advanced_resizing : true,
			theme_advanced_resize_horizontal : true,
			apply_source_formatting : true,
			force_br_newlines : true,
			force_p_newlines : false,
			relative_urls : true,
			document_base_url : '!siteURL!/'
		});    */

}



function loadTiny(id)
{
	/*if (tinyMCE.getInstanceById(id))
	{
	    tinyMCE.execCommand('mceFocus', false, id);
	    tinyMCE.execCommand('mceRemoveControl', false, id);
	}*/

	tinyMCE.execCommand('mceAddControl', false, id);
}

function closeTiny(id)
{
	alert(id);
	tinyMCE.execCommand('mceFocus', false, id);
	tinyMCE.execCommand('mceRemoveControl', false, id);
	$("#"+id).remove();
}

function fckValue(id)
{
	var oEditor = FCKeditorAPI.GetInstance(id);
	return oEditor.GetHTML(true);
}

function ajaxfilemanager(field_name, url, type, win) {
			var ajaxfilemanagerurl = siteURL+"/engine/php/includes/editor/jscripts/tiny_mce/plugins/ajaxfilemanager/ajaxfilemanager.php";
			switch (type) {
				case "image":
					break;
				case "media":
					break;
				case "flash":
					break;
				case "file":
					break;
				default:
					return false;
			}
            tinyMCE.activeEditor.windowManager.open({
                url: siteURL+"/engine/php/includes/editor/jscripts/tiny_mce/plugins/ajaxfilemanager/ajaxfilemanager.php",
                width: 782,
                height: 440,
                inline : "yes",
                close_previous : "no"
            },{
                window : win,
                input : field_name
            });
		}

var simpleTreeCollection;
$(document).ready(function(){
$("textarea[mode=tiny]").live("load",function(){
	loadTiny($(this).attr("id"));
});

$("div[type=uploadfile]").live("mouseover",function(){
 admin_addUploadFunctions(this);
});

});






function addTree2()
{
	simpleTreeCollection = $('#simpleTree').simpleTree({
		autoclose: true,
		afterDblClick:function(node){
			//simpleTreeCollection.get(0).addNode(1,'NEW NODE');
			loadAliases($('span:first',node).attr("pageid"));
		},
		afterAjax:function()
		{
			treeOptionsVisible();
		},
		afterContextMenu:function(node)
		{
			loadAliases($('span:first',node).attr("pageid"));
		},
		animate:true,
		drag:false,
		docToFolderConvert:true
	});
	treeOptionsVisible();

}

function saveAliases()
{
	var aliases = $("input[name=aliases]").attr("value");
	var realurl = $("input[name=aliases]").attr("realurl");
	$.post(siteURL+'/admin/modulesvisible/saveAliases/',
	{
		session:session,
		aliases:aliases,
		realurl:realurl
	},function(data)
	{
		if (data.length==0)
		{
			admin_closeDialog()
		}
		else
		{
			jAlert(data,"Ошибка");
		}
	});
}

function loadAliases(pageid)
{

	mDialog('/admin/modulesvisible/loadAliases/'+pageid);

}



function setSimpleTree()
{
	var simpleTreeCollection;
	$(document).ready(function(){
		simpleTreeCollection = $('.simpleTree').simpleTree({
			autoclose: true,
			drag:false,
			animate:true
		});
	});
	treeOptionsVisible();
}


function treeOptionsVisible()
{
	var span = $("span.activeTree");
	for (var i = 0; i < span.length; i++)
	{
		$(span[i]).children("span.treeOptions").css("display",'none');
    }
	$("span.activeTree").mouseout(function(){
	$(this).children("span.treeOptions").css("display",'none');
}).mouseover(function(){
	$(this).children("span.treeOptions").css("display",'block');
});

    var span = $("span.textTree");
	for (var i = 0; i < span.length; i++)
		$(span[i]).children("span.treeOptions").css("display",'none');
	//$("span.textTree").children("span.treeOptions").css("display",'none');
	$("span.textTree").mouseout(function(){
	$(this).children("span.treeOptions").css("display",'none');
}).mouseover(function(){
	$(this).children("span.treeOptions").css("display",'block');
});


$("#empty").remove();
}

function set_editor(textid,lang)
{
editAreaLoader.init({
	id: textid
	,start_highlight: true
	,font_size: '9'
	,font_family: 'verdana, monospace'
	,allow_resize: 'n'
	,allow_toggle: false
	,syntax_selection_allow: "css,html,js,php,xml"
	,language: 'ru'
	,syntax: lang
	,toolbar: ' charmap, |, search, go_to_line, |, undo, redo, |, select_font, syntax_selection, |, change_smooth_selection, highlight, reset_highlight, |, help'
	,plugins: 'charmap'
	,charmap_default: 'arrows'

});
}

function set_editor_elemtype(textid,lang)
{
editAreaLoader.init({
	id: textid
	,start_highlight: true
	,font_size: '9'
	,font_family: 'verdana, monospace'
	,allow_resize: 'n'
	,allow_toggle: false
	,is_multi_files: true
	,EA_file_close_callback: 'onCloseEditTab'
	,EA_load_callback: "editAreaElemTypeLoaded"
	,syntax_selection_allow: "css,html,js,php,xml"
	,language: 'ru'
	,syntax: lang
	,toolbar: ' charmap, |, search, go_to_line, |, undo, redo, |, select_font, syntax_selection, |, change_smooth_selection, highlight, reset_highlight, |, help'
	,plugins: 'charmap'
	,charmap_default: 'arrows'

});
}


function set_editor_pack(textid,lang)
{
editAreaLoader.init({
	id: textid
	,start_highlight: true
	,font_size: '9'
	,font_family: 'verdana, monospace'
	,allow_resize: 'n'
	,allow_toggle: false
	,is_multi_files: true
	,EA_file_close_callback: 'onCloseEditTab'
	,EA_load_callback: "editAreaPackLoaded"
	,syntax_selection_allow: "css,html,js,php,xml"
	,language: 'ru'
	,syntax: lang
	,toolbar: ' charmap, |, search, go_to_line, |, undo, redo, |, select_font, syntax_selection, |, change_smooth_selection, highlight, reset_highlight, |, help'
	,plugins: 'charmap'
	,charmap_default: 'arrows'

});
}

function set_editor_cattype(textid,lang)
{
editAreaLoader.init({
	id: textid
	,start_highlight: true
	,font_size: '9'
	,font_family: 'verdana, monospace'
	,allow_resize: 'n'
	,allow_toggle: false
	,is_multi_files: true
	,EA_file_close_callback: 'onCloseEditTab'
	,EA_load_callback: "editAreaCatTypeLoaded"
	,syntax_selection_allow: "css,html,js,php,xml"
	,language: 'ru'
	,syntax: lang
	,toolbar: ' charmap, |, search, go_to_line, |, undo, redo, |, select_font, syntax_selection, |, change_smooth_selection, highlight, reset_highlight, |, help'
	,plugins: 'charmap'
	,charmap_default: 'arrows'

});
}

function onCloseEditTab()
{
	jAlert("Не-не-не =)");
	return false;
}

function editAreaCatTypeLoaded(id)
{
	var new_file= {id: "template", text: $("textarea[name=template]").attr("value"), syntax: 'html', title: 'Основной шаблон'};
	editAreaLoader.openFile(id, new_file);
	var new_file= {id: "headcats", text: $("textarea[name=headcats]").attr("value"), syntax: 'html', title: 'Шапка категорий'};
	editAreaLoader.openFile(id, new_file);
	var new_file= {id: "centercats", text: $("textarea[name=centercats]").attr("value"), syntax: 'html', title: 'Тело категорий'};
	editAreaLoader.openFile(id, new_file);
	var new_file= {id: "footercats", text: $("textarea[name=footercats]").attr("value"), syntax: 'html', title: 'Футер категорий'};
	editAreaLoader.openFile(id, new_file);

	var new_file= {id: "headelems", text: $("textarea[name=headelems]").attr("value"), syntax: 'html', title: 'Шапка элементов'};
	editAreaLoader.openFile(id, new_file);
	var new_file= {id: "centerelems", text: $("textarea[name=centerelems]").attr("value"), syntax: 'html', title: 'Тело элементов'};
	editAreaLoader.openFile(id, new_file);
	var new_file= {id: "footerelems", text: $("textarea[name=footerelems]").attr("value"), syntax: 'html', title: 'Футер элементов'};
	editAreaLoader.openFile(id, new_file);
    var new_file= {id: "onCreate", text: $("textarea[name=onCreate]").attr("value"), syntax: 'php', title: 'onCreate'};
	editAreaLoader.openFile(id, new_file);
    var new_file= {id: "onChange", text: $("textarea[name=onChange]").attr("value"), syntax: 'php', title: 'onChange'};
	editAreaLoader.openFile(id, new_file);
    var new_file= {id: "onDelete", text: $("textarea[name=onDelete]").attr("value"), syntax: 'php', title: 'onDelete'};
	editAreaLoader.openFile(id, new_file);
    var new_file= {id: "onShow", text: $("textarea[name=onShow]").attr("value"), syntax: 'php', title: 'onShow'};
	editAreaLoader.openFile(id, new_file);
}

function editAreaPackLoaded(id)
{
	var new_file= {id: "template", text: $("textarea[name=template]").attr("value"), syntax: 'html', title: 'Основной шаблон'};
	editAreaLoader.openFile(id, new_file);
    var new_file= {id: "onCreate", text: $("textarea[name=onCreate]").attr("value"), syntax: 'php', title: 'onCreate'};
	editAreaLoader.openFile(id, new_file);
    var new_file= {id: "onChange", text: $("textarea[name=onChange]").attr("value"), syntax: 'php', title: 'onChange'};
	editAreaLoader.openFile(id, new_file);
    var new_file= {id: "onDelete", text: $("textarea[name=onDelete]").attr("value"), syntax: 'php', title: 'onDelete'};
	editAreaLoader.openFile(id, new_file);
    var new_file= {id: "onShow", text: $("textarea[name=onShow]").attr("value"), syntax: 'php', title: 'onShow'};
	editAreaLoader.openFile(id, new_file);
}

function editAreaElemTypeLoaded(id)
{
	var new_file= {id: "form", text: $("textarea[name=form]").attr("value"), syntax: 'html', title: 'Форма для добавления'};
	editAreaLoader.openFile(id, new_file);
	var new_file= {id: "actions", text: $("textarea[name=actions]").attr("value"), syntax: 'html', title: 'Действия при добавлении'};
	editAreaLoader.openFile(id, new_file);
	var new_file= {id: "display", text: $("textarea[name=display]").attr("value"), syntax: 'html', title: 'Шаблон отображения'};
	editAreaLoader.openFile(id, new_file);
    
}