// JavaScript Document
Choquin = Choquin || {};
Choquin.CSS = {
	getStyleSheets: function(){
		return $A(document.styleSheets).collect(function(styleSheet){
			return new Choquin.CSS.StyleSheet(styleSheet);
		});
	},
	
	getStyleRules: function(){
		return this.getStyleSheets().invoke("getStyleRules").flatten();
	},
	
	getRule: function(ruleName,deleteFlag){
	  if (document.styleSheets) {
		for (var i=0; i<document.styleSheets.length; i++) {
		  var styleSheet=document.styleSheets[i];
		  var ii=0;
		  var cssRule=false;
		  do {
			if (styleSheet.cssRules) {
			  cssRule = styleSheet.cssRules[ii];
			} else {
			  cssRule = styleSheet.rules[ii];
			}
			if (cssRule)  {
			  if (cssRule.selectorText==ruleName) {
				if (deleteFlag=='delete') {
				  if (styleSheet.cssRules) {
					styleSheet.deleteRule(ii);
				  } else {
					styleSheet.removeRule(ii);
				  }
				  return true;
				} else {
				  return cssRule;
				}
			  }
			}
			ii++;
		  } while (cssRule)
		}
	  }
	  return false;
	},
	
	deleteRule: function(ruleName) {
  		return getCSSRule(ruleName,'delete');
	},
	
	insertRule: function(ruleName) {
	  if (document.styleSheets) {
		if (!getCSSRule(ruleName)) {
		  if (document.styleSheets[0].addRule) {
			document.styleSheets[0].addRule(ruleName, null,0);
		  } else {
			document.styleSheets[0].insertRule(ruleName+' { }', 0);
		  }
		}
	  }
	  return this.getRule(ruleName);
	},
	
	getValues: function(iterator){
		//get all the values from all the stylesheets and all the rules
		var iterator = iterator || Prototype.K;
			return Choquin.CSS.getStyleRules().invoke('getStyleValues').flatten().select(iterator);
	},
	
	getBackgroundImages: function(){
		//returns all the uris on CSS
		return Choquin.CSS.getStyleRules().collect(function(rule){
			if(rule.style.backgroundImage!=""){
				Object.extend(rule.style,Choquin.CSS.Style);
				return rule.style.parseUrl(rule.style.backgroundImage);
			}
		}).compact();
		//return this.getValues(function(value){return value.startsWith('url(')}).invoke('parseUrl');
	},
	
	preloadImages: function(){
		//preload all the CSS images
		return this.getBackgroundImages().collect(function(uri){
			return new Element('img',{src:uri});
		});
	}
}



Choquin.CSS.StyleSheet = Base.extend({
	constructor: function(styleSheet){
		Object.extend(Object.extend(this,styleSheet),{
			'cssRules': styleSheet.cssRules || styleSheet.rules,
			'deleteRule': styleSheet.deleteRule || styleSheet.removeRule,
			'insertRule': styleSheet.insertRule || styleSheet.addRule
		});
		
		Object.removeProperties(this,'removeRule','addRule');
		
		if(this.href && !this.href.startsWith('http')){
			this.href = [document.location.protocol+"/",document.location.hostname.trim('/'),this.href.trim('/')].join('/');
		}
	},
	
	toString: function(){
		return '[styleSheet StyleSheet]';
	},
	
	getStyleRules: function(){
		return $A(this.cssRules).filter(function(rule){
			return rule.type==rule.STYLE_RULE;
		}).collect(function(rule){
			return new Choquin.CSS.CSSRule(rule,this);
		},this);
	}
});


Choquin.CSS.CSSRule = Base.extend({
	constructor: function(cssRule,parentStyleSheet){
		Object.extend(this,cssRule);
		if(this.style) {
			/*
			this does not work on IE
			Object.extend(this.style,Enumerable);
			this.style._each = Array.prototype._each;*/
			if(!this.style.parentRule) this.style.parentRule = this;
			this.style.href = parentStyleSheet.href;
			Object.extend(this.style,Choquin.CSS.Style);
		}
		this.parentStyleSheet = parentStyleSheet;
	},
	
	toString: function(){
		return '[cssRule CSSRule]';
	},
	
	getStyleValues: function(){
		return Object.properties(this.style).collect(function(styleName){
			var ret = Object.extend(new String(this.style[styleName]),Choquin.CSS.CSSRule.Value);
			//var ret = Object.extend(new String(this.style[styleName.camelize()]),Choquin.CSS.CSSRule.Value);
			ret.style = this.style;
			//console.debug(this.style);
			return ret;
		},this);
		/*
		does not work on IE
		return this.style.collect(function(styleName){
			var ret = Object.extend(new String(this.style[styleName.camelize()]),Choquin.CSS.CSSRule.Value);
			ret.style = this.style;
			//console.debug(this.style);
			return ret;
		},this);
		
		*/
		
		
	}
});


Choquin.CSS.CSSRule.Value = {
	parseUrl: function(){
		//example: Choquin.CSS.getStyleSheets()[0].getStyleRules()[0].style.parseUrl('fondo.gif');
		return this.style.parseUrl(this);
	}	
};


Choquin.CSS.Style = {
	parseUrl: function(url){
		//example: Choquin.CSS.getStyleSheets()[0].getStyleRules()[0].style.parseUrl('fondo.gif');
		var uri = url.getURIcontent();
		if(!this.href) return uri;
		//console.debug(uri,this.href.path());
		var base = uri.startsWith('/') ? this.href.urlProtocol() + this.href.urlDomain() : this.href.path();
		if(!uri.startsWith('http')) uri = (base+uri).parseFilePath();
		return uri;
	},
	
	parseStyleUrl: function(styleName){
		//example: Choquin.CSS.getStyleSheets()[0].getStyleRules()[0].style.parseStyleUrl('backgroundImage');
		return this.parseUrl(this[styleName]);
	}
}



