var getElementsByClassName = function (className, tag, elm){
	if (document.getElementsByClassName) {
		getElementsByClassName = function (className, tag, elm) {
			elm = elm || document;
			var elements = elm.getElementsByClassName(className),
				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
			for(var i=0, il=elements.length; i<il; i+=1){
				current = elements[i];
				if(!nodeName || nodeName.test(current.nodeName)) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	else if (document.evaluate) {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
			for(var j=0, jl=classes.length; j<jl; j+=1){
				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
			}
			try	{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
			}
			catch (e) {
				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
			}
			while ((node = elements.iterateNext())) {
				returnElements.push(node);
			}
			return returnElements;
		};
	}
	else {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
			for(var k=0, kl=classes.length; k<kl; k+=1){
				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
			}
			for(var l=0, ll=elements.length; l<ll; l+=1){
				current = elements[l];
				match = false;
				for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
					match = classesToCheck[m].test(current.className);
					if (!match) {
						break;
					}
				}
				if (match) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	return getElementsByClassName(className, tag, elm);
};

// Calendar code
var currentPage = 1;
var currentDateStamp = '';
var newEl;
// scriptaculous animation lock		
var lock = 0;
function activateLock() {
	lock = 1;
}
function releaseLock() {
	lock = 0;
}


function display_availability_info(date_stamp, date_string, clac_class) {
	if (lock != 1) {
		// initialize global variables
		currentDateStamp = date_stamp;
		currentPage = 1;
		
		// setup pointers
		var popup_box = document.getElementById('calc_popup_availability');
		var popup_date = document.getElementById('calc_popup_date');
		var popup_options = document.getElementById('calc_popup_options');
		var day_box = document.getElementById('day_'+date_stamp);
		
		if (popup_date) {
			popup_date.innerHTML = date_string;
		}
		if (popup_options) {
			var cur_date_options = document.getElementById(currentDateStamp+'-1');
			//alert(cur_date_options.innerHTML);
			if (cur_date_options) {
				popup_options.innerHTML = cur_date_options.innerHTML;
			}
			//alert(popup_options.innerHTML);
		}
		
		// Unselect all the other days, and select the clicked date
		var selected = getElementsByClassName('selected');
		for (var i=0; i<selected.length; i++) {
			selected[i].className = clac_class;
		}
		
		if (day_box) {
			day_box.className = "selected "+clac_class;
		}
		
		// Activate Block
		
		Effect.Appear("calc_popup_availability", {duration: 0.3, beforeStart: activateLock});
		new Effect.Highlight("calc_popup_date", {startcolor: '#666666', endcolor: '#93b0ca', duration: 0.5, afterFinish: releaseLock});
		
	}
}

function prevOptionsPage() {
	newPage = currentPage-1;
	var new_date_options = document.getElementById(currentDateStamp+'-'+newPage);
	var oldEl = document.getElementById('calc_popup_options');

	if (new_date_options && lock != 1) {
		newEl = document.getElementById(currentDateStamp+'-'+newPage);

		new Effect.Fade(oldEl,{
			duration: 0.5,
			beforeStart: activateLock,
			afterFinish: update_options
		});
		
		currentPage--;
		new Effect.BlindDown(oldEl, {
			duration: 1,
			queue: 'end',
			afterFinish: releaseLock
		});
	}
	return;
}

function nextOptionsPage() {
	newPage = currentPage+1;
	var new_date_options = document.getElementById(currentDateStamp+'-'+newPage);
	var oldEl = document.getElementById('calc_popup_options');

	if (new_date_options && lock != 1) {
		newEl = document.getElementById(currentDateStamp+'-'+newPage);

		new Effect.Fade(oldEl,{
			duration: 0.5,
			beforeStart: activateLock,
			afterFinish: update_options
		});
		
		currentPage++;
		new Effect.BlindDown(oldEl, {
			duration: 1,
			queue: 'end',
			afterFinish: releaseLock
		});
	}
	return;
}
function update_options() {
	var popup_options = document.getElementById('calc_popup_options');
	if (popup_options) {
		popup_options.innerHTML = newEl.innerHTML;
	}
}