var	mat_free_form_dd_disp = 'Other';

var mat_edit_profile = {
	fp_group_name: 'flow_pumps',
	fp_base_names: ['quantity', 'brand', 'model', 'gph'],
	fp_removing_child: false,
	
	me_group_name: 'monitoring_equipment',
	me_base_names: ['brand', 'model'],
	me_removing_child: false,
	
	li_group_name: 'lighting',
	li_base_names: ['system_type', 'ballast_brand', 'model', 'bulb_brand', 'bulb_temp', 'wattage'],
	li_wattages: {MH: ['70w', '150w', '175w', '250w', '400w', '1000w'], 
								T5: ['24w', '39w', '54w', '80w'], 
								PC: ['9w', '13w', '18w', '24w', '28/32w', '36w', '55w', '65w'], 
								VHO: ['75w', '95w', '110'],
								DIY: []},
	li_wattage_select_a_disp: 'Select Wattage',
	li_wattage_choose_a_disp: 'Choose System Type',
	li_removing_child: false,

	mat_toggle_free_form_input: function(event) {
		var self = mat_edit_profile;
		self.mat_toggle_specified_free_form_input(this);
	},
	
	mat_toggle_specified_free_form_input: function(input) {
		if (input && input.options) {
			var mat_free_form_input_other = document.getElementById(input.id + '_other');
			if (input.value == mat_free_form_dd_disp) {
				mat_free_form_input_other.disabled = false;
				mat_free_form_input_other.className = mat_free_form_input_other.className.replace(/hidden/g, '');
			} else {
				mat_free_form_input_other.disabled = true;
				mat_free_form_input_other.className += ' hidden';
			}
		}
	},
	
	add_flow_pump: function(event) {
		var self = mat_edit_profile;
		self.add_row('fp_removing_child', self.fp_group_name, self.fp_base_names, mat_edit_profile.remove_flow_pump);
	},
	
	remove_flow_pump: function(event) {
		var self = mat_edit_profile;		
		self.remove_row(event, this, 'fp_removing_child', self.fp_group_name, self.fp_base_names);
	},
	
	add_monitoring_equipment: function(event) {
		var self = mat_edit_profile;
		self.add_row('me_removing_child', self.me_group_name, self.me_base_names, mat_edit_profile.remove_monitoring_equipment);
	},
	
	remove_monitoring_equipment: function(event) {
		var self = mat_edit_profile;		
		self.remove_row(event, this, 'me_removing_child', self.me_group_name, self.me_base_names);
	},
	
	add_lighting: function(event) {
		var self = mat_edit_profile;
		if (!self.li_removing_child) { // TODO: may want to put some kind of sleep/wait here instead of dropping the request
			var lighting_body = document.getElementById(self.li_group_name);
			var li_system_types = document.getElementById('tank_lighting_0_system_type');
			var li_ballast_brands = document.getElementById('tank_lighting_0_ballast_brand');
			var li_bulb_brands = document.getElementById('tank_lighting_0_bulb_brand');
			var li_bulb_temps = document.getElementById('tank_lighting_0_bulb_temp');
			var li_wattages = document.getElementById('tank_lighting_0_wattage');
			if (lighting_body && lighting_body.rows && li_system_types && li_system_types.options && li_ballast_brands && li_ballast_brands.options && li_bulb_brands && li_bulb_brands.options && li_bulb_temps && li_bulb_temps.options && li_wattages && li_wattages.options) {
				var num_lighting = lighting_body.rows.length / 2;
				var new_tr_a = document.createElement("tr");
				new_tr_a.style.display = 'none'; // so we can call insertCell in IE on this row
				lighting_body.appendChild(new_tr_a); // so we can call insertCell in IE on this row
				new_tr_a.id = self.li_group_name + '_a_' + num_lighting;
				var new_tr_b = document.createElement("tr");
				new_tr_b.style.display = 'none';// so we can call insertCell in IE on this row
				lighting_body.appendChild(new_tr_b); // so we can call insertCell in IE on this row
				new_tr_b.id = self.li_group_name + '_b_' + num_lighting;
				new_tr_b.className = 'sub_row';
				
				for (var i = 0, il = self.li_base_names.length; i < il; i++) {
					if (i < 3) {
						var new_td = new_tr_a.insertCell(-1);
					} else {
						var new_td = new_tr_b.insertCell(-1);
					}
					if (self.li_base_names[i] != 'model') {
						var node_to_clone = self.li_base_names[i] == 'system_type' ? li_system_types : 
																self.li_base_names[i] == 'ballast_brand' ? li_ballast_brands : 
																self.li_base_names[i] == 'bulb_brand' ? li_bulb_brands : 
																self.li_base_names[i] == 'bulb_temp' ? li_bulb_temps : li_wattages;
						//var new_input = node_to_clone.cloneNode(true); // can't do this in IE, because added event to cloned node actually gets added to node that was cloned
						var new_input = document.createElement('select');
						var cloned_input = node_to_clone.cloneNode(true);
						for (var j = 0, jl = cloned_input.options.length; j < jl; j++) {
							new_input.appendChild(cloned_input.options[j].cloneNode(true));
						}
						cloned_input = null;
						if (self.li_base_names[i] == 'system_type') {
							mat_addEvent(new_input, 'change', mat_edit_profile.sync_lighting_wattage);
						} else if (new_input.length > 1 && self.li_base_names[i] == 'wattage') {
							new_input[0].innerHTML = self.li_wattage_choose_a_disp;
							for (var j = new_input.options.length - 2; j > 0; j--) {
								new_input.removeChild(new_input.options[j]);
							}
						}
						new_input.id = self.get_array_field_id(self.li_group_name, self.li_base_names[i], num_lighting);
						new_input.name = self.get_array_field_name(self.li_group_name, self.li_base_names[i], num_lighting);
						new_input.value = '';
						mat_removeEvent(new_input, 'change', mat_edit_profile.mat_toggle_free_form_input);
						mat_addEvent(new_input, 'change', mat_edit_profile.mat_toggle_free_form_input);
						new_td.appendChild(new_input);
						new_td.appendChild(document.createElement('br'));
						var new_other_input = self.get_new_text_input(self.li_group_name, self.li_base_names[i] + '_other', num_lighting);
						new_other_input.disabled = true;
						new_other_input.className = 'hidden';
						new_td.appendChild(new_other_input);
					} else {
						var new_input = self.get_new_text_input(self.li_group_name, self.li_base_names[i], num_lighting);
						new_td.appendChild(new_input);
					}
					if (i == (self.li_base_names.length - 1)) {
						//new_td.appendChild(document.createTextNode('\u00A0\u00A0'));
						new_td = new_tr_b.insertCell(-1);
						new_td.style.border = '0';
						var new_remove_row = document.createElement("a");
						new_remove_row.id = 'del_row_' + self.li_group_name + '_' + num_lighting;
						new_remove_row.href = '#';
						new_remove_row.className = 'delete_row_icon';
						mat_addEvent(new_remove_row, 'click', mat_edit_profile.remove_lighting);
						new_remove_row.appendChild(document.createTextNode('X'));
						new_td.appendChild(new_remove_row);
					}
				}
						
				new_tr_a.style.display = ''; // so we can call insertCell in IE on this row
				new_tr_b.style.display = ''; // so we can call insertCell in IE on this row
			}
		}
	},
	
	remove_lighting: function(event) {
		var self = mat_edit_profile;
		self.remove_row(event, this, 'li_removing_child', self.li_group_name, self.li_base_names, true);
	},
	
	sync_lighting_wattage: function(event) {
		// this points to the system_types drop-down
		var self = mat_edit_profile;
		var synced_index = -1;
		var parent_row = self.get_parent_row(this, /^lighting_/);
		if (parent_row) {
			synced_index = parent_row.id.match(/\d+$/);
		}
		var lighting_wattage = document.getElementById(self.get_array_field_id('lighting', 'wattage', synced_index));
		var lighting_wattage_other = document.getElementById(self.get_array_field_id('lighting', 'wattage_other', synced_index));
		if (lighting_wattage && lighting_wattage.length > 1) {
			lighting_wattage.selectedIndex = 0;
			lighting_wattage_other.disabled = true;
			lighting_wattage_other.className += ' hidden';
			var curr_option_index = (lighting_wattage.options.length - 2);
			while (curr_option_index > 0) {
				lighting_wattage.removeChild(lighting_wattage.options[curr_option_index--]);
			}
			if (this.value == '') {
				lighting_wattage.options[0].innerHTML = self.li_wattage_choose_a_disp;
			} else {
				lighting_wattage.options[0].innerHTML = self.li_wattage_select_a_disp;
			}
			if (self.li_wattages[this.value] && self.li_wattages[this.value].length > 0) {
				var last_option = lighting_wattage.options[lighting_wattage.options.length - 1];
				for (var i = 0, il = self.li_wattages[this.value].length; i < il; i++) {
					var new_option = document.createElement('option');
					new_option.value = self.li_wattages[this.value][i];
					new_option.innerHTML = new_option.value + ' - ' + this.value;
					lighting_wattage.insertBefore(new_option, last_option);
				}
			}
		}
	},
	
	get_new_text_input: function(group_name, field_name, row_num) {
		var self = mat_edit_profile;
		var new_text_input = null;
		var reference_for_new_input = document.getElementById('tank_' + group_name + '_0_' + field_name);
		if (reference_for_new_input && reference_for_new_input.type && row_num) {
			new_text_input = document.createElement('input');
			new_text_input.type = 'text';
			new_text_input.size = reference_for_new_input.size;
			new_text_input.maxLength = reference_for_new_input.maxLength;
			new_text_input.id = self.get_array_field_id(group_name, field_name, row_num);
			new_text_input.name = self.get_array_field_name(group_name, field_name, row_num);
		}
		return new_text_input;
	},
	
	get_array_field_id: function(group_name, field_name, row_num) {
		return 'tank_' + group_name + '_' + row_num + '_' + field_name;
	},
	
	get_array_field_name: function(group_name, field_name, row_num) {
		return 'tank_data[' + group_name + '][' + row_num + '][' + field_name + ']';
	},
	
	get_parent_row: function(child, parent_id_regex) {
		parent = null;
		if (child && parent_id_regex) {
			var parent = child.parentNode;
			while (parent && (!parent.id || !parent_id_regex.test(parent.id))) {
				parent = parent.parentNode;
			}
		}
		return parent;
	},
		
	add_row: function(removing_row_flag_name, row_group_name, input_base_names, remove_row_method) {
		var self = mat_edit_profile;
		if (!self[removing_row_flag_name]) { // TODO: may want to put some kind of sleep/wait here instead of dropping the request
			var group_body = document.getElementById(row_group_name);
			var brands = document.getElementById('tank_' + row_group_name + '_0_brand');
			if (group_body && group_body.rows && brands && brands.options) {
				var num_items = group_body.rows.length;
				var new_tr = document.createElement("tr");
				group_body.appendChild(new_tr); // so we can call insertCell in IE on this row
				new_tr.style.display = 'none'; // so we can call insertCell in IE on this row
				new_tr.id = row_group_name + '_' + num_items;
				
				for (var i = 0, il = input_base_names.length; i < il; i++) {
					var new_td = new_tr.insertCell(-1);
					if (input_base_names[i] == 'brand') {
						//var new_input = brands.cloneNode(true); // can't do in IE, because added event to cloned node actually gets added to node that was cloned
						var new_input = document.createElement('select');
						var cloned_input = brands.cloneNode(true);
						for (var j = 0, jl = cloned_input.options.length; j < jl; j++) {
							new_input.appendChild(cloned_input.options[j].cloneNode(true));
						}
						cloned_input = null;
						new_input.id = self.get_array_field_id(row_group_name, input_base_names[i], num_items);
						new_input.name = self.get_array_field_name(row_group_name, input_base_names[i], num_items);
						new_input.value = '';
						mat_removeEvent(new_input, 'change', mat_edit_profile.mat_toggle_free_form_input);
						mat_addEvent(new_input, 'change', mat_edit_profile.mat_toggle_free_form_input);
						new_td.appendChild(new_input);
						new_td.appendChild(document.createElement('br'));
						var new_other_input = self.get_new_text_input(row_group_name, input_base_names[i] + '_other', num_items);
						new_other_input.disabled = true;
						new_other_input.className = 'hidden';
						new_td.appendChild(new_other_input);
					} else {
						var new_input = self.get_new_text_input(row_group_name, input_base_names[i], num_items);
						new_td.appendChild(new_input);
					}
					if (i == (input_base_names.length - 1)) {
						//new_td.appendChild(document.createTextNode('\u00A0\u00A0'));
						new_td = new_tr.insertCell(-1);
						var new_remove_row = document.createElement("a");
						new_remove_row.id = 'del_row_' + row_group_name + '_' + num_items;
						new_remove_row.href = '#';
						new_remove_row.className = 'delete_row_icon';
						mat_addEvent(new_remove_row, 'click', remove_row_method);
						new_remove_row.appendChild(document.createTextNode('X'));
						new_td.appendChild(new_remove_row);
					}
				}
						
				new_tr.style.display = ''; // for IE
			}
		}
	},
	
	remove_row: function(event, del_row_icon, removing_row_flag_name, row_group_name, input_base_names, has_sub_row) {
		var self = mat_edit_profile;
		self[removing_row_flag_name] = true;
		var group_body = document.getElementById(row_group_name);
		if (group_body && group_body.rows) {
			if (group_body.rows.length > 2 || (!has_sub_row && group_body.rows.length > 1)) {
				var parent_row_regex = new RegExp('^' + row_group_name + '_');
				var child_item_regex = new RegExp('^tank_' + row_group_name + '_');
				var parent_row = self.get_parent_row(del_row_icon, parent_row_regex);
				if (parent_row) {
					if (has_sub_row) {
						var row_a_id = parent_row.id.replace('_b_', '_a_');
					}
					var sibling_index = parent_row.id.match(/\d+$/) * 1;
					var sibling = self.get_next_sibling_row(parent_row);
					group_body.removeChild(parent_row);
					if (has_sub_row) {
						parent_row = document.getElementById(row_a_id);
						group_body.removeChild(parent_row);
					}
					var child_nodes = null;
					while (sibling && sibling.id && parent_row_regex.test(sibling.id)) {
						sibling.id = sibling.id.replace('_' + (sibling_index + 1), '_' + sibling_index);
						var child_cells = sibling.cells;
						for (var i = 0, il = child_cells.length; i < il; i++) {
							var child_inputs = child_cells[i].childNodes;
							for (var j = 0, jl = child_inputs.length; j < jl; j++) {
								if (child_inputs[j].id) {
									if (child_item_regex.test(child_inputs[j].id)) {
										child_inputs[j].id = child_inputs[j].id.replace('_' + (sibling_index + 1) + '_', '_' + sibling_index + '_');
										if (child_inputs[j].name) {
											child_inputs[j].name = child_inputs[j].name.replace('[' + (sibling_index + 1) + ']', '[' + sibling_index + ']');
										}
									} else if (/^del_row_/.test(child_inputs[j].id)) {
										child_inputs[j].id = child_inputs[j].id.replace('_' + (sibling_index + 1), '_' + sibling_index);
									}
								}
							}
						}	
						sibling = self.get_next_sibling_row(sibling);
						if (sibling && (!has_sub_row || /_a_/.test(sibling.id))) {
							++sibling_index;
						}
					}					
				}
			} else {
				for (var i = 0; i < input_base_names.length; i++) {
					var input_id = self.get_array_field_id(row_group_name, input_base_names[i], 0);
					var clear_input = document.getElementById(input_id);
					if (clear_input && (clear_input.type == 'text' || clear_input.options)) {
						clear_input.value = '';
						var other_input = document.getElementById(input_id + '_other'); 
						if (other_input && other_input.type == 'text') {
							other_input.value = '';
						}
						self.mat_toggle_specified_free_form_input(clear_input);
					}
				}		
			}
			mat_cancelEvent(event, true);
		}	
		self[removing_row_flag_name] = false;
	},
	
	get_next_sibling_row: function(row) {
		var sibling;
		if (row && row.nextSibling) {
			sibling = row.nextSibling;
			while (sibling && !sibling.cells) {
				sibling = sibling.nextSibling;
			}
		}
		return sibling;
	},
	// show photo upload input field and hide thumbnail
	change_photo: function(event) { 
		var album_upload_id = $(this).attr("id").replace(/^.+_(.+_\d+$)/, "$1");
		$("#" + album_upload_id + "_thumbnail").hide("fast");
		$("#" + album_upload_id).show("normal");
		//$("#" + album_upload_id).focus();
		if (event.type != "focus")
			event.preventDefault();
	},
	// show photo thumbnail and hide input field
	show_change_photo: function(event) { 
		var album_upload_id = $(this).attr("id");
		$("#" + album_upload_id + "_thumbnail").show("normal");
		$("#" + album_upload_id).hide("fast");
	},
	
	check_show_change_photo: function(event) {
		$(":file.hidden").each(
			function (index, domE) {
				if (this != event.target) 
					self.change_photo(event);					
			}
		);
	}
};

