/*!======================================================================*\
|| #################################################################### ||
|| # vBulletin [#]version[#]
|| # ---------------------------------------------------------------- # ||
|| # Copyright ©2000-[#]year[#] vBulletin Solutions Inc. All Rights Reserved. ||
|| # This file may not be redistributed in whole or significant part. # ||
|| # ---------------- VBULLETIN IS NOT FREE SOFTWARE ---------------- # ||
|| # http://www.vbulletin.com | http://www.vbulletin.com/license.html # ||
|| #################################################################### ||
\*======================================================================*/
/**
* Handle Firebug calls when Firebug is not available (getfirebug.com)
*/
if (!window.console || !console.firebug)
{
	window.console = {};
	var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
	for (var i = 0; i < names.length; ++i) window.console[names[i]] = function() {};
}

YAHOO.namespace("vBulletin");
YAHOO.vBulletin.vB_XHTML_Ready = false;

/**
* The ready event
*/
var vB_XHTML_Ready = new YAHOO.util.CustomEvent();
YAHOO.util.Event.onAvailable("footer", function() { console.log("Fire vB_XHTML_Ready"); vB_XHTML_Ready.fire(); YAHOO.vBulletin.vB_XHTML_Ready = true; });

/**
* Breadcrumb handling
*/
vB_XHTML_Ready.subscribe(init_breadcrumb);

function init_breadcrumb(breadcrumb)
{
	var bc_container = YAHOO.util.Dom.get("breadcrumb");

	var bc_items = bc_container.getElementsByTagName("li");

	for (var i = 0; i < bc_items.length; i++)
	{
	}
}

/**
* @param mixed rootcontainer	- Id or Element of element containing imod items (undefined uses whole page)
* @param string	itemtag			- Tag type of imod items -- "li" for example (undefined selects any)
* @param string itemclass		- The class attached to item containers
* @param string	type			- The "typename" of the elements -- "post" or "vmessage".  Must match
*								  The cookie name after the cookie prefix.
* @param string idprefix		- The prefix before the itemid.  The item container must have the itemid with the
* 								  prefix.  If no prefix is specified, type + '_' is used.
* @param string	cookieprefix	- Prefix for the cookie name. Must match the cookieprefix used by the inlinemod php
* 								  script.  Defaults to 'vbulletin_inlinemod'.
*/
function register_inlinemod(rootcontainer, itemtag, itemclass, type, idprefix, cookieprefix)
{
	YAHOO.namespace("vBulletin.imodcollection." + type);
	YAHOO.vBulletin.imodcollection[type] = new InlineModCollection(type, type + '_imodsel', cookieprefix);
	YAHOO.vBulletin.imodcollection[type].add_controls(YAHOO.util.Dom.getElementsByClassName(itemclass, itemtag, rootcontainer), idprefix);
}

function register_inlinemod_checkboxes(rootcontainer, itemtag, itemclass, cbrootcontainer, type, idprefix, cookieprefix)
{
	var collection = new InlineModCollection(type, null, cookieprefix);

	collection.add_controls(YAHOO.util.Dom.getElementsByClassName(itemclass, itemtag, rootcontainer), idprefix);
	collection.add_checkboxes(cbrootcontainer);
}

//create a namespace to hold Collection Singletons
YAHOO.namespace("vBulletin.imodcollections");

function InlineModCollection(type, selector_menu, cookie_prefix)
{
	this.type = type;
	this.collection = new Array();

	//load the initial cookie
	if (typeof cookie_prefix == 'undefined')
	{
		cookie_prefix =  'vbulletin_inline';
	}
	this.cookiename = cookie_prefix + this.type;
	this.selecteditems = this.fetch_ids();

	this.set_display_count(this.selecteditems.length);

	//run some init functions
	this.init_selector(selector_menu);

	//store the singletons so we can look them up.
	YAHOO.vBulletin.imodcollections[type] = this;
}

//intentionally not on the prototype -- psuedo static functions
InlineModCollection.get = function(type)
{
	return YAHOO.vBulletin.imodcollections[type];
}

InlineModCollection.prototype.add_controls = function(elements, idprefix)
{
	if (typeof idprefix == "undefined")
	{
		idprefix = this.type + "_";
	}

	for (var i = 0; i < elements.length; i++)
	{
		var element = elements[i];
		var itemid = element.id.substr(idprefix.length);
		this.add_control(new InlineModControl(element, itemid, this));
	}
}

