/*
-------------------------------------------------
Author:     Craig Shoemaker
Revised:    12/02/02
-------------------------------------------------
  No copyright notice other than the one below
  from our friends at Apple.  Just have fun... ;)
*/

var PDSA_JSLIBRARY_STYLESHEETS = true;

if(typeof(PDSA_JSLIBRARY_BROWSER) == "undefined")
{
	alert("The PDSA.JSLibrary.Browser.js is required to use the PDSA.JSLibrary.StyleSheets.js library.");
}


/*--------------------------------
    Style Sheet Manipulation
  --------------------------------*/
function pdsaGetStyle(id)
	{
	return (IsLayers)?document.layers[id]:pdsaGetElement(id).style;
	}
function pdsaDisplay()
/*
Signatures:
    for manual set...strElementId,strState,strReturn(optional)
    for toggle.......strElementId,strReturn(optional)
*/
	{
	if(IsDom)
		{
        var strElementId;
        var strState;
        var strReturn;
        if(arguments[1] && arguments[1].toLowerCase() != "return")
            {
            strElementId = arguments[0];
            strState = arguments[1];
            strReturn = (arguments[2] && arguments[2].toLowerCase() == "return")?true:false;
            pdsaGetStyle(strElementId).display = strState;
            if(strReturn)
                return (strState != "" || strState != "none")?true:false;
            }
        else
            {
            strElementId = arguments[0];
            strReturn = (arguments[1] && arguments[1].toLowerCase() == "return")?true:false;
            var obj = pdsaGetStyle(strElementId);
            var strOrigDisplay = getStyleById(strElementId,"display");
            var strNewDisplay = (strOrigDisplay != "none")?"none":"block";
            obj.display = strNewDisplay;
            if(strReturn)
                return (strNewDisplay != "none")?true:false;
            }
		}
	}
function pdsaVisibility()
/*
Signatures:
    for manual set...strElementId,strState,strReturn(optional)
    for toggle.......strElementId,strReturn(optional)
*/
	{
    var strElementId;
    var strState;
    var strReturn;
    if(arguments[1] && arguments[1].toLowerCase() != "return")
        {
        strElementId = arguments[0];
        strState = arguments[1].toLowerCase();
        strReturn = (arguments[2] && arguments[2].toLowerCase() == "return")?true:false;
        pdsaGetStyle(strElementId).visibility = (IsLayers)?(strState == "hidden")?"hide":"show":strState;
        if(strReturn)
            return (strState == "visible")?true:false;
        }
    else
        {
        strElementId = arguments[0];
        strReturn = (arguments[1] && arguments[1].toLowerCase() == "return")?true:false;
        var obj = pdsaGetStyle(strElementId);
        var origVis = getStyleById(strElementId,"visibility");
        var strNewVis = (IsLayers)?(origVis == "hide" || origVis == "hidden")?"show":"hide":(origVis == "hidden")?"visible":"hidden";
        obj.visibility = strNewVis;
        if(strReturn)
            return (strNewVis == "visible")?true:false;
        }
	}
function pdsaLeft(strElementId,intSetVal) //intSetVal Optional
	{
	if(intSetVal)
		 setStyleById(strElementId,"left",intSetVal);
	else
		return pdsaValueStringToInt(getStyleById(strElementId,"left"));
	}
function pdsaTop(strElementId,intSetVal) //intSetVal Optional
	{
	if(intSetVal)
		 setStyleById(strElementId,"top",intSetVal);
	else
		return pdsaValueStringToInt(getStyleById(strElementId,"top"));
	}
function pdsaWidth(strElementId,intSetVal) //intSetVal Optional
	{
	if(!IsLayers)
		{
		if(intSetVal)
			 setStyleById(strElementId,"width",intSetVal);
		else
			return pdsaValueStringToInt(getStyleById(strElementId,"width"));
		}
	}
function pdsaHeight(strElementId,intSetVal) //intSetVal Optional
	{
	if(!IsLayers)
		{
		if(intSetVal)
			 setStyleById(strElementId,"height",intSetVal);
		else
			return pdsaValueStringToInt(getStyleById(strElementId,"height"));
		}
	}
