jQuery.fn.sifr=function(prefs){
	var p=jQuery.extend((prefs===false)?{unsifr:true}:{},arguments.callee.prefs,prefs);
	if(p.save){
		arguments.callee.prefs=jQuery.extend(p,{save:false});
	}
	if(this[0]===document){return;}
	
	if(!p.unsifr&&typeof p.before==='function'){
		p.before.apply(this,[p]);
	}
	this.each(function(){
		var t=jQuery(this);
		var a=t.children('.sIFR-alternate');
		if(a){
			t.html(a.html());
			if(p.unsifr){
				return;
			}
		}
		
		if(typeof p.beforeEach==='function'){
			p.beforeEach.apply(this,[t,p]);
		}
		
		var s=t.html('<span class="flash-replaced sIFR-replaced">'+(p.content||t.html()).replace(/^\s+|\s+$/g,'')+'</span>').children();
		a=t.append('<span class="alt sIFR-alternate">'+s.html()+'</span>').children('.sIFR-alternate');
		
		if(a.css('display')!=='none'){
			a.css('display','none');
		}
		
		var toHex=function(c){
			var h=function(n){
				if(n===0||isNaN(n)){return'00';}
				n=Math.round(Math.min(Math.max(0,n),255));
				return'0123456789ABCDEF'.charAt((n-n%16)/16)+'0123456789ABCDEF'.charAt(n%16);
			};
			c=(c)?c.replace(/rgb|\(|\)|#$/g,''):false;
			if(!c){return false;}
			if(c.indexOf(',')>-1){
				c=c.split(', ');
				return'#'+h(c[0])+h(c[1])+h(c[2]);
			}
			if(c.search('#')>-1&&c.length<=4){
				c=c.split('');
				return'#'+c[1]+c[1]+c[2]+c[2]+c[3]+c[3];
			}
			return c;
		};
		
		if(p.textTransform){
			if(p.textTransform.toLowerCase()==='uppercase'){
				s.html(s.html().toUpperCase());
			}
			
			if(p.textTransform.toLowerCase()==='lowercase'){
				s.html(s.html().toLowerCase());
			}
			
			if(p.textTransform.toLowerCase()==='capitalize'){
				var c=s.html().replace(/\>/g,'> ').split(' ');
				
				for(var i=0;i<c.length;i=i+1){
					c[i]=c[i].charAt(0).toUpperCase()+c[i].substring(1);
				}
				
				s.html(c.join(' ').replace(/\> /g,'>'));
			}
		}
		
		var f={
			flashvars:jQuery.extend({
					h:s.height()*(p.zoom||1),
					offsetLeft:p.offsetLeft||undefined,
					offsetTop:p.offsetTop||undefined,
					textAlign:p.textAlign||(/(left|center|right)/.exec(t.css('textAlign'))||['center'])[0],
					textColor:toHex(p.color||t.css('color'))||undefined,
					txt:p.content||s.html(),
					underline:(p.underline||(p.underline!==false&&t.css('textDecoration')==='underline'))?true:undefined,
					w:(p.width||s.width())*(p.zoom||1)
				},
				p.flashvars
			),
			height:p.height||s.height(),
			src:(p.path||'')+((p.path&&p.path.substr(p.path.length-1)!=='/')?'/':'')+(p.font||'')+((p.font&&p.font.indexOf('.swf')===-1)?'.swf':''),
			width:p.width||s.width(),
			wmode:'transparent'
		};
		
		f.flashvars.linkColor=toHex(p.link||t.find('a').css('color'))||f.flashvars.textColor;
		f.flashvars.hoverColor=toHex(p.hover)||f.flashvars.linkColor;
		if(p.zoom){
			f.flashvars.offsetTop=((p.offsetTop||0)+((s.height()-(s.height()*p.zoom))/2))*(p.zoomTop||1);
			f.flashvars.offsetLeft=((p.offsetLeft||0)+((s.width()-(s.width()*p.zoom))/2))*(p.zoomLeft||1);
		}
		
		t.flash(
			jQuery.extend(f,p.embedOptions),
			jQuery.extend({
				expressInstall:p.expressInstall||false,
				version:p.version||7,update:p.update||false},
				p.pluginOptions
			),
			function(f){
				var preHeight=t.height();
				var preWidth=t.width();
				s.html(jQuery.fn.flash.transform(f));
				var e=s.find(':first');
				e.css({
					verticalAlign:'text-bottom',
					display:'inline',
					width:p.width,height:p.height
				});
				var marginBottom=preHeight-t.height();
				var width=parseInt(e.css('width'),10)+parseInt(preWidth-t.width(),10);
				if(!p.height){
					e.css({marginBottom:marginBottom});
				}
				if(!p.width){
					e.css({width:width});
				}
				
				if(p.height&&p.verticalAlign==='middle'){
					e.css({
						marginTop:Math.floor((p.height-s.height())/2),
						marginBottom:Math.round((p.height-s.height())/2),
						height:s.height()
					});
					e.attr('height',s.height());
				}
				if(p.height&&p.verticalAlign==='bottom'){
					var a=t.find('.sIFR-alternate');
					e.css({
						marginTop:(p.height-s.height()),
						height:s.height()
					});
					e.attr('height',s.height());
				}
				if(p.css){
					e.css(p.css);
				}
			});
			
			if(typeof p.afterEach==='function'){
				p.afterEach.apply(this,[t,p]);
			}
		});
		
		if(!p.unsifr&&typeof p.after==='function'){
			p.after.apply(this,[p]);
		}
	};
	
	jQuery.sifr=function(prefs){jQuery().sifr(jQuery.extend({save:true},prefs));
};
jQuery.fn.unsifr=function(){return this.each(function(){jQuery(this).sifr(false);});};