InlineModCollection.prototype.add_checkboxes = function(root)
{
	var inputs = YAHOO.util.Dom.getElementsByClassName(this.type + '_checkall', 'input', root);
	for (var i = 0; i < inputs.length; i++)
	{
		var input = inputs[i];
		if (input.type == 'checkbox')
		{
			YAHOO.util.Event.on(input, "click", this.set_from_checkbox, this, true);
		}
	}
}

InlineModCollection.prototype.add_control = function(control)
{
	if (PHP.in_array(control.itemid, this.selecteditems) != -1)
	{
		control.checkbox.checked = true;
		control.set_inlinemod_highlight();
	}

	this.collection.push(control);
}

InlineModCollection.prototype.init_selector = function(selector_menu)
{
	var menu = YAHOO.util.Dom.get(selector_menu);

	if (menu != null)
	{
		var links = menu.getElementsByTagName("a");
		for (var i = 0; i < links.length; i++)
		{
			if (links[i].id && links[i].id.substr(0, menu.id.length) == menu.id)
			{
				YAHOO.util.Event.on(links[i], "click", this.set_all_selections, this, true);
			}
		}
	}
}

InlineModCollection.prototype.set_from_checkbox = function(e)
{
	var checkbox = YAHOO.util.Event.getTarget(e);
	for (var i = 0; i < this.collection.length; i++)
	{
		this.collection[i].set_selection_from_checkbox(checkbox);
	}

	this.update_collection_state();
}

InlineModCollection.prototype.set_all_selections = function(e)
{
	var option = YAHOO.util.Event.getTarget(e);

	if ('a' != option.tagName.toLowerCase())
	{
		option = YAHOO.util.Dom.getAncestorByTagName(option, 'A');
	}

	console.log("Do:%s, ID:%s", option.innerHTML, option.id);
	var action = option.id.split(":");

	for (var i = 0; i < this.collection.length; i++)
	{
		this.collection[i].set_selection(action);
	}

	this.update_collection_state();
	YAHOO.util.Event.stopEvent(e);
	YAHOO.vBulletin.vBPopupMenu.close_all();
}

InlineModCollection.prototype.update_collection_state = function()
{
	//remove the items on this page.
	var idarray = new Array();
	for (var i = 0; i < this.collection.length; i++)
	{
		idarray.push(this.collection[i].itemid);
	}

	if (idarray.length)
	{
		this.selecteditems = this.remove_items(this.selecteditems, idarray);
	}

	//add back the selected items.
	for (var i = 0; i < this.collection.length; i++)
	{
		if (this.collection[i].checkbox.checked)
		{
			this.selecteditems.push(this.collection[i].itemid);
		}
	}

	this.set_cookie(this.selecteditems);
	this.set_display_count(this.selecteditems.length);
}

InlineModCollection.prototype.update_state = function(itemid, selected)
{
	this.selecteditems = this.remove_items(this.selecteditems, new Array(itemid));
	if (selected)
	{
		this.selecteditems.push(itemid);
	}
	this.set_cookie(this.selecteditems);
	this.set_display_count(this.selecteditems.length);
}

InlineModCollection.prototype.remove_items = function(idarray, items_to_remove)
{
	var newarray = new Array()
	for (var i = 0; i < idarray.length; i++)
	{
		if (idarray[i] != '' && PHP.in_array(idarray[i], items_to_remove) == -1)
		{
			newarray.push(idarray[i]);
		}
	}
	return newarray;
}

/**
* Saves the inline moderation cookie
*/
InlineModCollection.prototype.set_cookie = function(cookie_array)
{
	var expires = new Date();
	expires.setTime(expires.getTime() + 3600000);
	set_cookie(this.cookiename, cookie_array.join('-'), expires);
}

/**
* Returns an array of IDs from the inlinemod cookie
*
* @return	boolean	True if array created
*/
InlineModCollection.prototype.fetch_ids = function()
{
	var cookie_ids = fetch_cookie(this.cookiename);
	if (cookie_ids != null && cookie_ids != '')
	{
		cookie_ids = cookie_ids.split('-');
		if (cookie_ids.length > 0)
		{
			return cookie_ids;
		}
	}

	return new Array();
}

InlineModCollection.prototype.set_display_count = function(count)
{
	var display = YAHOO.util.Dom.get(this.type + '_inlinemod_count');
	if (display)
	{
		display.innerHTML = count;
	}
}