var ma_tab_buttons = {
	corals_id: 'Image52',
	fish_id: 'Image53',
	critters_id: 'Image54',

	init: function (component_id, default_tab_num) {
		var self = ma_tab_buttons;
		default_tab_num = default_tab_num ? default_tab_num : 0;
		var component = $('#' + component_id);
		var tabs = $('#' + component_id + " a.tab");
		if (component && tabs.length > 0) {
			tabs.each(
				function (tab_num, tab) {
					self.init_tab(tab_num, tab, $(".tabbed_pane", component));
				}
			);
			if (tabs.eq(default_tab_num))
				tabs.eq(default_tab_num).click();
		}
	},
	
	init_tab: function(tab_num, tab, content_pane) {
		var self = ma_tab_buttons;
		if (content_pane && $("table", content_pane).eq(tab_num)) {
			$(tab).click(
				function (event) {
					self.select_tab($(tab).children('img').eq(0).attr('id'));
					self.click_tab(event, tab_num, tab, content_pane);
				}
			);
			$(tab).mouseout(
					function (event) {
						if (!$(tab).children('img').eq(0).hasClass('tab_selected')) {
							MM_swapImgRestore();
						}
					}
			);
		}
	},
	
	/**
	 * @param tab_num {int} zero-based tab number
	 * @param tab {HTMLElement} DOM tab that was clicked
	 * @param content_pane {jQuery} pane containing all table elements associated with the tabs
	 */
	click_tab: function(event, tab_num, tab, content_pane) {
		if (tab_num >= 0 && tab && content_pane && content_pane.find) {
			var self = ma_tab_buttons;
			var panes = $("table", content_pane);
			if (panes && panes.eq(tab_num)) {
				var pane_being_shown = $("table:visible", content_pane);
				var pane_to_show = panes.eq(tab_num);
				if (pane_being_shown != pane_to_show) {
					pane_being_shown.hide();
					pane_to_show.show();
				}
			}
			event.preventDefault();
		}		
	},
	
	select_tab: function(selected_tab_id) {
		var self = ma_tab_buttons;
		if (selected_tab_id) {
			switch (selected_tab_id) {
			case self.fish_id:
				$("#" + self.corals_id).attr('src', $("#" + self.corals_id).attr('src').replace(/_\d/, '_2'));
				$("#" + self.corals_id).removeClass('tab_selected');
				$("#" + self.fish_id).attr('src', $("#" + self.fish_id).attr('src').replace(/_\d/, '_1'));
				$("#" + self.fish_id).addClass('tab_selected');
				$("#" + self.critters_id).attr('src', $("#" + self.critters_id).attr('src').replace(/_\d/, '_2'));
				$("#" + self.critters_id).removeClass('tab_selected');
				break;
			case self.critters_id:
				$("#" + self.corals_id).attr('src', $("#" + self.corals_id).attr('src').replace(/_\d/, '_2'));
				$("#" + self.corals_id).removeClass('tab_selected');
				$("#" + self.fish_id).attr('src', $("#" + self.fish_id).attr('src').replace(/_\d/, '_2'));
				$("#" + self.fish_id).removeClass('tab_selected');
				$("#" + self.critters_id).attr('src', $("#" + self.critters_id).attr('src').replace(/_\d/, '_1'));
				$("#" + self.critters_id).addClass('tab_selected');
				break;
			default:
				$("#" + self.corals_id).attr('src', $("#" + self.corals_id).attr('src').replace(/_\d/, '_1'));
				$("#" + self.corals_id).addClass('tab_selected');
				$("#" + self.fish_id).attr('src', $("#" + self.fish_id).attr('src').replace(/_\d/, '_2'));
				$("#" + self.fish_id).removeClass('tab_selected');
				$("#" + self.critters_id).attr('src', $("#" + self.critters_id).attr('src').replace(/_\d/, '_2'));
				$("#" + self.critters_id).removeClass('tab_selected');
				break;
			}
		}
	}
};

