// JavaScript Document
comments_currenttab = 1;
comments_enlarged = false;
commentsx = 520;
commentsy = 740;
commentsw = 480;
commentsh = 130;
newsloaded = false;
castloaded = false;
whatsnewloaded = false;
casthide = true;
dohidecast = false;
dohidemenu = false;
castclock = 0;
menuclock = 0;
currentmenu = '';
clockID = 0;

t = 0;
clockID = 0;
clockAr = 0;
M_STRIPS = 0;
M_STORIES = 1;
M_CHAPTERS = 2;
mode = M_STRIPS;
target = 0;

//Preferences
prefShowHint = true;

//Standard lines
msg = new Array();
cap = new Array();
cap[0] = 'Artwork';		msg[0] = 'Drawings and sketches by Alien and fans.';
cap[1] = 'Community';		msg[1] = 'Forums, chat, avatars, banners and other swag.';
cap[2] = 'Cast';		msg[2] = 'Find out about our heroes and villains (and extras)';
cap[3] = 'Background';		msg[3] = 'The World That Is and those who live there.';
cap[4] = 'Archive';		msg[4] = 'Comic Archive';
cap[5] = 'Creators\' Corner';	msg[5] = 'Other things by Alien and Mithandir';
cap[6] = 'The Pixie Forest';	msg[6] = 'Whee! Pixies! (and wallpapers, ecards, games, ..)';
cap[7] = 'Latest';		msg[7] = 'Go straight to the latest strip.';
cap[8] = 'Next';		msg[8] = 'Go to the next strip';
cap[9] = 'First';		msg[9] = 'Go to the very first strip';
cap[10]= 'Previous';		msg[10]= 'Go to the previous strip';
cap[11]= 'Home';		msg[11]= 'The front page (and latest comic) beckons';

function getRef(divID,oDoc) {
    if( !oDoc ) { oDoc = document; }
    if( document.layers ) {
        if( oDoc.layers[divID] ) { return oDoc.layers[divID]; } else {
            //repeatedly run through all child layers
            for( var x = 0, y; !y && x < oDoc.layers.length; x++ ) {
                //on success, return that layer, else return nothing
                y = getRef(divID,oDoc.layers[x].document); }
            return y; } }
    if( document.getElementById ) {
        return document.getElementById(divID); }
    if( document.all ) {
        return document.all[divID]; }
    return false;
}

function setTop(divID, ypos) {
 dref = getRef(divID), noPx = document.childNodes ? 'px' : 0;
 if( !dref ) { return; }
 if( dref.style ) { dref = dref.style; }
 dref.top = ypos + noPx;
}

function setLeft(divID, xpos) {
 dref = getRef(divID), noPx = document.childNodes ? 'px' : 0;
 if( !dref ) { return; }
 if( dref.style ) { dref = dref.style; }
 dref.left = xpos + noPx;
}

function ShowHide(lyr, v) {
  dref = getRef(lyr);
  if( !dref ) { return; }
  if( dref.style ) { dref = dref.style; v=(v=='show')?'visible':(v='hide')?'hidden':v;}
  dref.visibility=v; 
}

function growtick(growby) {
   if(clockID) {
      clearTimeout(clockID);
      clockID  = 0;
   }
   
   t++;
   CommentsSize(commentsw, commentsh + growby);
   CommentsMove(commentsx, commentsy - growby);
   
   if (t < 10) setTimeout("growtick("+growby+")", 100); else {
      t = 0;
   }
}

function setImage(id, img) {
  dref = getRef(id);
  dref2 = getRef(id + 'ie');
  
  if (dref2.filters) 
     dref2.filters(0).src = img;
   else
    dref.style.backgroundImage = "url(" + img + ")";
}

function CommentsMove(x,y) {
  setLeft('commentsframe', x);
  setTop('commentsframe', y);
  commentsx = x;
  commentsy = y;
}

function CommentsToggle() {
  if (comments_enlarged) {
      CommentsSize(commentsw, 130);
      CommentsMove(commentsx, commentsy + 400);
      setImage('comments-06','images/expand.png');
      comments_enlarged = false;
  } else {
      growtick(40);
      comments_enlarged = true;
      setImage('comments-06','images/shrink.png');
  }
}