/**
* Inline Moderation Control Handler
*
* @param	object	HTML element that will take 'imod_highlight' class
* @param	string	Unique ID for related item - used to create imod_checkbox_$itemid
* @param	string	ID of mass-selection menu
*/
function InlineModControl(container, itemid, collection)
{
	var checkboxid = collection.type + "_imod_checkbox_" + itemid;

	this.container = YAHOO.util.Dom.get(container);
	this.checkbox = YAHOO.util.Dom.get(checkboxid);
	this.itemid = itemid;
	this.collection = collection;

	if (this.checkbox)
	{
		this.set_inlinemod_state();
		YAHOO.util.Event.on(this.checkbox, "click", this.set_inlinemod_state, this, true);
	}
	else
	{
		console.warn("No inlinemod selection checkbox found for " + checkboxid);
	}
}

InlineModControl.prototype.init_collection = function(selector_menu)
{
	if (typeof YAHOO.vBulletin.imodcollection == "undefined")
	{
		//singleton object
		YAHOO.vBulletin.imodcollection = new InlineModCollection(selector_menu);
	}

	return YAHOO.vBulletin.imodcollection;
}

InlineModControl.prototype.set_selection_from_checkbox = function(checkbox)
{
	//check for groupable checkboxes.
	if (checkbox.value != 'all')
	{
		if (checkbox.value != this.checkbox.value)
		{
			return;
		}
	}

	this.checkbox.checked = checkbox.checked;
	this.set_inlinemod_highlight();
}

InlineModControl.prototype.set_selection = function(action)
{
	switch (action[1])
	{
		case "invert": { this.checkbox.checked = !this.checkbox.checked; } break;
		case "none": { this.checkbox.checked = false; } break;

		case "class":
		{
			this.checkbox.checked = YAHOO.util.Dom.hasClass(this.container, action[2]);
		} break;
		case "flag":
		{
			if (typeof action[2] != undefined && !isNaN(action[2]))
			{
				this.checkbox.checked = this.checkbox.value & action[2];
			}
			else
			{
				this.checkbox.checked = true;
			}
		} break;
		default:
		case "all": { this.checkbox.checked = true; } break;
	}

	this.set_inlinemod_highlight();
}

InlineModControl.prototype.set_inlinemod_state = function()
{
	this.collection.update_state(this.itemid, this.checkbox.checked);
	this.set_inlinemod_highlight();
}

InlineModControl.prototype.set_inlinemod_highlight = function()
{
	var func = (this.checkbox.checked ? "addClass" : "removeClass");
	YAHOO.util.Dom[func](this.container, "imod_highlight");
	console.log("Set Inlinemod State for %s - %s", this.itemid, func);
}

/**
* Search box handling
*/
vB_XHTML_Ready.subscribe(init_searchboxes);

function init_searchboxes()
{
	var boxes = YAHOO.util.Dom.getElementsByClassName("searchbox", "input");

	for (var i = 0; i < boxes.length; i++)
	{
		new YAHOO.vBulletin.SearchBox(boxes[i]);
	}
}

YAHOO.vBulletin.LoadScript = function(url, callback)
{
	var script = document.createElement("script");
	YAHOO.util.Dom.setAttribute(script, "type", "text/javascript");

	if (typeof(callback) != "undefined")
	{
		if (script.readyState)
		{
			script.onreadystatechange = function()
			{
				if (script.readyState == "loaded" || script.readyState == "complete")
				{
					script.onreadystatechange = null;
					callback();
				}
			}
		}
		else
		{
			script.onload = function()
			{
				callback();
			}
		}
	}

	YAHOO.util.Dom.setAttribute(script, "src", url + "?" + Math.floor(Math.random()*100000));
	document.getElementsByTagName("head")[0].appendChild(script);
}

YAHOO.vBulletin.LoadCss = function(url)
{
	var link = document.createElement("link");
	YAHOO.util.Dom.setAttribute(link, "type", "text/css");
	YAHOO.util.Dom.setAttribute(link, "rel", "stylesheet");
	YAHOO.util.Dom.setAttribute(link, "href", url);
	document.getElementsByTagName("head")[0].appendChild(link);
}

YAHOO.vBulletin.SearchBox = function(element)
{
	this.element = element;
	this.default_value = this.element.value;

	YAHOO.util.Event.on(this.element, "focus", this.focus_handler, this, true);
	YAHOO.util.Event.on(this.element, "blur", this.blur_handler, this, true);
}