var ma_widgets = {
	init_minimizers: function() {
		var self = ma_widgets;
		$("a[class*='minimizer']").each(
			function () {
				var minimizer_type = this.className.replace(/^.*minimizer([^ ]*)( .*$|$)/, '$1');
				if (minimizer_type) {
					$(this).click(
						function (event) {
							$(".minimizable" + minimizer_type).slideToggle();
							self.toggle_minimizer_link_text($("#minimize" + minimizer_type + "_footer"));
							event.preventDefault();
						}
					);
				}
			}
		);
	},
	
	toggle_minimizer_link_text: function(link) {
		if (link && link.html())
			link.html().indexOf('minimize') > -1 ? link.html(link.html().replace('minimize', 'show')) 
					: link.html(link.html().replace('show', 'minimize'));
	}
};

var ma_ratings = {
	rate_tank: function(tank_num, tank_uid, rating, page) {
		var self = ma_ratings;
		if (tank_num && tank_uid && rating && page) {
			$.get(page, {_do: 'rate', u: tank_uid, t: tank_num, rating: rating}, function (data, status) {self.post_rate_tank(data, status, rating);}, 'json');
			//$.get(page, {_do: 'rate', u: tank_uid, t: tank_num, rating: rating}, function (data, status) {self.post_rate_tank(data, status, rating);}, 'json');
		}
	},
	
	post_rate_tank: function(data, status, rating) {
		if (data.status && data.status == true) {
			// call rate_tank with a diff _do to get new tank rating and update tank rating
			// then change the rating widget to text showing what the user selected
			$("#user_rating").text(rating);
			$("#rate_tank").fadeOut();
			$("#rate_tank").hide();
			$("#pre_rate_tank").fadeIn();
			if (data.tank_rating)
				$("#tank_score").text(data.tank_rating);
		} else {
			if (console)
				console.log("Error rating tank: " + status);
			// show error ... try again message
		}
	}
};

