/*
 * jQuery.bgSwitcher
 *
 * @version    0.2.7-beta
 * @author     Hiroshi Hoaki <rewish.org@gmail.com>
 * @copyright  2010-2011 Hiroshi Hoaki
 * @license    http://rewish.org/license/mit The MIT License
 * @link       http://rewish.org/javascript/jquery_bg_switcher
 */
(function(a){a.fn.bgSwitcher=function(b){return this.each(function(){a(this).data("bgSwitcher",new a.bgSwitcher(this,b))})};
a.bgSwitcher=function(c,b){this.node=a(c);
this.setOptions(b);
this.initialize()};
a.bgSwitcher.defaultOptions={images:null,interval:5000,autoStart:true,fadeSpeed:1000,loop:true,random:false,resize:false};
a.bgSwitcher.prototype={setOptions:function(b){this.options=a.extend(true,{},a.bgSwitcher.defaultOptions,b);
if(!(this.options.images instanceof Array)){throw new Error("options.images is invalid.")}if(typeof this.options.images[0]==="string"&&typeof this.options.images[1]==="number"&&typeof this.options.images[2]==="number"){this.sequence()}if(this.options.images.length<=1){throw new Error("Image must be at least more than two.")}},initialize:function(){this.preload();
this.index=-1;
this.next=this.options.random?this.random:this.order;
this.next();
this.normalSwitch(this.options.images[this.index]);
if(this.options.fadeSpeed>0){this.initFadeNode();
this.doSwitch=this.fadeSwitch}else{this.doSwitch=this.normalSwitch}if(this.options.autoStart){this.start()}if(this.options.resize){a(window).bind("resize.bgSwitcher",a.proxy(this.resizeHandler,this))}},start:function(){if(this.timeId){return}var b=this;
this.timeId=setInterval(function(){b.next();
b.doSwitch(b.options.images[b.index])},b.options.interval)},stop:function(){if(this.timeId){clearInterval(this.timeId);
this.timeId=null}},toggle:function(){if(this.timeId){this.stop()}else{this.start()}},reset:function(){this.index=0;
this.stop();
this.doSwitch(this.options.images[this.index]);
this.start()},order:function(){var b=this.options.images.length;
++this.index;
if(this.index===b){this.index=0}if(!this.options.loop&&this.index>=b-1){this.stop()}},random:function(){var c=this.options.images.length,b=this.index;
while(this.index===b){b=Math.floor(Math.random()*c)}this.index=b},sequence:function(){var d=[],e=this.options.images[0],c=this.options.images[1],b=this.options.images[2];
do{d.push(e.replace(/\.\w+$/,c+"$&"))}while(++c<=b);
this.options.images=d},preload:function(){this.loadedImages=[];
for(var c=0,b=this.options.images.length;
c<b;
++c){this.loadedImages[c]=new Image;
this.loadedImages[c].src=this.options.images[c]}},initFadeNode:function(){var b=this.node[0].tagName.toLowerCase();
if(b==="html"){throw new Error("FadeOut the HTML not allowed.")}if(b==="body"){this.initRootNode();
b="div"}var d=this.node.css("zIndex"),c=this.node.offset();
if(isNaN(d)){
	d=1000;
	this.node.css({zIndex:d})}this.fadeNode=a("<"+b+">");
	this.fadeNode.css({display:"block",position:"absolute",zIndex:d-1,top:24,left:32,width:this.node.innerWidth(),height:this.node.innerHeight(),backgroundImage:this.node.css("backgroundImage"),backgroundPosition:this.node.css("backgroundPosition")||[this.node.css("backgroundPositionX"),this.node.css("backgroundPositionY")].join(" "),backgroundRepeat:this.node.css("backgroundRepeat"),backgroundColor:this.node.css("backgroundColor"),backgroundAttachment:this.node.css("backgroundAttachment")});
this.origNode=this.node;
this.origNode.css({position:"absolute",background:"none"});
this.node=this.fadeNode.clone();
this.node.css("zIndex",d-2);
this.origNode.after(this.fadeNode,this.node)},initRootNode:function(){var h="bgSwitcher-"+(+new Date);
a("> *",this.node).not("script").wrapAll('<div id="'+h+'">');
var b=a("#"+h),d=this.node;
var f={backgroundImage:d.css("backgroundImage"),backgroundPosition:d.css("backgroundPosition")||[d.css("backgroundPositionX"),d.css("backgroundPositionY")].join(" "),backgroundRepeat:d.css("backgroundRepeat"),backgroundColor:d.css("backgroundColor"),backgroundAttachment:d.css("backgroundAttachment")};
var e=["Top","Bottom","Right","Left"];
for(var c=0;
c<4;
++c){var g="padding"+e[c];
f[g]=+d.css("margin"+e[c]).replace(/\D/g,"");
f[g]+=+d.css("padding"+e[c]).replace(/\D/g,"");
f[g]+="px"}b.css(f);
d.css({margin:0,padding:0,background:"none"});
this.node=b;
this.options.resize=true},resizeHandler:function(){var b=this.origNode.innerWidth();
this.node.width(b);
this.fadeNode.width(b)},normalSwitch:function(b){this.node.css("backgroundImage","url("+b+")")},fadeSwitch:function(c){var b=this;
this.fadeNode.stop(true,true);
this.fadeNode.css("backgroundImage",this.node.css("backgroundImage"));
this.fadeNode.show(0,function(){b.node.css("backgroundImage","url("+c+")");
b.fadeNode.fadeOut(b.options.fadeSpeed)})}}})(jQuery);


