flixn = {
  recordComplete: function(videoid) {
    flixnwc.recordEnable = false;
    flixnwc.showIframe('create', document.location, '&misc=' + videoid);
  }
};

flixnwc = {

  inputId: null,
  openContainer: null,

  containerId: 'flixnwc_container',
  iframeId: 'flixnwc_iframe',

  createHeader: 'http://widgets.flixn.com/comment/12/img/cisco/create_header.gif',
  createWidth: 400,
  createHeight: 300,

  viewHeader: 'http://widgets.flixn.com/comment/12/img/cisco/view_header.gif',
  viewWidth: 600,
  viewHeight: 550,

  recordEnable: true,

  recordHeader: 'http://widgets.flixn.com/comment/12/img/cisco/record_header.gif',
  recordWidth: 328,
  recordHeight: 353,

  closeImage: 'http://widgets.flixn.com/comment/12/img/cisco/close.gif',

  altImage: 'http://widgets.flixn.com/comment/12/img/cisco/record_alt.gif',

  recorderId: 'flixn_recorder',
  _recorderWidth: 328,
  _recorderHeight: 320,
  _recorderUrl: 'http://widgets.flixn.com/comment/12/flash/recorder_cisco.swf',

  hideObjects: true,
  hideSelects: true,

  _deltaX: null,
  _deltaY: null,
  _toDrag: null,

  drag: function(event) {
    var startX = event.clientX, startY = event.clientY;
    var origX = flixnwc._toDrag.offsetLeft, origY = flixnwc._toDrag.offsetTop;
    flixnwc._deltaX = startX - origX;
    flixnwc._deltaY = startY - origY;

    if (document.addEventListener) {
      document.addEventListener('mousemove', flixnwc.moveHandler, true);
      document.addEventListener('mouseup', flixnwc.upHandler, true);
    }
    else if (document.attachEvent) {
      flixnwc._toDrag.setCapture();
      flixnwc._toDrag.attachEvent('onmousemove', flixnwc.moveHandler);
      flixnwc._toDrag.attachEvent('onmouseup', flixnwc.upHandler);
      flixnwc._toDrag.attachEvent('onlosecapture', flixnwc.upHandler);
    }
    else {
    }
  },

  moveHandler: function(e) {
    if (!e)
      e = window.event;

    flixnwc._toDrag.style.left = (e.clientX - flixnwc._deltaX) + 'px';
    flixnwc._toDrag.style.top = (e.clientY - flixnwc._deltaY) + 'px';

    if (e.stopPropagation)
      e.stopPropagation();
    else
      e.cancelBubble = true;
  },

  upHandler: function(e) {
    if (!e)
      e = window.event;

    if (document.removeEventListener) {
      document.removeEventListener('mouseup', flixnwc.upHandler, true);
      document.removeEventListener('mousemove', flixnwc.moveHandler, true);
    }
    else if (document.detachEvent) {
      flixnwc._toDrag.detachEvent('onlosecapture', flixnwc.upHandler);
      flixnwc._toDrag.detachEvent('onmouseup', flixnwc.upHandler);
      flixnwc._toDrag.detachEvent('onmousemove', flixnwc.moveHandler);
      flixnwc._toDrag.releaseCapture();
    }

    if (e.stopPropagation)
      e.stopPropagation();
    else
      e.cancelBubble = true;
  },

  showIframe: function(page, uri, misc) {

    if (flixnwc.openContainer != null)
      flixnwc.closeIframe(flixnwc.openContainer);

    if (this.hideObjects) {
      this._setVisibilityStylesByTagName('object', 'hidden');
      this._setVisibilityStylesByTagName('embed', 'hidden');
    }

    if (this.hideSelects) {
      this._setVisibilityStylesByTagName('select', 'hidden');
    }

    this._buildIframe(page, uri, misc);
    this._placeIframe(page);

    this._showIframe(page);
    flixnwc.openContainer = page;
  },

  closeIframe: function(page) {
    if (page == 'create')
        flixnwc.recordEnable = true;

    if (this.hideObjects) {
      this._setVisibilityStylesByTagName('object', 'visible');
      this._setVisibilityStylesByTagName('embed', 'visible');
    }

    if (this.hideSelects) {
      this._setVisibilityStylesByTagName('select', 'visible');
    }

    this._hideIframe(page);

    flixnwc.openContainer = null;
  },

  _buildIframe: function(page, uri, misc) {
    var container = document.createElement('div');
    container.setAttribute('id', this.containerId + '_' + page);
    container.setAttribute('class', this.containerId);
    container.setAttribute('className', this.containerId);
    container.style.display = 'none';

    var header = document.createElement('div');
    header.setAttribute('id', this.containerId + '_header');

    if (document.attachEvent) {
      header.attachEvent('onmousedown', flixnwc.drag);
    } else {
      header.setAttribute('onmousedown', 'flixnwc.drag(event)');
    }
    flixnwc._toDrag = container;
    container.appendChild(header);

    var header_img = document.createElement('img');
    header_img.setAttribute('id', this.containerId + '_header_img');
    if (page == 'create')
      header_img.src = this.createHeader;
    else if (page == 'view')
      header_img.src = this.viewHeader;
    else if (page == 'record')
      header_img.src = this.recordHeader;
    header.appendChild(header_img);

    var f_location = document.location.protocol + '//';
    if (document.location.hostname == 'cisco.com')
        f_location = f_location + 'www.cisco.com';
    else
        f_location = f_location + document.location.hostname;
    f_location = f_location + document.location.pathname;

    if (page == 'create' && flixnwc.recordEnable == true) {
      var record_link = document.createElement('a');
      record_link.href = "javascript:flixnwc.showIframe('record', '" + f_location + "');";

      var alt_img = document.createElement('img');
      alt_img.setAttribute('id', this.containerId + '_header_alt_img');
      alt_img.src = this.altImage;

      record_link.appendChild(alt_img);
      header.appendChild(record_link);
    }

    var close_link = document.createElement('a');
    if (page == 'record')
      close_link.href = "javascript:flixnwc.closeIframe('" + page + "'); flixnwc.showIframe('create', '" + f_location + "');";
    else
      close_link.href = "javascript:flixnwc.closeIframe('" + page + "');";
    var close_img = document.createElement('img');
    close_img.setAttribute('id', this.containerId + '_close');
    close_img.src = this.closeImage;
    close_link.appendChild(close_img);
    header.appendChild(close_link);

    if (page == 'record') {
      this._buildRecorder(page, container);
    } else {
      var iframe = document.createElement('iframe');
      iframe.setAttribute('id', this.iframeId + '_' + page);
      iframe.setAttribute('class', this.iframeId);
      iframe.setAttribute('width', '100%');
      iframe.setAttribute('frameBorder', '0');
      iframe.setAttribute('scrolling', 'no');

// XXX
      var iframe_src = 'http://widgets.flixn.com/comment/12/' + page + '.php?id=' + flixnwc.id + '&uri=' + uri;
      if (misc != null)
        iframe_src = iframe_src + misc;

      iframe.src = iframe_src
      container.appendChild(iframe);
    }

    document.body.appendChild(container);
  },

  _buildRecorder: function(page, container) {
    var recorder = document.createElement('div');
    recorder.setAttribute('id', this.recorderId);
    container.appendChild(recorder);

    var noflash = document.createElement('a');
    noflash.href = 'http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash';
    noflash.target = '_blank';

    var noflashimg = document.createElement('div');
    noflashimg.setAttribute('id', 'flixn_noflash');
    noflash.appendChild(noflashimg);
    recorder.appendChild(noflash);

    document.body.appendChild(container);

    var so = new SWFObject(this._recorderUrl, this.recorderId + '_obj',
                           this._recorderWidth, this._recorderHeight, '8',
                           '#ffffff');
    so.addParam('menu', 'false');
    so.addParam('allowScriptAccess', 'always');
    so.addParam('swLiveConnect', 'true');
    so.write(this.recorderId);
  },

  _hideIframe: function(page) {
    document.body.removeChild(document.getElementById(this.containerId + '_' + page));
  },

  _placeIframe: function(page) {
    var windowSize = this._getWindowSize();
    this._scrollOffset = this._getScrollOffset();

    if (page == 'create') {
      x = this._scrollOffset[0] + (windowSize[0] - this.createWidth)/2;
      y = this._scrollOffset[1] + (windowSize[1] - this.createHeight)/2;
    } else if (page == 'view') {
      x = this._scrollOffset[0] + (windowSize[0] - this.viewWidth)/2;
      y = this._scrollOffset[1] + (windowSize[1] - this.viewHeight)/2;
    } else {
      x = this._scrollOffset[0] + (windowSize[0] - this.recordWidth)/2;
      y = this._scrollOffset[1] + (windowSize[1] - this.recordHeight)/2;
    }

    var iframe = document.getElementById(this.containerId + '_' + page);
    iframe.style.left = x + 'px';
    iframe.style.top = y + 'px';
  },

  _showIframe: function(page) {
    var iframe = document.getElementById(this.containerId + '_' + page);
    iframe.style.display = 'block';
  },

  _setVisibilityStylesByTagName: function(tagname, style) {
    objs = document.getElementsByTagName(tagname);
    for (var i = 0; i < objs.length; i++) {
      objs[i].style.visibility = style;
    }
  },

  _getScrollOffset: function() {
    var x = window.pageXOffset || document.documentElement.scrollLeft ||
            document.body.scrollLeft || 0;

    var y = window.pageYOffset || document.documentElement.scrollTop ||
            document.body.scrollLeft || 0;

    return [x, y];
  },

  _getWindowSize: function() {
    var width, height;

    if (window.innerWidth) {
      width = window.innerWidth;
      height = window.innerHeight;
    }

    if (document.documentElement &&
        document.documentElement.clientWidth) {

      cw = document.documentElement.clientWidth;
      if (!width || cw && cw < width) {
        width = cw;
      }
      if (!height) {
        height = document.documentElement.clientHeight;
      }
    } else if (document.body) {
      width = document.body.clientWidth;
      height = document.body.clientHeight;
    }

    return [width, height];
  }
};

var f_location = document.location.protocol + '//';
if (document.location.hostname == 'cisco.com')
    f_location = f_location + 'www.cisco.com';
else
    f_location = f_location + document.location.hostname;
f_location = f_location + document.location.pathname;

var flixnwc_create = document.getElementById('flixnwc_create');
flixnwc_create.href = "javascript:flixnwc.showIframe('create', '" + f_location + "')";

var flixnwc_view = document.getElementById('flixnwc_view');
flixnwc_view.href = "javascript:flixnwc.showIframe('view', '" + f_location + "')";
