/* ********************************************** 
** Copyright ® 2006-2007 OmniSpear, Inc. http://www.omnispear.com/
** Created By: Jared Armstrong - OmniSpear, Inc.
** License Terms: ~(LicenseTerms)~
** ******************************************* */
var OmniSort = {
  Version: '1.0.0',
  prototypeVersion: parseFloat(Prototype.Version.split(".")[0] + "." + Prototype.Version.split(".")[1])
}
/* ** ** */
OmniSort.Sortable = Class.create();
OmniSort.Sortable.prototype = {
		initialize: function(container,options){
			this.container            = $(container);//.childNodes[1];
            
            if(this.container==null) return false;

			if(typeof(this.container.tagName)!="undefined" && this.container.tagName!="TBODY" && this.container.tagName !="DIV"){
				for(var i=0;i<$(container).childNodes.length;i++){
					if($(container).childNodes[i].tagName=="TBODY"){
						this.container=$(container).childNodes[i];
						i=$(container).childNodes.length;
					}
				}
			}else if(typeof(this.container.tagName)!="undefined" && this.container.tagName =="DIV"){
					// we love forms
					//alert(typeof($(container).childNodes[0].innerHTML));
					if($(container).childNodes[0].tagName=="FORM" && $(container).childNodes.length>1){
						this.container=$(container).childNodes[1];
					}
			}
			
			
			
			//alert(this.container.innerHTML);
			this.allNodes=null;
			this.setOptions(options);
			if(!container) return;	
			this._attachBehaviors();
			
			
		}
		,
		setOptions: function(o){
			this.options= {
			};	
			Object.extend(this.options, o || {});
		},
		moveUp: function(s){
			thisP=this._findItem(s);
			if(thisP!=null && thisP >= 1){
				var row=this.container.childNodes[thisP];
				var toRow=this.container.childNodes[thisP-1];
				this.container.insertBefore(row,toRow);
			}
		},
		moveDown: function(s){
			thisP=this._findItem(s);
			//alert(thisP);
			if(thisP!=null && thisP<this.container.childNodes.length-1){
				var row=this.container.childNodes[thisP];
				var toRow=this.container.childNodes[thisP+1];
				//alert('rowId='+row.id + ' toRow='+toRow.id);
				this.container.insertBefore(toRow,row);
			}
		},
		moveTop: function(s){
			thisP=this._findItem(s);
			if(thisP!=null && thisP != 0){
				var row=this.container.childNodes[thisP];
				var toRow=this.container.childNodes[0];
				if(toRow.className=="noSort"){
					toRow=this.container.childNodes[1];
				}
				this.container.insertBefore(row,toRow);
			}
		},
		moveBottom: function(s){
			thisP=this._findItem(s);
			if(thisP!=null && thisP != this.container.childNodes.length-1){
				var row=this.container.childNodes[thisP];
				this.container.appendChild(row);
			}
		},
		remove: function(s){
			thisP=this._findItem(s);
			if(thisP!=null){
				var row=this.container.childNodes[thisP];
				this.container.removeChild(row);
				this._attachBehaviors();
			}
		},
		addRow: function(o){
			this.container.appendChild(o);
			this._attachBehaviors();
		},
		addPreRow: function(o){
		    if(this.allNodes==null || this.allNodes.length==0){
		      this.addRow(o);
		      return false;
		    }
		
			this.container.insertBefore(o,this.allNodes[0]);
			this._attachBehaviors();
		},
		serialize: function(){
			this._attachBehaviors();
			var sz="";
			for(var i=0;i<this.allNodes.length;i++){
				// modified by nick, cause he is cool like that.
				if(sz.length>0){sz+="&"}
				//if(sz.length>0){sz+="/"}
				sz+=this.allNodes[i].id+"="+(i+1);
				//sz+=this.allNodes[i].id+"/"+(i+1);
			}
			return sz;
		},
		reIndex: function(){
			this._attachBehaviors();
		},
		_findItem: function(s){
				for(var i=0;i<this.allNodes.length;i++){
						if(this.allNodes[i].id==s){
							//alert(this.container.childNodes[1].innerHTML);
							return this._findIndex(this.allNodes[i],this.container);
						}
				}
			return null;
		},
		_findIndex: function(ofObj, inObj){
			for(var i = 0; i < inObj.childNodes.length; i++){
				if(ofObj == inObj.childNodes[i]){
					return i;
				}
			}
			return 0;
		},
		_findParentItem: function(ofObj, inObj){
			for(var i = 0; i < inObj.childNodes.length; i++){
				if(ofObj == inObj.childNodes[i]){
					return inObj.childNodes[i];
				}
			}
			return null;
		},
		_attachBehaviors: function(){
				this.allNodes=document.getElementsByClassName("sortRow",this.container);
				//alert(this.allNodes.length);
				//for(var i=0;i<this.allNodes.length;i++){
						//Element.addMethods();
				//}
		}
};
