
var conf_popups = {};
function conf_open(SID,conf) {
  conf_popups[conf] = window.open('http://3'+conf+'.vc.eurescom.eu?SID='+SID+'&conf='+conf, 'conf'+conf, "width=430,height=630,toolbar=no,status=no,menubar=no,location=no");
}

var collab_popups = {};
function collab_open(SID,conf) {
  collab_popups[conf] = window.open('?SID='+SID+'&collab='+conf, 'collab'+conf, "width=1200,height=800,toolbar=no,status=no,menubar=no,location=no");
}

function set_schedule_type(type) {
  var daterow = $('date');
  var schedrow = $('schedule');

  if (daterow && schedrow) {
    if (type == 'single') {
      daterow.style.position = 'relative';
      daterow.style.visibility = 'visible';
      schedrow.style.visibility = 'hidden';
      schedrow.style.position = 'absolute';
    }
    else {
      daterow.style.visibility = 'hidden';
      daterow.style.position = 'absolute';
      schedrow.style.position = 'relative';
      schedrow.style.visibility = 'visible';
    }
  }
}


function set_invitation_type(type) {
  var inviterow = $('invite');

  if (inviterow) {
    if (type == 'custom') {
      inviterow.style.position = 'relative';
      inviterow.style.visibility = 'visible';
    }
    else {
      inviterow.style.visibility = 'hidden';
      inviterow.style.position = 'absolute';
    }
  }
}


var meetme = { infoBoxMap:{},
               userMap:{} };

meetme.infoBox = Class.create();
meetme.infoBox.prototype = {

  initialize: function(options) {
    this.options = options;
    this.serial = 1;
    this.users = {};
    this.userNum = 0;
    this.userMap = {};
    this.userElements = {};
    this.confName = false;

    this.userBox =
      domTd({},
            {},
            {}
      );

    this.userNumBox = domSpan({},{},{},'0');

    this.confNameBox = domSpan({'fontSize':'14px',
                               'fontWeight':'bold'},
                              {},
                              {},
                              "Conf "+'3'+this.options.conf);

    this.htmlElement = domDiv({},{},{},
      domTableCol([
        domTd({'padding':'4px',
               'borderBottom':'1px solid white',
               'color':'white',
               'backgroundImage':'url(img/titlebackground.png)'},
              {'valign':'middle'},
              {},
              domTableRow([
                domTd({},
                      {},
                      {},
                      this.confNameBox
                     ),
                domTd({},
                      {'align':'right'},
                      {},
                      domTableRow([
                        domTd({'width':'16px',
                               'height':'16px',
                               'paddingRight':'4px'},
                              {},
                              {},
                              domImg({},{'src':'img/users.png'},{})
                             ),
                        domTd({'width':'20px'},
                              {},
                              {},
                              this.userNumBox
                             )
                      ])
                     )

              ],{'width':'100%'})
             ),
        this.userBox
      ],{'width':'100%'})
    );
    this.htmlElement.className = 'infoBox';

    meetme.infoBoxMap[this.options.conf] = this;
    return this;
  },

  render: function() {
    var This = this;
    // Add new users and update talking state(s)
    $H(this.users).each(function(user,idx){
      if (!This.userElements[user[0]]) {
        // New user
        This.userElements[user[0]] =
          new meetme.user({ 'userID':user[0],
                            'photo':(This.userMap[user[0]].photo || 'default-photo.png'),
                            'name':(This.userMap[user[0]]['name'] || This.userMap[user[0]].CallerIDName || '[unknown]'),
                            'company':(This.userMap[user[0]].company || ''),
                            'callerid':(This.userMap[user[0]].CallerID || '[unknown]') });
        This.userBox.appendChild(This.userElements[user[0]].htmlElement);
        This.userNum++;
        This.userNumBox.innerHTML = This.userNum;
      }
      if (user[1] == 2) {
        // User is talking
        This.userElements[user[0]].setTalking(true);
      }
      else {
        // User is silent
        This.userElements[user[0]].setTalking(false);
      }
    });
    // Remove users
    $H(this.userElements).each(function(userElement,idx){
      if (!This.users[userElement[0]]) {
        // User has left
        This.userBox.removeChild(This.userElements[userElement[0]].htmlElement);
        delete This.userElements[userElement[0]];
        This.userNum--;
        This.userNumBox.innerHTML = This.userNum;
      }
    });
  },

  update: function(newstate) {
    if (newstate) {
      this.userMap = newstate.user;
      if (!this.confName && this.userMap[this.options.conf]) {
        // Update conference name
        this.confName = this.userMap[this.options.conf];
        this.confNameBox.innerHTML = "Conf "+'3'+this.options.conf+' ('+this.userMap[this.options.conf]+')';
      }
      if (newstate.stat) {
        this.serial = (newstate.stat.serial || 0);
        this.users = (newstate.stat.users || {});
      }

      this.render();
    }

    ajax_request(this.options.conf,this.serial);
  }
}



