var scroll_delay=500; // Delay before we start scrolling.
var scroll_delay_timer=false;
var scroll_interval=150; // Scroll speed.
var scroll_interval_timer=false;

/** Normalize index i to 0<=i<len */
function normalize(i)
{
  return (i+len) % len;
}

function carousel_up()
{
  var show=$('img_'+normalize(pos-1));
  var hide=$('img_'+normalize(pos+carousel_size-1));
  if(show)
  {
    move_before(show,$('img_'+pos));
    show.style.display='block';
  }
  if(hide)
    hide.style.display='none';
  pos=normalize(pos-1);
  document.forms['carousel'].pos.value=pos;
  if(!scroll_delay_timer)
    scroll_delay_timer=setTimeout('scr_on(carousel_up)',scroll_delay);
}

function carousel_down()
{
  var show=$('img_'+normalize(pos+carousel_size));
  if(show)
  {
    var hide=$('img_'+pos);
    move_before(show,$('more_down'));
    show.style.display='block';
    if(hide)
      hide.style.display='none';
    pos=normalize(pos+1);
    document.forms['carousel'].pos.value=pos;
    if(!scroll_delay_timer)
      scroll_delay_timer=setTimeout('scr_on(carousel_down)',scroll_delay);
  }
}

/** Starts scrolling. */
function scr_on(f)
{
  scroll_interval_timer=setInterval(f,scroll_interval);
}

/** Cancels scrolling. */
function scr_off()
{
  if(scroll_delay_timer)
  {
    clearTimeout(scroll_delay_timer);
    scroll_delay_timer=0;
  }
  if(scroll_interval_timer)
  {
    clearInterval(scroll_interval_timer);
    scroll_interval_timer=0;
  }
}

/** Translates a click on a thumbnail image into a POST request. */
function go(e)
{
  var carousel=document.forms['carousel'];
  if(carousel)
  {
    carousel.action=e.href;
    carousel.submit();
    return ONCLICK_OK;
  }
  else
    return ONCLICK_FAILED;
}


// TITLE EDITOR

function do_show_title_editor()
{
  var title_blck=$('title_block');
  var title_form=$('title_form');
  if(title_blck && title_form)
  {
    title_blck.style.display='none';
    title_form.style.display='block';
    title_form.title_txt.focus();
    return ONCLICK_OK;
  }
  return ONCLICK_FAILED;
}

function do_save_title()
{
  var title =$('title');
  var title_blck=$('title_block');
  var title_ctrl=$('title_ctrl');
  var title_form=$('title_form');
  var title_sub =$('title_sub');
  var title_busy=$('title_busy');
  if(title && title_blck && title_ctrl && title_form && title_sub && title_busy)
  {
    function update_title(xml)
      {
        title_sub.style.display ='inline';
        title_busy.style.display='none';
        if(report_alert(xml))
          return;
        title_blck.style.display='block';
        title_form.style.display='none';
        var xi=getFirstChildByTagName(xml,'image');
        if(xi)
        {
          var new_title=xi.getAttribute('title');
          title.innerHTML=new_title;
          document.title=new_title+' &laquo; '+xi.getAttribute('path');
          // Now set the carousel title.
          var ttl=$('ttl_'+img);
          ttl.innerHTML=new_title;
        }
      }
    if( ajax_form(title_form,{oncomplete:update_title}) )
    {
      title_sub.style.display='none';
      title_busy.style.display='inline';
      return ONCLICK_OK;
    }
    else
      alert('Error submitting new title.');
  }
  return ONCLICK_FAILED;
}

// CAPTION EDITOR

function do_show_caption_editor()
{
  var cap_para=$('cap_para');
  var cap_form=$('cap_form');
  if(cap_para && cap_form)
  {
    cap_form.cap_txt.style.height=''+Math.max(40,cap_para.offsetHeight)+'px';
    cap_para.style.display='none';
    cap_form.style.display='block';
    cap_form.cap_txt.focus();
    return ONCLICK_OK;
  }
  return ONCLICK_FAILED;
}

