(function($){
	
	
	var absoluteArray = new Array();
	var orgRun = $.livequery.run;
	
	$.livequery.run = function() {
		orgRun.apply(this,arguments);
		$.delay(updateAbsolute);
	}
	

	$(function(){
		
		if($.browser.msie && jQuery.browser.version == '6.0' ) {
			
			$('img[src$=".png"]').livequery(
				function() {
					$(this).data('-ie-png-src',$(this).attr('src'));
					$(this).attr('src','images/empty.png');
					var self = $(this);
					var img = new Image();
					img.onload = function() {
						//scale makes ie crash
						self.css('filter','progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+self.data('-ie-png-src')+'\',sizingMethod=\'fixed\')');
						self.width(this.width);
						self.height(this.height);
					}
					img.src = $(this).data('-ie-png-src');
					
				},
				function() {
					self.css('filter','');
					$(this).attr('src',$(this).data('-ie-png-src'));
				}
			);
			
			$('.ie-warning').hide(0);
			$.Style.each(function() {
				$.get(this.getHref(),function(data) {
					data = data.replace(/[ \n\t\r]+/g," ");
				/*	
					var result = split.split(/(^|})/g);
					$.each(result, function() {
						alert(this);
					});*/
				});
				var iterator = this.getRuleIterator();
				var inlineBlockIndexed = new Array();
				var base = this.getHref();
				iterator.each(function( i ) {
					if( this.css('position') == 'absolute' ) {
						$(this.getSelectorText()).livequery(addSizeFix,removeSizeFix);
					}
					if( this.css('backgroundImage')) {
						
						var url = this.css('backgroundImage');
						url = $.url(url.replace(/^url\('?(.*)'?\)$/,"$1")).setBase(base).assemble();
						this.css('backgroundImage','none');
						var type = 'fixed';
						if( this.css('backgroundRepeat') == 'repeat-x' ) {
							type = 'scale';
						}
						this.css('filter','progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+url+'\',sizingMethod=\''+type+'\')');
					}
				});
			});
			
			
			
			
			$(window).resize(function() { $.delay(updateAbsolute)});
			
		}
	});
	
	
	
	function addSizeFix() {
		absoluteArray.push(this);
		$.delay(updateAbsolute);
	}
	

	
	function removeSizeFix() {
		
	}
	
	function updateAbsolute() {
		$.each(absoluteArray,function() {
			ieResized.apply($(this));
		})
	}
		
	function ieResized() {
			
		$(this).css('left','').css('top','').css('right','').css('bottom','');
	
		var	 computedCss = {
					left:   this.css('left'),
					top:    this.css('top'),
					right:  this.css('right'),
					bottom: this.css('bottom'),
					width:  this.css('width'),
					height: this.css('height')
				};
			for ( a in computedCss ) {
				if( computedCss[a] != null  && computedCss[a] != 'auto' ) {
					computedCss[a] = parseInt(computedCss[a]);
				}
			}
		
	//	this.data('ie-fix-data',fixData);
		
		var parent = this.offsetParent();
		var innerWidth = parent.get(0).clientWidth;
		var innerHeight = parent.get(0).clientHeight;
		
		if( computedCss.left != 'auto' && computedCss.right != 'auto' ) {
			$(this).width(innerWidth-computedCss.left-computedCss.right);
		}
		
		if(  computedCss.top != 'auto' && computedCss.bottom != 'auto' ) {
			$(this).height(innerHeight-computedCss.top-computedCss.bottom);
		}
	}
	
	/*  taken form the jquery lib to sort the elements document order */
	var sortOrder;

	if ( document.documentElement.compareDocumentPosition ) {
		sortOrder = function( a, b ) {
			var ret = a.compareDocumentPosition(b) & 4 ? -1 : a === b ? 0 : 1;
			if ( ret === 0 ) {
				hasDuplicate = true;
			}
			return ret;
		};
	} else if ( "sourceIndex" in document.documentElement ) {
		sortOrder = function( a, b ) {
			var ret = a.sourceIndex - b.sourceIndex;
			if ( ret === 0 ) {
				hasDuplicate = true;
			}
			return ret;
		};
	} else if ( document.createRange ) {
		sortOrder = function( a, b ) {
			var aRange = a.ownerDocument.createRange(), bRange = b.ownerDocument.createRange();
			aRange.selectNode(a);
			aRange.collapse(true);
			bRange.selectNode(b);
			bRange.collapse(true);
			var ret = aRange.compareBoundaryPoints(Range.START_TO_END, bRange);
			if ( ret === 0 ) {
				hasDuplicate = true;
			}
			return ret;
		};
	}
	
})(jQuery);
