/* ------------------------------------------------------------------------

File name:            main.js
Date of creation:     19/04/2009
Date of last update:  19/04/2009
Author:               Pavel Zahradník, elektrico@email.cz
Purpose:              Main script - JavaScript
URL:                  http://www.panoramo.cz/js/main.css
Licence:              Copyright (c) 2009, 
                      Elektrico - ing. Pavel Zahradník, 
                      All rights reserved
Contact:              Táboritská 274/II.,  
                      379 01 Třeboň, 
                      +420 723 862 361, 
                      www.elektrico.cz, 
                      elektrico@email.cz
                      
------------------------------------------------------------------------ */

var myEditor;
var Menu_id_actual;
var Activelogin = false;
var ActiveloginInner = false;
var width_inner = 250;
var height_inner = 150;
var actual_ratio = 0.0;

function MarginBody() {
  var width = WindowSize("width");
  var body = document.getElementById("body");
  //document.write(width_window);
  if((width-980)>0) {
    body.style.marginLeft  = parseInt((width-980)/2)+"px";
    body.style.marginRight = parseInt((width-980)/2)+"px";
  } else {
    body.style.marginLeft  = "0px";
    body.style.marginRight = "0px";
  }
  body.style.visibility = "visible";
}
/*
function GetMenu(MenuId) 
{ 
  var MenuUrl = "./pages/menu.php?action="+MenuId;
  GetText(MenuUrl, ShowMenu, 'left_middle');
} 

function ShowMenu(xmlText, DestinationDivId)
{
  var div = document.getElementById(DestinationDivId);
  var ul_destination = document.getElementById(DestinationDivId+"_ul");
  var ul = document.createElement("ul");
  ul.setAttribute("id",DestinationDivId+"_ul");
  ul.innerHTML = xmlText;
  div.replaceChild(ul, ul_destination);
}
*/
function GetContent(page) {
  var Url = "./pages/"+page;
  //initRTE('<i>This is some <b>preloaded</b> content</i>', '../example.css');
  GetText(Url, ShowContent, 'border');
}

function ShowContent (xmlText, DestinationDivId) {
  var div = document.getElementById(DestinationDivId);
  //alert (xmlText);
  div.innerHTML = xmlText;
}

function EvalContent(page) {
  var Url = "./pages/"+page;
  GetText(Url, EvalShowContent, 'border');
  //alert("vole");
}

function EvalShowContent (xmlText, DestinationDivId) {
  if (DestinationDivId != 'null') {
    var div = document.getElementById(DestinationDivId);
  }
  
  var script, scripts;
  //alert (xmlText);
  //div.innerHTML = xmlText;
  //scripts = xmlText;
  
  scripts = [];
  var regexp = /<script[^>]*>([\s\S]*?)<\/script>/gi;
	while ((script = regexp.exec(xmlText))) scripts.push(script[1]);
			scripts = scripts.join('\n');
	
	if (scripts) (window.execScript) ? window.execScript(scripts) : window.setTimeout(scripts, 0);
	if (DestinationDivId != 'null') {div.innerHTML = xmlText;}
}


function DeleteUser(ResponseText, DestinationDivId) {
  if (ResponseText != 'true') {alert("Odtranění uživatele se nezdařilo.");} 
  setTimeout(function () {GetContent("users.php");}, 1000); 
}

function DeleteAction(ResponseText, DestinationDivId) {
  if (ResponseText != 'true') {alert("Odtranění akce se nezdařilo.");} 
  setTimeout(function () {GetContent("actions.php");}, 1000); 
}

function EditActions(ResponseText, DestinationDivId) {
  var div = document.getElementById(DestinationDivId);
  /*alert(ResponseText);*/
  if (ResponseText == 'true') {
    div.style.color = "green";
    div.innerHTML = "Operace proběhla úspěšně.";
    //GetText("./pages/session_test.php", ShowContent, 'body_inside');
    setTimeout(function () {GetContent("actions.php");}, 2000);
  } else {
    div.style.color = "red";
    div.innerHTML = "Operace se nezdařila. Některá pole nemají zřejmě platnou formu.";
    setTimeout(function () {div.innerHTML = "";}, 4000);
  }  
}

function Login(user_name, password) {
  var data = "user_name="+user_name+"&password="+password;
  PostText('./pages/login_input.php', data, LoginAccess, 'login_message');
}

function LoginAccess(ResponseText, DestinationDiv) {
  var div = document.getElementById(DestinationDiv);
  //alert(ResponseText);
  if (ResponseText == 'true') {
    div.style.color = "green";
    div.innerHTML = "Příhlášení proběhlo úspěšně.";
    //GetText("./pages/session_test.php", ShowContent, 'body_inside');
    setTimeout(function () {LoginSuccess(true);}, 2000);
  } else {
    div.style.color = "red";
    div.innerHTML = "Příhlášení se nezdařilo.";
    setTimeout(function () {GetText("./pages/login.php", ShowContent, 'login_inner');}, 2000);
  }   
}

