/*$Id: itemcollection.js,v 1.5 2004/05/19 13:20:54 fifa2k Exp $*/
var ua		= navigator.userAgent;
_ie_		=  /msie [56789]/i.test(ua);
_moz_		=  /mozilla\/[56789]/i.test(ua);

//CSS float property has different name in IE and Moz 
function _setFloat (o,sFloat){
  if (_ie_){
    o.style["styleFloat"] = sFloat;
  }else if (_moz_){
    o.style["cssFloat"] = sFloat;
  }
}

function _numberInArray(v,a){
  if (typeof a == "object" && typeof a[0] == "number"){
    eval("var t = /," + v + ",/g");
    var s = "," + a.join(",") + ",";
    return t.test(s)
  }
  return false;
}

function _removeNumberFromArray(v,a){
  if (typeof a == "object" && typeof a[0] == "number"){
    eval("var t = /," + v + ",/g");
    var s = "," + a.join(",") + ",";
    s = s.replace(t,",");
    s = s.substr(1,s.length - 2);
    var retval = [];
    if (0 != s.length){
      retval = s.split(",");
      for(var i = 0;i < retval.length;i ++){
        retval[i] = Number(retval[i]);
      }
    }
    return retval;
  }
  return a;
}

function _replaceNumberFromArray(v1,v2,a){
  if (typeof a == "object" && typeof a[0] == "number"){
    eval("var t = /," + v1 + ",/g");
    var s = "," + a.join(",") + ",";
    s = s.replace(t,"," + v2 + ",");
    s = s.substr(1,s.length - 2);
    var retval = s.split(",");
    for (var i = 0;i < retval.length;i ++){
      retval[i] = Number(retval[i]);
    }
    return retval;
  }
  return a;
}

function _compareNumbers(a, b) {
   return a - b
}

function xyItemCollection(){
  this.length = 0;
  this._item = [];
  this.selectedIndex = [];
}

xyItemCollection.prototype.item = function(idx){
  if (idx < this.length && idx >= 0){
    return this._item[idx];
  }
}

xyItemCollection.prototype.add = function(o){
  var i = this.length;
  this._item[i] = o;
  if ("function" == typeof this._item[i].onindexchange){
    this._item[i].onindexchange(i);
  }
  this.length ++;
}

xyItemCollection.prototype.sortBy = function(){}

xyItemCollection.prototype.swap = function(idx,idx2){
  var o = this._item[idx];
  this._item[idx] = this._item[idx2];
  if ("function" == typeof this._item[idx2].onindexchange){
    this._item[idx2].onindexchange(idx);
  }
  this._item[idx2] = o;
  if ("function" == typeof this._item[idx2].onindexchange){
    this._item[idx2].onindexchange(idx);
  }

  var fIdx = this.isSelected(idx);
  var fIdx2 = this.isSelected(idx2); 
  if (!(fIdx && fIdx2)){
    if(fIdx){
      this.selectedIndex = _replaceNumberFromArray(idx,idx2,this.selectedIndex);
    }else if(fIdx2){
      this.selectedIndex = _replaceNumberFromArray(idx2,idx,this.selectedIndex);
    }
  }
}

xyItemCollection.prototype.insertBefore = function(idx,refIdx){
  if (idx < refIdx){
    refIdx --;
    for (var i = idx; i < refIdx  ;i ++){
      this.swap(i,i + 1);
    }
  }else if (idx > refIdx){
    for(var i = idx;i > refIdx;i --){
      this.swap(i,i - 1);
   }
  }
}
//只有在删除项的时候调用
xyItemCollection.prototype._shift = function(idx){
  this._item[idx] = this._item[idx + 1];
  if ("function" == typeof this._item[idx].onindexchange){
    this._item[idx + 1].onindexchange(idx);
  }
  if(this.isSelected(idx + 1)){
      this.selectedIndex = _replaceNumberFromArray(idx + 1 , idx,this.selectedIndex);
  }
}

xyItemCollection.prototype.clear = function(){
  var l = this._item.length;

  for (var i = 0;i < l; i ++){
    var o = this._item[i];
    this.unselect(i);

    if("function" == typeof o.clear || "object" == typeof o.clear){
      if(0 == i){
        o.clear();
      }
    }else if("function" == typeof o.destroy || "object" == typeof o.destroy){
      o.destroy();
    }

    o = null;
    this._item[i] = null;
  }
  delete this.selectedIndex;delete this._item;
  this.length = 0;
  this._item = [];
  this.selectedIndex = [];
}


xyItemCollection.prototype.remove = function(idx){
  if (idx < this.length && idx >= 0){
    var o = this._item[idx];
    this.selectedIndex = _removeNumberFromArray(idx,this.selectedIndex);
    for(var i = idx;i < this.length - 1;i ++){
      this._shift(i);
    }
    this.length --;
  }
  if("function" == typeof o.destroy || "object" == typeof o.destroy){
    o.destroy();
  }
  return o;
}

xyItemCollection.prototype.isSelected = function(idx){
  return _numberInArray(idx,this.selectedIndex);
}


xyItemCollection.prototype.toggle = function(idx){
  if (true == this.isSelected(idx)){
    this.unselect(idx);
  }else{
    this.select(idx,true);
  }
}

xyItemCollection.prototype.selectRange = function(idx,idx2){
  var f = (idx > idx2) ? idx2 : idx;
  var t = (idx > idx2) ? idx : idx2;
  if (f < 0) f = 0;
  if (t >= this.length) t = this.length - 1;
  this.select(-1);
  for (var i = f;i <= t; i++){
    this.select(i,true);
  }
}

xyItemCollection.prototype.select = function(idx,bMultiple){
  bMultiple = typeof bMultiple == "undefined" ? false : bMultiple;
  if (!bMultiple){
    var t = this.selectedIndex;
    for(var i = 0;i < t.length;i ++){
      this.unselect(t[i]);
    }
  }
  if (idx >= 0 && idx < this.length){
    if (false == this.isSelected(idx)){
      this.selectedIndex[this.selectedIndex.length] = idx;
      if(typeof this._item[idx].onselect == "function"){
        this._item[idx].onselect();
      }
    }
  }
}


xyItemCollection.prototype.unselect = function(idx){
  if (true == this.isSelected(idx)){
    this.selectedIndex = _removeNumberFromArray(idx,this.selectedIndex);
    if(typeof this._item[idx].onblur == "function"){
      this._item[idx].onblur();
    }
  }
}


