
// FILE:    support.js
// VERSION: $Id: support.js,v 1.15 2002/11/21 01:53:22 root Exp root $
// COPYRIGHT 2000, 2001, 2002, Mark J. Taylor (mtaylor@squeakyporcupine.com).  All Rights Reserved.

// Cam stuff
var src_ip    = new Array(document.location.hostname, document.location.hostname)
var src_port  = new Array(9001,                       9002                      )

var miniwindow = new Array( 0, 0 )
var newcam     = new Array( new Image(), new Image() )
var inum       = new Array( Math.round(1000000+(Math.random() * 1000000)),
                            Math.round(1000000+(Math.random() * 1000000)) )
var once       = new Array( 1, 1 )

var ow_opened = 0
var previewTimeout

var q  = "'"
var qq = '"'


function iterate_cam(num) {
  inum[num]++
  eval("document.images.cam"+num+".src = newcam["+num+"].src")  // set the cam# image to the newcam image
  //if (! miniwindow[num]) { newcam[num].src = "http://"+src_ip[num]+":"+src_port[num]+"/image?"+inum[num] }  // start getting the next newcam image
  if (! miniwindow[num]) { newcam[num].src = "image.cgi?"+inum[num] }  // start getting the next newcam image
}


function first_cam(num) {
  if (once[num]) {
    once[num] = 0;  inum[num]++
    eval("function iterate_cam"+num+"() { iterate_cam("+num+"); }")                // create a function called "iterate_cam#"
    eval("newcam["+num+"].onload = iterate_cam"+num)                               // call "iterate_cam#" every time the image finishes loading
    //newcam[num].src = "http://"+src_ip[num]+":"+src_port[num]+"/image?"+inum[num]  // start the ball rolling
    newcam[num].src = "image.cgi?"+inum[num]  // start the ball rolling
  }
}


// print the HTML for a Cam reference
function printCam(num) {
  //var s = "http://"+src_ip[num]+":"+src_port[num]+"/image"
  var s = "?small=1"
  var w = 320
  var h = 240

  var m = document.URL.indexOf("mag=")
  if (m != -1) { m = parseFloat(document.URL.substring(m+4)); s = s+'&mag='+m } else { m = 1 }
  h *= m
  w *= m
  document.writeln('<TD>')
  document.write('<A HREF="javascript:ow_cam('+num+',\''+s+'\','+w+','+h+');" onmouseover="window.status=\'Live Mini-Window\'; return true;">')
  document.write('<IMG NAME="cam'+num+'" ALT="Click to force reload!" SRC="'+s+'" onload="first_cam('+num+')" WIDTH='+w+' HEIGHT='+h+' BORDER=3 onError="cam'+num+'.src='+"'noimage3.gif'"+'">')
  document.writeln('</A>')
  document.writeln('<BR><CENTER><TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0><TR><TD BGCOLOR="#FFFFFF"><FONT FACE="Arial,Helvetica"><A HREF="javascript:w='+w+'; h='+h+'; m=prompt(\'What magnification factor?\',\'\'); if(!m){m=1;} m=parseFloat(m); w*=m; h*=m; ow_cam('+num+',\''+s+'&mag=\'+m,w,h);" onmouseover="window.status=\'Live Mini-Window with Magnification!\'; return true;">X-Mag Technology!</A></FONT></TD></TR></TABLE></CENTER>')
  document.writeln('</TD>')
}


// Open Cam Mini-Window
function ow_cam(num, image, w, h) {
  miniwindow[num] = 1
  x = window.open(image, 'livecam'+num, 'dependent=no,channelmode=no,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width='+(w+40)+',height='+(h+40))
  x.focus()
}


function preview(n) {
  var i
  if (! eval('top.document.spot'+n)) { n=0 }
  for (i=0; i<pv.length/4; i++) { eval('top.document.spot'+i+'.src = "clear.gif"') }
  eval('top.document.spot'+n+'.src = "arrow_right.gif"')
  top.document.spot.src = pv[n*4+2]+'/'+pv[n*4+3]
  eval('top.document.spot'+n+'.src = "arrow_right.gif"')
  pvi = n
}