function CommentsTab(number) {
 if (comments_currenttab == number) {
    CommentsToggle();
 } else {
  comments_currenttab = number;
  for (i = 1; i < 5; i++) {
    if (i == number) {
      setImage('comments-0' + i,'images/comments_0' + i +'.png');
      ShowHide('comments-text' + i, 'show');
    }
     else {
      setImage('comments-0' + i,'images/commentsa_0' + i +'.png');
      ShowHide('comments-text' + i, 'hide');
    }
  }
  if (number == 2) {
    loadNews();
  }
 }
}

function CommentsSize(width, height) {
  getRef('commentsframe').style.height = height;
  getRef('comments-08').style.height = height - 46;
  getRef('comments-09').style.height = height - 46;
  getRef('comments-12').style.height = height - 66;  
  getRef('comments-14').style.top = height - 25;
  getRef('comments-15').style.top = height - 20;
  getRef('comments-16').style.top = height - 20;
  getRef('comments-ta').style.height = Math.max(height - 150, 50);
  for (i=1; i<5; i++)
    getRef('comments-text' + i).style.height = height - 38;
  commentsh = height;
  commentsw = width;
}

function HintMove(x,y) {
  dref = getRef('hint');
  
  if (x + 22 + dref.offsetWidth > document.body.clientWidth)
    mx = x - 22 - dref.offsetWidth;
   else
    mx = x + 22;
  
  if (y > 400)
     my = y - getRef('hint').offsetHeight + 25;
   else
     my = y - 10;
  if (my < 5) my = 5;
  
  setLeft('hint', mx);
  setTop('hint', my);
}

MI_NN4=navigator.appName.toLowerCase()=="netscape"&&!document.getElementById;
MI_NN=MI_NN4||document.getElementById&&!document.all;

function MoveHandler(e) {
	if (!prefShowHint) return;  
    Xpos = MI_NN?e.pageX:event.x + document.body.scrollLeft;
    Ypos = MI_NN?e.pageY:event.y + document.body.scrollTop;
    HintMove(Xpos, Ypos);
}

function docload (track) {
    readPrefs();	
  if (track) {
      document.onmousemove = MoveHandler;
  }
}

function ShowHint(title, text) {
   if (!prefShowHint) return;
   getRef('hint-title').innerHTML = title;
   getRef('hint-content').innerHTML = text;
   ShowHide('hint','show');
}

function HideHint() {
   ShowHide('hint','hide');
}

function Hint(id) {
  ShowHint(cap[id], msg[id]);
}

function ThumbHint(id) {
   id2 = id + striptopleft -1;
   if (mode == M_STRIPS) {
     if ((id2) <= storyline.to)
        ShowHint('Strip #' + id2 + ': ' + strip_names[id2], '<img src="'+strip_thumbs2[id2]+'" width="150" height="96" />');
   } else if (mode == M_STORIES) {
     if ((id2) <= chapter.to)
        ShowHint('Storyline #' + id2 + ': ' + storylines[id2].title, '<img src="images/subchapters/sub'+id2+'.jpg" width="100" height="80" />');
   } else if (mode == M_CHAPTERS) {
     ShowHint('Chapter #' + id2 + ': ' + chapters[id2].title, '<img src="images/chapters/chap'+id2+'.jpg" width="100" height="80" />');
   }
}

function StoryHint() {
   ShowHint('Storyline ' + storyline.id + ': ' + storyline.title,'Double-click to view entire storyline.<br> Single-click to select another storyline.')
}

function ChapterHint() {
   ShowHint('Chapter ' + chapter.id + ': ' + chapter.title,'Double-click to go to the first strip in this chapter.<br> Single-click to select another chapter.')
}

function StripThumbScrollLeft() {
 striptopleft = striptopleft - 2;
 
  if (mode == M_STRIPS) {
    from = storyline.from;
    to = storyline.to;
    if (striptopleft < storyline.from) {
      loadStoryLine(storyline.id-1, 'last');
      return;
    }
  } else if (mode == M_STORIES) {
    from = chapter.from;
    to = chapter.to;
    fn = 'images/subchapters/sub';
 
  } else {
    from = 0;
    to = chaptermax;
    fn = 'images/chapters/chap';
  }
  
   for (i = 6; i > 2; i--) {
     dref = getRef('archive-thumb-' + i);
     dref2 = getRef('archive-thumb-' + (i-2));
     dref.src = dref2.src;
     if (i+striptopleft-1 == currentstrip) {
        dref.style.borderColor = "red";
     } else {
        dref.style.borderColor = "white";
     }
   }
   
   for (i = 1; i < 3; i++) {
     dref = getRef('archive-thumb-' + i);
     if (mode == M_STRIPS) {
       dref.src = strip_thumbs1[i+striptopleft-1];  
     } else {
       dref.src = fn + (i+striptopleft-1) + '.jpg';  
     }
     if (i+striptopleft-1 == currentstrip) {
        dref.style.borderColor = "red";
     } else {
        dref.style.borderColor = "white";
     }     
   }
   archiveArrows();
}

