(function($) {


	$(function(){_scanStyleSheets();});
	
	var styles = { index: new Array() , name : {}, url : {} };

	function _scanStyleSheets( list, parent ) {
		var importList = new Array();
		if( list == null ) {
			list = document.styleSheets;
		}
		for( var i = 0 ; i<list.length ; i++ ) {
			var currStyle = null; //get by index or create an new one if it does not extist
			if( list[i]._index == null ) {
				currStyle = new $.Style(list[i],parent);
				
			} else {
				currStyle = $.Style.get( list[i]._index );
			}
			
			
			if( list[i].imports ) {
				_scanStyleSheets(list[i].imports, currStyle );
			} else {
				for( var j = 0 ; j<list[i].cssRules.length ; j++ ) {
					if( list[i].cssRules[j].type == 3 ) {
						_scanStyleSheets([list[i].cssRules[j].styleSheet], currStyle );
					}
				}
			}
		}
	}
	
	
	$.Style = function( style, parent  ) {
		this._style = style;
		this._index = styles.index.length;
		this._owner = (style.ownerNode||style.owningElement);
		this._style._index = this._index;
		this._parentStyleSheet = parent;
		this._media = {};
		this._href = this._style.href;
		
		var baseUrl = null;
		if( parent ) {
			baseUrl = $.url(parent.getHref());
		}
		this._href = $.url(this._style.href);
		if( !this._href._hostname ) {
			this._href.setBase(baseUrl);
		}
		this._href = this._href.assemble();
		
		var mediaList = {}, mediaText = null;
		if( this._style.media != null && this._style.media != '' ) {
			if( typeof(this._style.media) == 'object' ) {
				mediaText = this._style.media.mediaText;
			} else if( this._style.media ){
				mediaText = this._style.media;
			}
			mediaList = mediaText.replace(/ /g,"").split(',');
		}
		for ( var i=0 ; i<mediaList.length ; i++ ) {
			if( mediaList[i] != '' ) {
				this._media[mediaList[i]] = true;
			}
		}
		
		if(	this._owner ) {
			this.setName(this._owner.id);
		}
		if( this._style.parentStyleSheet ) {
			this._parentStyleSheet = $.Style.get(this._style.parentStyleSheet._index);
		}
		styles.index.push(this);
	}
	
	$.Style.get = function ( indexOrName ) {
		if( isNaN(indexOrName) ) {
			//@todo check if it is probably an url
			return styles.name[indexOrName];
		} else {
			return styles.index[indexOrName];
		}
	}
	
	$.Style.each = function ( callback ) {
		for ( var i=0 ; i<styles.index.length ; i++ ) {
			callback.apply(styles.index[i]);
		}
	}
	
	$.Style.prototype = {
		
		getHref : function() {
			return this._href;
		},
		
		getBaseUrl : function() {
			return this._baseUrl;
		},
		
		setName : function ( name ) {
			this._name = name;
			if( styles.name[name] == null ) {
				styles.name[name] = this;
			} else {
				//@todo throw an excpetion
			}
			return this;
		},
		
		setEnabled : function( enabled ) {
			this._style.disabled = !enabled;
			return this;
		},
		
		isEnabled : function() {
			return !this._style.disabled;
		},
		
		toggleEnabled : function() {
			this._style.disabled = !this._style.disabled;
			return this;
		},
		
		getRuleIterator : function() {
			return new $.Style.RuleIterator(this);
		},
		
		insertRule : function( selector , cssText, index ) {
			var style = this._style;
			if( index == null ) index = -1;
			if( style.insertRule ) {
				
				style.insertRule(selector +' {'+ cssText+'}',index );
			} else {
				style.addRule(selector, cssText, index );
				
				//try {
					//this.style.addRule(selector, cssText );
				//} catch ( e ) {
				
					
				//}
			}
			return this;
		}
	
	}
	
	$.Style.RuleIterator = function ( style ) {
		this._style = style;
		this._index = 0;
		this._rules = (this._style._style.cssRules||this._style._style.rules);
	}
	
	$.Style.RuleIterator.prototype = {
		each : function( callback ) {
		
			for( var i=0 ; i<this._rules.length ; i++ ) {
				this._index = i;
				callback.apply(this,[i]);
			}
		},
		
		getSelectorText : function() {
			return this._rules[this._index].selectorText;
		},
		
		css : function( name, value ) {
			if( value == null ) {
				if( this._rules[this._index]['style'] ) {
					return this._rules[this._index].style[name];
				}
				return null;
			} else {
				this._rules[this._index].style[name] = value;
				return this;
			}
		}
	}

	
	
	
})(jQuery); 