function previewGo(n) {
  if (n < 0) { n *= -1; n-- }
  if (document.location.protocol != "file:")
    document.location = pv[n*4+2]+'/'
  else
    document.location = pv[n*4+2]+'/index.html'
}


function over_preview(n) {
  window.status = pv[n*4+1]
  preview(n)
  pvi = -(1+n)
  clearTimeout(previewTimeout)
}

function restart_preview() {
  pvi = -pvi - 1
  previewTimeout = setTimeout("next_preview()", 6*1000)
}


function next_preview() {
  if (pvi < 0) { return }
  pvi++
  if (pvi >= pv.length/4) { pvi = 0; window.status = pv[0+1]; preview(0); return; }
  window.status = pv[pvi*4+1]
  preview(pvi)
  previewTimeout = setTimeout("next_preview()", 3*1000)
}


function printPreviewTable() {
  document.writeln('<TABLE BORDER=1 CELLPADDING=2 CELLSPACING=0>')

  for (i=0; i<pv.length/4; i++) {
    if ((i % 2) == 0) { document.writeln('<TR BGCOLOR="lightblue">') }
    else              { document.writeln('<TR BGCOLOR="#E0E0E0">')   }
    document.writeln('  <TD WIDTH=80 ALIGN="left" NOWRAP><FONT FACE="Arial,Helvetica">'+pv[i*4+0]+'</FONT>&nbsp;<IMG NAME="spot'+i+'" SRC="clear.gif" WIDTH=15 HEIGHT=15></TD>')
    document.writeln('<TD NOWRAP><A HREF="javascript:previewGo('+i+')" onmouseover="over_preview('+i+'); return true;" onmouseout="restart_preview();"><FONT FACE="Arial,Helvetica">'+ pv[i*4+1] + '</FONT></A></TD>')
    document.writeln('</TR>')
  }

  document.writeln('</TABLE>')
}


function printPuppyTable() {
  document.writeln('<TABLE BORDER=1 CELLPADDING=2><TR><TH COLSPAN=6 BGCOLOR="#C0C0C0"><B><FONT COLOR="blue"><A HREF="http://www.squeakyporcupine.com/">www.squeakyporcupine.com</A></FONT><BR>Schedule for the week of '+ps_week+'</B><BR><FONT SIZE="-2">(9am - 8pm EST)</FONT></TH></TR>')

  document.writeln('<TR BGCOLOR="yellow" ALIGN="center">')
  for (i=0; i<6; i++) { document.writeln('<TD WIDTH="16%"><FONT SIZE="-1"><B>'+ps_DOW[i]+'</B></FONT></TD>') }
  document.writeln('</TR>')

  document.writeln('<TR ALIGN="center">')
  for (i=0; i<6; i++) {
    document.writeln('  <TD BGCOLOR="'+ps_color[ps[i]]+'"><FONT SIZE="-1">'+ps_text[ps[i]]+'</FONT></TD>')
  }
  document.writeln('</TR></TABLE>')
}


// Open a Window - the "Print It" function.
function owprint(image,w,h,descr,span) {
  var wthumb
  var hthumb
  var magfactw = 1
  var magfacth = 1
  var mf

  if (w > (screen.width-60))  magfactw = (screen.width-60) / w
  if (h > (screen.height-60)) magfacth = (screen.height-60) / h
  // pick the smaller magnification factor, so the image fits in the screen width
  mf = (magfacth<magfactw) ? magfacth : magfactw

  wthumb = parseInt(w/4)
  hthumb = parseInt(h/4)
  // fit the thumbnail image within the screen's width
  if (wthumb > (screen.width-60)) { var m = (screen.width-60)/wthumb; wthumb *= m;  hthumb *= m }

  w *= mf
  h *= mf
  // make them integers!
  w = parseInt(w)
  h = parseInt(h)

  document.write('  <TD WIDTH='+wthumb)
  if (span != 1) document.write(' COLSPAN='+span)
  document.write('>')
  // The following line's link does not work in IE when descr contains a single quote character:
  //document.write('<A HREF="javascript:ow('+"'"+image+"'"+','+w+','+h+','+"'"+escape(descr)+"'"+');">')
  // The workaround: use double-quotes around the escaped description
// XXX: instead of "Click to enlarge", need some kind of escape(description).  MSIE blows chunks on "'" in non-escaped strings
  document.write("<A HREF="+q+"javascript:ow("+qq+image+qq+","+w+","+h+","+qq+escape(descr)+qq+");"+q+" onMouseOver="+q+"window.status="+qq+"Click image to enlarge."+qq+"; return true;"+q+">")
  document.write('<IMG SRC="'+image+'t.jpg" WIDTH='+wthumb+' HEIGHT='+hthumb+' ALT="Click to enlarge ('+w+' x '+h+')"></A>')
  if (descr.length > 0)
    document.write('<BR><FONT FACE="Arial,Helvetica" SIZE="-1">'+descr+'</FONT>')
  document.writeln('</TD>')
}