meetme.user = Class.create();
meetme.user.prototype = {

  initialize: function(options) {
    this.options = options;

    // Dynamic members
    this['callerid'] = this.options['callerid'];
    this['name'] = this.options['name'];
    this['company'] = this.options['company'];
    this['photo'] = this.options['photo'];
    this.talking = false;

    this.talkingIcon =
      domImg({},
             {'src':'img/blank32x32.gif',
              'width':32,
              'height':32},
             {});

    this.htmlElement = domDiv({},{},{},
    domTableRow([
      domTd({'paddingRight':'8px'},
            {'valign':'middle'},
            {},
            this.talkingIcon
           ),
      domTd({'paddingRight':'8px'},
            {},
            {},
            domDiv({'border':'1px solid white'},{},{},
              domDiv({'border':'1px solid black'},{},{},
                domImg({},
                       {'src':'photos/'+this.photo,
                        'width':'40',
                        'height':'40'},
                       {}
                )
              )
            )
           ),
      domTd({'paddingRight':'8px'},
            {},
            {},
            domTableCol([
                          domTd({'paddingBottom':'4px'},
                                {},
                                {},
                                domTableRow([
                                             domTd({'paddingRight':'8px'},
                                                   {},
                                                   {},
                                                   domSpan({'fontWeight':'bold'},
                                                           {},
                                                           {},
                                                           this['name']+
                                                           (this['company']?
                                                            ' ('+this['company']+')'
                                                            :
                                                            ''
                                                           )
                                                          )
                                                  )
                                           ])
                               ),
                          domTd({},
                                {},
                                {},
                                domTableRow([
                                             domTd({'paddingRight':'8px'},
                                                   {},
                                                   {},
                                                   domImg({},
                                                          {'src':'img/phone.gif',
                                                           'width':16,
                                                           'height':16},
                                                          {})
                                                  ),
                                             domTd({'paddingRight':'8px'},
                                                   {},
                                                   {},
                                                   domSpan({},
                                                           {},
                                                           {},
                                                           this['callerid'])
                                                  )
                                           ])
                               )
                       ])
           )
        ])
     );

    this.htmlElement.className = 'user';

    meetme.userMap[this.options.userID] = this;
    return this;
  },

  setTalking: function(talkingState) {
    if (talkingState) {
      this.talking = true;
      this.talkingIcon.src = 'img/talking.gif';
    }
    else {
      this.talking = false;
      this.talkingIcon.src = 'img/blank32x32.gif';
    }
  }
}





function ajax_request(conf,serial) {
  new Ajax.Request(
      'meetme-stats-deliver.plx',
      {
        'method':'get',
        'parameters':'conf='+conf+
                     '&serial='+serial+
                     '&nocache='+Math.random(),
        'onSuccess':ajax_success,
        'onFailure':ajax_failure
      }
    );
}

var parseText = '{}';
function ajax_success(req,json) {
  parseText = req.responseText;
  window.setTimeout("ajax_parse()",10);
}

function ajax_failure(req,json) {
  alert('AJAX failure');
}

function ajax_parse() {
  var response = parseText;
  var newstate = false;
  try {
    newstate = eval('(' + response + ')');
  }
  catch (e) {
    // document.write(response);
  }
  finally {
  }
  if (newstate) {
    meetme.infoBoxMap[newstate.conf].update(newstate);
  }
}





function domApplyStyle(element,style) {
  var styleAttrs = $H(style);
  styleAttrs.each(function(styleAttr,idx){
    if (styleAttr[0].length < 2) return;
    if (styleAttr[0].indexOf('_') == 0) return;
    element.style[styleAttr[0]] = styleAttr[1];
  });
}

function domApplyAttributes(element,attributes) {
  var attrAttrs = $H(attributes);
  attrAttrs.each(function(attrAttr,idx){
    if (attrAttr[0].length < 2) return;
    if (attrAttr[0].indexOf('_') == 0) return;
    switch(attrAttr[0]) {
      case 'id':
        element.id = attrAttr[1];
        break;
      default:
        element.setAttribute(attrAttr[0],attrAttr[1]);
    }
  });
}

function domApplyTriggers(element,triggers) {
  if (triggers.onclick) {
   element.onclick = triggers.onclick;
  }
  // FIXME: add more triggers here
}

function domImg(style,attrs,triggers) {
  var img = document.createElement('img');
  if (attrs) domApplyAttributes(img,attrs);
  if (style) domApplyStyle(img,style);
  if (triggers) domApplyTriggers(img,triggers);
  return img;
}

function domSpan(style,attrs,triggers,content) {
  var span = document.createElement('span');
  if (attrs) domApplyAttributes(span,attrs);
  if (style) domApplyStyle(span,style);
  if (triggers) domApplyTriggers(span,triggers);
  if (content) span.innerHTML = content;
  return span;
}

function domDiv(style,attrs,triggers,contentElement) {
  var div = document.createElement('div');
  if (attrs) domApplyAttributes(div,attrs);
  if (style) domApplyStyle(div,style);
  if (triggers) domApplyTriggers(div,triggers);
  if (contentElement) div.appendChild(contentElement);
  return div;
}

function domTd(style,attrs,triggers,contentElement) {
  var td = document.createElement('td');
  if (attrs) domApplyAttributes(td,attrs);
  if (style) domApplyStyle(td,style);
  if (triggers) domApplyTriggers(td,triggers);
  if (contentElement) td.appendChild(contentElement);
  return td;
}

function domTableCol(tds,style) {

  var table = document.createElement('table');
  table.setAttribute('cellpadding','0',0);
  table.setAttribute('cellspacing','0',0);
  table.setAttribute('border','0',0);
  if (style) domApplyStyle(table,style);

  var tbody = document.createElement('tbody');
  table.appendChild(tbody);

  tds.each(function(td,idx){
    if (!td) return;
    var row = document.createElement('tr');
    row.appendChild(td);
    tbody.appendChild(row);
  });

  return table;
}


function domTableRow(tds,style) {

  var table = document.createElement('table');
  table.setAttribute('cellpadding','0',0);
  table.setAttribute('cellspacing','0',0);
  table.setAttribute('border','0',0);
  if (style) domApplyStyle(table,style);

  var tbody = document.createElement('tbody');
  table.appendChild(tbody);
  var row = document.createElement('tr');
  tbody.appendChild(row);

  tds.each(function(td,idx){
    if (!td) return;
    row.appendChild(td);
  });

  return table;
}

