// Tyler Spaulding, 2007
// http://www.arguingwithmyself.com
// The contents of this file are public domain.  Do with them what you want.  I
// would prefer if you left this comment block intact, but I'm not going to
// stop you.


function CSS(c)
{
	var css = c;

	// returns the number of rules
	this.GetRuleCount = function()
	{
		if (css.cssRules)
		{
			return css.cssRules.length; // IE
		}
		else
		{
			return css.rules.length;    // other
		}
	}

	// returns all rules
	this.GetRules = function()
	{
		if (css.cssRules)
		{
			return css.cssRules;      // IE
		}
		else
		{
			return css.rules;         // other
		}
	}

	// returns the selector text for the ith rule
	this.GetSelector = function(i)
	{
		return this.GetRules()[i].selectorText;
	}

	// returns the rule text for the ith rule
	this.GetRule = function(i)
	{
		return this.GetRules()[i].style.cssText;
	}

	// updates the ith rule
	this.UpdateRule = function(i, selector, rule)
	{
		if (css.deleteRule)
		{
			css.deleteRule(i);
			css.insertRule(selector + "{" + rule + "}", i);
		}
		else
		{
			css.removeRule(i);
			css.addRule(selector, rule + " ", i);
		}
	}

	// adds a rule
	this.AddRule = function(selector, rule)
	{
		if (css.insertRule)
		{
			css.insertRule(selector + "{" + rule + "}", 0);
		}
		else
		{
			css.addRule(selector, rule + " ");
		}
	}

	// deletes the ith rule
	this.DeleteRule = function(i)
	{
		if (css.deleteRule)
		{
			css.deleteRule(i);
		}
		else
		{
			css.removeRule(i);
		}
	}

	// clears the stylesheet
	this.Clear = function()
	{
		if (css.cssRules)
		{
			// IE
			while (css.cssRules.length > 0)
			{
				css.deleteRule(0);
			}
		}
		else
		{
			// other
			while (css.rules.length > 0)
			{
				css.removeRule(0);
			}
		}
	}
}


var dCSS = {

	// returns the number of active stylesheets
	GetStylesheetCount : function()
	{
		return document.styleSheets.length;
	},

	// returns the ith stylesheet on the page
	GetStylesheet : function(i)
	{
		//return document.styleSheets[i];
		return new CSS(document.styleSheets[i]);
	},

	// adds a new stylesheet
	AddStylesheet : function()
	{
		var newsheet = document.createElement("link");
		newsheet.rel = "stylesheet";
		newsheet.href = "";
		newsheet.type = "text/css";
		document.getElementsByTagName("head")[0].appendChild(newsheet);
	},

	// placeholder until I learn how to delete a stylesheet
	DeleteStylesheet : function(css)
	{
	}
}