YAHOO.vBulletin.SearchBox.prototype.focus_handler = function(e)
{
	if (this.element.value == this.default_value)
	{
		this.element.value = "";
	}

	this.element.select();
}

YAHOO.vBulletin.SearchBox.prototype.blur_handler = function(e)
{
	if (this.element.value == "")
	{
		this.element.value = this.default_value;
	}
}

/**
* Popup menu handling
*/
vB_XHTML_Ready.subscribe(init_popupmenus);

/**
*
*/
function init_popupmenus()
{
	YAHOO.vBulletin.vBPopupMenu = new PopupFactory();
}

/**
*
*/
function PopupFactory()
{
	this.menu_open = false;
	this.timeout = null;
	this.menuclose_timeout = null;
	this.popups = new Object();

	this.instrument();
	YAHOO.util.Event.on(document, "click", this.close_all, this, true);
}

PopupFactory.prototype.instrument = function(baseNode)
{
	var menus = YAHOO.util.Dom.getElementsByClassName("popupmenu", undefined, baseNode);
	for (var i = 0; i < menus.length; i++)
	{
		if (!YAHOO.util.Dom.hasClass(menus[i], 'popupcustom'))
		{
			this.register(menus[i]);
		}
	}
}

/**
*
*/
PopupFactory.prototype.register = function(menu)
{
	var id = YAHOO.util.Dom.generateId(menu);
	this.popups[id] = new PopupMenu(menu, this);
}

/**
*
*/
PopupFactory.prototype.register_menuobj = function(menuobj)
{
	var id = YAHOO.util.Dom.generateId(menuobj.container);
	this.popups[id] = menuobj;
}

/**
*
*/
PopupFactory.prototype.close_all = function(e)
{
	if (this.menu_open)
	{
		for (var i in this.popups)
		{
			this.popups[i].close_menu();
		}
		this.menu_open = false;
	}
}

/**
*
*/
function PopupMenu(container, factory)
{
	this.init(container, factory);
}

//avoid wierdness with child classes
PopupMenu.prototype.init = function(container, factory)
{
	this.container = container;
	this.factory = factory;
	this.display = false;

	this.menu = null;
	this.activate_menu();

	this.control = null;
	this.activate_control();

	this.locator = null;
	this.form = null;
	this.popup_form_elements = new Array();
}


/**
*
*/
PopupMenu.prototype.activate_menu = function()
{
	var menus = YAHOO.util.Dom.getElementsByClassName("popupbody", "*", this.container);

	if (menus.length)
	{
		this.menu = menus[0];
		YAHOO.util.Dom.generateId(this.menu);

		// if we are not using click menu's (ie hover menus instead),
		if (YAHOO.util.Dom.hasClass(this.container, "hovermenu"))
		// bind menu close timer to mouse events of menu element
		{
			YAHOO.util.Event.on(this.menu, "mouseover", this.cancel_menutimer, this, true);
			YAHOO.util.Event.on(this.menu, "mouseout", this.start_menutimer, this, true);
		}
	}
	else
	{
		//console.log("Found no menu for control %s", this.control.innerHTML);
	}

	YAHOO.util.Dom.removeClass(menus[0], "popuphover");
	YAHOO.util.Event.on(menus[0], "click", this.cancel_close, this, true);
}

/**
*
*/
PopupMenu.prototype.activate_control = function()
{
	var controls = YAHOO.util.Dom.getElementsByClassName("popupctrl", "", this.container);
	if (controls.length)
	{
		this.control = controls[0];
		YAHOO.util.Dom.generateId(this.control);

		// always use click event to toggle the menus, whether menus are "hover" or "click"
		if (!YAHOO.util.Dom.hasClass(this.container, "noclick"))
		{
			YAHOO.util.Event.on(this.control, "click", this.toggle_menu, this, true);
		}
		
		// menus open on mouse over rather than click
		// and start the close-menu timer on mouseout (hover menu)
		if (YAHOO.util.Dom.hasClass(this.container, "hovermenu"))
		{
			YAHOO.util.Event.on(this.control, "mouseover", this.open_hovermenu, this, true);
			YAHOO.util.Event.on(this.control, "mouseout", this.start_menutimer, this, true);
		}

		// default behavior, if menu is a not hover menu generate click menu behavior
		else
		{
			if (!YAHOO.util.Dom.hasClass(this.container, "nomouseover"))
			{
				YAHOO.util.Event.on(this.control, "mouseover", this.mouseover, this, true);
				YAHOO.util.Event.on(this.control, "mouseout", this.mouseout, this, true);
			}
		}

		
	}
}

