function swapDisplay(id) {
  if ($(id))
    $(id).style.display = ($(id).style.display == 'block')?'none':'block';
}

function $oV(obj) {
    //todo: add radio, check
	return $(obj)?$(obj).get('value'):'';
}

function queryString(action, params) {
  var param = new Hash({'action': action, 'javascript': true});
  $splat(params).each(function(item) {
    switch($type(item)) {
      case 'string':
        param[item] = $oV(item)
	  break
	  case 'object':
        param.combine(item);
      break
   }  
  });

  return param.toQueryString();
}

function updateBox(update, url, action, params) {
  return new coverUpdater(url, queryString(action, params), update).updateContent();
}

function updateAttach(update, url, action, params, spinner) {
  return new attachUpdater(url, queryString(action, params), update, spinner).updateContent();
}

function updateSpread(update, url, action, params) {
  return new spreadUpdater(url, queryString(action, params), update).updateContent();
}

function Cart(artid, id) {
  return updateAttach('cart', URL_CGIBIN+'my_Order.php', 'additem', [{id:artid}], id)
}

function Vote(id, vote, pageid) {
  updateAttach('vote', URL_CGIBIN+'Vote.php', 'addvote', [{'vote':vote, 'pageid':pageid}], id)
}

function Comment(mode) {
	updateAttach('comment-container', URL_CGIBIN+'Comment_add.php', 'submit', ['pageid', 'identifier', 'message'], 'comment-response') 
    return false;
}

var coverUpdater = new Class({
  initialize: function(url, param, update){
    this.url = url;
    this.param = param;
    this.id = update
  },
  createCover: function() {
    // area to be covered
    var dim = $(this.id).getSize();
    // half of height
    var half = dim.y/2
    // cover box
    var cover =	new Element('div', {
      'styles': {
        'text-align':'center',
        'background-color':'#eee',
        'border':'1px solid #e0e0e0',
        '-moz-border-radius':'5px',
        'position':'absolute',
        'z-index': 12,
        'width':dim.x,
        'height':dim.y,
        'opacity':0.6
      }
    });
    // message box
    var msg = new Element('div', {
      'styles': {
        'width':'200px',
        'margin':'auto',
        'margin-top':half
       }
    }).inject(cover);
    // message spinner
    new Element('img', {
      'src': '/my_teia/pics/symbols/spinner.gif'
    }).inject(msg)
    // message text
    new Element('span', {
      'styles': {
        'font-size': '9px'
      },
      'html': ' Loading...'
    }).inject(msg);

    return cover;
  },
  updateContent: function() {
    // add a cover
    this.createCover().inject($(this.id), 'top')
    // update content
    new Request.HTML({
      url:this.url,
      data:this.param,
      //TODO: update not working
      update:$(this.update),
      onSuccess:function(nlist, ele, html) { $(this.id).set('html', html); }.bind(this)
    }).send();
  }
});

var attachUpdater = new Class({
  initialize: function(url, param, update, spinner){
    this.url = url;
    this.param = param;
    this.update = update
    this.spinner = (spinner)?spinner:update
    this.spinnerID = 'spinner'+this.spinner
  },
  updateContent: function() {
    this.attachSpinner()
    new Request.HTML({	
     url:this.url,
     data:this.param,
     update:$(this.update),
	 onSuccess:function() { this.dropSpinner(true)}.bind(this),
     onFailure:function() { this.dropSpinner(false)}.bind(this)
    }).send();

	return true
  },
  attachSpinner: function() {
    if (!$(this.spinner))
      return false;
    if ($(this.spinnerID)) { 
      $(this.spinnerID).set('src', URL_IMAGE_SYMBOLS+'/spinner.gif');
    }
    else {
      new Element('img', {
		'src': URL_IMAGE_SYMBOLS+'/spinner.gif',
        'id' : this.spinnerID
      }).inject(this.spinner)
    }
  },
  dropSpinner: function(stat) {
    if ($(this.spinnerID))
      $(this.spinnerID).set('src', (stat)?URL_IMAGE_SYMBOLS+'/accept.gif':URL_IMAGE_SYMBOLS+'/prohibited.gif');
  }
});

var spreadUpdater = new Class({
  initialize: function(url, param, update){
    this.url = url;
    this.param = param;
    this.update = update
  },
  updateContent: function() {
    this.spreadSpinner()
    new Request.HTML({	
     url:this.url,
     data:this.param,
     update:this.update,
     onFailure:function() { this.spreadSpinner(true)}.bind(this)
    }).send();
  },
  spreadSpinner: function(bool) {
    if (!$(this.update))
      return false;
    var pic =(bool)?'prohibited.gif':'spinner.gif';
    $(this.update).set('html', '<img src="'+URL_IMAGE_SYMBOLS+'/'+pic+'" alt=""/>');
  }
});
