
var activeCalendarFilterInputs = new Array();
var calendarTaxListCBPrefix = 'calendar_taxonomy_cb_';

$(function() {
	$('.view-data-node-title a').tooltip({
		fade: 250,
		showURL: false,
		bodyHandler: function() { 
			return $($(this).attr('body')).html();
		},
		extraClass: "fixed_width_tooltip"
	});

	// We need this line until the taxonomy stuff is working again
	return;


	$('#calendar_taxonomy_filter input').each(function(i) {
		cookieName = calendarTaxListCBPrefix + String(i);
		$(this).attr('cookie_name', cookieName);

		// We first determine if the cookie exists for this input
		// If so, we set the "checked" attribute to 1
//		if ($.cookie(cookieName) == "1" || $(this).attr('checked') == "1") {
			array_push(activeCalendarFilterInputs, $(this).val());
			$(this).attr('checked', '1');
//		} else {
//			$(this).removeAttr('checked');
//		}
	});

	toggle_events_by_taxonomy();

	$('#calendar_taxonomy_filter input').click(function() {
		// Get the name of the cookie for this particular input
		cookieName = $(this).attr('cookie_name');

		// Get the value of the clicked checkbox
		inputValue = $(this).val();

		// See if the checkboxes was checked or unchecked
		isChecked = ($(this).attr('checked')) ? true : false ;

		if (isChecked) {
			// Set a cookie so this one stays checked
			$.cookie(cookieName, '1');

			// Push the value onto the end of of the 'activeCalendarFilterInputs' array
			array_push(activeCalendarFilterInputs, inputValue);

			// Strip out duplicate entries in the array
			array_unique(activeCalendarFilterInputs);
		} else {
			// Remove the cookie to that subsequent page hits don't show this as checked
			$.cookie(cookieName, null);

			// Remove the value from the 'activeCalendarFilterInputs' array
			activeCalendarFilterInputs = remove_array_value(inputValue, activeCalendarFilterInputs);
		}

		// Now we'll show/hide the proper events
		toggle_events_by_taxonomy();
	});

});

function toggle_events_by_taxonomy() {
	// First, we'll hide all the inputs
	$('.monthview').hide();
	$('.monthview_no_taxonomy').show();

	// Finally, we'll loop through all the active inputs and set their visibility
	for (var i = 0; i < activeCalendarFilterInputs.length; i++) {
		$('.' + activeCalendarFilterInputs[i]).show();
	}
}

function in_array(needle, haystack, strict) {
	var found = false, key, strict = !!strict;

	for (key in haystack) {
		if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
			found = true;
			break;
		}
	}

	return found;
}

function array_push(array) {
    var i, argv = arguments, argc = argv.length;
 
    for (i=1; i < argc; i++) {
        array[array.length++] = argv[i];
    }
 
    return array.length;
}

function array_unique(array) {
	var p, i, j;
	for (i = array.length; i;) {
		for (p = --i; p > 0;) {
			if (array[i] === array[--p]) {
				for (j = p; --p && array[i] === array[p];);
				i -= array.splice(p + 1, j - p).length;
			}
		}
	}

	return true;
}

function remove_array_value(theValue, theArray) {
	var tmpArray = new Array();
	var rowCounter = 0;

	for (var i = 0; i < theArray.length; i++) {
		if (theArray[i] != theValue) {
			tmpArray[rowCounter] = theArray[i];
			rowCounter++;
		}
	}

	return tmpArray;
}

