window.onload = function() {
	
	/*if(window.parent) {
		if(window.parent.document) {
			Event.observe(window.parent.document, 'mouseover', function(event){
				if($('calendarEvtDIV')) {
					hideEvtDIV();
				}
			});
		}
	}*/
   Event.observe(document,'mouseover',function(event) {
   	    var eventElement = event.element();
		/*if(!(eventElement.readAttribute("class") == "eventDiv") && !(eventElement.id == "calendarEvtDIV")) {
			hideEvtDIV();
		}*/
		//if(eventElement.readAttribute("class")) {
		if(eventElement.readAttribute("class") == null) {
			//hideEvtDIV();
			return;
		}
		if(eventElement.readAttribute("class").indexOf("calendarDay") != -1)
			hideEvtDIV();
		
		//}
	})
	
	Event.observe(document,'click',function(event) {
		var evElem = event.element();
		//if(evElem.readAttribute("class")) {
		if(evElem.readAttribute("class") == null) {
			//hideEvtDIV();
			return;
		}
		if(evElem.readAttribute("class").indexOf("dayNumberCells") == -1)
			hideEvtDIV();
		
		//}
	})
	
	if (document.all) {
		var sels=document.getElementsByTagName('select');
		for (var i=0; i<sels.length; i++) {
			colorDisabled(sels[i]);
		}
	}
	
}

window.document.onmousemove = getMouseXY;
mousex = 0;
mousey = 0;


function mouseX(evt) {
	if (evt.pageX) 
	  return evt.pageX; 
	else if (evt.clientX)
	  return evt.clientX + (document.documentElement.scrollLeft ?  document.documentElement.scrollLeft : document.body.scrollLeft); 
	else return null;
}

function mouseY(evt) {
	if (evt.pageY) 
	  return evt.pageY; 
	else if (evt.clientY)
	  return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop); 
	else return null;
}

function getMouseXY(evt) {
	if( !evt )
	  evt = window.event;
	mousex = mouseX(evt);
	mousey = mouseY(evt);
}

function showCalendarEvtDIV(event) {
	var div = $("calendarEvtDIV");

	posx = mousex;
	posy = mousey;
	
	div.style.display = '';
	div.style.position = 'absolute';
	div.style.zIndex = '2000';
	
	var cal = $("calendarView");
	if( posx > (cal.offsetWidth - div.offsetWidth) )
	  posx -= div.offsetWidth;
	if( posy > (cal.offsetHeight - div.offsetHeight))
	  posy -= div.offsetHeight;
	div.style.left = posx + 'px';
	div.style.top  = posy + 'px';
}

function hideEvtDIV() {
	var div = $("calendarEvtDIV");
	div.innerHTML = "&nbsp;";
	div.style.display = 'none';
}

function populateEventsListDiv(year,month,day,calendarId,el) {
	el.style.backgroundColor = '#9CF';
	var eventId = 0;
	var obj = new eventData(eventId,year,month,day);
	var d = new Date();
	var t = d.getTime();
	obj.getData(t,el,calendarId);	
}

function showEventListDiv(el) {
	if(!trim($("calendarEvtDIV").innerHTML)) {
		hideEvtDIV();
		return false;
	}
	//el.style.backgroundColor = '#9CF';
	var div = $("calendarEvtDIV");
	posx = mousex;
	posy = mousey;
	
	div.style.display = '';
	div.style.position = 'absolute';
	div.style.zIndex = '2000';
	
	var cal = $("minicalAddonContainer");
	if( posx > (cal.offsetWidth - div.offsetWidth) )
	  posx -= div.offsetWidth;
	if( posy > (cal.offsetHeight - div.offsetHeight))
	  posy -= div.offsetHeight;  
	div.style.left = posx + 'px';
	div.style.top  = posy + 'px';
	
}

var eventData = Class.create ({
	initialize: function (eventId,year,month,day) {
		this.eventId = eventId;
		this.url = "ajax/calendar/event_data.php";
		this.query = "year=" + year + "&month=" + month + "&day=" + day;
		//this.divId = $("calendarEvtDIV");
		//this.element = $('calendarEvtDIV');
	},
	getData: function(t,el,calendarId) {
		//$('calendarEvtDIV').innerHTML = "&nbsp;";
		this.query += this.query + "&event_id=" +  this.eventId + "&calendar_superset_id=" + calendarId + "&t=" + t;
		var opt = {
	    method: "get",
	    parameters: this.query,
	    asynchronous: false
    	};
  		var ajx = new Ajax.Request(this.url, opt);
		if($("calendarEvtDIV")) {
			if(ajx.transport.responseText != -1) {
				$("calendarEvtDIV").innerHTML = ajx.transport.responseText;
				showEventListDiv(el);
			}
			else {
				hideEvtDIV();
			}
		}

	},
	getDataAsync:function(t) {
		this.url +=  "?" + this.query + "&event_id=" +  this.eventId + "&t=" + t;
		
		new Ajax.Request(this.url, {
  			method: 'get',
 			onSuccess: function(transport) {
	    		if (transport.responseText) {
	      			$("calendarEvtDIV").innerHTML = transport.responseText;
	      			//showCalendarEvtDIV();
	    		}
 			}
		});
	   //return true;
	}
	
	
});

