//////////////////////////////////////////
//                                      //
//               CONTENT                //
//                                      //
//////////////////////////////////////////

Content.PROPERTIES_XML_FILE_NAME = "properties.xml";

Content.currentNodeId = null;

function Content (dir) {
  this.init(dir);
}

Content.prototype.init = function(dir) 
{
  var filePath = dir + Content.PROPERTIES_XML_FILE_NAME;
  // import menu content properties xml file //
  var xmlDoc = XmlTools.importXML(filePath);
  this.parseXML(xmlDoc, filePath);
}

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

  if(XmlTools.isXMLFormatValid(structureElements, filePath))
  {   
    var treenodeTag = structureElements[0].getElementsByTagName(RootNode.TREENODE_TAG_NAME);
    var titleTag = treenodeTag[0].getElementsByTagName(Node.TAG_TITLE);
    var title = XmlTools.getLanguageNodeValue(titleTag[0]);
    var treenodechildsTag = treenodeTag[0].getElementsByTagName(Node.TAG_TREENODE_CHILDS);
    var directories = treenodeTag[0].getElementsByTagName(Node.TAG_DIR);
    
    // create root node //
    var rootNode = new RootNode();
    
    // create tree structure //
    Node.createNodeTree(directories, rootNode);
    
    // create tree javascript code //
    var treeJavascript = Tree.createTreeJavascriptCode(rootNode, null);
    
    // set members //
    rootNode.setTreeJavascriptCode(treeJavascript);
    this.setTitle(title);
    this.setTree(rootNode);
    this.isValid = true;
  }
}

Content.prototype.setTitle = function(title) 
{
  this.title = title;
}

Content.prototype.getTitle = function() 
{
  return this.title;
}

Content.prototype.setTree = function(tree) 
{
  this.tree = tree;
}

Content.prototype.getTree = function() 
{
  return this.tree;
}


Content.prototype.paintTreeComponent = function()
{
  var tree = this.getTree();
  var treeJavascriptCode = tree.getTreeJavascriptCode();
  var drawElem = tree.getDrawElem();
  Tree.draw(drawElem, treeJavascriptCode);  
}

Content.prototype.selectFirstTreeNode = function()
{
  var tree = this.getTree();
  root.children[0].select();
  this.nodeClick(tree.getChildNode(0).id);
}

Content.prototype.nodeClick = function(nodeId) 
{
  if(Content.currentNodeId != nodeId)
  {
    PageNavigator.currentPageIndex = -1;
  }

  System.getNodeIdTextFieldElem().value = nodeId; 

  Content.currentNodeId = nodeId;
  var node = this.getTree().findNode(nodeId);
  if(node.pages != null)
  {
    // change title panel //
    this.changeTitlePanel(node);

    // change page title panel //
    this.changePageTitlePanel(node, 0);

    // change page navigation panel //
    this.changePageNavigationPanel(node);
  }  
  return node.pages != null;
}

Content.prototype.changeTitlePanel = function(node)
{
  var titleContent = this.createTitlePanelContent(node);
  
  System.getTitlePanelElem().innerHTML = titleContent;
}


Content.prototype.createTitlePanelContent = function(node)
{
  var array = new Array();
  var index = 0;
  Content.prepareTitleArray(array, node, index);
  
  var str = "";
  str += "<table border=0 cellpadding=0 cellspacing=0 width=100% height=57 style=\"padding:3px;background-color:9496BD;border:1px solid #4F5C88;\"><tr valign=top height=100%><td>";
  
  if(this.getTitle() != "")
  {
    str += "<li class=\"title\">&nbsp;" +  this.getTitle() + "</li>";
  }
  
  for(var i=array.length-1; i>=0; i--)
  {
    if(array[i] != "")
    {
      str += "<li class=\"title\">&nbsp;" + array[i] + "</li>";
    }  
  }
  
  str += "</td></tr></table>";
  return str;
}

Content.prepareTitleArray = function(array, node, index)
{
  array[index] = node.title;
  if(node.parent != null)
  {
    index = index + 1;
    Content.prepareTitleArray(array, node.parent, index);  
  }
}