/**
*
*/
PopupMenu.prototype.cancel_close = function(e)
{
	YAHOO.util.Event.stopPropagation(e);
}

/**
*
*/
PopupMenu.prototype.mouseover = function(e)
{
	if (this.factory.menu_open)
	{
		this.open_menu(e);
	}
	
	/*Disable the delay open mouse menu
	else
	{
		this.factory.timeout = YAHOO.lang.later(1000, this, 'open_menu', [{e:e}]);
	}
	*/
}

/**
*
*/
PopupMenu.prototype.mouseout = function(e)
{
	if (this.factory.timeout != null)
	{
		this.factory.timeout.cancel();
	}
}

/**
* For hover menus, this will open current menu,
* and cancel any timers from previous hover menus
*/
PopupMenu.prototype.open_hovermenu = function(e)
{
	this.cancel_menutimer(e);
	this.open_menu(e);
}

/**
* Start the menu timer that closes the menu after mouseout delay
*/
PopupMenu.prototype.start_menutimer = function(e)
{
	this.factory.menuclose_timeout = YAHOO.lang.later(300, this, 'close_menu', [{e:e}]);
}

/**
* Cancel the menu timer that closes the menu after mouseout delay
*/
PopupMenu.prototype.cancel_menutimer = function(e)
{
	if (this.factory.menuclose_timeout != null)
	{
		this.factory.menuclose_timeout.cancel();
	}
}

/**
*
*/
PopupMenu.prototype.toggle_menu = function(e)
{
	if (this.display)
	{
		this.close_menu();
	}
	else
	{
		this.open_menu(e);
	}

	YAHOO.util.Event.stopEvent(e);
}

/**
*
*/
PopupMenu.prototype.load_menu = function(e)
{
	YAHOO.util.Connect.asyncRequest("POST", "load-popup.php?id=" + this.container.id, {
		success: this.handle_menu_load,
		failure: null,
		timeout: null,
		scope: this,
		argument: {e:e}
	}, "id=" + this.container.id);
}

/**
*
*/
PopupMenu.prototype.handle_menu_load = function(ajax)
{
	alert("Load menu!");
}

/**
*
*/
PopupMenu.prototype.open_menu = function(e)
{
	//if the menu isn't loaded yet, attempt to load it.
	if (YAHOO.lang.isNull(this.menu))
	{
		this.load_menu(e);
		return;
	}

	//console.log("Open menu, event type: %s, YUI.target=%s", e.type, YAHOO.util.Event.getTarget(e).tagName);
	if (this.factory.timeout != null)
	{
		this.factory.timeout.cancel();
	}

	this.factory.close_all();

	if (typeof(e) == "object")
	{
		var ctrl = YAHOO.util.Event.getTarget(e);
	}
	else
	{
		var ctrl = YAHOO.util.Dom.get(ctrl);
	}

	if (ctrl != null)
	{
		ctrl = (YAHOO.util.Dom.hasClass(ctrl, "popupctrl") ? ctrl : YAHOO.util.Dom.getAncestorByClassName(ctrl, "popupctrl"));
	}

	this.set_display(true, ctrl);

	this.factory.menu_open = true;
}

/**
*
*/
PopupMenu.prototype.close_menu = function()
{
	this.set_display(false);
	this.factory.menu_open = false;
}

