//$Id: pageoptions.js,v 1.10 2009/04/03 04:39:22 zim@netcomposite.com Exp $
var PageNames = {
 'see_also':'See also',
 'references':'References',
 'external_links':'External links',
 'comments':'Comments',
 'citation_options':'Citation',
 'print_options':'Print options',
 'bookmark':'Bookmark',
 'send_by_email':'Send by email'
};

var AccountPageNames = {
 'inbox':'Inbox',
 'my_profile':'My profile',
 'account_settings':'Account settings'
};

var waitHTML =
 '<div id="PageOptionsLoading">' +
  '<p><img src="/assets/images/icons/ic_loading_processing_gray.gif" width="48" height="48" alt="Please wait" />' +
   'Please wait...' +
  '</p>' +
 '</div>';

var currentID = 'see_also';
var pages = new Array();
var account = false;
var include_references = false;

function restoreTabs()
{
 if (account)
 {
  PageNames = AccountPageNames;
 }
 var ul = $('OptionsUl');
 ul.setAttribute('class', 'OptionsOpened');
 for (var i=0; i < ul.childNodes.length; i++)
 {
  var t = ul.childNodes[i];
  if (t.nodeType == 1)
  {
   var id1 = t.id;
   t.removeAttribute('class');
   t.removeAttribute('style');
   t.innerHTML = '<a href="?opt='+ id1 +'" onClick="return openPage(\'' + id1 + '\')">' + PageNames[id1] +'</a>';
  }
 }
}

function openPage(id, addon)
{
 $('OptionsUl').setAttribute('class', 'OptionsOpened');
 restoreTabs();
 var tab = $(id);
 if (id == 'see_also' || id == 'inbox')
 {
  tab.setAttribute('class', 'FirstTab');
  tab.style.background = '#FFF url(/assets/images/misc/pg_option_sld_shdow-r.gif) right top no-repeat';
 } else
 {
  tab.setAttribute('class', 'SelectTab');
  tab.style.background = 'background: #FFF url(/assets/images/misc/bg_slider-l.jpg) left top no-repeat';
 }
 tab.innerHTML = '<span>' + PageNames[id] +'</span>';
        $('PageOptionsBottom').innerHTML = '<a href="?opt=\'\'" onClick="return closePage()"><img src="/assets/images/icons/page_opt_close.jpg" width="40" height="13" alt="Close options details" /></a>';
 $('OptionsContent').style.display = 'block';
 if (id=='references' && !include_references)
 {
  pages[id] = $('ReferencesOptionsContent').innerHTML.replace('id="noPageOptionsLoading"','id="PageOptionsLoading"');
 }
 currentID = id;
 if (!pages[id])
 {
  $('OptionsContent').innerHTML = waitHTML;
  if (addon)
  {
   value = addon;
   value['id'] = id;
  } else
  {
   var value = new Array();
   value['id'] = id;
  }
  if (include_references)
  {
   value['include_references'] = include_references;
  }
  loadPage('uri', value);
 } else
 {
  $('OptionsContent').innerHTML = pages[id];
  if (typeof(currentIDEx) != 'undefined' && currentIDEx !== null)
  {
   if (pagesEx[currentIDEx[id]])
   {
    openPageEx(currentIDEx[id]);
   }
  }
 }
 return false;
}

function closePage()
{
 restoreTabs();
 $('OptionsUl').setAttribute('class', 'OptionsClosed');
 $('OptionsContent').style.display = 'none';
 $('PageOptionsBottom').innerHTML = '<a href="?opt=\'\'" onClick="return openPage(\'' + currentID + '\')"><img src="/assets/images/icons/page_opt_open.jpg" width="40" height="13" alt="See options details" /></a>';
 return false;
}

function loadPage(mode, value)
{
 var req=new j2httpRequest_js;
 var method=null;
 if (typeof (mode)!='object')
 {
  var method='GET';
 }
 req.onreadystatechange = function()
 {
  if (req.readyState == 4)
  {
   var result=req.responseJS;
   if (result['html'])
   {
    if (currentID == result['id'])
    {
     $('OptionsContent').innerHTML=result['html'];
    }
    pages[result['id']] = result['html'];
   }
  }
 }
 if (this.script_name)
 {
  value['script_name'] = this.script_name;
 }
 if (this.tab_name)
 {
  value['tab_name'] = this.tab_name;
 }
 if (this.meta_data)
 {
  value['meta_data'] = this.meta_data;
 }
 req.open(method, '/assets/templ/include_globals/options.php', false);
 req.send({'mode':mode,'value':value});
 return false;
}

function $(text) {return document.getElementById(text);}

function requestSend(field, id, message)
{
 var sendHTML =
  '<div id="PageOptionsLoading">' +
   '<img src="/assets/images/icons/ic_loading_processing_gray.gif" />' +
   message +
  '</div>';
 pages[id] = false;
 var params={'sendname':field.name, 'sendvalue':field.value, 'formname':$(field.form.id).name, 'id':id}
 loadPage($(field.form.id), params);
 $('OptionsContent').innerHTML = sendHTML;
 return false;
}

function textChange()
{
 var s = $('addon').value;
 var name = $('COMMON_NAME').value;
 var page_title = $('page_title').value;
 var link = $('link').value;
 var description = $('page_description').value;
 var text = $('text').value;
 s = s.replace(/%name%/g, name);
 s = s.replace(/%page_title%/g, page_title);
 s = s.replace(/%link%/g, link);
 s = s.replace(/%description%/g, description);
 s = s.replace(/%text%/g, text);
 if (document.MainForm)
 {
  document.MainForm.description.value = s;
 } else
 {
  $('description').value = s;
 }
 return false;
}

function commentTextChange()
{
 var src = document.getElementById("DESCRIPTION");
 src.value = src.value.substring(0,600);
        var t = src.value;
        var dst = document.getElementById("preview");
        dst.value = t;
        var cl = document.getElementById("CharCount");
        var cleft = 600 - src.textLength;
        cl.innerHTML = cleft + " characters left";
}


function closeDiv(id)
{
 $(id).style.display = 'none';
 return false;
}

function inputSwap(idshow,idhide)
{
 try
 {
  var sh = $(idshow);
  if (sh != null)
  {
   sh.style.display = "";
  }
 } catch(ex)
 {
  return;
 }
 try
 {
  var hd = $(idhide);
  if (hd != null)
  {
   hd.style.display = "none";
  }
 } catch(ex)
 {
  return;
 }
}

function listOptionSelect(a)
{
 var id = new Array('StandardToolsOptionsExPablish','StandardToolsOptionsExPage','StandardToolsOptionsExtBest','StandardToolsOptions','StandardToolsOptionsMode','StandardToolsOptionsExt1','StandardToolsOptionsExt2','StandardToolsOptionsExt3');
 for (var i=0; i < id.length; i++)
 {
  if (a != id[i] && $(id[i]))
  {
   $(id[i]).className='';
   $(id[i]).style.display='none';
  }
 }
 if ($(a).style.display=='none')
 {
  $(a).style.display='block';
  $(a).className='over';
 } else
 {
  $(a).style.display='none';
  $(a).className='';
 }
 return false;
}
