Books = Array();
AppointmentBook = function (parentId, slot, onselect) {

	Books[parentId] = this;
	this.view = document.getElementById(parentId);
	this.parentId = parentId;

	this.Days = Array();

	this.slot = slot;
	this.showHead = true;
	this.closed = false;
	this.selected = null;
	this.day = null;

	this.onselect = onselect || null;


this.showday_min = function (nd) {
        i = 0;
        this.day = this.Days[nd];

        if(!this.day) {
		this.closed = true;
		this.day = new this.AppointDay(nd, "", "");
		tview = this.startDay();
		
                row = document.createElement('tr');
                row.className =  "closed";
                cell = document.createElement('td');
                cell.colSpan = 2;
                cell.innerHTML = "Not Available";
                row.appendChild(cell);
                tview.appendChild(row);
        } else {
		tview = this.startDay();
                daymins = this.t_to_mins(this.day.stime);
                while(app = this.day.Appoints[i++]) {
                        while(daymins+this.slot+app.buf<=this.t_to_mins(app.start)) {
                                tview.appendChild(this.createRow(daymins, this.slot, "Open"));
                                daymins = daymins + this.slot;
                        }

                        tview.appendChild(this.createRow(this.t_to_mins(app.start), Number(app.length), app.client, i-1));
                        daymins = this.t_to_mins(app.start) + Number(app.length) + app.buf;
                }

                while(daymins+this.slot<this.t_to_mins(this.day.etime)) {
                        tview.appendChild(this.createRow(daymins, this.slot, "Open"));

                        if(this.slot == 0) {daymins = this.t_to_mins(this.day.etime);} else {daymins = daymins + this.slot;}
                }
        }
	this.display(tview);
}


this.showday_max = function (nd) {
	i = 0;
        this.day = this.Days[nd];

        if(!this.day) {
		this.closed = true;
		this.day = new this.AppointDay(nd, "", "");
		tview = this.startDay();
		
                row = document.createElement('tr');
                row.className =  "closed";
                cell = document.createElement('td');
                cell.colSpan = 2;
                cell.innerHTML = "Not Available";
                row.appendChild(cell);
                tview.appendChild(row);
        } else {
		tview = this.startDay();
                daymins = this.t_to_mins(this.day.stime);
                while(app = this.day.Appoints[i++]) {
			for(s=0;s<this.slot.length;s++) {
	                        while(daymins+this.slot[s]+app.buf<=this.t_to_mins(app.start)) {
					tview.appendChild(this.createRow(daymins, this.slot[s], "Open"));
					daymins = daymins + this.slot[s];
	                        }
			}
			
	      if(daymins+app.buf<this.t_to_mins(app.start)) {tview.appendChild(this.createRow(daymins, this.t_to_mins(app.start)-(daymins+app.buf), "Open"));}
			tview.appendChild(this.createRow(this.t_to_mins(app.start), Number(app.length), app.client, i-1));
			daymins = this.t_to_mins(app.start) + Number(app.length) + app.buf;
                }

		for(s=0;s<this.slot.length;s++) {
	                while(daymins+this.slot[s]<=this.t_to_mins(this.day.etime)) {
				tview.appendChild(this.createRow(daymins, this.slot[s], "Open"));
			
				if(this.slot[s] == 0) {daymins = this.t_to_mins(this.day.end);} else {daymins = daymins + this.slot[s];}
			}
		}
		
			if(daymins<this.t_to_mins(this.day.etime)) {tview.appendChild(this.createRow(daymins, this.t_to_mins(this.day.etime)-daymins, "Open"));}
			
        }
	this.display(tview);
}


this.showday_non = function (nd) {
        i = 0;
        this.day = this.Days[nd];

        if(!this.day) {
		this.closed = true;
		this.day = new this.AppointDay(nd, "", "");
		tview = this.startDay();
		
                row = document.createElement('tr');
                row.className =  "closed";
                cell = document.createElement('td');
                cell.colSpan = 2;
                cell.innerHTML = "Not Available";
                row.appendChild(cell);
                tview.appendChild(row);
        } else {
		tview = this.startDay();
                while(app = this.day.Appoints[i++]) {
                        tview.appendChild(this.createRow(this.t_to_mins(app.start), Number(app.length), app.client, i-1));
                }

                row = document.createElement('tr');
		row.onclick = new Function("Books['"+this.parentId+"'].select(this)");
	row.onmouseover = new Function("this.className='select1';this.style.cursor='pointer'");
	row.onmouseout = new Function("this.className=''");

                cell = document.createElement('td');
	cell.className = "formLabels";
                cell.colSpan = 2;
		cell.align = "center";
//	cell.onmouseover = new Function("this.className='select1';this.style.cursor='pointer'");
//	cell.onmouseout = new Function("this.className='formLabels'");
                if (this.slot.length==0){;}else{cell.innerHTML = "Add New";}
                row.appendChild(cell);
                tview.appendChild(row);
        }
	this.display(tview);
}

this.createRow = function (start, length, text, id) {
	row = document.createElement('tr');
	if(id>-1) {
		row.id = id;
	} else {
		row.id = this.s_t(start);
	}
	row.title = length+" mins";
	row.onclick = new Function("Books['"+this.parentId+"'].select(this)");
	row.onmouseover = new Function("this.className='select1 disabled';this.style.cursor='pointer'");
	row.onmouseout = new Function("this.className=''");

	cell = document.createElement('td');
	cell.className = "formLabels";
//	cell.onmouseover = new Function("this.className='select1';this.style.cursor='pointer'");
//	cell.onmouseout = new Function("this.className='formLabels'");
	cell.innerHTML = this.d_t(start)+' - '+this.d_t(start+length);
	row.appendChild(cell);

	cell = document.createElement('td');
	cell.className = "formFields";
	cell.innerHTML = text;
	row.appendChild(cell);

	return row;
}

this.createHead = function () {
	headrow = document.createElement('tr');
	headrow.className =  "formHeaders";
	head = document.createElement('td');
	head.colSpan = 2;
	tdate = new Date(this.day.year, this.day.month-1, this.day.day);
	if (this.slot.length==0){head.innerHTML ="After Hours";}else{head.innerHTML = this.dow[tdate.getDay()]+" - "+this.nom[tdate.getMonth()]+" "+this.day.day+", "+this.day.year;}
	headrow.appendChild(head);

	return headrow;
}

this.startDay = function () {
	this.closed = false;

	tview = document.createElement('tbody');

	if(this.showHead) {tview.appendChild(this.createHead());}

	return tview;
}

this.display = function (elm) {
	tview = document.createElement('table');
	tview.className =  "listview";
	tview.appendChild(elm);

	if(this.view.firstChild) {this.view.removeChild(this.view.firstChild);}

	this.view.appendChild(tview);
}

if(this.slot[0]) {this.showday = this.showday_max;this.ssort;} else if(slot>0) {this.showday = this.showday_min;} else {this.showday = this.showday_non;}
this.select = function (el) {this.selected = el; this.onselect(this)}

this.getName = function () {
    if(this.closed) {
		return "closed";
	} else if(this.day.Appoints[this.selected.id]) {
		return this.day.Appoints[this.selected.id].client;
	} else {
		return "open";
	}
}
this.getMonth = function () {return this.day.month;}
this.getDay = function () {return this.day.day;}
this.getYear = function () {return this.day.year;}
this.getAppointTime = function () {
	if(this.day.Appoints[this.selected.id]) {
		return this.day.Appoints[this.selected.id].start;
	} else if(this.selected.id) {
		return this.selected.id;
	} else {
		return "0:00";
	}
}
this.getSlotLength = function () {t = this.selected.title.split(" ");return t[0];}
this.getAppointId = function () {if(this.day.Appoints[this.selected.id]) {return this.day.Appoints[this.selected.id].id;}}
this.addDay = function (dateStr, start, end) {this.Days[dateStr] = new this.AppointDay(dateStr, start, end);}
this.addAppoint = function (day, id, start, length, client, buf, type) {if(this.Days[day]) {this.Days[day].Appoints[this.Days[day].Appoints.length] = new this.Appoint(id, start, length, client, buf, type);this.Days[day].asort();}}

this.AppointDay = function (dateStr, start, end) {
	dateA = dateStr.split("/");
	this.day = Number(dateA[1]);
	this.month = Number(dateA[0]);
	this.year = Number(dateA[2]);
	this.stime = start;
	this.etime = end;

	this.Appoints = Array();

	this.asort = function () {
		for(x=0;x<this.Appoints.length;x++) {
			for(y=this.Appoints.length-1;y>x;y--)  {
				if(this.t_to_mins(this.Appoints[y-1].start)>this.t_to_mins(this.Appoints[y].start)) {
					t=this.Appoints[y-1];
					this.Appoints[y-1]=this.Appoints[y];
					this.Appoints[y]=t;
				}
			}
		}
	}
	this.t_to_mins = function (time) {t = time.split(":");h = Number(t[0])*60;return Number(h+Number(t[1]));}
}
this.getType = function(){if(this.day.Appoints[this.selected.id]){return this.day.Appoints[this.selected.id].atype;}else{return 0;}}

this.Appoint = function (i, s, l, c, b, t) {this.id=i;this.start=s;this.length=l;this.client=c;this.buf=b;this.atype=t;}

this.ssort = function () {
	for(x=0;x<this.slot.length;x++) {
		for(y=this.slot.length-1;y>x;y--)  {
			if(this.slot[y-1]<this.slot[y]) {t=this.slot[y-1];this.slot[y-1]=this.slot[y];this.slot[y]=t;}
		}
	}
}
this.dow = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
this.nom = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
this.t_to_mins = function (time) {t = time.split(":");h = Number(t[0])*60;return Number(h+Number(t[1]));}
this.d_t = function (mins) {h = this.n_h(mins);m = this.n_m(mins);am=" AM";if(h > 12) {h = h-12;am=" PM";}if(h > 11) {am=" PM";}if(m < 10) {m = "0"+m;}return h+":"+m+am;}
this.s_t = function (mins) {h = this.n_h(mins);m = this.n_m(mins);if(h > 24) {h = 0;}if(m < 10) {m = "0"+m;}return h+":"+m;}
this.n_h = function (mins) {return Math.floor(mins/60);}
this.n_m = function (mins) {return mins%60;} 
}
