//////////////////////////////////////////
//                                      //
//               SYSTEM                 //
//                                      //
//////////////////////////////////////////


System.UNDEFINED_ALERT = "Nedefinirana napaka";

System.XML_FILE_NAME = "system_properties.xml";
System.STRUCTURE_TAG_NAME = "structure";
System.LANGUAGES_TAG_NAME = "languages";
System.LANGUAGE_TAG_NAME = "language";
System.CONTENT_TAG_NAME = "content";
System.DIR_TAG_NAME = "dir";


System.keyCode = null;
System.moz = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined'); 
System.ie = (typeof window.ActiveXObject != 'undefined'); 

// id's //
System.refreshTextFieldId = "refreshTextField";
System.languageTextFieldId = "languageTextField";
System.menuIndexTextFieldId = "menuIndexTextField";
System.nodeIdTextFieldId = "nodeIdTextField";
System.pageIndexTextFieldId = "pageIndexTextField";
System.menuPanelId = "menuPanel";
System.contentFrameId = "contentFrame";
System.treePanelElemId = "treePanel";
System.titlePanelId = "titlePanel";
System.pageTitlePanelId = "pageTitlePanel"
System.pageNavigationPanelId = "pageNavigationPanel";
System.pageNavigationPosPanelId = "pageNavigationPosPanel";
System.pageDocumentPanelId = "pageDocumentPanel";
System.homeMenuId = "homeMenu";

// tree panel size //
// 800x600: 190px; 1024x760: 260px //
System.treePanelElemWidthSmall = 190;
System.treePanelElemWidthLarge = 240;

var system = null;

function System () 
{
  this.languages = new Array();
  this.contentDirPath = null;
  this.menu = null;
}

System.init = function()
{
  // create system object //
  system = new System();
  var filePath = System.XML_FILE_NAME;
  // import system properties xml file //
  var xmlDoc = XmlTools.importXML(filePath);
  system.parseXML(xmlDoc, filePath);
  WizardManager.showCurrentPage();
}

System.changeLanguage = function(language)
{
  if(System.getLanguageTextFieldElem().value != language)
  {
    System.getLanguageTextFieldElem().value = language;  
    System.changeHomeIconTitle();
    Menu.changeLanguage();        
  }  
}

System.changeHomeIconTitle = function()
{
  var language = System.getLanguageTextFieldElem().value;
  if(language == "sl")
  {
    System.getHomeMenuElem().title = "Uvodnik";
  }
  else if (language == "en")
  {
    System.getHomeMenuElem().title = "Editorial";
  }
}

System.prototype.parseXML = function(xmlDoc, filePath)
{
  var structureElements = xmlDoc.getElementsByTagName(System.STRUCTURE_TAG_NAME);

  if(XmlTools.isXMLFormatValid(structureElements, filePath))
  {
    var languagesTag = structureElements[0].getElementsByTagName(System.LANGUAGES_TAG_NAME);
    var languageTag = languagesTag[0].getElementsByTagName(System.LANGUAGE_TAG_NAME);
    for(var i=0; i<languageTag.length; i++)
    {     
      var nameTag = languageTag[i].getElementsByTagName(Language.NAME_TAG_NAME);
      var imagePathTag = languageTag[i].getElementsByTagName(Language.IMAGE_PATH_TAG_NAME);
      var descTag = languageTag[i].getElementsByTagName(Language.DESC_TAG_NAME); 
      var welcomeTitleTag = languageTag[i].getElementsByTagName(Language.TITLE_TAG_NAME);          
      var welcomeTextTag = languageTag[i].getElementsByTagName(Language.TEXT_TAG_NAME);          
     
      var name = XmlTools.getNodeValue(nameTag, 0);
      var imagePath = XmlTools.getNodeValue(imagePathTag, 0);
      var desc = XmlTools.getNodeValue(descTag, 0);
      var welcomeTitle = XmlTools.getNodeValue(welcomeTitleTag, 0);
      var welcomeText = XmlTools.getNodeValue(welcomeTextTag, 0); 
     
      var language = new Language(name, imagePath, desc, welcomeTitle, welcomeText);
      this.languages[i] = language;
    }
    var contentTag = structureElements[0].getElementsByTagName(System.CONTENT_TAG_NAME);
    var dirTag = contentTag[0].getElementsByTagName(System.DIR_TAG_NAME);
    var contentDirPath = XmlTools.getNodeValue(dirTag, 0);
    this.contentDirPath = contentDirPath;
  }
}

System.prototype.getLanguage = function()
{
  for(var i=0; i<this.languages.length;i++)
  {
    var language = this.languages[i];
    if(language.name == this.getLanguageName())
    {
      return language;
    }
  }
  return null;
}

System.prototype.getLanguageName = function()
{
  return System.getLanguageTextFieldElem().value == "" ? null : System.getLanguageTextFieldElem().value;
}

