///	<summary>Class Style Helper
///	Helper class for all stylesheet-related scripting</summary>
function StyleHelper() {
}

///	<summary>Searches the stylesheet object for a desired style rule</summary>
///	<param name="styleSheetPath">path of stylesheet to get rule from</param>
///	<param name="selector">css selector of rule to get</param>
StyleHelper.GetStyleSheetRule = function(styleSheetPath, selector) {
	var cssRule = null;
	var styleSheet = StyleHelper.GetStyleSheet(styleSheetPath);
	var cssRules = StyleHelper.GetCssRules(styleSheet);
			// Loop all stylesheet rules to find the given selector
	for (var i = 0; i < cssRules.length; i++) {
		if (StyleHelper.FormatCssSelector(cssRules[i].selectorText).toLowerCase()  == selector.toLowerCase()) {
			cssRule = cssRules[i];
			i = cssRules.length; 
		}
	}
	return cssRule;
}

///	<summary>Lookup stylesheet object reference by it's path</summary>
///	<param name="styleSheetPath">path of stylesheet</param>
StyleHelper.GetStyleSheet = function(styleSheetPath) {
	var styleSheet = null;
	
	for (var i = 0; i < document.styleSheets.length; i++) {
		if (document.styleSheets[i].href.indexOf(styleSheetPath) != -1) {
			styleSheet = document.styleSheets[i];
			i = document.styleSheets.length;
		}
	}
	
	return styleSheet;
}

///	<summary>Gets the stylesheet object's rules using browser check</summary>
///	<param name="styleSheet">all rules in stylesheet</param>
StyleHelper.GetCssRules = function(styleSheet) {
	 var isInternetExplorer = (navigator.userAgent.toLowerCase().indexOf("msie") != -1);
	 var cssRules = null;
	 
	 if (isInternetExplorer) {
		 cssRules = styleSheet.rules;
	 } else {
		 cssRules = styleSheet.cssRules;
	 }
	
	 return cssRules;
}

///	<summary>Retrieves a property from the stylesheet.</summary>
///	<remarks>currentStyle is IE, getComputedStyle everything else, this is necessary b/c just using element.style does not get many properties from the stylesheet, it's supposed to be just for items set in script or inline styles.<remarks>
StyleHelper.GetStyleProperty = function(element, property, ieProperty) {
	ieProperty = (ieProperty == null) ? property : ieProperty;
	
	return (element.currentStyle) ? element.currentStyle[ieProperty] : getComputedStyle(element, "").getPropertyValue(property);
}

/// <summary>Formats a CSS selector so that a browser can locate style rules in a style sheet, mainly because Safari requires a different syntax.</summary>
/// <param name="selector">any given CSS selector string</param>
StyleHelper.FormatCssSelector = function(selector) {
	if (!SupportTest.oldSafariStyleParsing) {
		return selector;  // simply return the string if the browser isn't safari
	} else {
		var safariSelector = '';
		
		var fragments = selector.split(/\s+/); // split at whitespace
		for (i=0; i<fragments.length; i++) { // cycle through all the space-delineated units and concatenate the proper format to the string
		
			var test = fragments[i];
			if (test == '*') { // universal selector part
				safariSelector = safariSelector + test + ' ';
			} else if (test.match(/^\w+$/)) { // element name (make sure to return uppercase)
				safariSelector = safariSelector + test.toUpperCase() + ' ';
			} else if (test.match(/^#/)) { // non-specific id part
				test = test.replace(/#/, '');
				safariSelector = safariSelector + '*[ID"' + test + '"]' + ' ';
			} else if (test.match(/#/)) { // element-specific id (make sure to lowercase the element)
				testArr = test.split('#');
				safariSelector = safariSelector + testArr[0].toUpperCase() + '[ID"' + testArr[1] + '"]' + ' ';
			} else if (test.match(/^\./)) { // basic class name
				safariSelector = safariSelector + test + ' ';
			} else if (test.match(/\./)) { // class name with element selector
				testArr = test.split('.');
				safariSelector = safariSelector + testArr[0].toUpperCase() + '.' + testArr[1] + '[CLASS"' + testArr[1] + '"]' + ' ';
			} else if (test.match(/:/)) {
				testArr = test.split(':');
				safariSelector = safariSelector + testArr[0].toUpperCase() + ':' + testArr[1] + ' ';
			} else { // default
				//alert('error');
			}
		}
		safariSelector = safariSelector.replace(/\s$/, ''); // strip off last space
		// also strip last pseudo-class?
		return safariSelector;
	}
}

///	<summary>Retrieves a clip property from the stylesheet. Ex: top, left, bottom, right</summary>
///	<remarks>Necessary b/c ie can't use getStyleProperty b/c of its' available properties, ex: it has no style.clip.left, only style.clipLeft<remarks>
StyleHelper.GetClipProperty = function(element, property, ieProperty) {
	var clipProperty = null;
	
			// IE only
	if (element.currentStyle) {
		clipProperty = element.currentStyle[ieProperty];
	} else {
		var clip = StyleHelper.GetStyleProperty(element, "clip");
		var properties = clip.replace("rect(", "").replace(")", "").split(", ");
		
		switch (property) {
			case "top":
				clipProperty = properties[0];
				break;
			case "right":
				clipProperty = properties[1];
				break;
			case "bottom":
				clipProperty = properties[2];
				break;
			case "left":
				clipProperty = properties[3];
				break;
		}
	}
	
	return clipProperty;
}