function LoginSuccess(afterLogin) {
  if (afterLogin == true) {
    LoginClose(true);
    window.location.href = "index.php";
  }
  /*GetMenu('0');*/
  /*GetContent(page);*/
}

function LoginOpen()
{
  var body = document.getElementById("body");
  
  if (Activelogin == false) { 
    var loginDiv = document.createElement("div"); 
    loginDiv.setAttribute("id","login");
    loginDiv.style.position = "absolute";
    loginDiv.style.backgroundColor = "black";
    setOpacity(5.5, loginDiv);
    body.appendChild(loginDiv);
  } else {
    var loginDiv = document.getElementById("login");
  }
  
  
  ScrollX = getScrollXY("left");
  ScrollY = getScrollXY("top");
  
  loginDiv.style.width = parseInt(WindowSize('width')*actual_ratio)+"px";
  loginDiv.style.height = parseInt(WindowSize('height')*actual_ratio)+"px";
  loginDiv.style.left = parseInt(WindowSize('width')*((1/2 - actual_ratio/2))) + ScrollX +"px";
  loginDiv.style.top = parseInt(WindowSize('height')*((1/2 - actual_ratio/2))) + ScrollY +"px";
  
  if (ActiveloginInner == false) {
    var loginInner = document.createElement("div"); 
    loginInner.setAttribute("id","login_inner");
    loginInner.style.position = "absolute";
    body.appendChild(loginInner);
  } else {
    var loginInner = document.getElementById("login_inner");
  }
   
  loginInner.style.width = parseInt(width_inner*actual_ratio) + "px";
  loginInner.style.height = parseInt(height_inner*actual_ratio) + "px";
  loginInner.style.left = actual_ratio*(WindowSize('width') - width_inner)/2 + ScrollX + "px";
  loginInner.style.top = actual_ratio*(WindowSize('height') - height_inner)/2 + ScrollY + "px";

  actual_ratio += 0.1;
  
  if (actual_ratio > 1.0) {
    loginDiv.style.left = ScrollX + "px";
    loginDiv.style.top = ScrollY + "px";
    setOpacity(8.5, loginDiv);
    
    var Url = "./pages/login.php";
    GetText(Url, ShowContent, 'login_inner');
    return true;
  } else {
    Activelogin = true;
    ActiveloginInner = true;
    setTimeout(function () {LoginOpen()}, 50);
  }
}

function LoginScroll() {
  if (Activelogin == true) {
    var loginDiv = document.getElementById("login");
    var loginInner = document.getElementById("login_inner");
    
    ScrollX = getScrollXY("left");
    ScrollY = getScrollXY("top");
  
    loginDiv.style.width = WindowSize('width')+"px";
    loginDiv.style.height = WindowSize('height')+"px";
    loginDiv.style.left = ScrollX+"px";
    loginDiv.style.top = ScrollY+"px";
    
    loginInner.style.left = parseInt((WindowSize('width') - width_inner)/2 + ScrollX) + "px";
    loginInner.style.top = parseInt((WindowSize('height') - height_inner)/2 + ScrollY) + "px"; 
  }
}

function LoginResize() {
  if (Activelogin == true) {
    var loginDiv = document.getElementById("login");
    var loginInner = document.getElementById("login_inner");
    
    ScrollX = getScrollXY("left");
    ScrollY = getScrollXY("top");
  
    loginDiv.style.width = WindowSize('width')+"px";
    loginDiv.style.height = WindowSize('height')+"px";
    loginDiv.style.left = ScrollX+"px";
    loginDiv.style.top = ScrollY+"px";
    
    loginInner.style.left = parseInt((WindowSize('width') - width_inner)/2 + ScrollX) + "px";
    loginInner.style.top = parseInt((WindowSize('height') - height_inner)/2 + ScrollY) + "px"; 
  }
}

function LoginClose(success) {
  var loginDiv = document.getElementById("login");
  var loginInner = document.getElementById("login_inner");
 
  loginDiv.parentNode.removeChild(loginDiv);
  loginInner.parentNode.removeChild(loginInner);
  Activelogin = false;
  ActiveloginInner = false;
  actual_ratio = 0.0;
  
  if (success != true) { 
    /*GetMenu('1');*/
    GetContent("login_false.php");
  }
}

function GetContent(page) {
  var Url = "./pages/"+page;
  //initRTE('<i>This is some <b>preloaded</b> content</i>', '../example.css');
  GetText(Url, ShowContent, 'sdeleni_middle');
}

function ShowContent (xmlText, DestinationDivId) {
  var div = document.getElementById(DestinationDivId);
  /*alert (xmlText);*/
  div.innerHTML = xmlText;
}

function ShowMessage(id, start_rut) {
  setTimeout(function () {ShowMessage(id+1, false)}, 5000);
  GetContent('actions_show.php?ID='+id);
  /*alert (id);*/
}



