var Base=Class.create();
Base.prototype={
	id					:undefined,
	selects				:{},
	multiselects		:{},
	checks				:[],
	passwords			:[],
	files				:[],
	updates				:[],
	/* braodcasting */
	listeners			:[],
	broadcastMessage	:undefined,
	pe					:undefined,				/* periodically executor */
	time_def			:1.5					/* second before nav closes */
};
/* constructor */
Base.prototype.initialize=function(){};
/* simple Rollover */
Base.prototype.out=function(obj){$(obj).setStyle({cursor:''});};
/* simple Rollout */
Base.prototype.over=function(obj){$(obj).setStyle({cursor:'pointer'});};
/* sets the heights to be equal */
Base.prototype.equalize=function(boxes,way){
	var h1=0;var max=0;
	for(var i=0;i<boxes.length;i++){
		var h2=$(boxes[i]).getHeight();
		max=Math.max(h1, h2);
		h1=max;
	};
	max+='px';
	for(var i=0;i<boxes.length;i++){
		$(boxes[i]).setStyle({height:max});
	}
};
/* returns a number from a css px amount */
Base.prototype.cssNoValue=function(val){return Number(val.substr(0,val.length-2));};
/* generic error handler */
Base.prototype.error=function(obj){
	if(obj.id!=this.id){return;}
	this.show_message("error",obj.messages);
};
Base.prototype.show_message=function(type,messages){
	var msg = "";
	var len=messages.length;
	for(var i=0; i<len; i++)
	{
		msg+=(i+1)+". "+messages[i]+"\n";
	}
	switch(type)
	{
	case "error":
		alert("The following errors were found:\n\n" + msg);
		break;
	}
};
/* redirect to page */
Base.prototype.redirect=function(page){if(page){window.location=page;}else{this.error({id:this.id,messages:['page was empty. base.redirect()']})}};
Base.prototype.cancel=function(){};
/* generic ajax - return will be automatically eval'd */
Base.prototype.submit=function(){new Ajax.Request($(this.id).action, {method:$(this.id).method, parameters:$(this.id).serialize(true)});return false;};
/* creation */
Base.prototype.createButton=function(v,i){var b=MyBuilder.make('input',{type:'button',value:v,id:i});return b;}
Base.prototype.createSelectBox=function(n,t,o){
	var select=MyBuilder.make('select',{name:n},null);
	if(t!=undefined){t=t.strip();}
	var len=o.length;
	if (len==undefined){
		for(var each in o){
			var option=(t==o[each]) ? MyBuilder.make('option',{value:each,selected:'selected'}):
			MyBuilder.make('option',{value:each});
			var ot=document.createTextNode(o[each]);
			option.appendChild(ot);
			select.appendChild(option);
		}
	}else{
		for(var i=0;i < len;i++){
			var option=(t==o[i]) ? MyBuilder.make('option',{value:o[i],selected:'selected'}):
			MyBuilder.make('option',{value:o[i]});
			var ot=document.createTextNode(o[i]);
			option.appendChild(ot);select.appendChild(option);
		}
	}
	return select;
};
Base.prototype.createMultiSelect=function(n,t,o){
	var multi=MyBuilder.make('select',{name:n+'[]',multiple:''},null);
	var len=0;
	/** handle nulls */
	t=(t===null)?"":t;
	for(var each in o){
		len++;
		var option=(t.indexOf(o[each]) > -1) ? MyBuilder.make('option',{value:each,selected:''}): MyBuilder.make('option',{value:each});
		var ot=document.createTextNode(o[each]);
		option.appendChild(ot);
		multi.appendChild(option);
	}
	multi.setAttribute("size",len);
	return multi;
};
Base.prototype.createHiddenField=function(n,v){return MyBuilder.make('input',{type:'hidden',name:n,value:v});};
Base.prototype.createTextField=function(n,t,d){var field=MyBuilder.make('input',{type:'text',name:n,value:t},null);return field;};
Base.prototype.createFileField=function(n){return MyBuilder.make('input',{type:'file',name:n},null);};
Base.prototype.createCheckBox=function(n,c){return MyBuilder.make('input',{type:'checkbox',name:n,checked:c},null);};
Base.prototype.createTextArea=function(n,t){var textarea=MyBuilder.make('textarea',{name:n});var txt=document.createTextNode(t);textarea.appendChild(txt);return textarea;}

