function get_obj(idname){if(document.getElementById){return document.getElementById(idname);}
else if(document.all){return document.all[idname];}
else if(document.layers){return document.layers[idname];}
else{return null;}}
function openEventWindow(id,dt) {newwindow=window.open('/oncampus/calendar/includes/event.php?id='+id+'&dt='+dt,'OnCampus_Event','scrollbars=yes,width=566,height=275');if (window.focus) { newwindow.focus() } return false; }
function expandDate(id,image,anchor){var img=get_obj(image);var anchor=get_obj(anchor);var objStyle=get_obj(id);var headerid=id.replace("date_","date_header_");var objHeader=get_obj(headerid);var class_init=objHeader.className;var headerExp="date_header_expand";
if(img.src.match("expand.gif") && expandDate.arguments[3] != 'c'){img.src="/oncampus/calendar/images/collapse.gif";img.alt="Collapse";anchor.title="Click to Collapse";}
else if (img.src.match("collapse.gif") && expandDate.arguments[3] != 'e'){img.src="/oncampus/calendar/images/expand.gif";img.alt="Expand";anchor.title="Click to Expand";}
if(objStyle.style.display=="block" && expandDate.arguments[3] != 'e'){jQuery("#"+id).slideUp("fast"); objHeader.className=objHeader.className.replace(headerExp,"date_header");}
else if (objStyle.style.display=="none" && expandDate.arguments[3] != 'c'){jQuery("#"+id).slideDown("fast");if(objHeader.className!=headerExp)
objHeader.className=objHeader.className.replace("date_header",headerExp);} return false;}

function expandAllDates() {
	expandall = (readCookie('expandall') == 1) ? 0 : 1;
	createCookie('expandall',expandall,365);
	state = expandall ? 'e' : 'c';
	var daysEvents = getElementsByClass("event_body",document.getElementById('topevents_content'));

	for (dateclick = 0; dateclick < daysEvents.length; dateclick++) {
		expandDate(daysEvents[dateclick].id,daysEvents[dateclick].id.replace(/date/,"img"),daysEvents[dateclick].id.replace(/date/,"anchor"),state);
	}

	var newobj = document.getElementById("exp_all");
	if (newobj.innerHTML.indexOf("Expand") != -1) { newobj.innerHTML = "Collapse all events"; }
	else { newobj.innerHTML = "Expand all events"; }
}