/**
*
*/
PopupMenu.prototype.set_display = function(display, ctrl)
{
	if (YAHOO.lang.isNull(this.menu))
	{
		return;
	}

	this.display = display;

	if (display && YAHOO.env.ua.ie > 0 && YAHOO.env.ua.ie < 8) // Show -- IE 5, 6, 7
	{
		if (YAHOO.lang.isNull(this.locator))
		{
			var form_element = this.contains_form_elements(this.menu);

			if (form_element)
			{
				console.log("Popup '%s' contains form elements", this.control.innerHTML);
				this.form = form_element.form;
			}

			this.locator = this.menu.parentNode.insertBefore(document.createElement("div"), this.menu);
			YAHOO.util.Dom.addClass(this.locator, "popupbody");
			YAHOO.util.Dom.setStyle(this.locator, "visibility", "hidden");
			YAHOO.util.Dom.setStyle(this.locator, "display", "block");
			YAHOO.util.Dom.setStyle(this.locator, "margin", "0");
			YAHOO.util.Dom.setStyle(this.locator, "padding", "0");
			YAHOO.util.Dom.setStyle(this.locator, "border", "none");
			YAHOO.util.Dom.setStyle(this.menu, "display", "block");
			YAHOO.util.Dom.setStyle(this.locator, "width", this.menu.offsetWidth + "px");
			YAHOO.util.Dom.setStyle(this.locator, "height", this.menu.offsetHeight + "px");
			YAHOO.util.Dom.setStyle(this.menu, "display", "none");
			this.menu.form = document.body.appendChild(document.createElement("form"))
			this.menu.form.appendChild(this.menu);

			if (this.form)
			{
				YAHOO.util.Event.on(this.menu.form, "submit", this.handle_popup_form_submit, this, true);
				YAHOO.util.Event.on(this.menu.form, "reset", this.handle_popup_form_reset, this, true);
			}
		}

		if (this.check_menu(this.menu))
		{
			YAHOO.util.Dom.setStyle(this.menu, "display", "block");
			YAHOO.util.Dom.setXY(this.menu, this.get_menu_position(ctrl));
			this.set_control_style();
		}
	}
	else if (display) // Show -- IE8, other browsers
	{
		if (this.check_menu(this.menu))
		{
			YAHOO.util.Dom.setStyle(this.menu, "display", "block");
			this.set_offset(this.menu, ctrl);
			this.set_control_style();
		}
	}
	else // Hide -- All browsers
	{
		YAHOO.util.Dom.setStyle(this.menu, "display", "none");
		this.set_control_style();
	}
}

/**
*
*/
PopupMenu.prototype.check_menu = function(menu)
{
	// The menu must alert us that it isn't to be opened if it is "empty"
	if (!YAHOO.util.Dom.hasClass(menu, "noempty"))
	{
		return true;
	}

	var lis = this.menu.getElementsByTagName("li");
	if (lis.length)
	{
		for (var i = 0; i < lis.length; i++)
		{
			if (!YAHOO.util.Dom.hasClass(lis[i], "noempty"))
			{
				return true;
			}
		}
	}

	return false;
}

/**
*
*/
PopupMenu.prototype.register_popup_form = function(container)
{
	var form_element = this.contains_form_elements(container);

	if (form_element)
	{
		this.form = form_element.form;
	}
}

PopupMenu.prototype.contains_form_elements = function(container)
{
	var form_elements = null;

	form_elements = container.getElementsByTagName("input");
	if (form_elements.length)
	{
		return form_elements[0];
	}
	else
	{
		form_elements = container.getElementsByTagName("textarea");
		if (form_elements.length)
		{
			return form_elements[0];
		}
		else
		{
			form_elements = container.getElementsByTagName("select");
			if (form_elements.length)
			{
				return form_elements[0];
			}
		}
	}

	return false;
}

/**
*
*/
PopupMenu.prototype.handle_popup_form_submit = function(e)
{
	YAHOO.util.Event.stopEvent(e);

	var form = YAHOO.util.Event.getTarget(e);

	for (var i = 0; i < form.elements.length; i++)
	{
		var element = form.elements[i];

		if (element.name)
		{
			switch (element.tagName)
			{
				case "textarea":
				case "select":
				{
					this.replicate_form_value(element);
					break;
				}

				case "input":
				default:
				{
					switch (element.type)
					{
						case "hidden":
						case "text":
						case "password":
						{
							this.replicate_form_value(element);
							break;
						}

						case "checkbox":
						case "radio":
						{
							if (element.checked)
							{
								this.replicate_form_value(element);
							}
							break;
						}
					}
				}
			}
		}
	}

	/*var out = "";
	for (var i = 0; i < this.form.elements.length; i++)
	{
		out += this.form.elements[i].name + "=" + this.form.elements[i].value + (this.form.elements[i].checked ? "/checked" : "") + "\n";
	}
	if (confirm(out))
	{
		this.form.submit();
	}*/
	this.form.submit();
}

/**
*
*/
PopupMenu.prototype.replicate_form_value = function(element)
{
	if (!this.form.elements[element.name])
	{
		var input = document.createElement("input");

		input.name = element.name;
		input.type = "hidden";
		input.value = element.value;

		this.form.appendChild(input);
	}
	else
	{
		this.form.elements[element.name].value = element.value;
	}
}