function olCheck()
{
  if (this && this.document && (this.document.fgColor != "#000001")) { this.location.reload(true) }
}


szChecked = 0  // don't check the size more than once per load (MSIE bug)
function szCheck()
{
  var w_new, h_new

  if (szChecked != 0) return
  szChecked++

  w_new = 0

  if (this && this.innerWidth) {
    w_new = this.innerWidth
    h_new = this.innerHeight
  }
  else if (document && document.body) {
    // MSIE:
    w_new = document.body.clientWidth
    h_new = document.body.clientHeight
  }

  if (w_new != 0) {
    // keep aspect ratio
    var magfactw = w_new / window.document.width_orig
    var magfacth = (h_new - 46) / window.document.height_orig
    var mf = (magfacth<magfactw) ? magfacth : magfactw
    if (mf > 0) {
      ow(window.document.image_source, window.document.width_orig, window.document.height_orig, window.document.description, mf)
    }
  }
}


// Open a Window to the specified image.
function ow(image,w,h,descr,scale) {
  var w_win, h_win
  var w_orig, h_orig
  var pol
  var x

  w_orig = w
  h_orig = h
  if (isNaN(scale)) scale = 1
  w = w * scale
  h = h * scale
  pol = "0,30,30,30,30,0,"+w+",0,"+w+","+h+",0,"+h+",0,30"

  w_win = w + 0
  h_win = h + 46

  //if (ow_opened) { if (x.close) { x.close() } ow_opened=0 }  // clear out old contents
  //<a href="javascript:void(viewer(118075));"><img src="http://www2.fileplanet.com/images/110000/118075ss_sm.jpg" height="225" width="300" border="0" /></a>
  //function viewer(fileID) { window.open('/viewer.cgi?f=' + fileID, 'viewer', 'height=625,width=760,resizable=no'); }
  x = window.open("", 'picture', 'dependent=yes,toolbar=no,location=no,directories=no,personal=no,status=no,menubar=no,scrollbars=no,resizable=yes,width='+(w_win)+',height='+(h_win)+'')
  if (x.screenX + w_win > screen.width ) x.moveBy(screen.width - (x.screenX+w_win), 0)
  if (x.screenY + h_win > screen.height) x.moveBy(0, screen.height - (x.screenY+h_win))
  x.document.writeln('<HTML><HEAD>')
  x.document.writeln('<TITLE>Image '+image+'.jpg (click to close)</TITLE>')
  x.document.writeln('<STYLE TYPE="text/css">BODY {margin-top: 0; margin-left: 0; margin: 0;}</STYLE>')
  x.document.writeln('<SCRIPT LANGUAGE="JavaScript" SRC="/support.js"></SCRIPT>')
  x.document.writeln('</HEAD>')
  x.document.writeln('<BODY onLoad="olCheck()" onResize="szChecked=0; szCheck();" TEXT="#000001" BGCOLOR="#FFFFFF" marginheight=0 marginwidth=0 topmargin=0 leftmargin=0 rightmargin=0 border="no">')
  x.document.writeln('<MAP NAME="sqp_ow"><AREA SHAPE="rect" COORDS="0,0,30,30" HREF="javascript:owmax()" ALT="Click here to maximize"><AREA SHAPE="poly" COORDS="'+pol+'" href="javascript:ow_opened=0; close();" ALT="Click to close"></MAP>')
  x.document.writeln('<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH="100%">')
  x.document.writeln('<TR ALIGN="center"><TD><IMG SRC="'+image+'.jpg" LOWSRC="'+image+'t.jpg" BORDER=0 HEIGHT='+h+' WIDTH='+w+' ALT="Click to close" onLoad="loading.style.visibility='+"'hidden'"+'" USEMAP="#sqp_ow"></TD></TR>')
  x.document.writeln('<TR ALIGN="center"><TD><IMG SRC="/icons/loading.gif" NAME="loading" ID="loading" STYLE="visibility:visible;" BORDER=0 HEIGHT=22 WIDTH=20></TD></TR>')
  if (descr.length > 0)
    x.document.writeln('<TR ALIGN="center"><TD><FONT FACE="Arial,Helvetica" SIZE=2>'+unescape(descr)+'</FONT></TD></TR>')
  else
    x.document.writeln('<TR><TD>&nbsp;</TD></TR>')
  x.document.writeln('</TABLE>')
  x.document.writeln('</BODY>')
  x.document.writeln('</HTML>')

  x.document.width_orig   = w_orig
  x.document.height_orig  = h_orig
  x.document.scale        = scale
  x.document.description  = descr
  x.document.image_source = image

  x.document.close()
  x.focus()
  ow_opened = 1
}


