﻿var AP_CSS = function()
{
	if(window.navigator.userAgent.indexOf('MSIE')>-1) {
		this.get_style	= get_styleDesperado;
		this.get_rule	= get_ruleDesperado;
		this.set_rule	= set_ruleDesperado;
	} else {
		this.get_style	= get_styleStandards;
		this.get_rule	= get_ruleStandards;
		this.set_rule	= set_ruleStandards;
	}

	this.create = function(uri, title)
	{
		var css = document.createElement('link');

		css.rel		= 'stylesheet'; 
		css.type	= 'text/css';
		css.title	= title;
		css.href	= uri;
		
		var head = document.getElementsByTagName('head')[0];
		head.appendChild(css);
		this.disabled(title, true);
	}

	this.remove = function(title)
	{
		var styleSheets = new Array();
		var links = document.getElementsByTagName('link');
		for(var i=0; i<links.length; i++) {
			if(links[i].getAttribute('rel').indexOf('style')<0)continue;
			styleSheets.push(links[i]);
		}
		
		for(var i=0; i<styleSheets.length; i++) {
			if(styleSheets[i].title != title)continue;
			document.getElementsByTagName('head')[0].removeChild(styleSheets[i]);
		}
	}

	this.disabled = function(title, disabled)
	{
		var styleSheets = new Array();
		var links = document.getElementsByTagName('link');
		for(var i=0; i<links.length; i++) {
			if(links[i].getAttribute('rel').indexOf('style')<0)continue;
			styleSheets.push(links[i]);
		}
		
		for(var i=0; i<styleSheets.length; i++) {
			if(styleSheets[i].title != title)continue;
			styleSheets[i].disabled = disabled;
		}
	}

	function camelize(str)
	{
		return str.replace(/-([a-z])/g,
			function($0, $1){return $1.toUpperCase();});
	}

	function decamelize(str)
	{
		return str.replace(/[A-Z]/g,
			function($0){return '-'+$0.toLowerCase()});
	}

	function get_styleDesperado(el, property)
	{
		property = camelize(property);
		var value = (el.style[property]) ? el.style[property] : '';
		if(value)return value;
		if(!el.currentStyle)return '';
		return el.currentStyle[property];
	}

	function get_styleStandards(el, property, pseudo)
	{
		var value = (el.style[property]) ? el.style[camelize(property)] : '';
		if(value)return value;
		return document.defaultView.getComputedStyle(el, pseudo).getPropertyValue(decamelize(property));
	}

	this.set_style = function(el, property, value)
	{
		property = camelize(property);
		if(!el.style)return false;
		el.style[property] = value;
	}



	function get_ruleDesperado(selector, property)
	{
		property = camelize(property);
		var value='';

		for(var i=0; i < document.styleSheets.length; i++)parse_sheet(document.styleSheets[i]);
		return value;

		function parse_sheet(sheet)
		{
			if(sheet.imports)parse_sheet(sheet.imports);

			var rules	= sheet.rules;
			if(!rules)return false;
			for(var i=0; i<rules.length; i++) {
				if(!rules[i].selectorText)continue;
				if(rules[i].selectorText.toLowerCase() != selector.toLowerCase())continue;
				value = rules[i].style[property];
			}
		}
	}

	function get_ruleStandards(selector, property)
	{
		property = camelize(property);
		var value='';

		for(var i=0; i < document.styleSheets.length; i++)parse_sheet(document.styleSheets[i]);
		return value;

		function parse_sheet(sheet)
		{
			var rules = sheet.cssRules;
			for(var i=0; i<rules.length; i++) {
				if(rules[i].styleSheet)parse_sheet(rules[i].styleSheet);
				if(!rules[i].selectorText)continue;
				if(rules[i].selectorText.toLowerCase() != selector.toLowerCase())continue;
				value = rules[i].style[property];
			}
		}
	}


	function set_ruleDesperado(selector, property)
	{
		var pt = document.styleSheets.length;
		pt--;
		if(pt < 0)return false;
		document.styleSheets[pt].addRule(selector, '{'+property+'}');
	}

	function set_ruleStandards(selector, property)
	{
		var pt = document.styleSheets.length;
		pt--;
		if(pt < 0)return false;
		documents.styleSheet[pt].insertRule(selector+'{'+property+'}', documents.styleSheet[pt].cssRules.length);
	}
}

AP_CSS.prototype = 
{

}

Css = new AP_CSS();