/****
 ** This is used for the calendar on the homepage. When javascript is 
 ** enabled the calendar works with AJaX. If not, then it works with regular links.
 *****
 ** Author: GB - Green Valley
 ** Date: Apr 12 2011
 ****
*/
var cCalendar = {
	init: function (){
		if($('prevMonth') && $('nextMonth')){		
			$('calHideNoJS').style.display = 'none';
			$('prevMonth').onclick = $('nextMonth').onclick = function () {
				cCalendar.getMonth(this.href);
				return false;
			}
			var calDays = $('calTableBody').getElementsByClassName('calDay');
			for(var i=0; i<calDays.length; i++){
				calDays[i].onclick = function(){
					var yPos = 0;
					var curElem = this;
					while( curElem != null ){
						yPos += curElem.offsetTop;
						curElem = curElem.offsetParent;
					}
					
					var xPos = 0;
					var curElem = this;
					while( curElem != null ){
						xPos += curElem.offsetLeft;
						curElem = curElem.offsetParent;
					}
					
					cCalendar.getDay(this.href, xPos, yPos);
					return false;
				}
			}
		}
	},
	getMonth: function (url) {
		if($('homecalendar')){
			new Ajax.Request(url + '&call=ajax',
			{
				method:'get',
				onSuccess: function(transport){
					var response = transport.responseText || "no response text";
					$('homecalendar').innerHTML = response;
					cCalendar.init();
				},
				onFailure: function(){
					$('homecalendar').innerHTML = "An error occured. Please try again.";
				}
			});
		}
	},
	getDay: function (url, posX, posY){
		new Ajax.Request(url + '&call=ajax',
		{
			method:'get',
			onSuccess: function(transport){
				var response = transport.responseText || "no response text";
				cCalendar.showPopUp(response, posX, posY);
			},
			onFailure: function(){
				$('homecalendar').innerHTML = "An error occured. Please try again.";
			}
		});
	},
	showPopUp: function (content, posX, posY){
		
		if($('calPopUpBox') == null){
			var popupBox = new Element('div', { 'id': 'calPopUpBox'});
			$('homecalendar').insert({bottom: popupBox})
		}
		
		$('calPopUpBox').update(content);
		$('calPopUpBox').setStyle({
			  left: (posX - 435)+'px',
			  top: (posY - 2)+'px'
			});
		$('calPopUp_Close').observe('click', cCalendar.hidePopUp);
		if ( !$('calPopUpBox').visible() ) {
			$('calPopUpBox').show();
		}
		
	},
	hidePopUp: function (){
		$('calPopUpBox').hide();
	}
}
/****
 ** This is used for the calendar in the content area.
 *****
 ** Author: GB - Green Valley
 ** Date: Apr 28 2011
 ****
*/
var cCalenderContent = {
	init: function () {
		if($('c_c_calendaroverview')){
			var links = document.getElementsByClassName('c_c_calendarlink');
			for(var i = 0; i < links.length;i++){
				links[i].onclick = function () {
					cCalenderContent.toggleItem(this.id);
					return false;
				}
			}
			var currentitem = unescape(self.document.location.hash.substring(1));
			if(currentitem != ""){
				cCalenderContent.toggleItem("c_c_" + currentitem);
			}
		}
	},
	toggleItem: function (elemid) {
		if($(elemid + '_body').style.display == 'block'){
			this.hideItem(elemid);
		}else{
			this.showItem(elemid);
		}
	},
	showItem: function (elemId) {
		this.hideAll();
		$(elemId+'_body').style.display = 'block';
	},
	hideAll: function (){
		var bodyElements = document.getElementsByClassName('c_c_calendarbody');
		for(var i = 0; i < bodyElements.length;i++){
			bodyElements[i].style.display = 'none';
		}
	},
	hideItem: function (elemId){
		//$(elemId+'_body').style.display = 'none';
		Effect.BlindUp(elemId+'_body');
	}
}


/****
 ** Initialize the onloads.
 *****
 ** Author: GB - Green Valley
 ** Date: Apr 28 2011
 ****
*/
var inits = [
	'cCalendar',
	'cCalenderContent'
];

var init = new Boolean(false);
function initializeAll() {
	if(init){
		init=false;
		for (var i=0;i<inits.length;i++) {
			if (window[inits[i]])
				window[inits[i]].init();
		}
	}
}

/* for Mozilla/Opera9 */
if (document.addEventListener) {
	document.addEventListener("DOMContentLoaded", initializeAll, false);
}
/* for Safari */
if (/WebKit/i.test(navigator.userAgent)) { // sniff
	var _timer = setInterval(function() {
		if (/loaded|complete/.test(document.readyState)) {
			initializeAll(); // call the onload handler
		}
	}, 10);
}

/* for other browsers */
window.onload = initializeAll;
