oAjax = new AjaxRequest();
var tempEventArray = new String();

function addScheduler(id,sid){
	makeToolTip('Adding to your scheduler...');
	eval("setTimeout(\"processAdd('"+id+"',"+sid+")\",1000)");
}

function processAdd(id,sid){
	if(id != "" && sid != ""){
		oAjax.open("GET","/_components/presentations/model/actAdd.php?id="+id+"&sid="+sid+"&time"+Math.random());
		oAjax.onreadystatechange = function(){addedToScheduler(id,sid)};
		oAjax.send(null);	
	}	
}

function addedToScheduler(id,sid){
	destroyToolTip();
	if(oAjax.readyState == 4){
		if(oAjax.responseText == 1){
			makeToolTip('Successfully added to your scheduler!');
			setTimeout("destroyToolTip()",1000);
			oText = document.getElementById('addschedulertext');
			while(oText.firstChild){oText.removeChild(oText.firstChild)};
			oAnchor = document.createElement("a");
			oAnchor.setAttribute("href","javascript:void(0)");
			oAnchor.onclick = function(){removeScheduler(id,sid)};
			oAnchor.appendChild(document.createTextNode("Remove from your scheduler"));
			oText.appendChild(document.createTextNode(" | "));
			oText.appendChild(oAnchor);
		}else if(oAjax.responseText == -1){
			makeToolTip('Could not add to your scheduler, most likely because it is already there!');
			setTimeout("destroyToolTip()",2000);
		}else if(oAjax.responseText == -2){
			makeToolTip('There is already another item scheduled for that day and time slot.');
			setTimeout("destroyToolTip()",2000);
		}else{
			makeToolTip('Could not complete your request.');
			setTimeout("destroyToolTip()",1000);	
		}
	}
}

function removeScheduler(id,sid){
	makeToolTip('Removing from your scheduler...');
	eval("setTimeout(\"processRemove('"+id+"',"+sid+")\",1000)");
}

function processRemove(id,sid){
	if(id != "" && sid != ""){
		oAjax.open("GET","/_components/presentations/model/actRemove.php?id="+id+"&sid="+sid+"&time"+Math.random());
		oAjax.onreadystatechange = function(){removedFromScheduler(id,sid)};
		oAjax.send(null);	
	}	
}

function removedFromScheduler(id,sid){
	destroyToolTip();
	if(oAjax.readyState == 4){
		if(oAjax.responseText == 1){
			makeToolTip('Successfully removed from your scheduler!');
			setTimeout("destroyToolTip()",1000);
			oText = document.getElementById('addschedulertext');
			while(oText.firstChild){oText.removeChild(oText.firstChild)};
			oAnchor = document.createElement("a");
			oAnchor.setAttribute("href","javascript:void(0)");
			oAnchor.onclick = function(){addScheduler(id,sid)};
			oAnchor.appendChild(document.createTextNode("Add to your scheduler"));
			oText.appendChild(document.createTextNode(" | "));
			oText.appendChild(oAnchor);
		}else{
			makeToolTip('Could not complete your request.');
			setTimeout("destroyToolTip()",1000);	
		}
	}
}

/*
aEvents.push(new Array("Remi Test Presentation 3","VS1-02",22,16,00,60));*/

function loadScheduler(id){
	oAjax.open("GET","/_components/presentations/model/actLoad.php?id="+id+"&time"+Math.random());
	oAjax.onreadystatechange = function(){viewScheduler()};
	oAjax.send(null);
}