function setEventData(eventId,year,month,day) {
	if(!day)
		var day = 0;
	var obj = new eventData(eventId,year,month,day);
	var d = new Date();
	var t = d.getTime();
	obj.getDataAsync(t);	
}

function loadMore(date,year,month,calendarId) {
	var data = "action=popup&date=" + date + "&year=" + year + "&month=" + month + "&calendar_superset_id=" + calendarId;
	var Adresse = "index.php?" + data;
	var win = window.open(Adresse, "MoreEvents", "resizable=1,scrollbars=yes,width=450,height=300,left=200,top=100");
	win.focus();
}

function showEvent(evtId,year,month,calendarId) {
	var par = window.opener.document;
	par.location.href = "index.php?action=openevent&event_id=" + evtId + "&year="+year+"&month=" + month + "&calendar_superset_id=" + calendarId;
	this.window.close();
}

/*function openWin(eventId) {
	
}*/

function winOpen(url,name,width,height) {
	var win = window.open (
	url,
	name, 					// name
	"toolbar=0" 			// tool bar 
	+",dependent=1"			// dependent
	+",location=0" 			// address bar
	+",directories=0" 		// additional bars
	+",status=1" 			// status bar
	+",menubar=0" 			// menu bar
	+",scrollbars=0" 		// scroll bars
	+",resizable=1" 		// window resizable
	+",width=" + width 		// window width in pixels
	+",height=" + height 	// window height in pixels
	);
	/*if(parent.document) {
		parent.scroll(0,0);
	}*/
	win.focus();
}

function winOpenUpcoming(url,name,width,height) {
	var win = window.open (
	url,
	name, 					// name
	"toolbar=0" 			// tool bar 
	+",dependent=1"			// dependent
	+",location=0" 			// address bar
	+",directories=0" 		// additional bars
	+",status=1" 			// status bar
	+",menubar=0" 			// menu bar
	+",scrollbars=1" 		// scroll bars
	+",resizable=1" 		// window resizable
	+",width=" + width 		// window width in pixels
	+",height=" + height 	// window height in pixels
	);
	/*if(parent.document) {
		parent.scroll(0,0);
	}*/
	win.focus();
}

var openDTHMMObj = Class.create({
	initialize: function (windowCreateObj,windowId) {
		this.windowId = windowId;
		this.windowCreateObj = windowCreateObj;
		this.windowHandler;
		this.pageTitle;
		this.eventId;
		this.centerOnScreen = false;
		this.modalState = false;
		this.keepInViewPort = true;
	},
	closeWindow: function () {
		var isWind = this.windowCreateObj.isWindow(this.windowId);
		if(isWind === true) {
   	  		this.windowCreateObj.window(windowId).close();
    	}
	},
	openWindow: function(pageTitle,eventId) {
		this.eventId = eventId;
		this.pageTitle = pageTitle;
		this.windowHandler = this.windowCreateObj.createWindow(this.windowId, 0, 0, 460, 550); 
		this.windowHandler.setText(this.pageTitle);
		if(this.centerOnScreen === true)
			this.windowHandler.centerOnScreen();
		this.windowHandler.setModal(this.modalState);
		this.windowHandler.keepInViewport(this.keepInViewPort);
		var dat = new Date();
		var t = dat.getTime();
		var url = "ajax/calendar/newWindow.php?event_id=" + this.eventId + "&time=" + t;
		this.windowHandler.attachURL(url);
		
	},
	centWinInParDoc: function() {
		
	}
});
function openDHTM(eventId,pageTitle) { 
	hideEvtDIV();
    var obj = new openDTHMMObj(dhxWins,windowId);
    obj.closeWindow();
    obj.openWindow(pageTitle,eventId);
   
}

function openDHTMParent(eventId,pageTitle) {
	opener.openDHTM(eventId,pageTitle);
	opener.hideEvtDIV();
	self.close();
	
}

function nextSethOfMonths(month,year,calendarId) {
	//alert(month)
	var url = "ajax/calendar/three_months.php";
	var query_str = "month=" + month + "&year=" + year + "&calendar_superset_id=" + calendarId;
	var opt = {
	    method: "get",
	    parameters: query_str,
	    asynchronous: false
    };
  	var obj = new Ajax.Request(url, opt);
  	//alert(obj.transport.responseText);
  	$('minicalAddonContainer').innerHTML = '<table width="100%">' + obj.transport.responseText + '</table>';	
}

function trim(s) {
	return s.replace(/^\s+|\s+$/g, "");
}

function addEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, false); 
   return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
 } else { 
   return false; 
 } 
}

function dsbld(sel){
	for (var i=0; i<sel.options.length; i++) {
		if (sel.options[i].disabled) sel.options[i].selected=false;
	} 
}

function colorDisabled(obj) {
	for (var i=0; i<obj.options.length; i++) {
		if (obj.options[i].disabled) obj.options[i].style.color="InactiveCaptionText";
	}
}