function StripThumbScrollRight() {
  striptopleft = striptopleft + 2;
 
  if (mode == M_STRIPS) {
    from = storyline.from;
    to = storyline.to;
    if (striptopleft+4 > storyline.to) {
      loadStoryLine(storyline.id+1);
      return;
    }
  } else if (mode == M_STORIES) {
    from = chapter.from;
    to = chapter.to;
    fn = 'images/subchapters/sub';
 
  } else {
    from = 0;
    to = chaptermax;
    fn = 'images/chapters/chap';
  }
   
  for (i = 1; i < 5; i++) {
    dref = getRef('archive-thumb-' + i);
    dref2 = getRef('archive-thumb-' + (i+2));
    dref.src = dref2.src;
    if ((i+striptopleft-1 == currentstrip) && (mode == M_STRIPS)) {
      dref.style.borderColor = "red";
    } else {
       dref.style.borderColor = "white";
    }
  }
  
   for (i = 5; i < 7; i++) {
     dref = getRef('archive-thumb-' + i);
     if (i+striptopleft-1 > to) { 
       dref.src = 'images/1p.png';
       dref.style.borderWidth = 0;
     } else {
       if (mode == M_STRIPS) {
         dref.src = strip_thumbs1[i+striptopleft-1];  
       } else {
         dref.src = fn + (i+striptopleft-1) + '.jpg';  
       } 
       dref.style.borderWidth = 1;
     }
     if (i+striptopleft-1 == currentstrip) {
        dref.style.borderColor = "red";
     } else {
        dref.style.borderColor = "white";
     }     
   }
   archiveArrows();
}

function makeRequest(url) {
        var http_request = false;
        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
        http_request.onreadystatechange = function() { alertContents(http_request); };
        http_request.open('GET', url, true);
        http_request.send(null);

}

function alertContents(http_request) {

        if (http_request.readyState == 4) {
            if ((http_request.status == 200) || (http_request.status == 0)) {
                var xmldoc = http_request.responseXML;
				try {
                  var type = xmldoc.getElementsByTagName('type')[0].childNodes[0].nodeValue;
				} catch (error) {
				  alert(xmldoc.xml);
				}
                if (type == 'storyline') loadStorylineData(xmldoc);
                else if (type == 'chapter') loadChapterData(xmldoc);
                else if (type == 'chapterlist') loadChapterlistData(xmldoc);
                else if (type == 'news') loadNewsData(xmldoc);
                else if (type == 'comments') loadCommentsData(xmldoc);
                else if (type == 'cast') loadCastData(xmldoc);
                else if (type == 'whatsnew') loadWhatsnewData(xmldoc);
            } else {
                alert(http_request.status);
                alert('There was a problem with the request.');
            }
        }
}

/* Storyline object and functions */
function o_storyline(chapter, id, from, to, title) {
  this.id = id;
  this.from = from;
  this.to = to;
  this.title = title;
  this.chapter = chapter;
  this.loaded = false;
}

function tv(n, t) {
  try {
    return n.getElementsByTagName(t)[0].childNodes[0].nodeValue;
  } catch (error) {
    return '';
  }
}

