(function(){
  var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
  this.FXClass = function(){};
  FXClass.extend = function(prop) {
    var _super = this.prototype;
    initializing = true;
    var prototype = new this();
    initializing = false;
    for (var name in prop) {
      prototype[name] = typeof prop[name] == "function" &&
        typeof _super[name] == "function" && fnTest.test(prop[name]) ?
        (function(name, fn){
          return function() {
            var tmp = this._super;
            this._super = _super[name];
            var ret = fn.apply(this, arguments);       
            this._super = tmp;
            return ret;
          };
        })(name, prop[name]) :
        prop[name];
    }
    function FXClass() {
      if ( !initializing && this.init )
        this.init.apply(this, arguments);
    }
    FXClass.prototype = prototype;
    FXClass.constructor = FXClass;
    FXClass.extend = arguments.callee;
    return FXClass;
  };
})();

var Flixn = FXClass.extend({

  init: function() {},

  recordComplete: function(videoid) {
    this.recordEnable = false;
    this.showIframe('create', document.location, '&misc=' + videoid);
  },
  
  _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 FlixnIframe = Flixn.extend({

  inputId: null,
  openContainer: null,

  iframeId: 'flixnwc_iframe',
  containerId: 'flixnwc_container',

  containerWidth: 450,
  containerHeight: 300,

  f_location: null,
  prefixUrl: 'http://cisco.flixn.com/widgets/comment/22/',
  
  createHeader: 'http://cisco.flixn.com/widgets/comment/22/img/cisco/create_header.gif',
  viewHeader: 'http://cisco.flixn.com/widgets/comment/22/img/cisco/view_header.gif',
  closeImage: 'http://cisco.flixn.com/widgets/comment/22/img/cisco/close.gif',

  hideObjects: true,
  hideSelects: true,

  _deltaX: null,
  _deltaY: null,
  _toDrag: null,

  init: function() {},

  drag: function(event) {
    var startX = event.clientX, startY = event.clientY;
    var origX = FlixnIframeInstance._toDrag.offsetLeft, origY = FlixnIframeInstance._toDrag.offsetTop;
    FlixnIframeInstance._deltaX = startX - origX;
    FlixnIframeInstance._deltaY = startY - origY;

    if (document.addEventListener) {
      document.addEventListener('mousemove', FlixnIframeInstance.moveHandler, true);
      document.addEventListener('mouseup', FlixnIframeInstance.upHandler, true);
    }
    else if (document.attachEvent) {
      FlixnIframeInstance._toDrag.setCapture();
      FlixnIframeInstance._toDrag.attachEvent('onmousemove', FlixnIframeInstance.moveHandler);
      FlixnIframeInstance._toDrag.attachEvent('onmouseup', FlixnIframeInstance.upHandler);
      FlixnIframeInstance._toDrag.attachEvent('onlosecapture', FlixnIframeInstance.upHandler);
    }
    else {
    }
  },

  moveHandler: function(e) {
    if (!e)
      e = window.event;

    FlixnIframeInstance._toDrag.style.left = (e.clientX - FlixnIframeInstance._deltaX) + 'px';
    FlixnIframeInstance._toDrag.style.top = (e.clientY - FlixnIframeInstance._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', FlixnIframeInstance.upHandler, true);
      document.removeEventListener('mousemove', FlixnIframeInstance.moveHandler, true);
    }
    else if (document.detachEvent) {
      FlixnIframeInstance._toDrag.detachEvent('onlosecapture', FlixnIframeInstance.upHandler);
      FlixnIframeInstance._toDrag.detachEvent('onmouseup', FlixnIframeInstance.upHandler);
      FlixnIframeInstance._toDrag.detachEvent('onmousemove', FlixnIframeInstance.moveHandler);
      FlixnIframeInstance._toDrag.releaseCapture();
    }

    if (e.stopPropagation)
      e.stopPropagation();
    else
      e.cancelBubble = true;
  },
  
  showIframe: function(page, ident, group, uri) {

    if (FlixnIframeInstance.openContainer != null)
      FlixnIframeInstance.closeIframe(FlixnIframeInstance.openContainer);

    if (this.hideObjects) {
      this._setVisibilityStylesByTagName('object', 'hidden');
      this._setVisibilityStylesByTagName('embed', 'hidden');
    }

    if (this.hideSelects) {
      this._setVisibilityStylesByTagName('select', 'hidden');
    }

    this._buildIframe(page, ident, group, uri);
    this._placeIframe();

    this._showIframe();
    this.openContainer = page;
  },

  closeIframe: function() {
    if (this.hideObjects) {
      this._setVisibilityStylesByTagName('object', 'visible');
      this._setVisibilityStylesByTagName('embed', 'visible');
    }

    if (this.hideSelects) {
      this._setVisibilityStylesByTagName('select', 'visible');
    }

    this._hideIframe();

    this.openContainer = null;
  },
  
  _buildIframe: function(page, ident, group, uri) {
    var container = document.createElement('div');
    container.setAttribute('id', this.containerId);
    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', FlixnIframeInstance.drag);
    } else {
      header.setAttribute('onmousedown', 'FlixnIframeInstance.drag(event)');
    }
    this._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;
    header.appendChild(header_img);

    var close_link = document.createElement('a');
    close_link.href = "javascript:FlixnIframeInstance.closeIframe();";
    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);

    var iframe = document.createElement('iframe');
    iframe.setAttribute('id', this.iframeId);
    iframe.setAttribute('class', this.iframeId);
    iframe.setAttribute('width', '100%');
    iframe.setAttribute('frameBorder', '0');
    iframe.setAttribute('scrolling', 'no');

// XXX
    var iframe_src = this.prefixUrl + page + '.php?id=' + ident + '&uri=' + uri + '&group=' + encodeURIComponent(group);

    iframe.src = iframe_src
    container.appendChild(iframe);

    document.body.appendChild(container);
  },

  _hideIframe: function() {
    document.body.removeChild(document.getElementById(this.containerId));
  },

  _placeIframe: function() {
    var windowSize = this._getWindowSize();
    this._scrollOffset = this._getScrollOffset();

    x = this._scrollOffset[0] + (windowSize[0] - this.containerWidth)/2;
    y = this._scrollOffset[1] + (windowSize[1] - this.containerHeight)/2;

    var iframe = document.getElementById(this.containerId);
    iframe.style.left = x + 'px';
    iframe.style.top = y + 'px';
  },

  _showIframe: function() {
    var iframe = document.getElementById(this.containerId);
    iframe.style.display = 'block';
  }
  
});
var FlixnIframeInstance = new FlixnIframe();


var FlixnWidgetComment = Flixn.extend({

  ident: null,
  group: null,

  init: function() {},

  setIdent: function(identifier) {
    this.ident = identifier;
  },
  
  setGroup: function(groupname) {
    this.group = groupname;
  },

  showCreate: function() {
    this._showPage('create');
  },
  
  showView: function() {
    this._showPage('view');
  },

  _showPage: function(name) {
    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;

    FlixnIframeInstance.showIframe(name, this.ident, this.group, f_location);
  }
});