System.contentFrameInit = function()
{ 
  var image = new Image();
  image.src = "img/contentBg.gif";
  var body = null;
  
  try 
  {
    body = window.contentFrame.document.body
    
    body.style.border = "0px solid white";
  
    try 
    {
      window.contentFrame.loadBackgroundImage(image);
    } catch(ex){}
  } 
  catch(ex)
  {    
    System.getContentFrameElem().src = "errorContent.html";
    alert("Vsebinski frame se ni inicializiral. Verjetno zaradi tega,\nker ne najde pripadajočo .html datoteko!");
    window.contentFrame.loadBackgroundImage(image);
  }
  
  
  if(typeof(window.contentFrame.loadBackgroundImage) == 'undefined')
  {
    System.getContentFrameElem().src = "emptyContent.html";
  }  
  
  // glej censped.js //
  //parentWindow.hideTimerFrame();
} 

System.bodyKeyDown = function(event) 
{
  System.keyCode = event.keyCode;
}

System.bodyClick = function(event) 
{
  if ((window.event && window.event.keyCode != 1) || (window.Event && event.which == 1))
  {
    if(event.ctrlKey && event.altKey)
    {
      System.getRefreshTextFieldElem().value = "";
      System.getLanguageTextFieldElem().value = "";
      System.getMenuIndexTextFieldElem().value = "";
      System.getNodeIdTextFieldElem().value = "";
      System.getPageIndexTextFieldElem().value = "";
      System.init();
    }  
  }
}


System.convertClickToDefaultImagePath = function (elem, path)
{
  if(elem.src.indexOf("_click") != -1)
  {
    path = path.replace('_click', "");
    elem.src = path;
  }  
}

System.convertDefaultToClickImagePath = function (elem, path)
{
  if(elem.src.indexOf("_click") == -1)
  {
    var dotCount = path.lastIndexOf(".");
    var beginStr = path.substring(0, dotCount);
    var clickStr = "_click";
    var endStr = path.substring(dotCount);

    path = beginStr + clickStr + endStr;
    elem.src = path;
  }  
}


System.convertClickToDefaultImageStylePath = function (elem, path)
{
  if(elem.style.backgroundImage.indexOf("_click") != -1)
  {
    path = path.replace('_click', "");
    elem.style.backgroundImage = "url("+path+")";
  }  
}

System.convertDefaultToClickImageStylePath = function (elem, path)
{
  if(elem.style.backgroundImage.indexOf("_click") == -1)
  {
    var dotCount = path.lastIndexOf(".");
    var beginStr = path.substring(0, dotCount);
    var clickStr = "_click";
    var endStr = path.substring(dotCount);

    path = beginStr + clickStr + endStr;
    elem.style.backgroundImage = "url("+path+")";
  }  
}


// Filter effect //
System.effectElem = null;
System.effectIndex = 0;
System.effectIntervalObject = null;

System.beginFilterEffect = function(elem, interval, speed) 
{
  System.effectElem = elem;
  System.effectIndex = 0;
  System.effectIntervalObject = setInterval("System.effect("+speed+")", interval)
}

System.effect = function(speed) 
{
  if(System.effectIndex <=100)
  {
    System.effectElem.style.filter = "alpha(Opacity="+System.effectIndex+")"
    System.effectIndex = System.effectIndex + speed;
  }
  else
  {
    clearInterval(System.effectIntervalObject);
    System.effectIndex = 0;
  }
}

// System getters //
System.getRefreshTextFieldElem = function()
{
  return document.getElementById(System.refreshTextFieldId); 
}

System.getLanguageTextFieldElem = function() 
{
  return document.getElementById(System.languageTextFieldId); 
}

System.getMenuIndexTextFieldElem = function() 
{
  return document.getElementById(System.menuIndexTextFieldId); 
}

System.getNodeIdTextFieldElem = function() 
{
  return document.getElementById(System.nodeIdTextFieldId); 
}

System.getPageIndexTextFieldElem = function() 
{
  return document.getElementById(System.pageIndexTextFieldId); 
}

System.getContentFrameElem = function() 
{
  return document.getElementById(System.contentFrameId); 
}

System.getTreePanelElem = function() 
{
  return document.getElementById(System.treePanelElemId); 
}

System.getTitlePanelElem = function() 
{
  return document.getElementById(System.titlePanelId); 
}

System.getPageTitlePanelElem = function() 
{
  return document.getElementById(System.pageTitlePanelId); 
}

System.getPageNavigationPanelElem = function() 
{
  return document.getElementById(System.pageNavigationPanelId); 
}

System.getPageNavigationPosPanelElem = function() 
{
  return document.getElementById(System.pageNavigationPosPanelId); 
}

System.getPageDocumentPanelElem = function() 
{
  return document.getElementById(System.pageDocumentPanelId); 
}

System.getMenuPanelElem = function() 
{
  return document.getElementById(System.menuPanelId); 
}

System.getHomeMenuElem = function() 
{
  return document.getElementById(System.homeMenuId); 
}



System.debug = function(msg) 
{
  window.status = new Date().getTime() + ":  " + msg;
}


 