function loadStorylineData(xmldoc) {
  var strips = xmldoc.getElementsByTagName('strip');
  for (i = 0; i < strips.length; i++) {
    var strip = strips[i];
    var id = strip.attributes.getNamedItem("strip").value;
    var thumb1 = strip.attributes.getNamedItem("thumb1").value;
    var thumb2 = strip.attributes.getNamedItem("thumb2").value;
    var name = strip.childNodes[0].nodeValue;
    strip_names[id] = name;
    strip_thumbs1[id] = thumb1;
    strip_thumbs2[id] = thumb2;
  }
  var id = parseInt(xmldoc.getElementsByTagName('storyline')[0].childNodes[0].nodeValue);      
  var from = parseInt(xmldoc.getElementsByTagName('from')[0].childNodes[0].nodeValue);
  var to = parseInt(xmldoc.getElementsByTagName('to')[0].childNodes[0].nodeValue);
  var name = xmldoc.getElementsByTagName('name')[0].childNodes[0].nodeValue;
  var chapter = parseInt(xmldoc.getElementsByTagName('chapter')[0].childNodes[0].nodeValue);

  var focus = xmldoc.getElementsByTagName('showcommand')[0].childNodes[0].nodeValue;
  storylines[id] = new o_storyline(chapter, id, from, to, name);
  storylines[id].loaded = true;
  loadStoryLine(id, focus);
}

function loadChapterData(xmldoc) {
  var stories = xmldoc.getElementsByTagName('storyline');
  var chapter = parseInt(xmldoc.getElementsByTagName('chapter')[0].childNodes[0].nodeValue);
  
  for (i = 0; i < stories.length; i++) {
    var story = stories[i];
    var id = story.attributes.getNamedItem("story").value;
    var name = story.childNodes[0].nodeValue;
    if (typeof storylines[id] != 'object') 
      storylines[id] = new o_storyline(chapter, id, 0, 0, name);
  }
  var id = parseInt(xmldoc.getElementsByTagName('storyline')[0].childNodes[0].nodeValue);
  var from = parseInt(xmldoc.getElementsByTagName('from')[0].childNodes[0].nodeValue);
  var to = parseInt(xmldoc.getElementsByTagName('to')[0].childNodes[0].nodeValue);
  var name = xmldoc.getElementsByTagName('name')[0].childNodes[0].nodeValue;
  chapters[chapter] = new o_storyline(chapter, chapter, from, to, name);
  chapters[chapter].loaded = true;
  chapters[chapter].firststrip = parseInt(xmldoc.getElementsByTagName('firststrip')[0].childNodes[0].nodeValue);
  
  if (mode == M_STORIES) loadChapter(chapter, true);
}

function loadChapterlistData(xmldoc) {
  var chapters2 = xmldoc.getElementsByTagName('chapter');
  
  for (i = 0; i < chapters2.length; i++) {
    var chapter = chapters2[i];
    var id = chapter.attributes.getNamedItem("chapter").value;
    var firststrip = chapter.attributes.getNamedItem("first").value;
    var name = chapter.childNodes[0].nodeValue;
    if (typeof chapters[id] != 'object') 
      chapters[id] = new o_storyline(id, id, 0, 0, name);
    chapters[id].firststrip = firststrip;
    chaptermax = id;
  }
  chapterlist = true;
  chapter = chapters[storyline.chapter];
  if (mode == M_CHAPTERS) loadChapterlist(true);
}

function loadNewsData(xmldoc) {
  var news = xmldoc.getElementsByTagName('news');
  out = '';
  
  for (i = 0; i < news.length; i++) {
    var ni = news[i];
    var id = tv(ni,'id');
    var msg = tv(ni,'msg');
    out2 = '<div class="news">'
         + '<div class="newshead">' + '<div class="ntim">' + tv(ni, 'date') + '<br>' + tv(ni, 'time') + 'MST</div>'
         + tv(ni,'title') + '</div><div class="newsmsg">' + tv(ni, 'msg') + '</div>'
         + '<div class="newsfoot"><a href="mailto:'+tv(ni,'authormail') +'">'+ tv(ni,'author')+'</a></div>'
         + '<div class="newscmt"><a href="javascript:NewsComments('+ tv(ni, 'id') + ')">0' + ' comments</a></div></div>';
    out += out2;
  }
  newsloaded = true;
  getRef('comments-text2').innerHTML = out;
}

function loadCommentsData(xmldoc) {
  var comments = xmldoc.getElementsByTagName('comment');
  var target = xmldoc.getElementsByTagName('target')[0].childNodes[0].nodeValue;
  out = '';
  var link = ' ';
  for (i = 0; i < comments.length; i++) {
    var ni = comments[i];
    var link = tv(ni,'link');
	if (link == ' ') link = '';
    var msg = tv(ni,'msg');
	
	out2 = '<h4>';
	if (link != '') out2 += '<a href="'+link+'">';
	out2 += tv(ni,'nickname');
	if (link != ' ') out2 += '</a>';
	out2 += ' says:</h4> <div>'+ msg + '</div>';

    out += out2;
  }
  getRef(target).innerHTML = out;
}