Content.prototype.changePageTitlePanel = function(node, index)
{
  var pageTagList = node.pages;
  var pageTitleTag = pageTagList[index].getElementsByTagName(Node.TAG_TITLE);  
  var pageTitle = XmlTools.getLanguageNodeValue(pageTitleTag[0])

  var pageTitleContent = "";

  if(pageTitle != "")
  {
    pageTitleContent += "<span class=\"pageTitle\">" + pageTitle.toUpperCase() + "</span><br>";
  }
  else
  {
    pageTitleContent += "<span class=\"pageTitle\">&nbsp;</span><br>";
  }

  pageTitleContent += "<img src=\"img/divider1.gif\" width=100% height=1>";
  System.getPageTitlePanelElem().innerHTML = pageTitleContent;
}

Content.prototype.changePageNavigationPanel = function(node)
{
  PageNavigator.currentPageNode = node;

  var pageNavigationContent = this.createPageNavigationPanelContent(node);

  System.getPageNavigationPanelElem().innerHTML = pageNavigationContent;

  if(PageNavigator.currentPageIndex == -1)
  {
    PageNavigator.click(1);
  }
  else
  {
    PageNavigator.click(PageNavigator.currentPageIndex);
  }
}

Content.prototype.createPageNavigationPanelContent = function(node)
{
  var pageIndex = (PageNavigator.currentPageIndex == -1) ? 1 : PageNavigator.currentPageIndex;
  var docPath = node.getDocPath(pageIndex-1);
  var xlsPath = node.getXlsPath(pageIndex-1);

  var str = "";
  var pageCount = node.pages.length;
  if(pageCount <= 15)
  {    
    str += "<table border=0 cellpadding=2 cellspacing=0><tr>";

    for(var i=0; i<node.pages.length; i++)
    {
      str += "<td>";
      str += "<img id=\"nav"+(i+1)+"\" src=\"img/nav"+(i+1)+".gif\" class=\"navButton\" ";
      str += " onclick=\"PageNavigator.click("+(i+1)+");\" "    
      str += " onmouseover=\"PageNavigator.over("+(i+1)+");\" ";
      str += " onmouseout=\"PageNavigator.out("+(i+1)+");\">";
      str += "</td>";    
    }

    str += "<td width=100%>&nbsp;</td>";

    str += "<td><div id='pageDocumentPanel'>&nbsp;</td></td>";

    str += "<td><img src=\"img/leftNavArrow.gif\" onclick=\"PageNavigator.left();\" class=\"navButton\"</td>";
    str += "<td><img src=\"img/rightNavArrow.gif\" onclick=\"PageNavigator.right();\" class=\"navButton\"</td>";

    str += "</tr></table>";
  }
  else
  {
    alert("Maksimalno število podstrani na izbranem nivoju je 15!\nIzbrani nivo vsebuje trenutno "+pageCount+" podstrani.")  
  }  
  return str;
}

Content.prototype.changePageDocumentPanel = function(node)
{
  var pageDocumentPanelContent = this.createPageDocumentPanelContent(node);
  System.getPageDocumentPanelElem().innerHTML = pageDocumentPanelContent;
}

Content.prototype.createPageDocumentPanelContent = function(node)
{
  var pageIndex = (PageNavigator.currentPageIndex == -1) ? 1 : PageNavigator.currentPageIndex;
  var docPath = node.getDocPath(pageIndex-1);
  var xlsPath = node.getXlsPath(pageIndex-1);

  var str = "";
  var pageCount = node.pages.length;
  if(pageCount <= 15)
  {  
    var docStr = "";
    
    if(docPath != "")
    {
      docStr += "<td><a href='"+docPath+"' target='blank'><img src=\"img/wordIcon.gif\" class=\"word_excel\" title='Word document'></a></td>";
    } 
    if(xlsPath != "")
    { 
      if(docPath != "")
      {
        docStr += "<td><img src='../img/spacer.gif' style='width:3px;height:3px;'></td>";
      }
      docStr += "<td><a href='"+xlsPath+"' target='blank'><img src=\"img/excelIcon.gif\" class=\"word_excel\" title='Excel document'></a></td>";
    }
    
    if(docStr != "")
    {
      str += "<table border=0 cellpadding=0 cellspacing=0><tr>";
      str += docStr;
      str += "</tr></table>";
    }
    
  }  
  return str;
}

