/*
 * Variables used later
 * - Set here so they are easy to configure / find
 */
var DEFAULT_WINDOW_WIDTH = 800;
var DEFAULT_WINDOW_HEIGHT = 500;
var HOWTO_GUIDES_WINDOW_WIDTH = 700;
var HOWTO_GUIDES_WINDOW_HEIGHT = 599;
var MEAT_CUTS_WINDOW_WIDTH = 800;
var MEAT_CUTS_WINDOW_HEIGHT = 599;
var NEWS_WINDOW_WIDTH = 800;
var NEWS_WINDOW_HEIGHT = 522;



/*
 * Generic "Find" used for mouse events
 */
function find_target(e) {
	var target;
	if(window.event  &&  window.event.srcElement) {
		target = window.event.srcElement;
	} else if(e  &&  e.target) {
		target = e.target;
	}
	if(!target) {
		return null;
	}
	return target;
}



/*
 * Mouseover / Out listener
 * - update status bar when mouseover / off
 */
function monitor_all_links() {
  // Get all of the links & inputs within the selected page
	var all_mouseovers = $$("a", "input");
  // Assign a listener to each link
	all_mouseovers.each(
		function(val) {
		  // If we have an input make sure it is a submit / reset / button or image
			if(val.type  &&  val.type != "submit"  &&  val.type != "reset"  &&  val.type != "button"  &&  val.type != "image") {
			  // This is not a required input type
				return;
			}
			Event.observe(val, "mouseover", title_in_status_bar_over);
			Event.observe(val, "mouseout", title_in_status_bar_out);
		  // If this is a link, should it open in a new popup window ('target' is defined)?
			if(!val.target  ||  val.target == '') {
				return;
			}
		  // As this item is going to open in a new window, we need to update the title text as well
			Event.observe(val, "click", new_popup_window);
		}
	);
}
function fake_and_return_title(obj) {
  // Update the status bar of the window, after checking we have a 'title' to display
  // - If no 'title' was found we will try to create one
	if(obj.title) {
		return obj.title;
	} else if(obj.parentElement  &&  obj.parentElement.title) {
		return obj.parentElement.title;
	} else if(obj.parentNode  &&  obj.parentNode.title) {
		return obj.parentNode.title;
	} else if(obj.innerHTML) {
	  // Make sure we strip out any HTML content
		var new_title = obj.innerHTML.replace(/<[\/\!]*?[^<>]*?>/ig, '');
		return new_title;
	} else if(obj.type  &&  (obj.type == "submit"  ||  obj.type == "reset"  ||  obj.type == "button")  &&  obj.value) {
		var new_title = obj.value;
		if(actually_overwrite_title_value) {
			obj.title = new_title;
		}
		return new_title;
	} else if(obj.type  &&  obj.type == "image") {
		var new_title = obj.alt;
		if(actually_overwrite_title_value) {
			obj.title = new_title;
		}
		return new_title;
	} else if(obj.alt) {
		return obj.alt;
	}
	return "";
}
function wS(txt) {
	window.status = ((txt)?txt:'');
	return true;
}
function title_in_status_bar_over(e) {
  // I want to make sure everything is fully loaded before this code is run
	if(typeof find_target == "undefined") {
		return;
	}
	var target = find_target(e);
	if(!target) {
		return;
	}
	var title_attribute = fake_and_return_title(target);
	return wS(title_attribute);
}
function title_in_status_bar_out(e) {
  // I want to make sure everything is fully loaded before this code is run
	if(typeof find_target == "undefined") {
		return;
	}
	return wS();
}
function new_popup_window(e) {
	var target = find_target(e);
	if(!target  ||  !target.href  ||  target.href.indexOf(".gif") != -1  ||  target.href.indexOf(".jpg") != -1  ||  target.href.indexOf(".png") != -1) {
	  // Check to see if this is returning an image so a HREF does not exist
		if(target  &&  (target.tagName.toLowerCase() == "img"  ||  target.href.indexOf(".gif") != -1  ||  target.href.indexOf(".jpg") != -1  ||  target.href.indexOf(".png") != -1)  &&  target.parentNode) {
		  // As an image was found, the link should be the parent element
			target = target.parentNode;
		}
	  // Double-check before running the non-JavaScript version
		if(!target  ||  !target.href) {
		  // No 'target' or URL was found
			return;
		}
	}
	if(!target.href  &&  target.parentNode) {
	  // We've probably got an image reference here (child)!
		target = target.parentNode;
	}
	if(target.href) {
		if(target.target  ==  'howtoguide') {
			var popup_width = HOWTO_GUIDES_WINDOW_WIDTH;
			var popup_height = HOWTO_GUIDES_WINDOW_HEIGHT;
			var toolbar_visibility = 'no';
			var menubar_visibility = 'no';
			var location_visibility = 'no';
			var scrollbar_visibility = 'yes';
			var popup_url = target.href;
		} else if(target.target.indexOf('meatcut') != -1) {
			var popup_width = MEAT_CUTS_WINDOW_WIDTH;
			var popup_height = MEAT_CUTS_WINDOW_HEIGHT;
			var toolbar_visibility = 'no';
			var menubar_visibility = 'no';
			var location_visibility = 'no';
			var scrollbar_visibility = 'no';
			var popup_url = '/image.php?img=' + target.href;
		} else if(target.target.indexOf('newsimage') != -1) {
			var popup_width = NEWS_WINDOW_WIDTH;
			var popup_height = NEWS_WINDOW_HEIGHT;
			var toolbar_visibility = 'no';
			var menubar_visibility = 'no';
			var location_visibility = 'no';
			var scrollbar_visibility = 'no';
			var popup_url = '/image.php?img=' + target.href;
		} else {
			var popup_width = DEFAULT_WINDOW_WIDTH;
			var popup_height = DEFAULT_WINDOW_HEIGHT;
			var toolbar_visibility = 'yes';
			var menubar_visibility = 'yes';
			var location_visibility = 'yes';
			var scrollbar_visibility = 'yes';
			var popup_url = target.href;
		}
		var params =
			'width=' + popup_width +
			',height=' + popup_height +
			',left=' + ((screen.width)?(screen.width-popup_width)/2:0) +
			',top=' + ((screen.height)?((screen.height-popup_height)/2)-30:0) +
			',scrollbars=' + scrollbar_visibility +
			',toolbar=' + toolbar_visibility +
			',resizable=yes' +
			',status=yes' +
			',menubar=' + menubar_visibility +
			',location=' + location_visibility;
		var popupName = window.open(popup_url, target.target, params);
		if(popupName) {
			popupName.focus();
			Event.stop(e);
		}
	}
}



Event.observe(window, "load", monitor_all_links);