function loadCastData(xmldoc) {
  var cast = xmldoc.getElementsByTagName('cast');
  out = '';
  
  for (i = 0; i < cast.length; i++) {
    var ci = cast[i];
    var ckey = ci.attributes.getNamedItem("ckey").value;
    var title = ci.childNodes[0].nodeValue;
    
    out2 = '<a href="index.php?p='+ ckey +'" onMouseOver="ShowCast()" onMouseOut="HideCast()"  class="mitem">'
         + '<img src="/icon/icon_' + ckey + '.jpg" alt="'+ title +'">'
         + title + "</a>";
    out += out2;
  }
  castloaded = true;
  dref = getRef('cast').innerHTML = out;
  dref = getRef('castframe');
  dref.style.top = commentsy - dref.offsetHeight;
  ShowHide('castframe','show');
}

function loadWhatsnewData(xmldoc) {
  var cast = xmldoc.getElementsByTagName('item');
  out = '';
  
  for (i = 0; i < cast.length; i++) {
    var ci = cast[i];
    var ckey = ci.attributes.getNamedItem("ckey").value;
    var group = ci.attributes.getNamedItem("group").value;
    var date = ci.attributes.getNamedItem("date").value;
    var title = ci.childNodes[0].nodeValue;
    
    out2 = '<a href="index.php?p='+ ckey +'" onMouseOver="ShowMenu(\'whatsnew\')" onMouseOut="HideMenu()"  class="wnitem">'
         + '<b>' + group + ':</b><br /> ' + title + "<br /><i>" + date + "</i></a>";
    out += out2;
  }
  whatsnewloaded = true;
  dref = getRef('menu_whatsnew').innerHTML = out;
  ShowMenu('whatsnew');
}


function makeRequest2(type, id, other) {
  makeRequest('http://www.fantasycomic.com/php/xmlreq.php?req=' + type + '&id=' + id + other);
}

function loadStoryLine(id, focus) {
  //first 'unload' the storyline
  mode = M_STRIPS;
  for (i = 1; i < 7; i++) {
    dref = getRef('archive-thumb-' + i);
    dref.src = 'images/1p.png';
  }
    
  dref = getRef('archive-story');
  dref.src = 'images/subchapters/sub' + id + '.jpg';
  
  //then check if it has already been loaded
  if ((typeof storylines[id] != 'object') || (!storylines[id].loaded)) {
    makeRequest2('storyline', id, '&showcommand=' + focus);
  } else {
    //Loading the storyline.
    storyline = storylines[id];
    if (focus == 'last') {
      striptopleft = storyline.to - 5;
      if ((striptopleft % 2) != (storyline.from % 2)) striptopleft++;
      if (striptopleft < storyline.from) striptopleft == storyline.from;
    } else
     striptopleft = storyline.from;
    for (i = 1; i < 7; i++) {
      dref = getRef('archive-thumb-' + i);
      dref.style.width = 62;
      if (i+striptopleft-1 <= storyline.to) {
          dref.src = strip_thumbs1[i+striptopleft-1];
      	  dref.style.borderWidth=1;
          if (i+striptopleft-1 == currentstrip) 
            dref.style.borderColor = "red";
           else
            dref.style.borderColor = "white";
      } else {
      	  dref.style.borderWidth=0;
      }
    } 
    if (storyline.chapter != chapter.id) {
       getRef('archive-chapter').src = 'images/chapters/chap' + storyline.chapter + '.jpg';
       if (!chapterlist) loadChapterlist(false); else chapter = chapters[storyline.chapter];
    }
    archiveArrows();
  }
}
  
function loadChapter(id, active) {  
  if (active) {
    mode = M_STORIES;
    setTimeout("ShowHide('archive-story-qm','show')",500);
    //first 'unload' the storyline
    for (i = 1; i < 7; i++) {
      dref = getRef('archive-thumb-' + i);
      dref.src = 'images/1p.png';
    }
  }
  
  //then check if it has already been loaded
  if ((typeof chapters[id] != 'object') || (!chapters[id].loaded)) {
    makeRequest2('chapter', id, '');
  } else if (active) {
    chapter = chapters[id];
    striptopleft = chapter.from;
    for (i = 1; i < 7; i++) {
      dref = getRef('archive-thumb-' + i);
      dref.style.width = 50;
      if (i+striptopleft-1 <= chapter.to) {
          dref.src = 'images/subchapters/sub' + (i+striptopleft-1) + '.jpg';
      	  dref.style.borderWidth=1;
          dref.style.borderColor = "white";
      } else {
      	  dref.style.borderWidth=0;
      }
    }
  }
  getRef('archive-chapter').src = 'images/chapters/chap' + chapter.id + '.jpg';
  archiveArrows();
}
  
