/*
 * Bugfix for Prototype's show & hide functions.
 * i had submitted it on http://prototype.lighthouseapp.com/projects/8886/tickets/177-element-show-hide
 * */
Element.addMethods({
	hide: function(element) {
		var el=$(element);
		if(el.style.display=='none')return element;
		el._display=(el._display!=null?el._display:el.style.display);
		el.style.display = 'none';

		return element;
	},
	show:function(element) {
		var el=$(element);
		if(el.style.display!='none')return element;
		el.style.display = (el._display?el._display:'');
		el._display=null;

		return element;
	}
});

// taken from http://prototype.lighthouseapp.com/projects/8886/tickets/351-new-swap-method-for-elements
Element.addMethods({
  swap: (function() {
    if ('swapNode' in document.documentElement)
      return function(element, other) {
        return $(element).swapNode($(other));
      };
    return function(element, other) {
       element = $(element);
       other = $(other);
       var next = other.nextSibling, parent = other.parentNode;
       element.parentNode.replaceChild(other, element);
       return parent.insertBefore(element, next);
    };
  })()
 });


// extend the array object to support indexed insertions
// submitted at http://prototype.lighthouseapp.com/projects/8886-prototype/tickets/356-arrayinsert
Array.prototype.insert=function(element,where) {
	var slice1=this.slice(0,where);
	var slice2=this.slice(where);

	return new Array.concat(slice1,element,slice2);
};


//extend the array object to support searching thrtough indexed arrays
// if returnIndex is true, then return the keyName, else return the value from that cell
Array.prototype.nextValue=function(startIndex,returnIndex) {
	for(var i=startIndex+1;i<this.length;i++){
		if(this[i]){
			return (returnIndex?i:this[i]);
		}
	}
	return null;
};


//extend the array object to support searching thrtough indexed arrays
// if returnIndex is true, then return the keyName, else return the value from that cell
Array.prototype.prevValue=function(startIndex,returnIndex) {
	for(var i=startIndex-1;i>=0;i--){
		if(this[i]){
			return (returnIndex?i:this[i]);
		}
	}
	return null;
};

/*
 * Misc string functions
 * */
function nl2br(str){
	 return String(str).replace(/(\n)/g, '<br/>\n');
}

function rmbr(str){
	 return String(str).replace(/(\n)/g, '\n');
}
