/*
	Name: FreeFall-js-extensions.js
	Desc: Extensions on the standard javascript prototypes.
	Author: Krystan Gontscharow (Gontro)
	Dependencies: mootools-release-1.11.js
*/

// Array\Collection extensions.
Object.prototype.swap = function(index, newIndex) { 
  if (($type(this) != 'collection' || $type(this) != 'array') && ($type(index) != 'number' || $type(newIndex) != 'number' || index < 0 || index >= this.length || !this[index] || !this[newIndex])) return false; var newSet = new Array(); for (var count = 0; count < this.length; count++) {
    if (count == newIndex) newSet[count] = this[index]; else if (count == index) newSet[count] = this[newIndex]; else newSet[count] = this[count];
  } return newSet;
};

// Element extensions.
Object.prototype.removeChildren = function() { 
  if ($type(this) != 'element') return false; this.getChildren().each(function(element) {
    element.remove();
  }); return true;
};

// String extensions.
Object.prototype.copyToClipboard = function() { 
  if ($type(this) != 'element') return false; if (this.createTextRange || !this.value) {
    var range = this.createTextRange(); if (range) range.execCommand('Copy');
  } else {
    var flashcopier = 'flashcopier'; if(!document.getElementById(flashcopier)) {
      var divholder = document.createElement('div'); divholder.id = flashcopier; document.body.appendChild(divholder);
    } document.getElementById(flashcopier).innerHTML = ''; var divinfo = '<embed src="js/_clipboard.swf" FlashVars="clipboard='+encodeURIComponent(this.value)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>'; document.getElementById(flashcopier).innerHTML = divinfo;
  } return true;
};

// Select extensions.
Object.prototype.getSelectedValues = function(concat) { 
  if ($type(this) != 'element' || this.getTag() != 'select') return false; if (!concat) concat = ';'; var returnStr = false; for (var count = 0; count < this.options.length; count++) if (this.options[count].selected) if (!returnStr) returnStr = this.options[count].value; else returnStr += concat+this.options[count].value; return returnStr;
};
Object.prototype.getSelectedValue = function() { 
  if ($type(this) != 'element' || this.getTag() != 'select') return false; return this.options[this.selectedIndex].value;
};
Object.prototype.getSelectedText = function() { 
  if ($type(this) != 'element' || this.getTag() != 'select') return false; return this.options[this.selectedIndex].text;
};
Object.prototype.containsValue = function(value) { 
  if ($type(this) != 'element' || this.getTag() != 'select' || $type(value) != 'string') return false; for (var count = 0; count < this.options.length; count++) if (value == this.options[count].value) return true; return false;
};
Object.prototype.containsText = function(text) { 
  if ($type(this) != 'element' || this.getTag() != 'select' || $type(text) != 'string') return false; for (var count = 0; count < this.options.length; count++) if (text == this.options[count].text) return true; return false;
};
Object.prototype.appendOptions = function(options) { 
  if (($type(this) != 'element' || this.getTag() != 'select') && ($type(options) != 'collection' || $type(options) != 'array')) return false; for (var count = 0; count < options.length; count++) this.adopt(options[count].clone()); return true;
};
Object.prototype.createOptionLast = function(label, value) {
  if ($type(this) != 'element' || this.getTag() != 'select' || $type(label) != 'string') return false; var value = value ? value : label; var newOption = new Element('option', {
    'label': label,
    'value': value
  }); newOption.setText(label); this.adopt(newOption); this.selectedIndex = this.options.length-1;
};
Object.prototype.moveOptionUp = function() { 
  if ($type(this) != 'element' || this.getTag() != 'select' || this.selectedIndex <= 0) return false; var newIndex = this.selectedIndex-1; var oldSet = this.options.swap(this.selectedIndex, newIndex); this.removeChildren(); this.appendOptions(oldSet); this.selectedIndex = newIndex;
};
Object.prototype.moveOptionDown = function() { 
  if ($type(this) != 'element' || this.getTag() != 'select' || this.selectedIndex == -1 || this.selectedIndex == this.options.length-1) return false; var newIndex = this.selectedIndex+1; var oldSet = this.options.swap(this.selectedIndex, newIndex); this.removeChildren(); this.appendOptions(oldSet); this.selectedIndex = newIndex;
};
Object.prototype.removeSelected = function() { 
  if ($type(this) != 'element' || this.getTag() != 'select' || this.selectedIndex < 0) return false; for (var count = this.length - 1; count >= 0; count--) if (this.options[count].selected) this.options[count].remove();
};

function setSelectedIndex(eSelect, value) {
  for (var count = eSelect.length - 1; count >= 0; count--) {
    if (eSelect.options[count].value == value) {
      eSelect.selectedIndex = count;
      break;
    }
  }
}


