function activate(menu)
  {
  document.getElementById(menu).style.background = "#CECFCE";
  document.getElementById(menu).firstChild.firstChild.firstChild.nextSibling.style.color = "#636973";
  document.getElementById("menu_"+menu).style.display= "block";
  
  document.getElementById(old_menu).style.background = "#636973";
  document.getElementById(old_menu).firstChild.firstChild.firstChild.nextSibling.style.color = "#FFFFFF";
  document.getElementById("menu_"+old_menu).style.display= "none";
  
  old_menu = menu;
  document.cookie = "editor_menu = " + menu; 
}
function butt_pressed(button)
  {
  button.className="mybutton_pressed";
}

function butt_released(button)
  {
  button.className="mybutton_released";
}

HTMLmode = true;
function changeMode(mode)
  {
  if (mode==1)
     {
     document.getElementById("b_html").disabled = true;
     document.getElementById("b_wysiwyg").disabled = false;
     h_text = document.frames["mainframe"].document.body.innerHTML;
     h_text = correct_crosslinks(h_text);
     document.frames["mainframe"].document.body.innerText=h_text;
     HTMLmode = false;
  }
  if (mode==2)
     {
     document.getElementById("b_wysiwyg").disabled = true;
     document.getElementById("b_html").disabled = false;
     h_text = document.frames["mainframe"].document.body.innerText;
     h_text = correct_crosslinks(h_text);
     document.frames["mainframe"].document.body.innerHTML=h_text;
     HTMLmode = true;
  }
  
}

function doCommand(command)
  {
  if (document.queryCommandSupported(command))
     {
     if (HTMLmode!=true){return;}
     mainframe.focus();
     mainframe.document.execCommand(command);
     mainframe.focus();
  }
  else
     {
     switch (command){
       case "addImage" :     command_addImage(); break;
       case "analize" :      command_analize(); break;
       case "defaults":      command_setDefaults();break;
       case "AlignJustify":  command_Justify();break;
     }
  }
  
}

function command_Justify()
{
    // Sperva delajem justify Left tomu kotorij u nas budet Jusified
    mainframe.focus();
    mainframe.document.execCommand("JustifyLeft");
    mainframe.focus();

    // Potom zapominajem , tot element kotoromu bil sdelan Left
    b=mainframe.document.body;
    f=b.getElementsByTagName('P');
	if(f.length+''!='undefined'){
        for(i=f.length-1;i>=0;i--){
            tempJust = f[i].getAttribute("align");
            f[i].setAttribute("tempMark",tempJust);
        }
    }
    // Sperva delajem justify Right tomu kotorij u nas budet Jusified
    mainframe.focus();
    mainframe.document.execCommand("JustifyRight");
    mainframe.focus();
    
    // Nahodim tot element kotorij u nas menjalsa i ego menjajem na Justify
    b=mainframe.document.body;
    f=b.getElementsByTagName('P');
	if(f.length+''!='undefined'){
        for(i=f.length-1;i>=0;i--){
            tempJust = f[i].getAttribute("align");
            oldJust = f[i].getAttribute("tempMark");
            if (tempJust!=oldJust){
                f[i].setAttribute("align","justify");
            }
            f[i].removeAttribute("tempMark");
        }
        
    }
		
}


function command_crosslink(lang)
  {
  if (window.Popup)
     {
     Popup.close();
     Popup=open("../crosslinks/site_tree.php?lang="+lang,"","toolbars=no,width=400,height=400,left=300,top=200,resizable=yes,statusbars=no");
  }
  else
     Popup=open("../crosslinks/site_tree.php?lang="+lang,"","toolbars=no,width=400,height=400,left=300,top=200,resizable=yes,statusbars=no");
}
function command_crosslink_inner(lang)
  {
  if (window.Popup)
     {
     Popup.close();
     Popup=open("../crosslinks/site_tree.php?lang="+lang+"&mode=3","","toolbars=no,width=400,height=400,left=300,top=200,resizable=yes,statusbars=yes,scrollbars=yes");
  }
  else
     Popup=open("../crosslinks/site_tree.php?lang="+lang+"&mode=3","","toolbars=no,width=400,height=400,left=300,top=200,resizable=yes,statusbars=yes,scrollbars=yes");
}

function command_addImage()
  {
  if (window.Popup)
     {
     Popup.close();
     Popup=open("tools/addimage.php","","toolbars=no,width=400,height=100,left=300,top=200,resizable=yes,statusbars=no");
  }
  else
     Popup=open("tools/addimage.php","","toolbars=no,width=400,height=100,left=300,top=200,resizable=yes,statusbars=no");
}

function toggleMenu(menu)
  {
  if (menu.style.display=="block" || menu.style.display=="inline")
     menu.style.display="none";
  else 
     menu.style.display="inline";
}
function command_analize()
  {
  window.showModalDialog("tools/analize_html.php",window,"dialogWidth:500px;dialogHeight:400px;dialogLeft:300px;dialogTop:200;resizable:yes;status:no");
//  window.open("tools/analize_html.php","","dialogWidth:500px;dialogHeight:400px;dialogLeft:300px;dialogTop:200;resizable:yes;status:no");
}
function command_setDefaults()
  {
  if (window.Popup)
     {
     Popup.close();
     Popup=open("tools/setDefaults.php","","toolbars=no,width=500,height=500,left=250,top=150,resizable=yes,statusbars=no");
  }
  else
     Popup=open("tools/setDefaults.php","","toolbars=no,width=500,height=500,left=250,top=150,resizable=yes,statusbars=no");
}

function savingData()
  {
  if (HTMLmode==false)
     text_to_save = document.frames["mainframe"].document.body.innerText;
  if (HTMLmode==true)
     text_to_save = document.frames["mainframe"].document.body.innerHTML;

  text_to_save = correct_crosslinks(text_to_save)  
  opener.window.document.getElementById(opener_element).value = text_to_save;
  window.close();
}


function correct_crosslinks(text)
  {
  currentLink  = document.frames["mainframe"].document.location.href.replace("\x3F","\\x3F");
  var re = new RegExp(currentLink,"gim");
  text = text.replace(re, "");   
  return text;
}


