(function($){
	$.fn.extend({
		sprites:function(_){
			opt=_||{}
			this.each(function(){
				var _=clone(opt),
					th=_.me=$(this)
				
				_=$.extend({
					method:'corners',
					pasta:'<div></div>',
					hover:false,
					duration:300,
					easing:'linear',
					processFu:function(){
						({
						 	vStretch:function(){
								_.layers=_.layers||{}
								_.corners=_.corners||{}
								_.tails=_.tails||{}

								_.layers['t']=_.corners['t']=_.etal.clone()
									.css({
										top:0,
										backgroundPosition:'0 0'
									})
								_.layers['b']=_.corners['b']=_.etal.clone()
									.css({
										bottom:0,
										backgroundPosition:'-'+_.width/3+'px 0'
									})
								_.layers['c']=_.tails['c']=_.etal.clone()
									.css({
										top:_.height,
										bottom:_.height,
										backgroundPosition:'-'+_.width/3*2+'px 0',
										backgroundRepeat:'repeat-y'
									})
								$.each(_.layers,function(k,d){
									d
									.css({
										position:'absolute',
										backgroundImage:_.url,
										zIndex:-1,
										left:0,
										width:_.width/3
									})
									.appendTo(_.me)
								})
								$.each(_.corners,function(k,d){
									d
									.css({
										height:_.height
									})
								})
								_.me.css({background:'none'})
							},
						 	gStretch:function(){
								var step=_.hover?_.height/6:_.height/3
								_.layers=_.layers||{}
								_.corners=_.corners||{}
								_.tails=_.tails||{}
								_.hovers=_.hovers||{}

								_.layers['l']=_.corners['l']=_.etal.clone()
									.css({
										left:0,
										backgroundPosition:'0 0'
									})
								_.layers['r']=_.corners['r']=_.etal.clone()
									.css({
										right:0,
										backgroundPosition:'0 -'+step+'px'
									})
								_.layers['c']=_.tails['c']=_.etal.clone()
									.css({
										right:_.width,
										left:_.width,
										backgroundPosition:'0 -'+step*2+'px'
									})
								$.each(_.layers,function(k,d){
									d
									.css({
										position:'absolute',
										backgroundImage:_.url,
										zIndex:-1,
										top:0,
										height:step
									})
									.appendTo(_.me)
								})
								$.each(_.corners,function(k,d){
									d
									.css({
										width:_.width
									})
								})
								if(_.hover)
									$.each(_.layers,function(k,d){
										_.hovers[k]=d.clone().appendTo(_.me).hide()
										if(!($.browser.msie&&$.browser.version<9))
											_.hovers[k].css({opacity:0})
									}),
									_.hovers['l'].css({backgroundPosition:'0 -'+step*3+'px'}),
									_.hovers['r'].css({backgroundPosition:'0 -'+step*4+'px'}),
									_.hovers['c'].css({backgroundPosition:'0 -'+step*5+'px'})
								_.me.css({background:'none'})
							},
							corners:function(){
								_.layers=_.layers||{}
								_.corners=_.corners||{}
								_.tails=_.tails||{}
								_.layers['l-t']=_.corners['l-t']=_.etal.clone()
									.css({
										left:0,
										top:0,
										backgroundPosition:'0 0'
									})
								_.layers['r-t']=_.corners['r-t']=_.etal.clone()
									.css({
										right:0,
										top:0,
										backgroundPosition:'-'+(_.width/4)+'px 0'
									})
								_.layers['l-b']=_.corners['l-b']=_.etal.clone()
									.css({
										left:0,
										bottom:0,
										backgroundPosition:'0 -'+(_.height/4)+'px'
									})
								_.layers['r-b']=_.corners['r-b']=_.etal.clone()
									.css({
										right:0,
										bottom:0,
										backgroundPosition:'-'+(_.width/4)+'px -'+(_.height/4)+'px'
									})
								_.layers['t-l']=_.etal.clone()
									.css({
										left:0,
										top:_.height/4,
										bottom:_.height/4,
										width:_.width/4,
										backgroundPosition:'-'+(_.width/2)+'px 0'
									})
								_.layers['t-l-c']=_.layers['t-l'].clone()
									.css({backgroundPosition:'-'+(_.width/2)+'px -'+(_.height/2)+'px'})
								_.layers['t-r']=_.etal.clone()
									.css({
										right:0,
										top:_.height/4,
										bottom:_.height/4,
										width:_.width/4,
										backgroundPosition:'-'+(_.width/2+_.width/4)+'px 0'
									})
								_.layers['t-r-c']=_.layers['t-r'].clone()
									.css({backgroundPosition:'-'+(_.width/2+_.width/4)+'px -'+(_.height/2)+'px'})
								_.layers['t-t']=_.etal.clone()
									.css({
										top:0,
										height:_.height/4,
										left:_.width/4,
										right:_.width/4,
										backgroundPosition:'0 -'+(_.height/2)+'px'
									})
								_.layers['t-t-c']=_.layers['t-t'].clone()
									.css({backgroundPosition:'-'+(_.width/2)+'px -'+(_.height/2)+'px'})
								_.layers['t-b']=_.etal.clone()
									.css({
										bottom:0,
										height:_.height/4,
										left:_.width/4,
										right:_.width/4,
										backgroundPosition:'0 -'+(_.height/2+_.height/4)+'px'
									})
								_.layers['t-b-c']=_.layers['t-b'].clone()
									.css({backgroundPosition:'-'+(_.width/2)+'px -'+(_.height/2+_.height/4)+'px'})
								
								_.layers['bgc']=_.etal.clone()
									.css({
										left:_.width/4,
										right:_.width/4,
										top:_.height/4,
										bottom:_.height/4,
										backgroundColor:_.me.css('backgroundColor')
									})
									
								$.each(_.layers,function(k,d){
									d
									.css({
										position:'absolute',
										backgroundImage:_.url,
										zIndex:-1
									})
									.appendTo(_.me)
								})
								$.each(_.corners,function(k,d){
									d
									.css({
										width:_.width/4,
										height:_.height/4
									})								
								})
								_.me.css({background:'none'})
							}
						})[_.method]()
					},
					showHoverFu:function(){
						$.each(_.hovers,function(k,d){
							if($.browser.msie&&$.browser.version<9)
							d.show()
							else
							d
								.stop()
								.show()
								.animate({
									opacity:1
								},{
									duration:_.duration,
									easing:_.easing
								})
						})
						$.each(_.layers,function(k,d){
							if($.browser.msie&&$.browser.version<9)
							d.hide()
							else
							d
								.stop()
								.show()
								.animate({
									opacity:0
								},{
									duration:_.duration,
									easing:_.easing,
									complete:function(){
										d.hide()
									}
								})
						})
					},
					hideHoverFu:function(){
						$.each(_.hovers,function(k,d){
							if($.browser.msie&&$.browser.version<9)
							d.hide()
							else
							d
								.stop()
								.show()
								.animate({
									opacity:0
								},{
									duration:_.duration,
									easing:_.easing,
									complete:function(){
										d.hide()
									}
								})
						})
						$.each(_.layers,function(k,d){
							if($.browser.msie&&$.browser.version<9)
							d.show()
							else
							d
								.stop()
								.show()
								.animate({
									opacity:1
								},{
									duration:_.duration,
									easing:_.easing
								})
						})
					},
					hoverFu:function(){
						_.me
							.bind('mouseenter',function(){
								_.showHoverFu()
								_.hoverIn.call(_.me,_)
							})
							.bind('mouseleave',function(){
								_.hideHoverFu()
								_.hoverOut.call(_.me,_)
							})
					},
					preFu:function(){
						var img=$(new Image())
						_.url=th.css('backgroundImage')
						_.src=_.url.replace(/(^url\('?"?)|('?"?\)$)/g,'')
						if(_.me.css('position')=='static')
							_.me.css({position:'relative'})
						
						if(_.me.css('display')=='inline')
							_.me.css({display:'inline-block'})
						
						if(_.me.css('zIndex')=='auto')
							_.me.css({zIndex:1})

						_.etal=$(_.pasta).css({position:'absolute',zIndex:-1})
						_.hh=_.me.outerHeight()
						_.hw=_.me.outerWidth()
						img
							.css({
								position:'absolute',
								left:'-999%',
								top:'-999%'
							})
							.load(function(){							
								_.width=img.width()
								_.height=img.height()
								img.remove()
								_.processFu()
							})
							.appendTo('body')
							.attr({src:_.src})
							
						if(_.hover)
							_.hoverFu()
					},
					hoverIn:function(){},
					hoverOut:function(){}
				},_)
				
				_.preFu()
				th.data({sprites:_})
			})
			return this
		}
	})
})(jQuery)
