/*
    General javascript functions
*/

/*
    Scan DOM hierarchy up until we hit an element where the width is defined
*/
function wbxGetFirstAncestorWidth(element)
{
    var size = $(element).getSize();
    if (size.x > 0)
    {
        return(size.x);
    }
    else
    {
        wbxGetFirstAncestorWidth(element.parentNode);
    }

}

/*
    Scan DOM hierarchy up until we hit an element where the height is defined
*/
function wbxGetFirstAncestorHeight(element)
{
   var size = $(element).getSize();
   //alert('size.y:'+size.y);
    if (size.y > 0)
    {
        return(size.y);
    }
    else
    {
        wbxGetFirstAncestorWidth(element.parentNode);
    }


}

/*
    Get the current width of the broser
*/
function wbxGetBrowserWidth()
{
    if (window.innerWidth)
    {
        return window.innerWidth;
    }
    else if (document.documentElement && document.documentElement.clientWidth != 0)
    {
        return document.documentElement.clientWidth;
    }
    else if (document.body)
    {
        return document.body.clientWidth;
    }

    return 0;
}

/*
    Return the font size of an element
*/
function wbxGetFontSize(element)
{
    return(16);
    // NOTES: need to account for percentage values
    /*if (element.style.fontSize == "" || element.style.fontSize == 0 )
    {
        if (element.tagName.toLowerCase() == "body")
        {
            //alert("returning 16")
            return(16);
        }
        else
        {
            if (element.parentNode == null)
            {
                return(16);
            }
            else
            {
                return(wbxGetFontSize(element.parentNode));
            }
        }
    }
    else
    {
        return(element.style.fontSize);
    }*/
}

/*
    Convert pixal sizes to elastic values
*/
function wbxConvertPixelsToElastic(pixels, parent)
{
    var ems;
    //alert("wbxGetFontSize(parent):"+wbxGetFontSize(parent));
    ems = pixels/wbxGetFontSize(parent);
    //alert("pixels:"+pixels+", ems:"+ems);
    ems = Math.round(ems*1000)/1000;
    return(ems+'em');
}

/*
    Force an element to align vertically with its parent according to parent's valign property
*/
function wbxForceVerticalAlignment(parent, child)
{
	var valign = parent.getStyle('vertical-align');
	var childSize = child.getSize();
	var parentSize = parent.getSize();
	switch (valign)
	{
		case "middle":
			child.setStyle('margin-top', wbxConvertPixelsToElastic((parentSize.y - childSize.y)/2, parent));
		break;

		case "bottom":
			child.setStyle('margin-top', wbxConvertPixelsToElastic(parentSize.y - childSize.y, parent));
		break;

	}
}




