//
// Copyright (c) 2011 Global Gate Systems LLC.
// All rights reserved.
//

(function($) {
    var moz = ((document.all)? false : true);
    var ie = ((document.all)? true : false);
    var opera = ((navigator.userAgent.indexOf("Opera") > -1) ? true : false);

    $.fn.schedAvail = function(options) {
        var o = $.extend({}, $.fn.schedAvail.defaults, options);

        return this.each(function() {
	    $this = $(this);

	    if(!this.d) {
		this.d = new Array;
		this.d.di = new Array;
		this.d.da = new Array;
		this.d.o = o;

		this.d.n1 = 32;

		// $this.css ({
		//     backgroundcolor: o.background,
		//     color: o.foreground
		// });

		this.d.di.mousedown_x = 0;
		this.d.di.mousedown_y = 0;
		this.d.di.mouseup_x = 0;
		this.d.di.mouseup_y = 0;

		this.d.di.scheddiv_layer = null;
		this.d.di.draw_layer = null;
		this.d.di.drag_layer = null;
		this.d.di.over_layer = null;
		this.d.di.tb_layer = null;
		this.d.di.ibox = null;

		this.d.di.mi_layer = null;
	    }

	    this.fnMouseDown = function(e) {
		if(!e) e = event;

		var x = null;
		var y = null;

		var srcobj = null;

		if(ie) srcobj = e.srcElement;
		else srcobj = this;

		if(!e || !srcobj || !srcobj.d || !srcobj.d.sched) return;

		var s = srcobj.d.sched;

		if (e.pageX || e.pageY) {
		    x = e.pageX;
		    y = e.pageY;
		}
		else if(e.clientX || e.clientY) {
		    x = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		    y = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
		}

		s.d.di.mousedown_x = x;
		s.d.di.mousedown_y = y;

		if(moz) {
		    // Removes default drag behaviour on image
		    if(e.preventDefault) e.preventDefault();
		}

		s.d.di.drag_layer.style.left = x;
		s.d.di.drag_layer.style.top = y;
		s.d.di.drag_layer.style.width = "2px";
		s.d.di.drag_layer.style.height = "2px";
		s.d.di.drag_layer.style.visibility = "visible";

		// console.log("HHH GOT MouseDown"+
		// 	    "\nHard Event  x: "+e.clientX+" y: "+e.clientY+
		// 	    "\nMouse coord x: "+s.d.di.mousedown_x+" y: "+s.d.di.mousedown_y+
		// 	    "\nSched coord x: "+s.d.di.schedg_pos_x+" y: "+s.d.di.schedg_pos_y+
		// 	    "\nImg. coord  x: "+x+" y: "+y);

		$.fn.schedAvail.addEventListener(srcobj, "mousemove", s.fnMouseMove);
		$.fn.schedAvail.addEventListener(srcobj, "mouseup", s.fnMouseUp); 
		e.cancelBubble = true;

		return false;     
	    }

	    this.fnMouseMove = function(e) {
		if(!e) e = event;

		var x = null;
		var y = null;

		var x0,y0,x1,y1;

		var srcobj = null;

		if(ie) srcobj = e.srcElement;
		else srcobj = this;

		if(!e || !srcobj || !srcobj.d || !srcobj.d.sched) return;

		var s = srcobj.d.sched;

		if (e.pageX || e.pageY) {
		    x = e.pageX;
		    y = e.pageY;
		}
		else if(e.clientX || e.clientY) {
		    x = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		    y = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
		}

		if(x < (s.d.o.grapher_pos_x+s.d.o.periodimg_offset_x)) l = s.d.o.grapher_pos_x+s.d.o.periodimg_offset_x;
		else if(x > (s.d.o.grapher_pos_x+s.d.o.g_width)) l = s.d.o.grapher_pos_x+s.d.o.g_width;
		else l = x;

		if(y < (s.d.o.grapher_pos_y+s.d.o.periodimg_offset_y)) y = s.d.o.grapher_pos_y+s.d.o.periodimg_offset_y;
		else if(y > (s.d.o.grapher_pos_y+s.d.o.g_height)) y = s.d.o.grapher_pos_y+s.d.o.g_height;
		else y = y;

		x0 = Math.min(s.d.di.mousedown_x,x);
		y0 = Math.min(s.d.di.mousedown_y,y);
		x1 = Math.max(s.d.di.mousedown_x,x);
		y1 = Math.max(s.d.di.mousedown_y,y);

		w = x - s.d.di.mousedown_x;
		h = y - s.d.di.mousedown_y;

		s.d.di.drag_layer.style.left = (x0 - (s.d.o.grapher_pos_x))+"px";
		s.d.di.drag_layer.style.top = (y0 - (s.d.o.grapher_pos_y))+"px";
		s.d.di.drag_layer.style.width = (x1-x0)+"px";
		s.d.di.drag_layer.style.height = (y1-y0)+"px";


		if(s.d.di.mi_layer) {
		    s.d.di.mi_layer.innerHTML = 
			"Down  at: "+s.d.di.mousedown_x+","+s.d.di.mousedown_y+"<br>\n"+
			"Moved to: "+x+","+y+"<br>\n"+
			"TTTTT at: "+(s.d.o.grapher_pos_x+s.d.o.periodimg_offset_x)+
			","+(s.d.o.grapher_pos_y+s.d.o.periodimg_offset_y)+"<br>\n"+
			"Box   to: "+x+","+y+" w: "+w+" H: "+h+"<br>\n";
		}
	    }      

	    this.fnMouseUp = function(e) {
		if(!e) e = event;

		var x = null;
		var y = null;

		var srcobj = null;

		if(ie) srcobj = e.srcElement;
		else srcobj = this;

		if(!e || !srcobj || !srcobj.d || !srcobj.d.sched) return;

		var s = srcobj.d.sched;

		if (e.pageX || e.pageY) {
		    x = e.pageX;
		    y = e.pageY;
		}
		else if(e.clientX || e.clientY) {
		    x = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		    y = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
		}

		s.d.di.mouseup_x = x;
		s.d.di.mouseup_y = y;

		$.fn.schedAvail.removeEventListener(srcobj,"mousemove",s.fnMouseMove);  
		$.fn.schedAvail.removeEventListener(srcobj,"mouseup",s.fnMouseUp);

		s.d.di.drag_layer.style.visibility = "hidden";

		// console.log("HHH GOT MouseUp"+
		// 	    "\nHard Event  x: "+e.clientX+" y: "+e.clientY+
		// 	    "\nMouse coord x: "+s.d.di.mouseup_x+" y: "+s.d.di.mouseup_y+
		// 	    "\nSched coord x: "+s.d.di.schedg_pos_x+" y: "+s.d.di.schedg_pos_y+
		// 	    "\nImg. coord  x: "+x+" y: "+y);

		s.fnUpdateScheddivImage(s,0,0,0,0);
	    }

	    this.fnSchedShow = function(title) {
		var dn = "s1";
		if(this.d.di.scheddiv_layer) {
		    this.d.di.scheddiv_layer.d.s.style.visibility = "visible";
		    this.d.di.scheddiv_layer.style.visibility = "visible";
		    this.d.di.tb_layer.style.visibility = "visible";
		    this.d.di.drag_layer.style.visibility = "visible";
		    if(this.d.di.over_layer) this.d.di.over_layer.style.visibility = "visible";
		    if(this.d.di.mi_layer) this.d.di.mi_layer.style.visibility = "visible";
		}
		else {
		    var draw_img;
		    draw_img = document.createElement("IMG");
		    draw_img.style.position = "absolute";
		    draw_img.style.left = this.d.o.grapher_pos_x+"px";
		    draw_img.style.top = this.d.o.grapher_pos_y+"px";
		    draw_img.style.width = this.d.o.g_width+"px";
		    draw_img.style.height = this.d.o.g_height+"px";
		    draw_img.style.border = 2;
		    draw_img.src = "/images/std/schedavailbackground.png";
		    this.appendChild(draw_img);
		    this.d.di.draw_layer = draw_img;

		    // var mi_layer;
		    // mi_layer = document.createElement("div");
   		    // mi_layer.id = "milayer";
		    // mi_layer.style.position = "absolute";
		    // mi_layer.style.width = "300px";
		    // mi_layer.style.height = "100px";
		    // mi_layer.style.left = "5px";
		    // mi_layer.style.top = "155px";
		    // mi_layer.style.border = 2;
		    // mi_layer.style.background = "#bbbbbb";
		    // this.appendChild(mi_layer);
		    // this.d.di.mi_layer = mi_layer;

		    var scheddiv_layer;
		    scheddiv_layer = document.createElement('div');
   		    scheddiv_layer.style.position = "absolute";
		    scheddiv_layer.style.width = this.d.o.g_width+"px";
		    scheddiv_layer.style.height = this.d.o.g_height+"px";
		    scheddiv_layer.style.zIndex = 2;
		    scheddiv_layer.style.left = this.d.o.grapher_pos_x+"px";
		    scheddiv_layer.style.top = this.d.o.grapher_pos_y+"px";
		    scheddiv_layer.style.overflow = "hidden";      
		    scheddiv_layer.style.marginTop = "0px";
   		    scheddiv_layer.innerHTML = title;
		    scheddiv_layer.d = new Array;
		    scheddiv_layer.d.s = this;
		    this.d.di.scheddiv_layer = scheddiv_layer;

		    var toolbar_layer;
		    toolbar_layer = document.createElement('div');
   		    toolbar_layer.style.position = "absolute";
		    toolbar_layer.style.background = "#8080ff";
		    toolbar_layer.style.width = "200px";
		    toolbar_layer.style.height = this.d.o.g_height+"px";
		    toolbar_layer.style.zIndex = 2;
		    toolbar_layer.style.left = 0;
		    toolbar_layer.style.top = this.d.o.grapher_pos_y+"px";
		    toolbar_layer.style.overflow = "hidden";      
		    toolbar_layer.style.margin = "0px";
		    toolbar_layer.style.position = "absolute";
		    toolbar_layer.style.width = "200px";
		    toolbar_layer.style.height = this.d.o.g_height+"px";
		    toolbar_layer.style.zIndex = 2;
		    toolbar_layer.style.left = (this.d.o.grapher_pos_x+this.d.o.g_width)+"px";
		    toolbar_layer.style.top = this.d.o.grapher_pos_y+"px";
 		    toolbar_layer.style.border = "2px solid black";
 		    //toolbar_layer.style.margin = "4px";

  		    // toolbar_layer.innerHTML = "<br><ul>"+
		    // 	//"<a href=\"javascript:void(0);\" onclick=\"setsel('"+dn+"',1);\">Scheduled</A><br>"+
		    // 	//"<a href=\"javascript:void(0);\" onclick=\"showSched('"+dn+"');\">Show Sched</A><br>"+
		    // 	//"<a href=\"javascript:void(0);\" onclick=\"showsel('"+dn+"');\">Show Sel</A><br>"+
		    // 	//"<a href=\"javascript:void(0);\" onclick=\"setsel('"+dn+"',1);\">Scheduled</A><br>"+
		    // 	"<a href=\"javascript:void(0);\" onclick=\"setsel('"+dn+"',0);\">Available</A><br>"+
		    // 	"<a href=\"javascript:void(0);\" onclick=\"setsel('"+dn+"',1);\">Unavailable</A><br>"+
		    // 	"<p><p>"+
		    // 	"<a href=\"javascript:void(0);\" onclick=\"setall('"+dn+"',0);\">Set ALL to available</A><br>"+
		    // 	"<a href=\"javascript:void(0);\" onclick=\"setall('"+dn+"',1);\">Set ALL to unavailable</A><br>"+
		    // 	"<p><p>"+
		    // 	"<a href=\"javascript:void(0);\" onclick=\"saveSched('"+dn+"',1);\">Save Changes</A><br>"+
		    // 	"<a href=\"javascript:void(0);\" onclick=\"cancelSchedChanges('"+dn+"');\">Cancel</A><br>"+
		    // 	"<a href=\"javascript:void(0);\" onclick=\"fnCancel(this);\">Cancel2</A><br>"+
		    // 	"</ul>";

		    var a,u,br,p;

		    // a = document.createElement('a');
		    // a.s = this;
		    // a.setAttribute('href','javascript:void(0);');
		    // a.setAttribute('onclick', 'this.s.fnShowSel()');
		    // a.innerHTML ="Show Sel<br>";
		    // toolbar_layer.appendChild(a);

		    a = document.createElement('a');
		    a.s = this;
		    a.setAttribute('href','javascript:void(0);');
		    a.setAttribute('onclick', 'this.s.fnSetSel(0)');
		    a.innerHTML ="Available<br>";
		    toolbar_layer.appendChild(a);

		    a = document.createElement('a');
		    a.s = this;
		    a.setAttribute('href','javascript:void(0);');
		    a.setAttribute('onclick', 'this.s.fnSetSel(1)');
		    a.innerHTML ="Unavailable<br>";
		    toolbar_layer.appendChild(a);

		    p = document.createElement('p');
		    toolbar_layer.appendChild(p);

		    a = document.createElement('a');
		    a.s = this;
		    a.setAttribute('href','javascript:void(0);');
		    a.setAttribute('onclick', 'this.s.fnSetAll(0)');
		    a.innerHTML ="Set ALL to available<br>";
		    toolbar_layer.appendChild(a);

		    a = document.createElement('a');
		    a.s = this;
		    a.setAttribute('href','javascript:void(0);');
		    a.setAttribute('onclick', 'this.s.fnSetAll(1)');
		    a.innerHTML ="Set ALL to unavailable<br>";
		    toolbar_layer.appendChild(a);

		    toolbar_layer.appendChild(document.createElement('p'));

		    a = document.createElement('a');
		    a.s = this;
		    a.setAttribute('href','javascript:void(0);');
		    a.setAttribute('onclick', 'this.s.fnSaveSched()');
		    a.innerHTML ="Save Changes<br>";
		    toolbar_layer.appendChild(a);

		    a = document.createElement('a');
		    a.s = this;
		    a.setAttribute('href','javascript:void(0);');
		    a.setAttribute('onclick', 'this.s.fnCancel()');
		    a.innerHTML ="Cancel<br>";
		    toolbar_layer.appendChild(a);

		    toolbar_layer.style.background = "#ccccff";
		    this.d.di.tb_layer = toolbar_layer;
		    this.appendChild(this.d.di.tb_layer);

		    var img = document.createElement("IMG");
		    img.src = this.d.o.rowhdrimg;
		    img.style.position = "absolute";
		    img.style.width = (this.d.o.periodimg_width*this.d.o.periods)+"px";
		    img.style.height = this.d.o.rowhdrimgheight+"px";
		    img.style.left = this.d.o.periodimg_offset_x+"px";
		    img.style.top = this.d.o.periodimg_offset_y-this.d.o.rowhdrimgheight+"px";
		    scheddiv_layer.appendChild(img);

		    // Add column headers
		    img = document.createElement("IMG");
		    img.src = this.d.o.colhdrimg;
		    img.style.position = "absolute";
		    img.style.width = this.d.o.colhdrimgwidth+"px";
		    img.style.height = (this.d.o.periodimg_height*this.d.o.days)+"px";
		    img.style.left = this.d.o.periodimg_offset_x-this.d.o.colhdrimgwidth+"px";
		    img.style.top = this.d.o.periodimg_offset_y+"px";
		    scheddiv_layer.appendChild(img);
		    var i,j;
		    for(i=0;i<this.d.o.days;i++) {
			for(j=0;j<this.d.o.periods;j++) {
			    this.fnAddScheddivImage(scheddiv_layer,i,j);
			}
		    }

		    // Add the image for each period
		    var hrs;
		    hrs = 24;
		    var perhr;
		    perhr = this.d.o.periods/hrs;
		    for(i=1;i<=hrs;i++) {
			img = document.createElement("IMG");
			img.src = this.d.o.hourdivimg;
			img.style.position = "absolute";
			img.style.width = "1px";
			img.style.height = this.d.o.rowhdrimgheight+(this.d.o.periodimg_height*this.d.o.days)-1+"px";
			img.style.left = this.d.o.periodimg_offset_x+(i * this.d.o.periodimg_width * perhr)-1+"px";
			img.style.top = this.d.o.periodimg_offset_y-this.d.o.rowhdrimgheight+"px";
			scheddiv_layer.appendChild(img);
		    }

		    var dragDiv;
		    dragDiv = document.createElement("div");
		    dragDiv.style.visibility = "hidden";
		    dragDiv.style.position = "absolute";
		    dragDiv.style.border = "2px dashed white";
		    dragDiv.style.width = "0px";
		    dragDiv.style.height = "0px";
		    dragDiv.style.zIndex = 5;
		    dragDiv.style.left = "10px";
		    dragDiv.style.top = "10px";

		    dragDiv.style.overflow = "hidden";      
		    scheddiv_layer.appendChild(dragDiv);
		    this.d.di.drag_layer = dragDiv;

		    // Put a div over the image to handle mouse events
		    this.d.di.over_layer = document.createElement("div");
		    this.d.di.over_layer.d = new Array();
		    this.d.di.over_layer.d.sched = this;
		    this.d.di.over_layer.style.position = "absolute";
		    this.d.di.over_layer.style.left = "0px";
		    this.d.di.over_layer.style.top = "0px";
		    // this.d.di.over_layer.style.left = this.d.o.grapher_pos_x+"px";
		    // this.d.di.over_layer.style.top = this.d.o.grapher_pos_y+"px";
		    this.d.di.over_layer.style.width = this.d.o.g_width+"px";
		    this.d.di.over_layer.style.height = this.d.o.g_height+"px";
		    this.d.di.over_layer.style.zIndex = 6;
		    // IE doesn't trigger mouse events on background=transparent elements, we
		    // work around it using transparency via alpha filter.
		    if(ie) {
			this.d.di.over_layer.style.background = "#ffffff";
			this.d.di.over_layer.style.filter = "alpha(opacity=0)";
		    }
		    this.d.di.over_layer.style.overflow = "hidden";      
		    this.d.di.over_layer.style.visibility = "visible";
		    scheddiv_layer.appendChild(this.d.di.over_layer);

		    $.fn.schedAvail.addEventListener(this.d.di.over_layer, "mousedown",this.fnMouseDown);

		    // if(moz) {
		    // 	$.fn.schedAvail.addEventListener(this.d.di.over_layer, "mousedown",this.fnMouseDown);
		    // 	//this.d.di.over_layer.mousedown(this,fnMouseDown);
		    // }        

		    // if(ie) {
		    // 	console.log("IE here");

		    // 	// Removes default drag behaviour on image
		    // 	$.fn.schedAvail.addEventListener(img, "drag", function() {return false;});

		    // 	// Add mouse handlers to the image itself
		    // 	$.fn.schedAvail.addEventListener(img, "mousedown", this.fnMouseDown);

		    // 	//$.fn.schedAvail.addEventListener(this.d.di.over_layer, "mousedown",this.fnMouseDown);
		    // 	$.fn.schedAvail.addEventListener(this.d.di.over_layer, "mousedown",fake);





		    // 	// In some aspects opera behaves like ie, on others like firefox.
		    // 	// However, the click events have tested 100% reliable on Opera
		    // 	// and not on IE. Display warning.
		    // 	// if(!opera) {
		    // 	//     alert("Internet Explorer mouse events are not reliable. We recommend using\n"+
		    // 	// 	  "a more standards oriented browser like Firefox or Opera while viewing this\n"+
		    // 	// 	  "interactive page. However, feel free to continue using IE with its known.\n"+
		    // 	// 	  "limitations and please be patient with problems caused by your browser.\n");
		    // 	// }
		    // }

		    if(this) this.appendChild(scheddiv_layer);
		    else document.getElementsByTagName('body')[0].appendChild(scheddiv_layer);
		}
	    };

	    this.fnCancel = function() {
		this.d.di.scheddiv_layer.d.s.style.visibility = "hidden";
		this.d.di.scheddiv_layer.style.visibility = "hidden";
		this.d.di.tb_layer.style.visibility = "hidden";
		this.d.di.drag_layer.style.visibility = "hidden";
		if(this.d.di.over_layer) this.d.di.over_layer.style.visibility = "hidden";
		if(this.d.di.mi_layer) this.d.di.mi_layer.style.visibility = "hidden";
	    }

	    this.fnAddScheddivImage = function(layer,dayidx,periodidx) {
		var img = document.createElement("IMG");
		img.src = this.d.o.stimg[0];
		img.id = "d"+dayidx+"_p"+periodidx;
		img.style.position = "absolute";
		img.style.width = (this.d.o.periodimg_width-1)+"px";
		img.style.height = (this.d.o.periodimg_height-1)+"px";
		img.style.left = this.d.o.periodimg_offset_x+(periodidx * this.d.o.periodimg_width)+"px";
		img.style.top = this.d.o.periodimg_offset_y+(dayidx * this.d.o.periodimg_height)+"px";
		img.setAttribute('schedsel','0');
		img.setAttribute('schedstatus','0');
		layer.appendChild(img);
	    }


	    this.fnUpdateScheddivImage = function(s,x0,y0,x1,y1) {
		var id;
		var img;
		var i,j;
		var startdayidx,enddayidx;
		var startperiodidx,endperiodidx;

		x0 = Math.min(s.d.di.mousedown_x,s.d.di.mouseup_x) - (s.d.o.grapher_pos_x+s.d.o.periodimg_offset_x);
		y0 = Math.min(s.d.di.mousedown_y,s.d.di.mouseup_y) - (s.d.o.grapher_pos_y+s.d.o.periodimg_offset_y);
		x1 = Math.max(s.d.di.mousedown_x,s.d.di.mouseup_x) - (s.d.o.grapher_pos_x+s.d.o.periodimg_offset_x);
		y1 = Math.max(s.d.di.mousedown_y,s.d.di.mouseup_y) - (s.d.o.grapher_pos_y+s.d.o.periodimg_offset_y);

		if(x0 < 1) startperiodidx = -1;
		else startperiodidx = Math.floor(x0/s.d.o.periodimg_width);

		if(y0 < 1) startdayidx = -1;
		else startdayidx = Math.floor(y0/s.d.o.periodimg_height);

		if(x1 < 1) endperiodidx = -1;
		else endperiodidx = Math.floor(x1/s.d.o.periodimg_width);
		if(endperiodidx < startperiodidx) endperiodidx = startperiodidx;

		if(y1 < 1) enddayidx = -1;
		else enddayidx = Math.floor(y1/s.d.o.periodimg_height);
		if(enddayidx < startdayidx) enddayidx = startdayidx;

		if(enddayidx >= s.d.o.days) enddayidx = s.d.o.days - 1;
		if(endperiodidx >= s.d.o.periods) endperiodidx = s.d.o.periods - 1;

		if(s.d.di.mi_layer) {
		    s.d.di.mi_layer.innerHTML = 
			"Block: "+x0+","+y0+"  "+x1+","+y1+"<br>\n"+
			"Start: P:"+startperiodidx+" D:"+startdayidx+"<br>\n"+
			"End  : P:"+endperiodidx+" D:"+enddayidx+"<br>\n";
		}

		for(i=0;i<s.d.o.days;i++) {
		    for(j=0;j<s.d.o.periods;j++) {
			id = "d"+i+"_p"+j;
			img = document.getElementById(id);
			img.src = s.d.o.stimg[img.getAttribute('schedstatus')];
			img.setAttribute('schedsel','0');
		    }
		}

		if(startperiodidx == endperiodidx == -1) return;
		if(startdayidx == enddayidx == -1) return;

		for(i=startdayidx;i<=enddayidx;i++) {
		    for(j=startperiodidx;j<=endperiodidx;j++) {
			id = "d"+i+"_p"+j;
			img = document.getElementById(id);
			if(img) {
			    img.src = s.d.o.stimg[2];
			    img.setAttribute('schedsel','1');
			}
		    }
		}

		//document.getElementById('dragdiv').style.visibility = "hidden";
	    }

	this.fnSaveSched = function() {
	    dayschedtext = this.fnGetSchedSelText();

	    $.ajax({url : this.d.o.ub+"/11/",
		    data : {
			'sched' : dayschedtext
		    },
		    method: 'POST',
		    success : function(data, textStatus, jqXHR) {
			location.reload();
		    },
		    error:function (xhr, ajaxOptions, thrownError){alert("err",xhr.status+": "+xhr.statusText);}
		   });
	}

	this.fnShowSched = function() {
	    dayschedtext = this.getSchedSelText();
	    if(!ajaxObjBusy) geturl('POST',URLbase+"/11","sched="+encodeURI(dayschedtext),saveSchedAjaxhandler);
	    else alert("Busy, please try again in a few seconds.");
	    alert("HHH showSched\n"+dayschedtext+sched2text(dayschedtext));
	    alert("HHH var\n"+encodeURI(dayschedtext));
	}

	this.fnSetAll = function(status) {
	    var id;
	    var img;
	    var i,j;

	    for(i=0;i<this.d.o.days;i++) {
		for(j=0;j<this.d.o.periods;j++) {
		    id = "d"+i+"_p"+j;
		    img = document.getElementById(id);
		    img.setAttribute('schedstatus',status);
		    img.setAttribute('schedsel','0');
		    img.src = this.d.o.stimg[status];
		}
	    }
	}

	this.fnSetSel = function(status) {
	    var id;
	    var img;
	    var i,j;

	    for(i=0;i<this.d.o.days;i++) {
		for(j=0;j<this.d.o.periods;j++) {
		    id = "d"+i+"_p"+j;
		    img = document.getElementById(id);
		    if(img && (img.getAttribute('schedsel') == 1)) {
			img.setAttribute('schedstatus',status);
			img.setAttribute('schedsel','0');
			img.src = this.d.o.stimg[status];
		    }
		}
	    }
	}

	this.fnShowSel = function() {
	    var id;
	    var img;
	    var i,j;
	    var text;

	    text = "";

	    for(i=0;i<this.d.o.days;i++) {
		for(j=0;j<this.d.o.periods;j++) {
		    id = "d"+i+"_p"+j;
		    img = document.getElementById(id);
		    if(img && (img.getAttribute('schedsel') == 1)) {
			text += id+" "+img.getAttribute('schedsel')+"\n";
		    }
		}
	    }

	    alert(text);
	}

	this.fnGetSchedSelText = function() {
	    var startdayidx,enddayidx;
	    var startperiodidx,endperiodidx;
	    var laststatus;
	    var i,j;
	    var id,img;
	    var dayschedtext;

	    dayschedtext = "";
	    for(i=0;i<this.d.o.days;i++) {
		startperiodidx = 0;
		endperiodidx = -1;
		laststatus = -1;
		for(j=0;j<this.d.o.periods;j++) {
		    id = "d"+i+"_p"+j;
		    img = document.getElementById(id);
		    if(img.schedstatus == 1)
			img.src = this.d.o.stimg[img.getAttribute('schedstatus')];
		    if(j==0) laststatus = img.getAttribute('schedstatus');
		    if(img.getAttribute('schedstatus') != laststatus) {
			endperiodidx=j-1;
			if(laststatus == 1) dayschedtext += laststatus+"="+i+"_"+startperiodidx+"-"+endperiodidx+"\n";
			startperiodidx = j;
		    }
		    laststatus = img.getAttribute('schedstatus');
		}
		if(laststatus == 1) dayschedtext += laststatus+"="+i+"_"+startperiodidx+"-"+(j-1)+"\n";
	    }
	    return dayschedtext;
	}

	this.fnSched2text = function(intext) {
	    var wkday = new Array;
	    wkday[0] = "Monday";
	    wkday[1] = "Tuesday";
	    wkday[2] = "Wednesday";
	    wkday[3] = "Thursday";
	    wkday[4] = "Friday";
	    wkday[5] = "Saturday";
	    wkday[6] = "Sunday";

	    var t1 = new Array;
	    var t2 = new Array;
	    var t3 = new Array;

	    var outtext = "";

	    var d0,d1,h0,h1;
	    var periodminutes;
	    var durationmin;
	    var fromhr,tohr;
	    var frommin,tomin;
	    var laststatus;
	    var tmp;

	    var l = intext.split("\n");

	    var i,j;

	    try {
		for(i=0;i<l.length;i++) {
		    t1 = l[i].split("=",2);
		    if(t1) {
			t2 = t1[1].split("_",2);
			t3 = t2[1].split("-",2);

			periodminutes = (24*60/numperiods);
			h0 = t3[0] * periodminutes;
			h1 = eval(t3[1]+" + 1") * periodminutes;
			durationmin = (h1-h0);

			fromhr = Math.floor(h0/60);
			frommin = h0%60;
			tohr = Math.floor(h1/60);
			tomin = h1%60;

			outtext += wkday[t2[0]]+" From: "+fromhr+":"+frommin+" To: "+tohr+":"+tomin+"\n";
		    }
		}
	    }
	    catch(_) {}

	    return outtext;
	}

	    this.fnSchedShow("<center><b>Availability Schedule</b></center>");

	});

    };


    // Default opts
    $.fn.schedAvail.defaults = {
        foreground: 'black',
	days: 7,
	periods: 96,
	stimg: ["/images/std/available.png",
		"/images/std/scheduled.png",
		"/images/std/selected.png",
		"/images/std/unavailable.png"],
	rowhdrimg: "/images/std/rowhdr.png",
	rowhdrimgheight: 25,
	colhdrimg: "/images/std/colhdr.png",
	colhdrimgwidth: 80,
	hourdivimg: "/images/std/hourdiv.png",
	g_width: 775,
	g_height: 255,
	g_pos_x: 120,
	g_pos_y: 10,

	grapher_pos_x: 160,
	grapher_pos_y: 300,

	periodimg_offset_x: 95,
	periodimg_offset_y: 60,
	periodimg_width: Math.floor((775 - 95 - 4) / 96), // width - offset_x - right_margin
	periodimg_height: Math.floor((255 - 60 - 10) / 7), // height - offset_y - bottom_margin

	g_x_startpos: 47,
	g_x_endpos: 700,
	g_y_startpos: 47,
	g_y_endpos: 700,

	endopt: 1
    };

    // Public functions

    $.fn.schedAvail.addEventListener = function(o, type, handler) {
	if(ie) o.attachEvent("on" + type, handler);
	else if(moz) o.addEventListener(type, handler, false);
    }

    $.fn.schedAvail.removeEventListener = function(o, type, handler) {
	if(ie) o.detachEvent("on" + type, handler);
	else if(moz) o.removeEventListener(type, handler, false);
    }

    // Private functions
    function debug($obj) {
        if(window.console && window.console.log) {
	    console.log('obj: ');
	    console.log($obj);
	    console.log('opts: ');
	    console.log($obj.d.o);
	    console.log('size: ' + $obj.size());
	}
    };

})(jQuery);