/*(function($) {
	var styleSheetsById = {};
	var styleSheets = new Array();
	var fakeRuleIndex = 1;
	
	var Style = function( style, id ,data ) {
		this.style = style;
		var owner = (style.ownerNode||style.owningElement);
		
		if( owner ) {
			id = id||owner.id;
		}
		styleSheets.push(this);
		this.setIndex(styleSheets.length);
		
		if( id ) {
			styleSheetsById[id] = this;
		}
		
		//@todo if we are in ie and we have an external stylesheet we need to load it again
		//      for fixing rules like test > test
	}
	
	Style.load = function( url, id ) {
		var style = $('<link rel="stylesheet" id="'+id+'" type="text/css" href="'+url+'" media="screen" />');
		$('head').append(style);
		
		//@todo directly create the stlye object
		//      if we do a call to the style object and it looks if this.style is set and if not it trys to find it
		// 
		
	}
	
	
	Style.prototype = {
		
		_ieFixRules : function( data ) {
			var self = this;
			var ruleList = data.match(/([\s\S]*?){([\s\S]*?)\}/g);
			$.each( ruleList, function() {
				var rule = this.replace(/[\n\r]/g,"").match(/([\s\S]*?){([\s\S]*?)\}/);
				self.addRule( rule[1], rule[2]);
			});
		},
		
		_style : function() {
			if( this.style == null ) {
				alert("stlye not loaded, error");
				/*
				
				
				for( var i = 0 ; i<document.styleSheets.length ; i++ ) {
					if(  (document.styleSheets[i].ownerNode||document.styleSheets[i].owningElement) == this.elms ) {
						this.style = document.styleSheets[i];
						new Style(document.styleSheets[i],id,data);
						break;
					}
				}
				
				if we couldn't find do a ajax request with blocking to wait untill loaded
				
			}
			return this.style;
		},
		
		setIndex : function( index ) {
			this._style().index = index;
			
			return this;
		},
		
		addRule : function( selector , cssText ) {
			var style = this._style();
			if( style.insertRule ) {
				style.insertRule(selector +' {'+ cssText+'}',0 );
			} else {
				//@todo this should be moved to the ie fix, that should overwirte the addRule method
				if( selector.match(/[,\[\]>:]/g) ) {
					style.addRule('.ie-fake-selector-'+fakeRuleIndex, cssText );
					$(selector).addClass('ie-fake-selector-'+fakeRuleIndex);
					fakeRuleIndex++;
				} else {
					style.addRule(selector, cssText );
				}
				//try {
					//this.style.addRule(selector, cssText );
				//} catch ( e ) {
				
					
				//}
			}
			return this;
		},
		
		disable : function() {
			this._style().disabled = true;
			return this;
			
		},
		
		toggle : function() {
			this._style().disabled = !this._style().disabled;
			return this;
		},
		
		enable : function() {
			this._style().disabled = false;
			return this;
		}
	}
	
	$.Style = function( idOrIndex) {
		if( isNaN(idOrIndex) ) {
			return styleSheetsById[idOrIndex];
		} else {
			return styleSheets[idOrIndex];
		}
	}
	
	
})(jQuery);*/
