﻿////////////////////////////////////////////////////////////////////////////////
//
//  css.js -- DOM Level 2 CSS Functions
//
//  ©2009, Josh Moyer <JMoyer@nodomain.net>.  All rights reserved.
//
////////////////////////////////////////////////////////////////////////////////

//
// Returns the effective value of a given element's specified CSS2 property,
// similar to getComputedStyle() available on some browsers.
//
// pn: property name (string)
// ee: element (dom::Element)
// ea:  array
//
function getCSSProperty(pn, ee, ea)
{
    var ps = "ee.style." + pn;
	var pv;

    if (!ea) ea = new Array(ee);
    else ea.push(ee);
    
    print("ea[" + (ea.length-1) + "]" +
	".nodeName = " + ea[ea.length-1].nodeName);
    
    if (ee.style)
    {
		pv = eval(ps);
        if (pv) return pv;
	else if (ee.parentNode) getCSSProperty(ee.parentNode, ea);
    }

	throw new Error("slideFrom() unable to determine element color.");
}

//
// Return the css::CSSRuleList collection for the provided css::CSSStyleSheet.
//
function getStyleSheetRules(css)
{
	if (css.rules) return(css.rules);  // IE
	else if (css.cssRules) return(css.cssRules);
	else throw new Error("rules() can't find CSS rule collection.");
}

//
// Parses CSS2 RGB color value strings and returns an array with one
// element each of red, green and blue values in that order.
//
function parseCSSColorValue(rgbs)
{
	var r = null; // radix
	var rgba = new Array; // array to be returned
	var rgbsfc = rgbs.charAt(0).toLowerCase();
	var rgbsl = rgbs.length;
	var output = "|" + rgbs + "("+ rgbsl +")>"; // debug output
	var p = false; // percentages?

	if (rgbsfc == "#")
	{      
		r = 16;

		if (rgbsl == 4 || rgbsl == 7)
		{
			if (rgbsl == 4)
			{
				rgba[0] = rgbs.charAt(1) + rgbs.charAt(1);
				rgba[1] = rgbs.charAt(2) + rgbs.charAt(2);
				rgba[2] = rgbs.charAt(3) + rgbs.charAt(3);
			}

			if (rgbsl == 7)
			{
				rgba[0] = rgbs.charAt(1) + rgbs.charAt(2);
				rgba[1] = rgbs.charAt(3) + rgbs.charAt(4);
				rgba[2] = rgbs.charAt(5) + rgbs.charAt(6);
			}

			output += "[" + rgba[0] + "][" + rgba[1] + "][" + rgba[2] + "]>";
   		}
		else throw new Error("parseCSSColorValue() unable to for hex value.");
	}

	if (rgbsfc == "r")
	{
		r = 10;

		rgbs = rgbs.slice(4,rgbs.indexOf(")"));
		output += rgbs + ">";

		rgba = rgbs.split(",");
		if (rgba.length != 3)
				throw new Error("parseCSSColorValue() unable to for decimal value.");
		output += "[" + rgba[0] + "][" + rgba[1] + "][" + rgba[2] + "]>";

		if (rgbs.indexOf("%") != -1)
		{
			p = true;
			rgba[0] = rgba[0].slice(0,rgba[0].indexOf("%"));
			rgba[1] = rgba[1].slice(0,rgba[1].indexOf("%"));
			rgba[2] = rgba[2].slice(0,rgba[2].indexOf("%"));
			output += "[" + rgba[0] + "][" + rgba[1] + "][" + rgba[2] + "]>";
		}
	}

	if (!r) throw new Error("parseCSSColorValue() unable to.");

	rgba[0] = parseInt(rgba[0],r);
	rgba[1] = parseInt(rgba[1],r);
	rgba[2] = parseInt(rgba[2],r);
	output += "[" + rgba[0] + "][" + rgba[1] + "][" + rgba[2] + "]-";

	if (isNaN(rgba[0]) || isNaN(rgba[1]) || isNaN(rgba[2]))
		throw new Error("parseCSSColorValue() unable to for int.");

	if (p)
	{
		rgba[0] *= 2.55;	rgba[1] *= 2.55;	rgba[2] *= 2.55;
		output += "[" + rgba[0] + "][" + rgba[1] + "][" + rgba[2] + "]%";
	}

	print(output);

	return rgba;
}

function parseCSSColorValueTests()
{
	//parseCSSColorValue("#1111");
	//parseCSSColorValue("#ABC");
	//parseCSSColorValue("fff");
	//parseCSSColorValue("#444444");
	//parseCSSColorValue("rgb");
	//parseCSSColorValue("rgb(55,300,-12) ");
	//parseCSSColorValue("rgb(-55% ,100%, 0)");
	//parseCSSColorValue("rgb( 100% ,0%, 33% )");
}

