/* 2008-10-08 (mca) */
/* scans current page for 'editable sections */

var enableEdit = function()
{
  var g = {};
  g.editElmId = 'content-id';
  g.editColl = [];
  g.editBox = null;
  g.adminRights = '2908';
  g.cacheUrl = 'server/admin/cache/clear-all.aspx';
  g.window = null;
  g.toggleImgPlus = 'content/scripts/dhtmlx/imgs/plus.gif';
  g.toggleImgMinus = 'content/scripts/dhtmlx/imgs/minus.gif';
  g.editWinDef = [10,10,780,600];
  
  function init()
  {
    var ck = galaxy.readCookie('auth-level');
    if(ck && ck==g.adminRights)
    {
      makeEditBox();
      getEditCollection();
      buildEditList();
    }
  }

  function launchWindow()
  {
    var url,title,win,elm,ew;
    
    url = this.href;
    title = this.getAttribute('edit-title');

    win = new dhtmlXWindows();
    win.setImagePath("content/scripts/dhtmlx/imgs/index.html");
    
    win.createWindow("w1",g.editWinDef[0], g.editWinDef[1], g.editWinDef[2], g.editWinDef[3]);
    //win.createWindow("w1", 10, 10, 780, 600);
    win.window('w1').bringToTop();
    win.window('w1').setText(title);
    win.window('w1').setModal(true);
    win.window('w1').attachURL(url);
    
    return false;
  }
  
  function saveWindowDef(win)
  {
    var d,p;
    
    // width, height
    d = win.getDimension();
    d[0] = (d[0]<10?300:d[0]);
    d[1] = (d[1]<10?300:d[1]);
    
    // left, top
    p = win.getPosition();
    p[0] = (p[0]<10?10:p[0]);
    p[1] = (p[1]<10?10:p[1]);
    
    g.editWinDef = [p[0],p[1],d[0],d[1]];
    galaxy.createCookie('editwin',g.editWinDef,30);
  }
  
  function clearCache()
  {
    if(confirm('Ready to clear server memory cache?'))
    {
      window.open(g.cacheUrl,'cache_window','status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=0,height=100,width=200');
    }
    return false;
  }
  
  
  function makeEditBox()
  {
    var title,img;

    img = document.createElement('img');
    img.src = g.toggleImgPlus;
    img.id = 'edit-toggle-image';

    title = document.createElement('h3');
    title.appendChild(img);
    title.className = 'edit-list-toggle';
    title.onclick = toggleEditList;
    
    g.editBox = document.createElement('div');
    g.editBox.className='edit-box';
    g.editBox.appendChild(title);
    
    document.body.appendChild(g.editBox);
  }
  
  function getEditCollection()
  {
    var coll,i,j;
    
    j=0;
    coll = document.getElementsByTagName('div')
    for(i=0;i<coll.length;i++)
    {
      if(coll[i].getAttribute(g.editElmId) && coll[i].getAttribute(g.editElmId)!='')
      {
        g.editColl[j++] = coll[i];
      }
    }
  }
  
  function getEditableElement(id)
  {
    var coll,i,rtn;
    
    coll = document.getElementsByTagName('div')
    for(i=0;i<coll.length;i++)
    {
      if(coll[i].getAttribute(g.editElmId) && coll[i].getAttribute(g.editElmId)==id)
      {
        rtn = coll[i];
      }
    }
    
    return rtn;
  }
  
  function toggleEditList()
  {
    var list = document.getElementById('edit-content-list');
    if(list)
    {
      list.style.display = (list.style.display=='block'?'none':'block');
    }

    var img = document.getElementById('edit-toggle-image');
    if(img)
    {
      img.src = (img.src.indexOf(g.toggleImgPlus)!=-1?g.toggleImgMinus:g.toggleImgPlus);
    }
    
    return false;
  }
  
  function buildEditList()
  {
    var i,a,item,list,box;
    
    list = document.createElement('ul');
    list.id = "edit-content-list";
    list.style.display='none';
    
    if(g.editColl.length==0)
    {
      item = document.createElement('li');
      item.appendChild(document.createTextNode('No items to edit'));
      list.appendChild(item);
    }
    else
    {

      item = document.createElement('li');
      item.appendChild(document.createTextNode('Edit Regions'));
      item.className ='section';
      list.appendChild(item);
      
      for(i=0;i<g.editColl.length;i++)
      {
        a = document.createElement('a');
        a.className = 'edit-link';
        a.href=g.editColl[i].getAttribute('edit-url');
        a.setAttribute(g.editElmId,g.editColl[i].getAttribute(g.editElmId));
        a.setAttribute('edit-title',g.editColl[i].getAttribute('edit-title'));
        a.onmouseover=toggleBorderOn;
        a.onmouseout=toggleBorderOff;
        a.onclick = launchWindow;
        a.title='Edit this content';
        a.appendChild(document.createTextNode(g.editColl[i].getAttribute('edit-title')));
        
        item = document.createElement('li');
        item.appendChild(a);
        list.appendChild(item);
      }
      
      // add cache-clearing link
      a = document.createElement('a');
      a.className = 'cache-link';
      a.href='#';
      a.onclick = clearCache;
      a.title='Clear server cache';
      a.appendChild(document.createTextNode('Clear Cache'));
      
      item = document.createElement('li');
      item.appendChild(a);
      list.appendChild(item);
    }
    g.editBox.appendChild(list);
  }
  
  function toggleBorderOff()
  {
    var i,id;
    id = this.getAttribute(g.editElmId);
    
    for(i=0;i<g.editColl.length;i++)
    {
      if(g.editColl[i].getAttribute(g.editElmId)==id)
      {
        $(g.editColl[i]).removeClass('selected-block');
      }
    }
    
    return false;
  }

  function toggleBorderOn()
  {
    var i,id;
    id = this.getAttribute(g.editElmId);
    
    for(i=0;i<g.editColl.length;i++)
    {
      if(g.editColl[i].getAttribute(g.editElmId)==id)
      {
        $(g.editColl[i]).addClass('selected-block');
      }
    }
    
    return false;
  }

  var that = {};
  that.init = init;
  that.saveWindowDef = saveWindowDef;
  
  return that;
  
};

// start on load
$(document).ready(function()
{
  var ed = enableEdit();
  ed.init();
});