function loadChapterlist(active) {  
  if (active) {
    mode = M_CHAPTERS;
    setTimeout("ShowHide('archive-chapter-qm','show')",500);
    ShowHide('archive-story-qm','show')
    //first 'unload' the storyline
    for (i = 1; i < 7; i++) {
      dref = getRef('archive-thumb-' + i);
      dref.src = 'images/1p.png';
    }
  }
  
  //then check if it has already been loaded
  if (!chapterlist) {
    makeRequest2('chapterlist', 0, '');
  }
  
  if (active) {
    striptopleft = chapter.id - 5;
    if ((striptopleft % 2) != 0) striptopleft++;
    if (striptopleft + 5 < chaptermax) striptopleft += 2;
    if (striptopleft <0) striptopleft = 0;
    for (i = 1; i < 7; i++) {
      dref = getRef('archive-thumb-' + i);
      dref.style.width = 50;
      if (i+striptopleft-1 <= chaptermax) {
          dref.src = 'images/chapters/chap' + (i+striptopleft-1) + '.jpg';
      	  dref.style.borderWidth=1;
          dref.style.borderColor = "white";
      } else {
      	  dref.style.borderWidth=0;
      }
    }
  }
  archiveArrows();
}

function loadNews() {
  if (!newsloaded) makeRequest2('news',0);
}

function clickThumb(id) {
  id2 = id + striptopleft -1;
  if (mode == M_STRIPS) {
    window.location = 'index.php?p=c' + id2;
  } else if (mode == M_STORIES) {
    loadStoryLine(id2);
    ShowHide('archive-story-qm','hide');
  } else if (mode == M_CHAPTERS) {
    loadChapter(id2, true);
    ShowHide('archive-chapter-qm','hide');
  }
}

function clickStory(leftButton) {
  if (leftButton) {
    window.location = 'index.php?p=storyline' + storyline.id;
  } else {
    loadChapter(chapter.id, true);
    HideHint();
    return false;
  }
}

function clickChapter(leftButton) {
  if (leftButton) {
    window.location = 'index.php?p=c' + chapter.firststrip;
  } else {
    loadChapterlist(true);
    HideHint();
    return false;
  }
}

function archiveArrows() {
  if (mode == M_STRIPS) {
    if (striptopleft > 1) { 
      if (striptopleft == storyline.from) getRef('archive-left').src = 'images/archive-left-a.png';
      				     else getRef('archive-left').src = 'images/archive-left.png';
      ShowHide('archive-left','show');        
    } else ShowHide('archive-left','hide');
    if (striptopleft+5 < stripmax) { 
      if (striptopleft+6 > storyline.to) getRef('archive-right').src = 'images/archive-right-a.png';
      				     else getRef('archive-right').src = 'images/archive-right.png';
      ShowHide('archive-right','show'); 
    } else ShowHide('archive-right','hide');    
  } else if (mode == M_STORIES) {
    if (striptopleft > chapter.from) ShowHide('archive-left','show'); else ShowHide('archive-left','hide');
    if (striptopleft+5 < chapter.to) ShowHide('archive-right','show'); else ShowHide('archive-right','hide');
  } else if (mode == M_CHAPTERS) {
    if (striptopleft > 0) ShowHide('archive-left','show'); else ShowHide('archive-left','hide');
    if (striptopleft+5 < chaptermax) ShowHide('archive-right','show'); else ShowHide('archive-right','hide');
  }
}

function comNav(dir) {

   if (clockAr == 0) {
   	clockAr = setTimeout("ShowHide('c-"+dir+"', 'show')", 1000);
   }
}

function comNavOff() {
   if (clockAr != 0) clearTimeout(clockAr);
   clockAr = 0;
   ShowHide('c-left','hide');
   ShowHide('c-right','hide');
}

