/**
 * Copyright (c) 2004
 *      Ali Onur Cinar &060;cinar(a)zdo.com&062;
 *
 * SONY is the registered trademark of SONY Corporation.
 *
 * JavaScript to control SONY SNC-RZ30N cameras.
 * http://www.zdo.com/articles/SonySNC.php 
 *
 * Usage:
 *
 * <script type="text/javascript" src="SonySNC.js"></script>
 *
 * <script type="text/javascript">
 *   var camera = new SonySNC('labcam', 'http://192.168.1.1');
 *
 *   camera.goPreset(2);  // go preset 2
 *   camera.move('u');    // move camera up
 *   camera.capture();    // capture it 
 * </script>
 * 
 */

/**
 * Constructor.
 *
 * @param id  a unique id for camera
 * @param url camera's address
 */
function SonySNC (id, url)
{
  this.init(id, url);
}

SonySNC.prototype =
{
  /**
   * Initialize.
   *
   * @param url sony camera url
   */
  init : function (id, url)
  {
    this.id = id;
    this.url = url;
    this.cmdDelay = 500;
    
    this.createForm();
  },

  /**
   * Moves camera in given direction.
   *
   * @param d movement direction, can be
   *          u - up
   *          d - down
   *          l - left
   *          r - right
   *          h - home
   *          i - zoom in
   *          o - zoom out
   */
  move : function (d)
  {
    switch (d)
    {
      case 'u':
        this.send('8101060103030302FF');
        setTimeout("this.stop()", cmdDelay);
        break;
      case 'd':
        this.send('8101060103030301FF');
        setTimeout("this.stop()", cmdDelay);
        break;
      case 'l':
        this.send('8101060103030203FF');
        setTimeout("this.stop()", cmdDelay);
        break;
      case 'r':
        this.send('8101060103030103FF');
        setTimeout("this.stop()", cmdDelay);
        break;
      case 'h':
        this.send('81010604FF');
        break;
      case 'i':
        this.send('8101040724FF');
        setTimeout("this.send('8101040700FF')", cmdDelay);
        break;
      case 'o':
        this.send('8101040734FF');
        setTimeout("this.send('8101040700FF')", cmdDelay);
        break;
    }
  },

  /**
   * Moves camera to given preset position.
   *
   * @param n preset number
   */
  goPreset : function (n)
  {
    if (n < 9)
    {
      n = '0' + n;
    }

    this.send('8101043F02'+n+'FF');
  },

  /**
   * Send message to camera.
   *
   * @param s message string
   */
  send : function (s)
  {
    var form = eval('document.sncForm_' + this.id);
    form.visca.value = s;
    form.submit();
	location.reload(true);
	//location.replace('#cam');
},

  /**
   * Send stop message to camera.
   *
   */
  stop : function ()
  {
    this.send('8101060103030303FF');
  },

  /**
   * Set command delay.
   *
   * @param d command delay
   */
  delay : function (d)
  {
    cmdDelay = d;
  },

  /**
   * Capture the current frame and show
   * in a popup window.
   *
   */
  capture : function ()
  {
    var body =   
      '<?xml version="1.0" encoding="iso-8859-1"?>'
    + ' <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
    + ' <html xmlns="http://www.w3.org/1999/xhtml">'
    + ' <head>'
    + '   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />'
    + '   <title>Camera Capture</title>'
    + '   <meta http-equiv="Pragma" content="no-cache" />'
    + '   <meta http-equiv="Cache-Control" content="no-cache" />'
    + ' </head>'
    + ' <body xml:lang="en" lang="en">'
    + ' <div>'
    + '<img src="'
    + this.url
    + '/oneshotimage.jpg" style="width:640px; height:480px;"'
    + ' alt="Captured" />'
    + '<input type="button" value="Close" onclick="self.close();" />'
    + ' </div></body></html>';
     
    var win = window.open('', 'captured_'+this.id,
      'width=700, height=530, resizable=1');
    win.document.writeln(body);
  },

  /**
   * Creates the communication form.
   *
   */
  createForm : function ()
  {
    var body =
      '<ilayer name="clear" width="1" height="1"></ilayer>'
    + '<form name="sncForm_' + this.id + '"'
    + '  action="' + this.url + '/command/ptzf.cgi"'
    + '  method="post"'
    + '  target="" >'
    + '  <input type="hidden" name="visca" value="" />'
    + '</form>';
    
    document.writeln(body);
    
  }
}