// --------------------------------------------------------------------- \\
// Copyright  2001 by Apple Computer, Inc., All Rights Reserved.
// http://developer.apple.com/internet/javascript/styles.html
// --------------------------------------------------------------------- \\
// ugly workaround for missing support for selectorText in Netscape6/Mozilla
// call onLoad() or before you need to do anything you would have otherwise used
// selectorText for.
var ugly_selectorText_workaround_flag = false;
var allStyleRules;
// code developed using the following workaround (CVS v1.15) as an example.
// 
http://lxr.mozilla.org/seamonkey/source/extensions/xmlterm/ui/content/XMLTermCommands.js
function ugly_selectorText_workaround()
	{
	if((navigator.userAgent.indexOf("Gecko") == -1) || 
(ugly_selectorText_workaround_flag))
		{
		return;
		}
	var styleElements = document.getElementsByTagName("style");
	for(var i = 0; i < styleElements.length; i++)
		{
		var styleText = styleElements[i].firstChild.data;
		allStyleRules = styleText.match(/\b[\w-.]+(\s*\{)/g);
		}
	for(var i = 0; i < allStyleRules.length; i++)
		{
		allStyleRules[i] = allStyleRules[i].substr(0, (allStyleRules[i].length - 2));
		}
	ugly_selectorText_workaround_flag = true;
	}
function setStyleById(i,p,v)	// args: i) element id p) property v) value
	{
	pdsaGetStyle(i)[p] = v;
	}
function getStyleById(i,p)
    {
    if(IsMoz5Up)
        return getStyleById_Mozilla(i,p);
    else
        return getStyleById_General(i,p);
    }
function getStyleById_Mozilla(i,p)
    {
    if(IsMoz5Up)
        {
        var styleObject = document.getElementById( i );
        if (styleObject != null)
            {
            styleObject = styleObject.style;
            if (styleObject[p])
                {
                return styleObject[ p ];
                }
            }
        styleObject = getStyleBySelector("#"+i);
        return (styleObject != null)?styleObject[p]:null;
        }
    }
function getStyleById_General(i, p)	// args: i) element id p) property
	{
	var n = pdsaGetElement(i);
	var s = eval("pdsaGetStyle(i)." + p);
	// try inline
	if((s != "") && (s != null))
		{
		return s;
		}
	// try currentStyle
	if(n.currentStyle)
		{
		var s = eval("n.currentStyle." + p);
		if((s != "") && (s != null))
			{
			return s;
			}
		}
	// try styleSheets
	var sheets = document.styleSheets;
	if(sheets.length > 0)
		{
		// loop over each sheet
		for(var x = 0; x < sheets.length; x++)
			{
			// grab stylesheet rules
			var rules = sheets[x].cssRules;
			if(rules.length > 0)
				{
				// check each rule
				for(var y = 0; y < rules.length; y++)
					{
					var z = rules[y].style;
					// selectorText broken in NS 6/Mozilla: see
					// http://bugzilla.mozilla.org/show_bug.cgi?id=51944
					ugly_selectorText_workaround();
					if(allStyleRules)
						{
						if(allStyleRules[y] == i)
							{
							return z[p];
							}
						}
					else
						{
						// use the native selectorText and style stuff
						if(((z[p] != "") && (z[p] != null)) || (rules[y].selectorText == i))
							{
							return z[p];
							}
						}
					}
				}
			}
		}
	return null;
	}
function getStyleBySelector(selector)
	{
	if(!IsMoz5Up)
		return null;
	var sheetList = document.styleSheets;
	var ruleList;
	var i, j;

	// look through stylesheets in reverse order that they appear in the 
document
	for (i=sheetList.length-1; i >= 0; i--)
		{
		ruleList = sheetList[i].cssRules;
		for (j=0; j<ruleList.length; j++)
			{
			if (ruleList[j].type == CSSRule.STYLE_RULE && ruleList[j].selectorText == 
selector)
				return ruleList[j].style;
			}
		}
	return null;
	}
// setStyleByClass: given an element type and a class selector, style property and value, apply the style.
// args:
//  t - type of tag to check for (e.g., SPAN)
//  c - class name
//  p - CSS property
//  v - value
var ie = (document.all) ? true : false;
function setStyleByClass(t,c,p,v){
	var elements;
	if(t == '*')
		{
		// '*' not supported by IE/Win 5.5 and below
		elements = (ie) ? document.all : document.getElementsByTagName('*');
		}
	else
		{
		elements = document.getElementsByTagName(t);
		}
	for(var i = 0; i < elements.length; i++)
		{
		var node = elements.item(i);
		for(var j = 0; j < node.attributes.length; j++)
			{
			if(node.attributes.item(j).nodeName == 'class')
				{
				if(node.attributes.item(j).nodeValue == c)
					{
					eval('node.style.' + p + " = '" +v + "'");
					}
				}
			}
		}
	}

// getStyleByClass: given an element type, a class selector and a property,
// return the value of the property for that element type.
// args:
//  t - element type
//  c - class identifier
//  p - CSS property
function getStyleByClass(t, c, p) {
	// first loop over elements, because if they've been modified they
	// will contain style data more recent than that in the stylesheet
	var elements;
	if(t == '*')
		{
		// '*' not supported by IE/Win 5.5 and below
		elements = (ie) ? document.all : document.getElementsByTagName('*');
		}
	else
		{
		elements = document.getElementsByTagName(t);
		}
	for(var i = 0; i < elements.length; i++){
		var node = elements.item(i);
		for(var j = 0; j < node.attributes.length; j++)
			{
			if(node.attributes.item(j).nodeName == 'class')
				{
				if(node.attributes.item(j).nodeValue == c)
					{
					var theStyle = eval('node.style.' + p);
					if((theStyle != "") && (theStyle != null))
						{
						return theStyle;
						}
					}
				}
			}
		}
	// if we got here it's because we didn't find anything
	// try styleSheets
	var sheets = document.styleSheets;
	if(sheets.length > 0)
		{
		// loop over each sheet
		for(var x = 0; x < sheets.length; x++)
			{
			// grab stylesheet rules
			var rules = sheets[x].cssRules;
			if(rules.length > 0)
				{
				// check each rule
				for(var y = 0; y < rules.length; y++)
					{
					var z = rules[y].style;
					// selectorText broken in NS 6/Mozilla: see
					// http://bugzilla.mozilla.org/show_bug.cgi?id=51944
					ugly_selectorText_workaround();
					if(allStyleRules)
						{
						if((allStyleRules[y] == c) || (allStyleRules[y] == (t + "." + c)))
							{
							return z[p];
							}
						}
					else
						{
						// use the native selectorText and style stuff
						if(((z[p] != "") && (z[p] != null)) && ((rules[y].selectorText == c) || (rules[y].selectorText == (t + "." + c))))
							{
							return z[p];
							}
						}
					}
				}
			}
		}
	return null;
	}

// setStyleByTag: given an element type, style property and
// value, and whether the property should override inline styles or
// just global stylesheet preferences, apply the style.
// args:
//  e - element type or id
//  p - property
//  v - value
//  g - boolean 0: modify global only; 1: modify all elements in document
function setStyleByTag(e, p, v, g)
	{
	if(g)
		{
		var elements = document.getElementsByTagName(e);
		for(var i = 0; i < elements.length; i++)
			{
			elements.item(i).style[p] = v;
			}
		}
	else
		{
		var sheets = document.styleSheets;
		if(sheets.length > 0)
			{
			for(var i = 0; i < sheets.length; i++)
				{
				var rules = sheets[i].cssRules;
				if(rules.length > 0)
					{
					for(var j = 0; j < rules.length; j++)
						{
						var s = rules[j].style;
						// selectorText broken in NS 6/Mozilla: see
						// http://bugzilla.mozilla.org/show_bug.cgi?id=51944
						ugly_selectorText_workaround();
						if(allStyleRules)
							{
							if(allStyleRules[j] == e)
								{
								s[p] = v;
								}
							}
						else
							{
							// use the native selectorText and style stuff
							if(((s[p] != "") && (s[p] != null)) && (rules[j].selectorText == e))
								{
								s[p] = v;
								}
							}
						}
					}
				}
			}
		}
	}
// getStyleByTag: given an element type and style property, return
// the property's value
// args:
//  e - element type
//  p - property
function getStyleByTag(e, p)
	{
	var sheets = document.styleSheets;
	if(sheets.length > 0)
		{
		for(var i=0;i<sheets.length;i++)
			{
			var rules = sheets[i].cssRules;
			if(rules.length > 0)
				{
				for(var j = 0; j < rules.length; j++)
					{
					var s = rules[j].style;
					// selectorText broken in NS 6/Mozilla: see
					// http://bugzilla.mozilla.org/show_bug.cgi?id=51944
					ugly_selectorText_workaround();
					if(allStyleRules)
						{
						if(allStyleRules[j] == e)
							{
							return s[p];
							}
						}
					else
						{
						// use the native selectorText and style stuff
						if(((s[p] != "") && (s[p] != null)) && (rules[j].selectorText == e))
							{
							return s[p];
							}
						}
					}
				}
			}
		}
	// if we don't find any style sheets, return the value for the first
	// element of this type we encounter without a CLASS or STYLE attribute
	var elements = document.getElementsByTagName(e);
	var sawClassOrStyleAttribute = false;
	for(var i = 0; i < elements.length; i++)
		{
		var node = elements.item(i);
		for(var j = 0; j < node.attributes.length; j++)
			{
			if((node.attributes.item(j).nodeName == 'class') ||
			   (node.attributes.item(j).nodeName == 'style'))
			   	{
				sawClassOrStyleAttribute = true;
				}
			}
		if(!sawClassOrStyleAttribute)
			{
			return elements.item(i).style[p];
			}
		}
	}

