/* $Id: lib.js,v 1.33 2011/02/10 19:22:26 bob Exp $ */

// Splintered striper 1.3
// reworking of Zebra Tables and similar methods which works not only for tables and even/odd rows,
// but as a general DOM means of assigning any number of classes to children of a parent element.
// Patrick H. Lauke aka redux / www.splintered.co.uk
// Distributed under the Creative Commons Attribution-ShareAlike license - http://creativecommons.org/licenses/by-sa/2.0/

// Converted over to using Prototype to allow for nested lists
// by Robert Bottomley, UC Riverside.

/*
 * What we want striped.
 */
function striperLoadEvent() {
/*           Parent							Child
 *           Selector						Classes */
	striper ('table.data tbody.striped',	'odd,.');
	striper ('table.data2 tbody.striped',	'odd,.');
	striper ('ul.striped',					'odd,.');
	striper ('ul.big_links',				'.');
	striper ('ol.striped',					'odd,.');
	striper ('dl.striped',					'odd,.');
	striper ('table.greenbar tbody.striped','odd,odd,.,.');
}

var striperIncOnNonblankOnly = false; // default is to increment striping on each line

/*
 * Summary:      Function that applies any number of classes to all child elements
 *               contained in all occurrences of a parent element (either with or without a specific class)
 * Parameters:   parentSelector - parent selector (same as CSS selectors)
 *               styleClasses - comma separated list of any number of style classes (using 2 classes gives the classic "zebra" effect)
 * Globals:      striperIncOnNonblankOnly - defaults to FALSE; increment the style when first column is non-blank only.
 * Return:       none
 */
function striper(parentSelector, styleClasses)
{
	var i=0, currentParent, currentChild;

	// capability and sanity check
	if ((document.getElementsByTagName) && (parentSelector) && (styleClasses)) {
		// turn the comma separate list of classes into an array
		var styles = styleClasses.split(',');

		// get an array of all parent elements
		var parentItems = $$(parentSelector);

		// loop through all parent elements
		parentItems.each(function(currentParent) {
			var j=0, k=0;

			// get all child elements of the current parent element
			var childItems = currentParent.childElements();

			// loop through all child elements
			childItems.each(function(currentChild) {
				// based on the current element and the number of styles in the array, work out which class to apply
				if (striperIncOnNonblankOnly) {
					if ($(currentChild).firstDescendant().innerHTML != '&nbsp;') {
						j++;
					}
				} else {
					j++;
				}

				k = (j+(styles.length-1)) % styles.length;

				if (styles[k] != '.') {
					// add the class to the child element
					$(currentChild).addClassName(styles[k]);
				}

				// add mouseover rules for hover effect
				currentChild.onmouseover = function() {
					$(this).addClassName('ruled');
				}

				currentChild.onmouseout = function() {
					$(this).removeClassName('ruled');
				}
			});
		});
	}
}

// Activate Striper.
document.observe ('dom:loaded', striperLoadEvent);


/*
 * Toggles the visibility of element of class xpander using the blind effect.
 */

function toggleXpander(item) {
	new Effect.toggle($($($(item).next('.xpander')).identify()),'blind');
	item.toggleClassName('xpander-on');
}


// Look for elements of class "xpander" and hide them, then add a "Show"
// button before them.
// Look for elements of class "this-xpander" and hide them, then add an
// observer to the element before (it will be the control).
//
document.observe('dom:loaded', function () {
	$$('.xpander').each(function(item) {
		item.hide();
		item.insert({before: '<a class="xpander-control" href="#" onclick="javascript:toggleXpander(this); return false" title="toggle view"></a>\n'});
	});

	$$('.this-xpander').each(function(item) {
		item.hide();
		control = item.previous();
		control.on('click', function(event) {
			new Effect.toggle($($($(this).next()).identify()),'blind');
			this.toggleClassName('this-xpander-on');
			event.stop();
		});
		control.update('<span class="accessibility">'+control.innerHTML+'</span><a title="toggle view" href="#"></a>');
	});
});

/*
 * Toggle the visibility of margin notes (class="marginalia").
 */
function toggleNotes() {
	$$('.marginalia').each(function(item) {
		Effect.toggle(item, 'appear');
	});
}

/*
 * AJAX call to get calendar info from events.ucr.edu.
 */
function requestCalInfo (id, params) {
    var options = {
        method: 'get',
        parameters: params,
        onFailure: function(XHR) {$(id).innerHTML = '<div class="error"><p>An error occurred during loading: '+XHR.statusText+'.</p></div>'}
        //onComplete: function(XHR) {$$(".tip").each(function(item) {new Tooltip(item);});}
    };
    var myAjax = new Ajax.Updater({success: id}, "/calendar.php", options);
}

/*
 * If site uses async Google Analytics, let's capture some stuff.
 */
var _gaq = _gaq || [];

function trackDownloads(selector) {
	$$(selector).each(function(item) {
		item.on('click',function(event) {
			_gaq.push(['_trackEvent', 'Documents', 'Download', this.href]);
		});
	});
}

// Once the document is loaded, track the downloading of various file types.
document.observe ('dom:loaded', function(){
	trackDownloads('a[href$="pdf"]');
	trackDownloads('a[href$="doc"]');
	trackDownloads('a[href$="xls"]');
	trackDownloads('a[href$="ppt"]');
	trackDownloads('a[href$="vsd"]');
	trackDownloads('a[href$="zip"]');
	trackDownloads('a[href$="mov"]');
	trackDownloads('a[href$="jpg"]');
	trackDownloads('a[href$="png"]');
	trackDownloads('a[href$="gif"]');
});

// Tooltip code removed until we get it working in IE8.