function createCookie(name,value,days) {
if (days) {
 var date = new Date();
 date.setTime(date.getTime()+(days*24*60*60*1000));
 var expires = "; expires="+date.toGMTString();
 }
 else expires = "";
 document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
 var nameEQ = name + "=";
 var ca = document.cookie.split(';');
 for(var i=0;i < ca.length;i++) {
 var c = ca[i];
 while (c.charAt(0)==' ') c = c.substring(1,c.length);
 if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
 }
 return null;
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

/* "Rate this article" code is below */

if (typeof(jQuery) == "function") {
jQuery(document).ready(function(){
	if (document.getElementById('rac_comment') ) {
		jQuery("#rac_comment").val('');
		jQuery("#rac_cancel").click(function() {
			jQuery("div.rac_helpful").unbind('mouseout');
			jQuery("div.rac_helpful").mouseout(function() { rac_revert(); });
			rac_hide_comment(1);		
			rac_revert();
		});

		rac_setup_binds();

		jQuery('#rac_improve').submit(function() {
			rac_inputs = [];
			jQuery(':input', this).each(function() {
				rac_inputs.push(this.name + '=' + escape(this.value));
			})
			jQuery.ajax({
				data: rac_inputs.join('&'),
				url: this.action,
				type: "POST",
				timeout: 2000,
				error: function() {
					console.log("Failed to submit your comment.");
				},
				success: function(r) { rac_ajax_do(r); }
			}) 
			return false;
		})
	}
});

function rac_setup_binds() {
	rac_hide_comment(1);
	jQuery("div.rac_helpful").children().unbind();
	jQuery("div.rac_helpful").mouseout(function() {
		rac_revert();
	});

	var rac_arr = [ "1", "2", "3", "4", "5" ];

	jQuery.each(rac_arr, function() {
		var rac_s_val = this;
		jQuery("div.rac_helpful a#rac_star" + this).mouseover(function() {
			for (i = 1; i <= 5; i++) {
jQuery("div.rac_helpful a#rac_star" + i + " img").
attr('src',jQuery("div.rac_helpful a#rac_star" + i + " img").attr('src').
replace(
(rac_s_val >= i ? 'fade' : 'full'), 
(rac_s_val >= i ? 'full' : 'fade') ).
replace('half', (rac_s_val >= i ? 'full' : 'fade'))
);
			}
		}).mouseout(function() {
			for (var i = 5; i >= rac_s_val; i--) {
jQuery("div.rac_helpful a#rac_star" + i +" img").
attr('src',jQuery("div.rac_helpful a#rac_star" + i +" img").
attr('src').replace('full','fade').replace('half', 'fade') );
			}
		});
		if (this <= 2) {
			jQuery("div.rac_helpful a#rac_star" + this).click(function(){
				rac_hide_comment(0);
				jQuery("#rac_star_val").val(jQuery(this).attr("id").substr(8,1) );
				rac_destroy_binds(rac_s_val);
				return false;
			});
		} else if (this > 2) {
			var rac_inputs = [];
			jQuery("div.rac_helpful a#rac_star" + this).click(function() {
			rac_destroy_binds(rac_s_val);
			jQuery("#rac_star_val").val(jQuery(this).attr("id").substr(8,1));
			jQuery(':input', jQuery('#rac_improve')).each(function() {
				rac_inputs.push(this.name + '=' + escape(this.value));
			})
			jQuery.ajax({
				data: rac_inputs.join('&'),
				url: jQuery('#rac_improve').attr('action'),
				type: "POST",
				timeout: 2000,
				error: function() {
					console.log("Failed to submit your comment.");
					},
				success: function(r) { rac_ajax_do(r); }
			});
			return false;
			});
		}
	});
}

function rac_destroy_binds(s_val) {
	jQuery("div.rac_helpful").mouseout(function() { rac_revert(s_val); });
	jQuery("div.rac_helpful a#rac_star" + s_val).unbind('mouseout');
	rac_revert(s_val);
}

function rac_hide_comment(rac_do) {
	jQuery("#rac_improve").css('visibility',(rac_do ? 'hidden' : 'visible'));
}

function rac_revert(cur_star) {
	if (typeof(cur_star) == "undefined") { cur_star = jQuery("#rac_avg_star").val(); }
	for (var i = 1; i <= 5; i++) {
		jQuery("div.rac_helpful a#rac_star" + i +" img").
			attr('src', jQuery("div.rac_helpful a#rac_star" + i +" img").
			attr('src').replace(
			(i <= cur_star ? 'fade' : 'full'),
			(i <= cur_star ? 'full' : 'fade')).
			replace('half', (cur_star >= i ? 'full' : 'fade'))
		);
		if (cur_star >= (i-.75) && cur_star < (i-.25)) {
			jQuery("div.rac_helpful a#rac_star" + i +" img").
			attr('src', 
				jQuery("div.rac_helpful a#rac_star" + i +" img").
				attr('src').replace( (i <= cur_star ? 'fade' : 'full'),
				(i <= cur_star ? 'full' : 'fade') ).
				replace( 'fade' , 'half')
			);
		}
	}
}

// This is the general AJAX response handler
function rac_ajax_do(stuff) {
	jQuery(stuff).find("id").each(function() {
		if (jQuery(this).text() != '0') {
			// If the id is -1 or 1, the submission was either
			// successful, or the person is somehow trying to
			// repost their rating.  Either way, the "rac_msg
			// gets swapped, and the JS is unbound.
			rac_hide_comment(1);
			jQuery("a[@id^='rac_star']").unbind();
			jQuery("div.rac_helpful").children().unbind();
			jQuery("#rac_msg").html(jQuery("msg",stuff).text());

			jQuery("div.rac_helpful").unbind();
			// Revert the star level to the average
			// Comment out the following line to keep the
			// star level at the user's rating level until the page
			// is re-loaded.
			// jQuery("div.rac_helpful").mouseout(function() { rac_revert(); });

		} else if (jQuery(this).text() == '0') {
			alert(jQuery("msg",stuff).text());
		}
	});
}

}