function KillClock2() {
   clearTimeout(castclock);
}
function ShowCast() {
  KillClock2;
  if (castloaded) {
    if (casthide) ShowHide('castframe','show');
  } else {
    makeRequest2('cast',currentstrip,'');
  }
  casthide = false;  
  dohidecast = false;
}
function HideCast() {
  KillClock2();
  if (!dohidecast) castclock = setTimeout("DoHideCast()", 1000);
  dohidecast = true;
}
function DoHideCast() {
   KillClock2();
   if (dohidecast) {
      ShowHide('castframe','hide');
      casthide = true;
   }
}

function NewsComments(nid) {
  window.open(nid ,'addcomment','scrollbars=yes,width=600,height=500');
}

function ExpandHint() {
 if(!comments_enlarged) {
   ShowHint('Expand', 'Click here if you want this panel to be a bit bigger.');
 } else ShowHint('Shrink', 'Eeep! Down again!');

}

function UpdateHint() {
  ShowHint('Update countdown','This timer counts down to the <i>expected</i> release of the next strip.<br>The next strip is <b>' + updatestatus + '</b>'); 
}

function ShowMenu(menu) {
   clearTimeout(menuclock);
   if ((currentmenu != '') && (currentmenu != 'menu_' + menu)) {
     ShowHide(currentmenu, 'hide');
   }
   currentmenu = 'menu_' + menu;
   ShowHide(currentmenu, 'show');
   dohidemenu = false;
}

function HideMenu() {
   clearTimeout(menuclock);
  if (!dohidemenu) menuclock = setTimeout("DoHideMenu()", 300);
  dohidemenu = true;
}

function DoHideMenu() {
   clearTimeout(menuclock);
   if (dohidemenu) {
      ShowHide(currentmenu,'hide');
      currentmenu = '';
   }
}

function ShowWhatsNew() {
  if (whatsnewloaded)
     ShowMenu('whatsnew');
   else
     makeRequest2('whatsnew', 10, '');
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=")
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1 
    c_end=document.cookie.indexOf(";",c_start)
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end))
    } 
  }
return ""
}

function customizeSite() {
   prefShowHint	= document.customize.opt_showhints.checked;
   
   var  Cookie = '';
   if (!prefShowHint) Cookie+='showhint=false'
   setCookie('cts_pref',Cookie,365);
}

function readPrefs() {
   var Cookie = getCookie('cts_pref');
   var prefs = Cookie.split('|');
   
   for (pref in prefs) {
	  pref = prefs[pref].split('=');
	  if (pref[0] == 'showhint') prefShowHint = (pref[1] == 'true');
   }
}

function populateCustomizeForm() {
  readPrefs();
  document.customize.opt_showhints.checked = prefShowHint;
}

function StartCountdownTimer(seconds) {
   var nDate = new Date();
   target = nDate.getTime() + seconds*1000;
    UpdateCountDown();
}
function LZ(number) {
  if (number < 10) return "0" + number; else return number;
}
function UpdateCountDown() {
   if(clockID) {
      clearTimeout(clockID);
      clockID  = 0;
   }
   var days, hours,minutes,seconds, tmp;
   var nDate = new Date();
   var msg;
   
   cntfrom = Math.floor((target - nDate.getTime())/1000);
   if (cntfrom > 0) {
     days = Math.floor(cntfrom/(24*3600));
     tmp = cntfrom % (24*3600);
     hours = Math.floor(tmp/3600);
     tmp = tmp % 3600;
     minutes = Math.floor(tmp/60);
     seconds = tmp % 60; 
	 if (days > 0) {msg = "" + days+ "d " ;} else {msg = "";}
     msg = msg + "" + hours + ":" + LZ(minutes) + ":" + LZ(seconds);
	 clockID = setTimeout("UpdateCountDown()", 800);
	} else {
	 msg = "NOW!!!";
   }  
   getRef('archive-timer').innerHTML ="Next Update: "+ msg;
}

function CloseDialog() {
  ShowHide('dialog', 'hide');
}

function SearchDialog() {
  getRef('dialog-text').innerHTML = '<b>Search Terms:</b><br/><form name="searchform">'
                                   +'<input value="search" name="p" type="hidden"> '
                                   +'<input name="s" value="" id="dialog-searchbox" type="text"><input value="Search" type="submit"></form>';
  ShowHide('dialog', 'show');
  document.searchform.s.focus();
}

//findpos script courtesy of quirksmode.org
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}