/**
*
*/
PopupMenu.prototype.handle_popup_form_reset = function(e)
{
	YAHOO.util.Event.getTarget(e).reset();
	this.form.reset();
}

/**
*
*/
PopupMenu.prototype.get_menu_position = function(ctrl)
{
	YAHOO.util.Dom.setStyle(this.locator, "display", "block");

	this.set_offset(this.locator, ctrl);

	var position = YAHOO.util.Dom.getXY(this.locator);

	YAHOO.util.Dom.setStyle(this.locator, "display", "none");

	return position;
}

/**
*
*/
PopupMenu.prototype.set_offset = function(menu, ctrl)
{
	if (!ctrl)
	{
		return;
	}

	var offset_px = ctrl.offsetHeight + "px";

	if (YAHOO.lang.isUndefined(menu.oLeft))
	{
		menu.oLeft = YAHOO.util.Dom.getStyle(menu, "left");
		menu.oRight = YAHOO.util.Dom.getStyle(menu, "right");
	}

	YAHOO.util.Dom.setStyle(menu, "top", offset_px);
	YAHOO.util.Dom.setStyle(menu, "left", menu.oLeft);
	YAHOO.util.Dom.setStyle(menu, "right", menu.oRight);

	var viewport_region = YAHOO.util.Dom.getClientRegion();
	var menu_region = YAHOO.util.Dom.getRegion(menu);

	// horizontal handler
	if (menu_region["right"] > viewport_region["right"])
	{
		//console.log("Set RTL");

		YAHOO.util.Dom.setStyle(menu, "right", "0px");
		YAHOO.util.Dom.setStyle(menu, "left", "auto");

		menu_region = YAHOO.util.Dom.getRegion(menu);
	}

	if (menu_region["left"] < viewport_region["left"])
	{
		//console.log("Set LTR");

		YAHOO.util.Dom.setStyle(menu, "right", "auto");
		YAHOO.util.Dom.setStyle(menu, "left", "0px");

		menu_region = YAHOO.util.Dom.getRegion(menu);
	}

	// vertical handler
	if (menu_region["bottom"] > viewport_region["bottom"])
	{
		//console.info("Overlap, height = %s, therefore %s", (menu_region["bottom"] - menu_region["top"]), (menu_region["height"] * -1));

		YAHOO.util.Dom.setStyle(menu, "top", (menu_region["bottom"] - menu_region["top"]) * -1 + "px");

		menu_region = YAHOO.util.Dom.getRegion(menu);
	}

	if (menu_region["top"] < viewport_region["top"])
	{
		YAHOO.util.Dom.setStyle(menu, "top", offset_px);
	}
}

/**
*
*/
PopupMenu.prototype.set_control_style = function()
{
	var func = (this.display ? "addClass" : "removeClass");

	YAHOO.util.Dom[func](this.control, "active");
}

// #############################################################################
// Collapsible element handlers

vB_XHTML_Ready.subscribe(init_collapsers);

function init_collapsers()
{
	new vBCollapseFactory();
}

/**
*
*/
function vBCollapseFactory()
{
	var links = YAHOO.util.Dom.getElementsByClassName("collapse", "a");
	for (var i = 0; i < links.length; i++)
	{
		new vBCollapse(links[i], this);
	}
	//-ch note: this replaces setting display:none property server-side
	apply_collapses();
}

/**
*
*/
function vBCollapse(link, factory)
{
	this.init(link, factory);
}

/**
*
*/
vBCollapse.prototype.init = function(link, factory)
{
	this.link = link;
	this.factory = factory;
	this.targetid = null;
	this.target = null;
	this.image = null;
	var target = this.link.id.match(/^collapse_(.*)$/);
	this.targetid = target[1];
	this.target = YAHOO.util.Dom.get(this.targetid);
	//-ch note: add a back-reference to this object as a property to the target element
	//will be used by apply_collapses()
	this.target.vBCollapseInstance = this; //back-reference

	var image = this.link.getElementsByTagName("img");
	this.image = image[0];

	if (this.target)
	{
		YAHOO.util.Event.on(this.link, "click", this.toggle_collapse, this, true);
	}
	else
	{
		YAHOO.util.Dom.setStyle(this.link, "display", "hidden");
	}
}