function owmax()
{
  var magfacth, magfactw

  if (window.document.height_orig < window.document.width_orig) {
    magfactw = screen.width / window.document.width_orig
    magfacth = magfactw
  }
  else {
    magfacth = screen.height / window.document.height_orig
    magfactw = magfacth
  }
  window.moveTo(0, 0)
  window.resizeTo(document.width_orig*magfactw, document.height_orig*magfacth)
  if (navigator.userAgent.indexOf("MSIE") != -1) { szChecked=0; szCheck(); }  // for MSIE, perform size check
}


function owprint2(t,x,y,c)
{
  document.write('<TD>'+t+'</TD>')
}


var pps_width = 820
function printPage(stuff)
{
  var i
  var c = 0
  var px = 0
  var img, width, height, descr
  var span
  var need_tr

  // XXX
  // Want to pre-scan each section for maximum TD width.

  document.writeln('<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=10>')
  need_tr = 0

  for (i=0; i<stuff.length; i+=4,c++) {
    img = stuff[i+0];  width = stuff[i+1];  height = stuff[i+2];  descr = stuff[i+3]

    // new row if too many pixels
    if ((img == "") || (px+width/4 >= pps_width)) {
      if (i != 0) document.writeln('</SCRIPT>\n')
      c = 0
      if (img == "") {
        if (++i < stuff.length) {
          if (stuff[i++] != "") {
            if (need_tr) document.writeln('</TR>')
            document.writeln('</TABLE><HR><CENTER><H2>'+stuff[i-1]+'</H2></CENTER><TABLE BORDER=0 CELLPADDING=0 CELLSPACING=10>')
            need_tr = 0
          }
          img = stuff[i+0];  width = stuff[i+1];  height = stuff[i+2];  descr = stuff[i+3]
        }
      }
      px = 0
    }

    // check for end of input array
    if (i >= stuff.length) break

    // check for first column: start a new table row
    if (c == 0) {
      if (need_tr) document.writeln('</TR>')
      document.write('<TR VALIGN="top" ALIGN="center">\n<SCRIPT LANGUAGE="JavaScript">\n')
      need_tr = 1
    }

    span = (width/4) / parseInt((pps_width-20) / 5)
    span = parseInt(span)
    if (isNaN(span)) span = 1
    if (span > 5) span = 5
    if (span == 0) span = 1
    document.write('  owprint("'+img+'", '+width+','+height+', "'+descr+'", '+span+')\n')

    // increment pixel count
    px += width/4
  }

  // close table, if not on first column
  if (c != 0) {
    document.writeln('</SCRIPT>\n</TR>')
    need_tr = 0
  }

  if (need_tr) document.writeln('</TR>')
  document.writeln('</TABLE>')
}