var BaseForm = Class.create();
BaseForm.prototype = Object.extend(new Base(), {
	pid			:undefined,				/* iD of the parent */
	opened		:false,					/* flag to managing state */
	dispatcher	:"inlineDispatcher",	/* broadcaster */
	message		:"update",				/* method called by the broadcaster */
	date_vars	:{},					/* calendar functionality */
	opened		:false
});
BaseForm.prototype.initialize=function(){};
BaseForm.prototype.cancel=function(fromdispatch){this.toggle();};
BaseForm.prototype.update=function(obj){if(obj.id!=this.id){return;}this.toggle();};
BaseForm.prototype.closeCals=function(obj){for (var each in this.date_vars){if(this.date_vars[each]==obj){continue;}this.date_vars[each].close();}};
BaseForm.prototype.toggle=function(){
	if(this.opened){
		if(this.link)
			$(this.link).setStyle({display:'block'});
		$(this.id).setStyle({display:'none'});
	}else{
		if(this.link)
			$(this.link).setStyle({display:'none'});
		$(this.id).setStyle({display:'block'});
	}
	this.opened=!this.opened;
};
BaseForm.prototype.addHidden=function(n,i,v){$(this.id).appendChild(this.createHiddenField(n,i,v));};
BaseForm.prototype.addHiddens=function(){
	$(this.id).appendChild(this.createHiddenField("inline-id", this.id));
	$(this.id).appendChild(this.createHiddenField("dispatcher", this.dispatcher));
	$(this.id).appendChild(this.createHiddenField("message", this.message));
};
var StraightForm = Class.create();
StraightForm.prototype = Object.extend(new BaseForm(),{
	button			:undefined
});
StraightForm.prototype.initialize=function(id,link)
{
	this.id=id;
	this.form=$(id);
	this.addHiddens();
	if(link)
		this.link=link;
	/* 
	 * remove onsubmit from form
	 */
//	$(this.id).setAttribute("onsubmit","return false;");
	// add the submit handler
	Event.observe($(this.id), 'submit', this.submit.bind(this));
};
StraightForm.prototype.update=function(obj)
{
	if(obj.id==this.id){this.close();}
};
StraightForm.prototype.open=function()
{
	if(this.button!==undefined)
		this.button.update("close");
//		new Effect.BlindDown(this.form, {duration:0.2});
	this.form.toggle();
	this.opened=true;
};
StraightForm.prototype.close=function()
{
	if(this.link)
		$(this.link).show();
	this.clean();
	this.form.toggle();
	this.opened=false;
}
StraightForm.prototype.clear=function()
{
//		this.show_message();
	var len = this.form.elements.length;
	for(var i=0; i<len; i++)
	{
		var type=this.form.elements[i].type;
		if(type!="hidden" && type!="button" && type!="submit")
			this.form.elements[i].value = '';
	}
};
StraightForm.prototype.reset=function()
{
	this.form.reset();
	var len = this.form.elements.length;
	for(var i=0; i<len; i++)
	{
		var type = this.form.elements[i].type;
		if(type==undefined || type=="button" || type=="hidden" || type=="submit") { continue; }
		this.form.elements[i].style.background = '#fff';
	}
};
StraightForm.prototype.clean=function()
{
	this.clear();
	this.reset();
};
var SmallNote = Class.create();
SmallNote.prototype = Object.extend(new Base(), {

	id				:undefined,
	note			:undefined,
	form 			:undefined,
	button			:undefined,
	add_action		:undefined,
	remove_action	:undefined,
	add_text		:undefined,
	remove_text		:undefined,
	add_class		:null,
	remove_class	:null
});
SmallNote.prototype.initialize=function(id,button,add_action,remove_action,add_text,remove_text,add_class,remove_class){};
SmallNote.prototype.set_status=function(which)
{
	this.status=which;
	this.update();
};
SmallNote.prototype.Toggle=function(e)
{
	if(this.status==1)
	{
		this.form.action=this.remove_action;
	} else {
		this.form.action=this.add_action;
	}
	this.transmit(e);
};
SmallNote.prototype.update=function()
{
	this.time_def=1.5;
	if(this.note!=undefined)
	{
		if(this.status==1)
		{
			this.note.update("Added");
		} else {
			this.note.update("Removed");
		}
		this.pe = new PeriodicalExecuter(this.cleanup.bind(this),this.time_def);
	}
};
SmallNote.prototype.cleanup=function()
{
	this.pe.stop();
	if(this.status==1)
	{
		this.button.update(this.remove_text);
		if(this.remove_class)
			this.button.up().className=this.remove_class;
	} else {
		this.button.update(this.add_text);
		if(this.add_class)
			this.button.up().className=this.add_class;
	}
	new Effect.Fade(this.note, {duration:0.2});
};
SmallNote.prototype.transmit=function(e)
{
	this.build();
	this.position(e);
	this.note.update("Sending");
	this.submit();
};
SmallNote.prototype.position=function(event)
{
	var pos_x	= Event.pointerX(event)+'px';
	var pos_y	= Event.pointerY(event)+'px';
	var pos 	= Position.cumulativeOffset(this.button);
	var note_x 	= (this.cssNoValue(pos_x) + 20) + 'px';
	var note_y 	= pos[1] + 'px';
	$(this.note).setStyle({top:note_y,left:note_x});
	new Effect.Appear(this.note, {duration:0.0});
};
SmallNote.prototype.build=function()
{
	if(this.note!=undefined){return;}
	var note = MyBuilder.make('div',{style:'display:none'},'smallNote',"");
	this.note = $(note);
	var context = $('origin');
	context.appendChild(this.note);
};
/* favoriting note */
var FavNote = Class.create();
FavNote.prototype = Object.extend(new SmallNote(), {

	id				:undefined,
	note			:undefined,
	form 			:undefined,
	button			:undefined,
	add_action		:undefined,
	remove_action	:undefined,
	add_text		:undefined,
	remove_text		:undefined,
	add_class		:null,
	remove_class	:null
});
FavNote.prototype.initialize=function(id,button,add_action,remove_action,add_text,remove_text,add_class,remove_class)
{
	this.id=id;
	this.form=$(id);
	this.button=$(button);
	this.add_action=add_action;
	this.remove_action=remove_action;
	this.add_text=add_text;
	this.remove_text=remove_text;
	this.add_class=add_class;
	this.remove_class=remove_class;
	Event.observe(this.button,"click",this.Toggle.bind(this));
};