//0.2.2 image changer
;(function($){
	$.fn.sImg=function(o,cb){
		this.each(function(){
			var th=$(this),
				data=th.data('sImg'),
				_={
					duration:1000,
					sleep:300,
					spinner:false,
					preFu:function(){
						_.me
							.css({
								width:_.me.width(),
								height:_.me.height()
							})
						_.me.css('position')=='static'
							&&_.me.css({position:'relative',zIndex:1})
						
						/*_.img
							.css({
								position:'absolute',
								zIndex:1,								
								left:_.img.prop('offsetLeft'),//_.img.prop('offsetLeft'),//_.img.css('left')
								top:_.img.prop('offsetTop')//_.img.css('top'),
								//right:_.img.css('right'),
								//bottom:_.img.css('bottom')//_.img.prop('offsetTop')
							})*/
					},					
					chngFu:function(src){
						if(src==_.src)
							return false
						_.src=src
						_.buff=_.buff||$('<img>').css({position:'absolute',top:'-999%',left:'-999%'})
						if(_.clone&&_.clone.is(':animated'))
							_.clone.stop()
						if(_.clone)
							_.clone.remove()
						if(_.spinner!==false)
							if(typeof _.spinner=='string')
								_.spinner=$(_.spinner).hide()
							else
								if(typeof _.spinner=='object')
									_.spinner
										.appendTo(_.me)										
										.fadeIn()
						_.buff
							.appendTo('body')
							.unbind('load')
							.load(function(){
								setTimeout(function(){
									_.clone=_.img.clone().appendTo(_.me)					
									_.spinner
										&&_.spinner.fadeOut()
									_.img.attr({src:src})
									_.clone
										.stop()
										.animate({
											opacity:0
										},{
											duration:_.duration,
											complete:function(){
												_.clone.remove()
												cb&&cb()
											}
										})
										_.buff.detach()
								},_.sleep)
							})
							.attr({src:src})
					},
					init:function(){
						_.img=$('>img',_.me)
						_.preFu()						
					}
				}
			data?_=data:th.data({sImg:_})
			typeof o=='object'&&$.extend(_,o)
			_.me||_.init(_.me=th)
			
			if(_.spinner!==false)
				if(typeof _.spinner=='string')
					_.spinner=$(_.spinner).hide()
			
			typeof o=='string'&&_.chngFu(o)
		})
		return this
	}
})(jQuery)
