jQuery.fn.extend({
	bgSlider:function(opt)
	{
		var src=this
		var block=false,keeper,strchCSS={width:'100%',height:'100%',left:0,top:0,position:'absolute','z-index':-1},
		//var block=false,keeper,strchCSS={width:'100%',height:'100%',left:0,top:0,position:'fixed','z-index':-1},
		dfl={
			interval:4000,
			speed:1500,
			pags:false,
			slideshow:false,
			preload:false,
			current:0
		}
		opt=jQuery.extend(dfl,opt)
		if(opt.pags)opt.pags=jQuery(opt.pags)
		if(opt.preload){
			var tmp=[]
			for(var i=0;i<this.length;i++){
				tmp[i]=new Image()
				tmp[i].src=this[i]
			}
		}
		var loadSrc=function(bgi){
			if(opt.pags)opt.pags.parent().eq(opt.current).addClass('current').siblings().removeClass('current');
			// use an img tag to support in-browser image scaling
			// temporary work-around with the indexOf, until all images
			// can be formatted to the same size
			/*
			var tag = "<div><img src=\"" + bgi + "\" style=\"width:100%\" /></div>";
			if(bgi.indexOf("cim001") < 0)
			{
				tag = "<div><img src=\"" + bgi + "\" style=\"height:956px\" /></div>";
			}
			*/
			// default to horz axis for scaling
			//var tag = "<div><img src=\"" + bgi + "\" style=\"width:100%\" /></div>";
			var tag = "<div><img src=\"" + bgi + "\" style=\"height:1000px\" /></div>";
			// use the screen dimensions to decide, which axis to scale the image
			// if jQuery(window).width()/jQuery(window).height() < 1.8 then use height
			//if((jQuery(window).width()/jQuery(window).height()) < 1.8)
			//{
			//	tag = "<div><img src=\"" + bgi + "\" style=\"height:100%\" /></div>";
			//}
			
			//keeper.append(t=jQuery('<div></div>').css(strchCSS).css({'background-image':'url('+bgi+')'}))
			keeper.append(t=jQuery(tag).css(strchCSS));
			t.css({opacity:0}).animate({opacity:1},opt.speed,function(){
				jQuery(this).siblings().remove();
				block=false;
			})
		}
		jQuery('body').append(keeper=jQuery('<div id="bgSlider"></div>').css(strchCSS));
		
		keeper.bind('bgSliderNext',function(){
			if(!block){
				block=true
				opt.current++
				if(!(opt.current<src.length))opt.current=0
				loadSrc(src[opt.current])
			}
		})
		keeper.bind('bgSliderPrev',function(){
			if(!block){
				block=true
				if(opt.current==0)opt.current=src.length
				opt.current--
				loadSrc(src[opt.current])
			}
		})		
		if(opt.pags) 
		{
		    jQuery(opt.pags).live('click',function()
		    {
			    if(!block)
			    {
				    block=true
				    loadSrc(src[opt.current=this.rel-1])				
			    }
			    if(opt.callback != null) opt.callback(this);
			    return false
		    })
		    if(opt.slideshow)setInterval(function(){keeper.trigger('bgSliderNext')},opt.interval)
		    loadSrc(src[opt.current])
        }
    }
})
