Liszts = new Array();
var Filters = {};

 function pad(number, length) {
   
    var str = '' + number;
    while (str.length < length) {
        str = '0' + str;
    }
   
    return str;

}


function addWCEvent(draggable,droppable,e){
  //alert(draggable.id+"|"+droppable.id);
    
	
	//flash the box
	
	// send ajax update
	url = "/cal/cal_event.response.php";
	pars = "drop="+droppable.id+"&drag="+draggable.id;
	var myAjax = new Ajax.Updater( {success: droppable.id}, url, {method: 'get', parameters: pars,  evalScripts: true}); 
		
	
	

}


function Liszt(classname,listname,listitemprefix,displaykey,editformname,feedbackcontainerid,extra) {

 this.ids = new Array(); 
 this.classname = classname;
 this.listname = listname;
 this.listitemprefix = listitemprefix;
 this.displaykey = displaykey;
 this.editformname = editformname;
 this.feedbackcontainerid = feedbackcontainerid;
 this.extra = extra;
 this.newId;
 
 this.autoorder = true;
 this.reverseinsert = false;
 
 //this.filters =  new Array();
 //alert(classname+"-"+listitemprefix);
 Liszts[Liszts.length] = this;
 

 

 Liszt.prototype.send = function(recordid,command,container_id,extra_args,success) {
 
   url = "/liszt.response.php";
	 
	 
	 	 
   if (extra_args == null) extra_args = "nil=0";
	 if (this.extra != '') extra_args += "&"+this.extra;
   //ajaxEngine.registerAjaxElement(container_id);
   //alert("ajaxEngine.sendRequest( 'listChange','value=" + newval +"','command=" + command +"','container_id=" + container_id +"'"+extra_args +")");  // 'value' has to be renamed
   ////eval("ajaxEngine.sendRequest( 'listChange','value=" + newval +"','command=" + command +"','container_id=" + container_id +"'"+extra_args +")");  // 'value' has to be renamed
   pars = "";
	 if (recordid != null) pars = "recordid=" + recordid +"&";
	 pars += "command=" + command +"&classname="+this.classname+"&listname="+this.listname+"&container_id=" + container_id +"&"+extra_args;
	 if(Filter) pars+=Filter.getfilters();  // 'value' has to be renamed
   
													 // need to fill the rest of the items send from ARGS
													 // should look like "order=1" ... might need to be encoded
		
	 //successFunc = "";											 
   //alert(pars);
	 if (!success) success = this.successFunc;
	 rerror = this.reportError;
	 var myAjax = new Ajax.Updater( {success: container_id}, url, {method: 'get', parameters: pars,  onComplete:success, onFailure: rerror, evalScripts: true}); 
	//alert(pars);										 
 }
 
 Liszt.prototype.setautoorder = function(ao) {
   this.autoorder = ao;
 }
 
 Liszt.prototype.setreverseinsert = function(ri) {
   this.reverseinsert = ri;
 }

 
 Liszt.prototype.add = function() {
     var newval = "";
	 var stuff = "";
	 //alert(this.listname);
	 if (document.getElementById("additem_"+this.listname)) {
	   newval = document.getElementById("additem_"+this.listname).value;
	   newval = escape(newval);
	   //alert("newval"+newval);

       stuff+=this.displaykey+"="+newval;
   	 } else {
	 
	 	 var sform = document.getElementById(this.editformname);
  	     var stuff = "";
  	 //for (i=0;i<sform.elements.length;i++) {
  	 //  stuff += sform.elements[i].name+"="+escape(sform.elements[i].value)+"&";
  	 
  	 //}
  	 stuff = Form.serialize(sform);
  	 //alert(stuff);
  	 
	 }
   
   mylist = document.getElementById(this.listname);
   payload = "adding"+newval;
	 addLi = $("addli_"+this.listname);
	 this.newId = this.listitemprefix+"_temp";
	 if (this.reverseinsert) {
	   
	   new Insertion.After(addLi.id, "<li id='"+this.newId+"'>"+payload+"</li>");

	   //mylist.insertAfter(newLi,addLi);
	 } else {
	 	 newLi = document.createElement("li");
	   
	   newLi.innerHTML = ""; //initial display before ajax
	   newLi.id = this.newId; //+listsize;
	   newLi.className = "listitem";
	   mylist.insertBefore(newLi,addLi);
       //alert("?");
	 }
	 	 
	 //alert("newLi.id="+newLi.id);
	 listsize[this.listname]++;
	 
	 emp = $('empty_message_'+this.listname);
	 if (emp != null) emp.innerHTML="";
	 
	 
	 
	 
	 
	 //if (this.autoorder) stuff+= "order="+listsize[this.listname]+"&";
	 // ordering needs to be fixed. this is a temp fix
	 if (this.autoorder) stuff+= "&order=1000&";
	 
	 
	 return stuff;
 
 }

 Liszt.prototype.additem = function() {
     stuff = this.add();
     alert(stuff);
	 //stuff+=this.displaykey+"="+newval
	 //alert(newLi.innerHTML);
	 eval("func = function(){"+this.listname+".addSuccess();}");
	 // make this get back a script that is evaluated, inserts the html, and updates the id with the new db id
	 this.send("","add",this.newId,stuff,func);
	 
	 
 }
 
 Liszt.prototype.addsaved = function(newval) {
  mylist = document.getElementById(this.listname);
   payload = "adding "+newval;
	 addLi = $("addli_"+this.listname);
	 this.newId = this.listitemprefix+"_temp";
	 if (this.reverseinsert) {
	   
	   new Insertion.After(addLi.id, "<li id='"+this.newId+"'>"+payload+"</li>");

	   //mylist.insertAfter(newLi,addLi);
	 } else {
	 	 newLi = document.createElement("li");
	   
	   newLi.innerHTML = "";
	   newLi.id = this.newId; //+listsize;
	   newLi.className = "listitem";
	   mylist.insertBefore(newLi,addLi);
	 }
	 
	 
	 
	 stuff=this.displaykey+"="+newval
	 eval("func = function(){"+this.listname+".addSuccess();}");
	 this.send("","add",this.newId,stuff,func);
	 
	 
 } 
 
 
 Liszt.prototype.addedit = function(containerid) {
   this.send("-1","edit",containerid);
 
 }
 
 Liszt.prototype.addfromiframe = function(recordid,containerid) {
 
   newId = this.listitemprefix+"_"+recordid;
	 //alert(newId);
	 mylist = document.getElementById(this.listname);
	 addLi = $("addli_"+this.listname);
	 if (this.reverseinsert) {
	   
	   new Insertion.After(addLi.id, "<li id='"+newId+"'>loading...</li>");
		 //tester = document.getElementById(newId);
		 //alert(tester.id);

	   //mylist.insertAfter(newLi,addLi);
	 } else {
	 	 newLi = document.createElement("li");
	   
	   newLi.innerHTML = "";
	   newLi.id = newId; //+listsize;
	   newLi.className = "listitem";
	   mylist.insertBefore(newLi,addLi);
	 }
	 	 
	 //alert("newLi.id="+newLi.id);
	 listsize[this.listname]++;
	 
	 
	 var stuff = this.displaykey+"="+recordid;
	 //alert(stuff);
	 eval("func = function(){"+this.listname+".addfromiframeSuccess();}");
   this.send(recordid,"cancel",newId,stuff,func);
	 
	 		
	 
	 
 
 
 }
 
 Liszt.prototype.addfromiframeSuccess = function() {
   addLi = $("addli_"+this.listname);
	 //alert(addLi.id);
				
	 addLi.innerHTML = "<a href=# onclick=\""+this.listname+".addedit('addli_"+this.listname+"');return false;\">Add photo</a>"; //"<? echo $this->addFormContent(); ?>";
  	
 
 }
 
 Liszt.prototype.addexisting = function(value,recordid,ordering) {
   mylist = document.getElementById(this.listname);
	 //newLi = document.createElement("li");
	 //payload = "adding"+newval+"-listsize"+listsize;
	 //newLi.innerHTML = value;
	 newid = this.listitemprefix+"_"+recordid;
	 //newLi.id = newid; //this.listitemprefix+"_temp"; 
	 className = "listitem";
	 //addLi = $("addli_"+this.listname);
	 //alert(ordering);
	 if (ordering == 0) {
	   new Insertion.Top(mylist, "<li id='"+newid+"' class='"+className+"'>"+value+"</li>");
	 } else {
	   // ############# how do we get this to place it in the correct ordering spot????
		 
	   new Insertion.Before(mylist.childNodes[ordering], "<li id='"+newid+"' class='"+className+"'>"+value+"</li>");
	 }
	 //mylist.insertBefore(newLi,addLi);
	 
	 
   //alert("Stuff:"+stuff);
	 
	 listsize[this.listname]++;
	 Sortable.destroy(this.listname);
	 eval("reorder = function() { "+this.listname+".reorder();}");
	 Sortable.create(this.listname,{onUpdate:eval(reorder),handle:this.listname+'move',only:'listitem'});
	 
	 // create addmove action!!
	 var stuff = this.displaykey+"="+value;
   this.send(recordid,"cancel",newid,stuff);
 
 }
 

 
 Liszt.prototype.addSuccess = function() {
   
   tempId = this.listitemprefix+"_temp"; //.$REQUEST['order']; ?>"    //_"+(listsize-1);
	 newOb = document.getElementById(tempId);
	 //alert(tempId);
	// alert(newOb.parent);
	 
	 idOb = document.getElementById('added');
	 Element.cleanWhitespace(idOb);
	 newId = idOb.innerHTML;
	 //alert("("+newId+")");
	 
	 newOb.id = newId;
	 
	 // remove added
	 
	 addedDiv = document.getElementById("added");
	 newOb.removeChild(addedDiv);
	 //alert(this.listname+'_move');
	 
	 // add it to the sort
	 //document.add_onload();
	 
	 //on values, the following lines execute before the added item can be fully loaded 

     //setTimeout ( function () {

		 Sortable.destroy(this.listname),1000;
		 eval("reorder = function() { "+this.listname+".reorder();}");
		Sortable.create(this.listname,{onUpdate:eval(reorder),handle:this.listname+'_move',only:'listitem',scroll:window});
		 
	 
	 //}, 500 );
	 //$this->addOnload("function reorder_".$this->listname."() { ".$this->jsLiszt.".reorder()}\n");
		   //$this->addOnload("Sortable.create('".$this->listname."',{onUpdate:reorder_".$this->listname."});\n");
	 
	 // then display the add box again.
	 
 }
 
 
 Liszt.prototype.deleteitem = function(containerid,recordid) {
   //alert(id);
	 //alert(containerid+"-"+recordid);
   oldLi = document.getElementById(containerid);
	 //alert(oldLi.innerHTML);
	 oldLi.parentNode.removeChild(oldLi);
	 //oldLi.removeNode();
   
	 
	 this.send(recordid,"delete");
	 
 }
 
 Liszt.prototype.edititem = function(containerid,recordid) {
   this.send(recordid,"edit",containerid);
	 
 
 }
 
  Liszt.prototype.scheduleitem = function(containerid,recordid) {
   this.send(recordid,"schedule",containerid);
     
 
 }
 
 Liszt.prototype.completeitem = function(containerid,recordid,display_val) {
   var stuff = this.displaykey+"="+display_val;
   
   this.send(recordid,"complete",containerid,stuff);
 
 }
 
  Liszt.prototype.additemfromcalendar = function(containerid,recordid,display_val,adddate,list_type) {
   var stuff = this.displaykey+"="+display_val+"&adddate="+adddate;
   this.send(recordid,"complete",containerid,stuff);
 
 }
 
 Liszt.prototype.additemfromtweet = function(containerid,recordid,display_value,adddate,tweetid) {
   
   var stuff = this.displaykey+"="+display_value+"&completed="+adddate+"&tweet_id="+tweetid;
   //alert(stuff);
   this.send(recordid,"save",containerid,stuff);
 
 }
 
  Liszt.prototype.additemfromfb = function(containerid,recordid,display_value,adddate,statusid) {
   
   var stuff = this.displaykey+"="+display_value+"&completed="+adddate+"&facebook_id="+statusid;
   //alert(stuff);
   this.send(recordid,"save",containerid,stuff);
 
 }
 
 
 Liszt.prototype.incompleteitem = function(containerid,recordid,display_val) {
   var stuff = this.displaykey+"="+display_val+"&liszt="+this.listname;
	 //alert(stuff);
   this.send(recordid,"incomplete",containerid,stuff);
 
 }
 
  Liszt.prototype.addsaved = function(containerid,recordid,display_val) {
   var stuff = this.displaykey+"="+display_val;
   this.send(recordid,"addsaved",containerid,stuff);
 }
 
 Liszt.prototype.usesaved = function(containerid,recordid,display_val) {
   var stuff = this.displaykey+"="+display_val+"&liszt="+this.listname;
   this.send(recordid,"usesaved",containerid,stuff);
 }
 
 Liszt.prototype.addtag = function(id) {
   newtag = document.getElementById("addtag_"+id).value;
   var stuff = "tag_name="+newtag;
   this.send(id,"addtag","addtagform_"+id,stuff);
 }
 
 Liszt.prototype.saveitem = function(containerid) {
 
   // if this is new (record_id = -1) then we have to add a new add button 
   if (containerid == 'addli_'+this.listname) {
	 
	   // add new div
		 this.add();
		 // call addActon
		 
		 // have addAction return addbutton and javascript to fill out new div -  and rename new div
	 
	 
	 } else {
     // this has to get all of the values from the form
  	 // then put them into a list
  	 // and send them via ajax somehow.
  	 var sform = document.getElementById(this.editformname);
  	 var stuff = "";
  	 //for (i=0;i<sform.elements.length;i++) {
  	 //  stuff += sform.elements[i].name+"="+escape(sform.elements[i].value)+"&";
  	 
  	 //}
  	 stuff = Form.serialize(sform);
  	 //alert(stuff);
  	 
   
     this.send(null,"save",containerid,stuff);
	 }
 }
 
 
 
 
 Liszt.prototype.addtogooglecalendar = function(containerid) {
 

     // this has to get all of the values from the form
  	 // then put them into a list
  	 // and send them via ajax somehow.
  	 var sform = document.getElementById("schedule_form"); // this will be a bug, if they open two boxes to schedule
  	 //var stuff = "";
  	 //for (i=0;i<sform.elements.length;i++) {
  	 //  stuff += sform.elements[i].name+"="+escape(sform.elements[i].value)+"&";
  	 
  	 //}
  	 //stuff = Form.serialize(sform);
  	 //alert(stuff);
       dateval = $F(sform['date']);
       date_parts = dateval.split("-");
       dateval = dateval.replace(/-/g,"");
       dateval_Date = new Date(date_parts[0],date_parts[1],date_parts[2]);
       dateval_plus_one = new Date(dateval_Date.getTime() + 1000 * 60 * 60 * 24);
       dateval2 = dateval_plus_one.getFullYear()+pad(dateval_plus_one.getMonth(),2)+pad(dateval_plus_one.getDate(),2);
       alert(dateval2);
       
       url = "http://www.google.com/calendar/event?action=TEMPLATE"
       +"&text="+$F(sform['title'])
       +"&dates="+dateval+"/"+dateval2
       +"&details="+escape("")
       +"&sprop=website:www.fullmeaning.com";
       
       window.open(url,"calendar");
       
       //escape("<a href='http://www.fullmeaning.com/complete.php?type="+$F(sform['type'])+"&id="+$F(sform['recordid'])+"'>Did it!</a>")
  	 
   
     this.send($F(sform['recordid']),"cancel",containerid);
  
 }
 

 
 
 
 Liszt.prototype.canceledit = function(recordid,containerid,display_val) {
   var stuff = this.displaykey+"="+display_val;
   //alert("Stuff:"+stuff);
   this.send(recordid,"cancel",containerid,stuff);
 }
 
 Liszt.prototype.canceladd = function(recordid,containerid,display_val) {
   var stuff = this.displaykey+"="+display_val;
   //alert("Stuff:"+stuff);
   this.send(recordid,"canceladd",containerid,stuff);
 }
 
 Liszt.prototype.moveup = function(lastcontainer,thiscontainer){
 
 }
 
 Liszt.prototype.reorder = function(){
   neworder = Sortable.serialize(this.listname,[]);
	 //alert(neworder);
   this.send(null,'reorder',this.feedbackcontainerid,neworder);
 }
 
 Liszt.prototype.refresh = function(){
   //alert(this.listname);
	 eval("func = function(){"+this.listname+".refreshSuccess();}");
   this.send(null,'refresh',this.listname,"",func);
 }
 
 
 Liszt.prototype.refreshSuccess = function() {
 	 Sortable.destroy(this.listname);
	 eval("reorder = function() { "+this.listname+".reorder();}");
	 Sortable.create(this.listname,{onUpdate:eval(reorder),handle:this.listname+'move',only:'listitem',scroll:window});
 
 }
 /*
  Liszt.prototype.refresh = function(recordid,containerid,display_val) {
   var stuff = this.displaykey+"="+display_val;
 	 this.send(recordid,"cancel",containerid,stuff);
 }
 */
 
 Liszt.prototype.newItem = function(id) {
   myItem = document.getElementById("newitem");
	 myItem.id = this.list_item_prefix+id;
	 //alert("new!:"+myItem.id);
 }
 
 Liszt.prototype.successFunc = function(){
   //alert("ajax success");
 
 }
 
 Liszt.prototype.reportError = function() {
   alert("ajax failed");
 }
 
 Liszt.prototype.toggleView = function () {
   list_outer = $(this.listname).parentNode;

	 //alert(list_outer.id);
	 //alert(list_outer.style.display);
	 
   Element.toggle(list_outer);
	 //$(listname).style.display = "none";
	 //alert(list_outer.style.display);
		
	 
 
 
 }
 
 Liszt.prototype.showControl = function (recordid) {
 
    controlOb = "control_"+this.listname+"_"+recordid;
		Element.show(controlOb);
 
 }
 
  Liszt.prototype.hideControl = function (recordid) {
 
    controlOb = "control_"+this.listname+"_"+recordid;
		Element.hide(controlOb);
 
 }
 
 
}

/*
function ValueLiszt (classname,listname,listitemprefix,displaykey,editformname,feedbackcontainerid,extra) {
  toggleGoals = function() {
	 
	
	}


}

ValueLiszt.prototype = new Liszt;

*/
