window.title_text= "HCJP Editor v0.2.2"

// document.getElementsByAttribute([string attributeName],[string attributeValue],[boolean isCommaHyphenOrSpaceSeparatedList:false])
document.getElementsByAttribute=function(attrN,attrV,multi){
    attrV=attrV.replace(/\|/g,'\\|').replace(/\[/g,'\\[').replace(/\(/g,'\\(').replace(/\+/g,'\\+').replace(/\./g,'\\.').replace(/\*/g,'\\*').replace(/\?/g,'\\?').replace(/\//g,'\\/');
    var
        multi=typeof multi!='undefined'?
            multi:
            false,
        cIterate=document.getElementsByTagName('*'),
        aResponse=[],
        attr,
        re=new RegExp(multi?'\\b'+attrV+'\\b':'^'+attrV+'$'),
        i=0,
        elm;
    while((elm=cIterate.item(i++))){
        attr=elm.getAttributeNode(attrN);
        if(attr &&
            attr.specified &&
            re.test(attr.value)
        )
            aResponse.push(elm);
    }
    return aResponse;
}

function Ajax(odkud, kam, val){
    var http_zadost;

    if (window.XMLHttpRequest) { // Mozilla, Safari, Opera, Konqueror, ...
        http_zadost = new XMLHttpRequest();
        http_zadost.overrideMimeType('text/html');
    } else if (window.ActiveXObject) { // Internet Explorer
        http_zadost = new ActiveXObject("Microsoft.XMLHTTP");
    }

    http_zadost.onreadystatechange = function() { Zapis(http_zadost, kam, val); };
    http_zadost.open('GET', odkud, true);
    http_zadost.send(null);
}

function AjaxPOST(odkud, kam, co, val){
    var http_zadost;

    if (window.XMLHttpRequest) { // Mozilla, Safari, Opera, Konqueror, ...
        http_zadost = new XMLHttpRequest();
        http_zadost.overrideMimeType('text/html');
    } else if (window.ActiveXObject) { // Internet Explorer
        http_zadost = new ActiveXObject("Microsoft.XMLHTTP");
    }

    http_zadost.onreadystatechange = function() { Zapis(http_zadost, kam, val); };
    http_zadost.open('POST', odkud, true);
    http_zadost.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    http_zadost.send(co);
}

function Zapis(http_zadost, kam, val) {

    if (http_zadost.readyState == 4) {
        if (http_zadost.status == 200) {
            if (val==true){
                document.getElementById(kam).value= http_zadost.responseText;
            } else{
                document.getElementById(kam).innerHTML= http_zadost.responseText;
            }
        } else {
            alert("Problem with ajax("+ kam+ ")!");
        }
    }
}

function Click(){
    if (click==0){
        click= 1;
        document.getElementsByName(this.name+ "List")[0].style.display='Inherit';
    }
    else {
        click= 0;
        document.getElementsByName(this.name+ "List")[0].style.display='None';
    }
}

function HideAll(){
    for(i= 0; i<document.getElementsByAttribute("id", "MenuText").length; i++){
        document.getElementsByName(document.getElementsByAttribute("id", "MenuText")[i].name+ "List")[0].style.display='None';
    }
}

function HideAllOthers(what){
    for(i= 0; i<document.getElementsByAttribute("id", "MenuText").length; i++){
        if (document.getElementsByAttribute("id", "MenuText")[i].name!=what){
            document.getElementsByName(document.getElementsByAttribute("id", "MenuText")[i].name+ "List")[0].style.display='None';
        }
        else{
            document.getElementsByName(document.getElementsByAttribute("id", "MenuText")[i].name+ "List")[0].style.display='None';
        }
    }
}

//Zmeni MenuText prvek na oznaceny
function ChMenuTextOMO(){
    this.id= 'MenuTextOMO';
    if (click==1){
        HideAllOthers(this.name);
        document.getElementsByName(this.name+ "List")[0].style.display='Inherit';
    }
    else{
        HideAll();
    }
}

//Zmeni MenuText prvek na puvodni
function ChMenuText(){
    this.id= 'MenuText';
}

//Zmeni MenuText prvek na oznaceny
function ChMenuItemOMO(){
    this.id= 'MenuItemOMO';
}

//Zmeni MenuText prvek na puvodni
function ChMenuItem(){
    this.id= 'MenuItem';
}

function ShowHideById(what, showhide){
    if (showhide.toLowerCase()=="show"){
        document.getElementById(what).style.display= "Inherit";
    } else if (showhide.toLowerCase()=="hide"){
        document.getElementById(what).style.display= "None";
    }        
}

function ShowHideByAttribute(attr, value){  // kurva, jsem kus kokota! kdybch tohle sepsal uz driv, dalo by se vse udelat o moc jednoduseji!
    display= document.getElementsByAttribute(attr, value)[0].style.display;

    if (display=="none"){
        document.getElementsByAttribute(attr, value)[0].style.display= "Inherit";
    } else {
        document.getElementsByAttribute(attr, value)[0].style.display= "None";
    } 
}

// Jestlize nekdo clickne na zalozku HTML
function OnClickView(){
    ShowHideById('ViewBox', 'show');
    ShowHideById('EditBox', 'hide'); 

    SavePage(window.page_name);

    document.getElementsByAttribute("name", "Edit")[0].id= "SwitchBarUnMarked";
    document.getElementsByAttribute("name", "View")[0].id= "SwitchBarMarked";
}

// Jestlize nekdo clickne na zalozku Creole
function OnClickEdit(){
    ShowHideById('EditBox', 'show');
    ShowHideById('ViewBox', 'hide');

    document.getElementsByAttribute("name", "Edit")[0].id= "SwitchBarMarked";
    document.getElementsByAttribute("name", "View")[0].id= "SwitchBarUnMarked";
}

// Nastavi titulek dokumentu
function SetTitle(text){
    if (text!=undefined){
        document.title= text+ " - "+ window.title_text;
    }
    else{
        document.title= " "+ window.title_text;
    }
}

function SetStatusBarText(text){
    if (text!=undefined){
        document.getElementById("StatusBarText").innerHTML= text;
    }
    else{
        document.getElementById("StatusBarText").innerHTML= " ";
    }
}

function LoadListOfProjects(){
    Ajax("getprojects", "OpenProjectWindowList");
}

function LoadListOfProjectsForDel(){
    Ajax("getprojectsfordel", "DeleteProjectWindowList");
}

function OpenProject(name){
    Ajax("getprojecttree?project_name="+ name, "ListBox");
    OpenPage(name+ "/index.txt")

    window.project_name= name;
    SetStatusBarText("Opened");
}

function OpenPage(name){
    end= ""
    end= name.substring(name.length-6, name.length)

    reg= /\..*/

    if ((end!=".txt") && (!reg.test(end))){
        name= name+ ".txt";
    }

    Ajax("getsource?page="+ name, "EditBoxContent", true);
    Ajax("gethtml?page="+ name, "ViewBoxContent");

    window.page_name= name;

    SetTitle(name.substring(0, name.length-4));
    SetStatusBarText("Opened");
}

function NewProject(){
    name= document.getElementById('NewProjectWindowValue').value;
    Ajax('createproject?name='+ name, 'StatusBarText');
    ShowHideById('NewProjectWindow', 'hide'); 
    document.getElementById('NewProjectWindowValue').value= '';

    setTimeout("OpenProject('"+ name+ "');",500);
}

function NewPage(){
    name= document.getElementById('NewPageWindowValue').value;
    Ajax('createpage?name='+ name, 'StatusBarText');
    ShowHideById('NewPageWindow', 'hide'); 
    document.getElementById('NewPageWindowValue').value= "";

    setTimeout("OpenProject('"+ window.project_name+ "');",500);
}

function SavePage(name){
    content= document.getElementById('EditBoxContent').value;

    AjaxPOST('savepage', 'StatusBarText', 'name='+ window.page_name+ '&content='+ content);
    setTimeout("OpenPage('"+ window.page_name+ "');",500);
}

function SaveProject(){
    Ajax("saveproject", 'StatusBarText');
}

function DeletePage(){
    name= document.getElementById('DeletePageName').value;

    if (name=="index"){
        alert("You cant delete index!");
        document.getElementById('DeletePageName').value= "";
    }

    Ajax("deletepage?name="+ name, 'StatusBarText');
    document.getElementById('DeletePageName').value= "";
    
    setTimeout("OpenProject('"+ window.project_name+ "');",500);
}

function DeleteProject(name){
    Ajax('deleteproject?name='+ name, 'StatusBarText');
    document.getElementById("ListBox").innerHTML= "";
    window.page_name= "";
    window.project_name= "";
    
    OpenPage("");
    SetTitle();
}



// Yap, jsem lama, ale jinak nemuzu dostat z divu ve FF name (zkuste si udelat div, dat mu atribut name a metodu onclick= alert(this.name))
var Name = new Array("MenuTextFile", "MenuTextOptions", "MenuTextHelp");
click= 0;

for (i= 0; i<document.getElementsByAttribute("id", "MenuText").length; i++){
    document.getElementsByAttribute("id", "MenuText")[i].name= Name[i];
    document.getElementsByAttribute("id", "MenuText")[i].onmouseover= ChMenuTextOMO; // Po najeti mysi na MenuText prvek
    document.getElementsByAttribute("id", "MenuText")[i].onmouseout= ChMenuText;   // Po odjeti mysi z MenuText prvku
    document.getElementsByAttribute("id", "MenuText")[i].onclick= Click;
}

for (i= 0; i<document.getElementsByName('MenuItem').length; i++){
    document.getElementsByName('MenuItem')[i].onmouseover = ChMenuItemOMO;
    document.getElementsByName('MenuItem')[i].onmouseout = ChMenuItem;
}

SetTitle();
document.getElementById("EditBoxContent").value= ""
Ajax("loadproject", "EditBoxContent", true);
setTimeout('OpenProject(document.getElementById("EditBoxContent").value)',500);

window.setInterval('SetStatusBarText("Ready")', 10000);