// byteMyCode.com
// ©1998-2006  All Rights Reserved.

// install window.onload handler

// Add install event handler
function installEventHandler(element,event,handler)
{
	if (element.addEventListener)
	{
    		element.addEventListener(event,handler,false);
	}
	else if(element.attachEvent)
	{
    		element.attachEvent("on"+event,handler);
	}
}

// Hide
function hide(strName)
{
	// Get item
	var cItem = document.getElementById(strName);
	if (cItem)
	{
		cItem.style.display = "none";
	}
}


// Show
function show(strName)
{
	// Get item
	var cItem = document.getElementById(strName);
	if (cItem)
	{
		cItem.style.display = "";
	}
}

// Contains DOM
function containsDOM(container, containee)
{
	var isParent = false;
	do
	{
		if ((isParent = container == containee))
			break;
		containee = containee.parentNode;
	}
	while (containee != null);

	return isParent;
}

// Snippet menu
function snippetMenu()
{
	nav=document.getElementById("snippet_menu_bottom").style;
        con=document.getElementById("snippet_menu_top");
        if (nav.display == "none")
        {
                nav.display = 'block';
                con.onmouseout = function(evt)
                {
			if (checkMouseLeave(this, evt))
			{
				nav=document.getElementById("snippet_menu_bottom").style;
				nav.display = 'none';
			}
                }
        }
        else
        {
		nav.display = 'none';
		con.onmouseout = function (evt)
		{
			if (checkMouseLeave(this, evt))
			{
				nav=document.getElementById("snippet_menu_bottom").style;
				nav.display= 'none';
			}
		}
        }
}

function checkMouseLeave(element, evt)
{
	evt = (evt) ? evt : ((window.event) ? window.event : "");
	window.status = evt;
	if (evt.relatedTarget)
	{
		return !containsDOM(element, evt.relatedTarget);
	}
	else
	{
		if (element.contains(evt.toElement))
		{
		        return(false);
		}
		else
		{
		        return(true);
		}
	}
}

// Add commas
function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

// Update member points
function updateMemberPoints(nPoints)
{
	// Change member points
	var cItem = document.getElementById("member_points");
	if (cItem)
	{
		cItem.innerHTML = addCommas(nPoints);
	}
}

// Toggle show comment
function toggleShowComment(nCommentID)
{
	// Get item
	var cItem = document.getElementById("comment_" + nCommentID);

	// Validate
	if (cItem)
	{
		// -- Get cookie
		var strCookie = getCookie("Comments");
		if (strCookie == null)
		{
			strCookie = "";
		}
		var aCookie = strCookie.length > 0 ? strCookie.split("||") : [];

		// -- Hidden?
		if (cItem.style.display != "none")
		{
			// -- -- Hide
			cItem.style.display = "none";

			// -- -- Remove from cookie
			strCookie = "";
			for (var i = 0; i < aCookie.length; i++)
			{
				if (aCookie[i] != nCommentID)
				{
					if (strCookie.length == 0)
					{
						strCookie = aCookie[i];
					}
					else
					{
						strCookie += "||" + aCookie[i];
					}
				}
			}
		}
		else
		{
			// -- -- Show
			cItem.style.display = "";

			// -- -- Make cookie
			if (strCookie.length == 0)
			{
				strCookie = nCommentID;
			}
			else
			{
				strCookie += "||" + nCommentID;
			}
		}
		// -- Set cookie
		setCookie("Comments", strCookie);
	}
}

// Change class
function changeClass(cItem, strClass)
{
	if (cItem)
	{
		cItem.className = strClass;
	}
}


function hasClass(node, className)
{
	if (node.className == className)
	{
		return true;
	}

  	var reg = new RegExp('(^| )'+ className +'($| )')
  	if (reg.test(node.className))
  	{
    		return true;
	}

	return false;
}

function absolutePosition(el)
{
	var sLeft = 0, sTop = 0;
	var isDiv = /^div$/i.test(el.tagName);
	if (isDiv && el.scrollLeft)
	{
		sLeft = el.scrollLeft;
	}
	if (isDiv && el.scrollTop)
	{
		sTop = el.scrollTop;
	}
	var r = { x: el.offsetLeft - sLeft, y: el.offsetTop - sTop };
	if (el.offsetParent)
	{
		var tmp = absolutePosition(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}

	return r;
};

function removeNode(node)
{
	if (typeof node == 'string')
	{
		node = $(node);
	}

	if (node && node.parentNode)
	{
		return node.parentNode.removeChild(node);
	}
	else
	{
		return false;
	}
}

function addSubmitEvent(form, func)
{
	var oldSubmit = form.onsubmit;
	if (typeof oldSubmit != 'function')
	{
		form.onsubmit = func;
	}
	else
	{
		form.onsubmit = function()
		{
			return oldSubmit() && func();
		}
	}
}

function addClass(node, className)
{
	if (hasClass(node, className))
	{
		return false;
	}

	node.className += ' ' + className;

	return true;
}

function removeClass(node, className)
{
	if (!hasClass(node, className))
	{
		return false;
	}
	node.className = eregReplace('(^| )'+ className +'($| )', '', node.className);
	return true;
}

function eregReplace(search, replace, subject)
{
	return subject.replace(new RegExp(search,'g'), replace);
}