vBCollapse.prototype.collapse = function() {
	YAHOO.util.Dom.setStyle(this.target, "display", "none");
	this.save_collapsed(true);

	if (this.image)
	{
		var img_re = new RegExp("\\.png$");
		this.image.src = this.image.src.replace(img_re, '_collapsed.png');
	}
}//collapse

vBCollapse.prototype.expand = function() {
	YAHOO.util.Dom.setStyle(this.target, "display", "")
	this.save_collapsed(false);

	if (this.image)
	{
		var img_re = new RegExp("_collapsed\\.png$");
		this.image.src = this.image.src.replace(img_re, '.png');
	}
}//expand

/**
* Toggles the collapse state of an object, and saves state to 'vbulletin_collapse' cookie
*
* @return	boolean	false
*/
vBCollapse.prototype.toggle_collapse = function(e)
{
	YAHOO.util.Event.stopEvent(e);
	if (!is_regexp)
	{
		return false;
	}

	if (YAHOO.util.Dom.getStyle(this.target, "display") == "none")
	{
		this.expand();
	}
	else
	{
		this.collapse();
	}
	return false;
}

/**
* Updates vbulletin_collapse cookie with collapse preferences
*
* @param	boolean	Add a cookie
*/
vBCollapse.prototype.save_collapsed = function(addcollapsed)
{
	var collapsed = fetch_cookie('vbulletin_collapse');
	var tmp = new Array();

	if (collapsed != null)
	{
		collapsed = collapsed.split('\n');

		for (var i in collapsed)
		{
			if (YAHOO.lang.hasOwnProperty(collapsed, i) && collapsed[i] != this.targetid && collapsed[i] != '')
			{
				tmp[tmp.length] = collapsed[i];
			}
		}
	}

	if (addcollapsed)
	{
		tmp[tmp.length] = this.targetid;
	}

	expires = new Date();
	expires.setTime(expires.getTime() + (1000 * 86400 * 365));
	set_cookie('vbulletin_collapse', tmp.join('\n'), expires);
}

function apply_collapses() {
	var collapsed = fetch_cookie('vbulletin_collapse');
	if (collapsed != null) {
		collapsed = collapsed.split('\n');
		for (var cid in collapsed) {
			var elementToCollapse = YAHOO.util.Dom.get(collapsed[cid]);
			if (elementToCollapse) {
				//-ch note: using back-reference set on line 1141
				elementToCollapse.vBCollapseInstance.collapse();
			}//safety-check: does the element actually exist on the page. It's possible it doesn't.
		}//loop thru each collapse id
	}//collapsed cookie exists
}//apply_collapses

/****************************************************
* Ajax bit initialization
* This was in the old globals.js so I'm moving it here
* Might be a candidate for reorganization
****************************************************/

// #############################################################################
// Initialize a PostBit

/**
* This function runs all the necessary Javascript code on a PostBit
* after it has been loaded via AJAX. Don't use this method before a
* complete page load or you'll have problems.
*
* @param	object	Object containing postbits
*/
function PostBit_Init(obj, postid)
{
	console.log("PostBit Init: %d", postid);

 	YAHOO.vBulletin.vBPopupMenu.instrument(obj);

	if (typeof vB_QuickEditor != 'undefined')
	{
		// init quick edit controls
		vB_AJAX_QuickEdit_Init(obj);
	}

	if (typeof vB_QuickReply != 'undefined')
	{
		// init quick reply button
		qr_init_buttons(obj);
	}

	if (typeof YAHOO.vBulletin.imodcollection != "undefined" && typeof YAHOO.vBulletin.imodcollection.post != "undefined")
	{
		var collection = YAHOO.vBulletin.imodcollection.post;
		collection.add_control(new InlineModControl(obj, postid, collection));
	}

	if (typeof mq_init != 'undefined')
	{
		// init multi-quote button
		mq_init(obj);
	}

	if (typeof init_reputation_popupmenus != 'undefined')
	{
		init_reputation_popupmenus(obj);
	}

	if (typeof vB_Lightbox != 'undefined')
	{
		init_postbit_lightbox(obj, false, true);
	}

	//do we still need this?
	child_img_alt_2_title(obj);
}


/*======================================================================*\
|| ####################################################################
|| # Downloaded: [#]zipbuilddate[#]
|| # CVS: $RCSfile$ - $Revision: 26385 $
|| ####################################################################
\*======================================================================*/
