
// Cookie handlers

/**
* Sets a cookie
*
* @param	string	Cookie name
* @param	string	Cookie value
* @param	date	Cookie expiry date
*/
function set_cookie(name, value, expires)
{
	console.log("Set Cookie :: %s = '%s'", name, value);
	document.cookie = name + '=' + escape(value) + '; path=/' + (typeof expires != 'undefined' ? '; expires=' + expires.toGMTString() : '');
}

/**
* Deletes a cookie
*
* @param	string	Cookie name
*/
function delete_cookie(name)
{
	console.log("Delete Cookie :: %s", name);
	document.cookie = name + '=' + '; expires=Thu, 01-Jan-70 00:00:01 GMT' +  '; path=/';
}

/**
* Fetches the value of a cookie
*
* @param	string	Cookie name
*
* @return	string
*/
function fetch_cookie(name)
{
	cookie_name = name + '=';
	cookie_length = document.cookie.length;
	cookie_begin = 0;
	while (cookie_begin < cookie_length)
	{
		value_begin = cookie_begin + cookie_name.length;
		if (document.cookie.substring(cookie_begin, value_begin) == cookie_name)
		{
			var value_end = document.cookie.indexOf (';', value_begin);
			if (value_end == -1)
			{
				value_end = cookie_length;
			}
			var cookie_value = unescape(document.cookie.substring(value_begin, value_end));
			console.log("Fetch Cookie :: %s = '%s'", name, cookie_value);
			return cookie_value;
		}
		cookie_begin = document.cookie.indexOf(' ', cookie_begin) + 1;
		if (cookie_begin == 0)
		{
			break;
		}
	}
	console.log("Fetch Cookie :: %s (null)", name);
	return null;
}

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++)
	{
	}
}


// 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