function do_save_caption()
{
  var caption =$('caption');
  var cap_para=$('cap_para');
  var cap_ctrl=$('cap_ctrl');
  var cap_form=$('cap_form');
  var cap_sub =$('cap_sub');
  var cap_busy=$('cap_busy');
  if(caption && cap_para && cap_ctrl && cap_form && cap_sub && cap_busy)
  {
    function update_caption(xml)
      {
        cap_para.style.display='block';
        cap_form.style.display='none';
        cap_sub.style.display ='inline';
        cap_busy.style.display='none';
        var xc=xml.getElementsByTagName('caption');
        if(xc)
        {
          var txt=getInnerText(xc[0]);
          caption.innerHTML=txt.replace(/\n/g,'<br />');
          cap_ctrl.innerHTML=txt.length? 'edit': 'add caption';
          // Now set the carousel caption.
          var para=$('cap_'+img);
          xc=xml.getElementsByTagName('brief');
          para.innerHTML=getInnerText(xc[0]);
        }
      }
    if( ajax_form(cap_form,{oncomplete:update_caption}) )
    {
      cap_sub.style.display='none';
      cap_busy.style.display='inline';
      return ONCLICK_OK;
    }
    else
      alert('Error submitting comment.');
  }
  return ONCLICK_FAILED;
}

function do_show_comment_editor()
{
  var cmt_ctrl=$('cmt_ctrl');
  var cmt_form=$('cmt_form');
  if(cmt_ctrl && cmt_form)
  {
    cmt_ctrl.parentNode.style.display='none';
    cmt_form.style.display='block';
    scrollIntoView(cmt_form);
    cmt_form.cmt_author.focus();
    return ONCLICK_OK;
  }
  return ONCLICK_FAILED;
}

function update_num_comments(num)
{
  var nc=$('nc_'+img);
  if(num==0)
  {
    if(nc)
      fade_out(nc);
  }
  else
  {
    if(!nc)
    {
      nc=document.createElement('span');
      nc.id='nc_'+img;
      nc.className='num_comments';
      var dtc=$('dtc_'+img);
      fade_in(nc);
      dtc.insertBefore(nc,dtc.firstChild);
    }
    nc.innerHTML=''+num+'&nbsp;comment'+(num>1? 's': '');
  }
}

/** Construct a DIV from XML <comment> */
function div_comment(comment)
{
  var div=document.createElement('div');
  div.className='comment';
  var txt   =getInnerText( getFirstChildByTagName(comment,'text') );
  var author=getInnerText( getFirstChildByTagName(comment,'author') );
  var date  =getInnerText( getFirstChildByTagName(comment,'date') );
  div.innerHTML='<p>'+txt+'</p><p class="by">posted by <span class="author">'
    +author+'</span> on '+date+'</p>';
  return div;
}

/** Pop up an alert dialogue, if the album XML contains an <alert> element. */
function report_alert(album)
{
  var a=getFirstChildByTagName(album,'alert');
  if(a)
  {
    alert(getInnerText(a));
    return true;
  }
  return false;
}

function do_submit_comment()
{
  var cmt_head=$('cmt_head');
  var cmt_ctrl=$('cmt_ctrl');
  var cmt_form=$('cmt_form');
  var cmt_busy=$('cmt_busy');
  var cmt_sub =$('cmt_sub');
  if(cmt_ctrl && cmt_form && cmt_busy && cmt_sub)
  {
    function update_comment(xml)
      {
        cmt_sub.style.display='inline';
        cmt_busy.style.display='none';
        if(report_alert(xml))
          return;
        var cs=xml.getElementsByTagName('comment');
        for(var i=cs.length-1; i>=0; i--)
        {
          var id=cs[i].getAttribute('id');
          if($(id))
              break; // We already have this comment, so give up.
          else if(i==0)
              fade_in(cmt_head,'block'); // Make the Comment header visible.
          var div=div_comment(cs[i]);
          div.setAttribute('id',id);
          fade_in(div);
          cmt_form.parentNode.insertBefore(div,cmt_form);
        }
        update_num_comments(cs.length);
        cmt_form.reset();
        cmt_form.style.display='none';
        cmt_ctrl.parentNode.style.display='block';
      }
    if( ajax_form(cmt_form,{oncomplete:update_comment}) )
    {
      cmt_sub.style.display='none';
      cmt_busy.style.display='inline';
      return ONCLICK_OK;
    }
    else
      alert('Error submitting comment.');
  }
  return ONCLICK_FAILED;
}

function do_delete_comment(e)
{
  // Find comment.
  while(e && e.parentNode)
  {
    e=e.parentNode;
    if(e.className=='comment')
    {
      var id=e.getAttribute('id');
      if(id==null || id=='')
        break;
      ajax('?d=e&img='+img,{data:'do=delcomment&cid='+id});
      fade_out(e);
      // If there are no more comments, then hide the header, too.
      var cdivs=$('comments').getElementsByTagName('div');
      if(!(cdivs && cdivs.length>1))
          fade_out($('cmt_head'),true);
      update_num_comments(Math.max(0,cdivs.length-1));
      return ONCLICK_OK;
    }
  }
  return ONCLICK_FAILED;
}
