// For all the code that needs to run when the page is loaded.
function add_onload(func) {
	var onload = window.onload;
	if (typeof(onload) != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			onload();
			func();
		}
	}
}

// Shrinks all 'external-image' images that would break the site.
function shrink_big_images () {
	var max_image_size = 640; // Maximum image size.  720?
	var external_images = document.getElementsByTagName('img');
	var str = '';
	for (var i = 0; i < external_images.length; i++) {
		if (external_images[i].className == 'external-image') {
			if (external_images[i].width > max_image_size) {
				external_images[i].width = max_image_size;
			}
		}
	}
}

add_onload(shrink_big_images);

// Useful JS functions that were created because IE and prototype don't get along
function set_size (id, width, height) {
	$(id).style.width = width + 'px';
	$(id).style.height = height + 'px';
}
function hide(id) {
	$(id).style.display = 'none';
}
function show(id) {
	$(id).style.display = '';
}

// Tags related JS
// Remove filter hovers
function show_remove(obj) {
	obj.childNodes[obj.childNodes.length-1].style.display = '';
}
function hide_remove(obj) {
	obj.childNodes[obj.childNodes.length-1].style.display = 'none';
}

// Alerts (in the header)
function Alerts(total) {
	this.current_page = 1;
	this.last_page = Math.ceil(total / 4);
	if (this.current_page == this.last_page) {
		$('alert-next-button').className = 'alert-next-inactive';
	}
}
Alerts.prototype.next = function () {
	if (this.current_page == this.last_page) {
		return;
	}
	new Effect.BlindUp('alerts-' + this.current_page, { scaleFrom:80, duration:0.4, queue:{ position:'end', scope:'alerts' }});
	new Effect.BlindDown('alerts-' + (this.current_page + 1), { scaleTo:80, duration:0.4, queue:{ position:'end', scope:'alerts' }});
	this.current_page++;
	if (this.current_page > 1) {
		$('alert-prev-button').className = 'alert-prev';
	}
	if (this.current_page == this.last_page) {
		$('alert-next-button').className = 'alert-next-inactive';
	}
}
Alerts.prototype.previous = function () {
	if (this.current_page == 1) {
		return;
	}
	new Effect.BlindUp('alerts-' + this.current_page, { scaleFrom:80, duration:0.4, queue:{ position:'end', scope:'alerts' }});
	new Effect.BlindDown('alerts-' + (this.current_page - 1), { scaleTo:80, duration:0.4, queue:{ position:'end', scope:'alerts' }});
	this.current_page--;
	if (this.current_page == 1) {
		$('alert-prev-button').className = 'alert-prev-inactive';
	}
	if (this.current_page < this.last_page) {
		$('alert-next-button').className = 'alert-next';
	}
}

// check uncheck radio boxes
var checkflag = "false";
function check(field_name){
	var field = document.getElementsByName(field_name);	
	if (checkflag == "false") {
		for (i = 0; i < field.length; i++){
			field[i].checked = true;
		}
			checkflag = "true";
			return "Uncheck All"; 
		}else{
		for (i = 0; i < field.length; i++){
			field[i].checked = false;
		}
			checkflag = "false";
			return "Check All"; 
		}
}

function post_install_global() {
	var agent = navigator.userAgent.toLowerCase();
	if (agent.indexOf('msie') == -1) {
		navigator.plugins.refresh(true);
	}
	history.go(-1);
}
