// =============================================================================
// = CCE Sitewide JS File - Used for zebra stripes, sorting initialization etc =
// =============================================================================

Event.observe(window, 'load', function(){

	$$('#content ul, #content table, #sitemap, #links ').each(function(parent) {
		parent.getElementsBySelector('tr, .links_wrapper li, .sitemap > li').each(function(row, index) {

			// Toggle odd class for zebra striping
			if(index % 2 == 0){
				row.addClassName('odd');
			}
			// Add "first-cell" class to emulate first-child for IE
			row.getElementsBySelector('td:first-child').each(function(cell) {
				cell.addClassName('first-cell');
			});
			
			// Add "last-cell" class to emulate last-child for IE
			row.getElementsBySelector('td:last-child').each(function(cell) {
				cell.addClassName('last-cell');
				
				// Add mailtos for email links
				if(cell.innerHTML.indexOf('@') > 0){
					email = cell.innerHTML;
					cell.innerHTML = "<a href='mailto: "+email+"'>"+email+"</a>";
				}
			});
			
		});
	});
	
	// Add numbered classes for widths in CSS
	$$('#content table tr:first-child').each(function(tr) {
		tr.getElementsBySelector('td').each(function(td, index) {
			num = index + 1;
			td.addClassName('cell_'+num);
		});
	});
	
	// IE FIXES
	//if (Prototype.Browser.IE) {
	// =================================================
	// = Various Class Replacements for CSS2 Selectors =
	// =================================================
		$$('#sidenav > li').each(function(li) {
			li.addClassName('top_item');
		});
		
		$$('#sidenav li:last-child a').each(function(a) {
			a.addClassName('last_item_anchor');
		});
		
		$$('#sidenav > li > a').each(function(elem) {
			elem.addClassName('top_item_anchor');
		});
		
		$$('#sidenav .current > a').each(function(elem) {
			elem.addClassName('current_anchor');
		});
		
		$$('#sidenav .current > span').each(function(elem) {
			elem.addClassName('current_span');
		});
		
		$$('#map + h2').each(function(elem) {
			elem.addClassName('map_heading');
		});
		
		$$('#map + h2 + p').each(function(elem) {
			elem.addClassName('map_paragraph');
		});
		
		$$('#map + h2 + p + hr').each(function(elem) {
			elem.addClassName('map_rule');
		});
		
	// }
	
	$$('a').each(function(anch) {

		function is_external(anch){
			var hostname = window.location.hostname;
			hostname = hostname.replace("www.","").toLowerCase();
			var href = anch.href.toLowerCase();
			return (href.indexOf("http")!=-1 && href.indexOf(hostname)==-1) ? true : false;
		}
		
		function is_pdf (anch) {
			var href = anch.href.toLowerCase();
			return href.indexOf("pdf")!=-1;
		}

		if(is_external(anch) || is_pdf(anch)){
			anch.target = '_blank';
			anch.addClassName('external');
		}
		
	});
	
	
});