/***** BEGIN MAT global functions *****/
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
/***** END MAT global functions *****/


/*
AddEvent Manager (c) 2005-2006 Angus Turnbull http://www.twinhelix.com
Free usage permitted as long as this credit notice remains intact.
*/
// o : object to attach event to
// t : name of event (without 'on')
// f : function to call when event is fired
// l : if true, disable use of addEventListener()
// see http://www.twinhelix.com/javascript/addevent/
if (typeof mat_addEvent != 'function') {
	var mat_addEvent = function(o, t, f, l) {
		var d = 'addEventListener', n = 'on' + t, rO = o, rT = t, rF = f, rL = l;
		if (o[d] && !l) return o[d](t, f, false);
		if (!o._evts) o._evts = {};
		if (!o._evts[t]) {
			o._evts[t] = o[n] ? { b: o[n] } : {};
			o[n] = new Function('e',
			 'var r = true, o = this, a = o._evts["' + t + '"], i; for (i in a) {' +
			  'o._f = a[i]; r = o._f(e||window.event) != false && r; o._f = null;' +
			  '} return r');
			if (t != 'unload') mat_addEvent(window, 'unload', function() {
				mat_removeEvent(rO, rT, rF, rL);
			});
		}
		if (!f._i) f._i = mat_addEvent._i++;
		o._evts[t][f._i] = f;
	};
	mat_addEvent._i = 1;

	var mat_removeEvent = function(o, t, f, l) {
		var d = 'removeEventListener';
		if (o[d] && !l) return o[d](t, f, false);
		if (o._evts && o._evts[t] && f._i) delete o._evts[t][f._i];
	};
}


// Optional cancelEvent() function you can call within your event handlers to
// stop them performing the normal browser action or kill the event entirely.
// Pass an event object, and the second "c" parameter cancels event bubbling.
function mat_cancelEvent(e, c)
{
	e.returnValue = false;
	if (e.preventDefault) e.preventDefault();
	if (c) {
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
	}
}