function viewScheduler(){
	if(oAjax.readyState == 4){
		if(oAjax.responseText == -1){
			makeToolTip('Could not load your scheduler because there are no items in it.');
			setTimeout("destroyToolTip()",1000);	
		}else{
			tempEventArray = oAjax.responseText;
			oCont = document.createElement("div");
			oCont.setAttribute("class","cal_cont");
			oCont.setAttribute("className","cal_cont");
			for(i=0;i<5;i++){
				oDiv = document.createElement("div");
				oDiv.setAttribute("class","cal_head");
				oDiv.setAttribute("className","cal_head");
				if(!i){
					setStyle(oDiv,"margin-left:40px;")
					oDiv.setAttribute("className","cal_head cal_headfirst");
				}
				oDiv.appendChild(document.createTextNode("Nov. "+(i+15)));
				oCont.appendChild(oDiv);
			}
			oTime = document.createElement("div");
			oTime.setAttribute("class","cal_time");
			oTime.setAttribute("className","cal_time");
			setStyle(oTime,"height:606px;border-right:1px solid #000;width:40px;font-size:80%;padding-top:30px;");
			intTop = 50;
			for(xhour=7;xhour<19;xhour++){
				for(xtime=0;xtime<2;xtime++){
					oSpan = document.createElement("span");
					setStyle(oSpan,"position:absolute;top:"+intTop+"px;left:13px");
					oSpan.appendChild(document.createTextNode(xhour+":"+((!xtime) ? "00" : xtime*30)));
					oTime.appendChild(oSpan);	
					intTop+=24;
				}
			}
			oCont.appendChild(oTime);
			aEvents = oAjax.responseText.split("(@)");
			for(i=0;i<aEvents.length;i++){
				aevent = aEvents[i].split("(^)");
				intLeft = Math.ceil((aevent[2]-15)*130)+50;
				intTop = Math.ceil((((aevent[3]-7)*48) + ((aevent[4]==0) ? 0 : ((aevent[4]/5)*4))) + 50);
				intHeight = Math.ceil((aevent[5]/5)*4);
				//alert("vs: "+aevent[1]+" left: "+intLeft+" top: "+intTop+" height: "+intHeight);
				oDiv = document.createElement("div");
				oDiv.setAttribute("class","cal_item");
				oDiv.setAttribute("className","cal_item");
				setStyle(oDiv,"left:"+intLeft+"px;top:"+intTop+"px;min-height:"+intHeight+"px;");
				/*oAnchor = document.createElement("a");
				oAnchor.appendChild(document.createTextNode("X"));
				oAnchor.setAttribute("style","float:right");
				oAnchor.setAttribute("href","javascript:removeScheduleItem("+i+",'"+id+"')");
				oDiv.appendChild(oAnchor);*/
				oDiv.appendChild(document.createTextNode(aevent[0]));
				oSpan = document.createElement("span");
				oSpan.setAttribute("class","cal_additional");
				oSpan.setAttribute("className","cal_additional");
				oSpan.appendChild(document.createElement("br"));
				oSpan.innerHTML = ("Start time: "+aevent[3]+":"+aevent[4]+"<br>Duration:"+aevent[5]+" minutes");
				oDiv.appendChild(oSpan);
				oDiv.setAttribute("name",i);
				/*if(!document.all){
					//oDiv.onmouseover = eval("function(){makeToolTip('"+aevent[0]+"<br>"+aevent[1]+"<br>Start time: "+aevent[3]+":"+aevent[4]+"<br>Duration:"+aevent[5]+" minutes')}");
					oDiv.onmouseover = function(){makeToolTip(aevent[0]+"<br>"+aevent[1]+"<br>Start time: "+aevent[3]+":"+aevent[4]+"<br>Duration: "+aevent[5]+" minutes")};
					oDiv.onmouseout = function(){destroyToolTip()};
				}else{
					oDiv.attachEvent('onmouseover', showDesc);
					oDiv.attachEvent('onmouseout', destroyToolTip); 
				}*/
				oDiv.appendChild(document.createElement("br"));
				oCont.appendChild(oDiv);
			}
			oAnchor = document.createElement("a");
			oAnchor.appendChild(document.createTextNode("Print Schedule"));
			oAnchor.setAttribute("href","/_components/baltpresentations/model/actPrint.php");
			oCont.appendChild(oAnchor);
			drawSimple(oCont);
		}
	}
}

/*function showDesc(e){
	var s = new String(tempEventArray);
	aEvents = s.split("(@)");
	aevent = aEvents[e.srcElement.name].split("(^)");
	makeToolTip(aevent[0]+"<br>"+aevent[1]+"<br>Start time: "+aevent[3]+":"+aevent[4]+"<br>Duration:"+aevent[5]+" minutes");	
}*/

function removeScheduleItem(intItem,id){
	destroyToolTip();
	_closedraw();
	aEvents.splice(intItem,1);
	viewScheduler(id);
}

function setStyle(obj,style){
	if(document.all) obj.style.setAttribute("cssText",style);
	else obj.setAttribute("style",style);
}
