// JavaScript Document
$(function(){
	$.fn.sbScroll2011=function(options){
		var defaults={
			speed:3000,
			imageWidth:100,
			imageHeight:100,
			pad:3,
			showimages:5,
			minOpacity:.5,
			minPercent:50,
			zIndex:5000
		}
	  options = $.extend(defaults, options);
	  var dblpad=(options.pad*2);
	  if(options.minOpacity>1){options.minOpacity=1;}else if(options.minOpacity<0){options.minOpacity=0;}
	  if(options.speed<500){options.speed=500;}
	  var $this=$(this);
	  $this.height((options.imageHeight)+"px").hide(0);
	  var imgCount=$("img",this).length;
	  if(imgCount<3){return;}
	  //there must be one more image than what is shown
	  if(imgCount<=options.showimages){options.showimages=imgCount-1;}
	  $this.width(((options.showimages*options.imageWidth)+(dblpad*options.showimages))+"px").css({"padding":"0px","margin":"0px auto","border":"0px","text-align":"","overflow":"hidden"});
	  //wrap in container then wrap each image
	  $("img",this).css({"border":"0px","padding":"0px","margin":"0px"}).wrapAll("<div class='iHolderWrap' />").wrap("<div class='iHolder' />");
	  var $iHolderWrap=$(".iHolderWrap",this)
	  $iHolderWrap.css({"position":"relative","width":"auto","height":(options.imageHeight)+"px","margin":"0px","padding":"0px","border":"0px"});
	  $(".iHolder",this).css({"position":"absolute","width":options.imageWidth+"px","height":options.imageHeight+"px","padding":"0px","margin":"0px","border":"0px","top":"0px"});
	  //middle might be 2 images if showimages is even number
	  var mid1=Math.floor(options.showimages/2);
	  var mid2=Math.ceil(options.showimages/2);
	  if(mid1<mid2){
		//odd number of images
		mid1=mid2;
	  }else{
		//even number of images
		mid2=mid1+1;
	  }
	  var stepPercent=Math.floor((100-options.minPercent)/(mid1-1));
	  var stepOpacity=((1-options.minOpacity)/(mid1-1));
	  //set the image sizes and opacity
	  var curOp=options.minOpacity-stepOpacity;
	  var curPer=options.minPercent-stepPercent;
	  var totLeft=0;
	  var viewWidth=0;
	  var posObj=new Array();
	  for(x=0;x<imgCount;x++){
		  	var img=$("img:eq("+x+")",this);
			var zI=options.zIndex;
			var newTop=0;
			  if(x<options.showimages){
				  if((x+1)<mid1){
					  curOp+=stepOpacity;
					  curPer+=stepPercent;
					  zI+=x;
				  }else if((x+1)>mid2){
					  curOp-=stepOpacity;
					  curPer-=stepPercent;
					  zI-=x
				  }else{
					  curOp=1;
					  curPer=100;
				  }
				  var w=Math.floor((options.imageWidth/100)*curPer);
				  var h=Math.floor((options.imageHeight/100)*curPer);
				  newTop=Math.floor((options.imageHeight-h)/2)
				  if(totLeft==0){
					totLeft+=options.pad;  
				  }
				  viewWidth+=w+dblpad;
				  posObj.push({left:totLeft,width:w,height:h,Opacity:curOp,zIndex:zI,top:newTop});
			  }else{
				  curOp=0;
				  var w=Math.floor((options.imageWidth/100)*curPer);
				  var h=Math.floor((options.imageHeight/100)*curPer);
				  newTop=Math.floor((options.imageHeight-h)/2);
				  zI-=x;
				  if(posObj.length<=options.showimages){
					  posObj.push({left:totLeft,width:w,height:h,Opacity:0,zIndex:zI,top:newTop});
				  }
			  }
			  img.css({"width":w+"px","height":h+"px"}).fadeTo(0,curOp);
			  img.parent().css({"left":totLeft+"px","top":newTop+"px"});
			  if(x<options.showimages){
				totLeft+=w+dblpad;  
			  }
	  }
	  this.width(viewWidth+"px");
	  this.show(0);
	  function anim(){
		//get the images holders to move
		var c=options.showimages
		var s1=Math.floor(options.speed/2);
		var imgh=$(".iHolder:lt("+(c+1)+")");
		//move first non showing image holder to last position
		$(imgh[c]).css({"left":posObj[(c-1)].left+"px"});
		for(x=0;x<=c;x++){
			var newPos=x-1;
			if(x==0){
				$(imgh[x]).css("z-index",posObj[options.showimages].zIndex);
				//fade out
				$("img",imgh[x]).fadeTo(s1,0,function(){
					var ob=$iHolderWrap.children(":first").clone();
					ob.css("left",posObj[options.showimages].left+"px");
					$iHolderWrap.append(ob);
					var ret=$iHolderWrap.children(":first").remove();
				});
			}else{
				//fade and move
				$(imgh[x]).css("z-index",posObj[newPos].zIndex);
				if(x<c){
					//do the moves
					$(imgh[x]).animate({"left":posObj[newPos].left+"px","top":posObj[newPos].top+"px"},{"duration":s1,"queue":false}).children(0).animate({"width":posObj[newPos].width+"px","height":posObj[newPos].height+"px"},{"duration":s1,"queue":false}).fadeTo(s1,posObj[newPos].Opacity);
				}else{
					//do the moves and retrigger anim ,"complete":function(){return window.setTimeout(anim,3000);}
					$(imgh[x]).animate({"left":posObj[newPos].left+"px","top":posObj[newPos].top+"px"},{"duration":s1,"queue":false,"complete":function(){return window.setTimeout(anim,options.speed);}}).children(0).animate({"width":posObj[newPos].width+"px","height":+posObj[newPos].height+"px"},{"duration":s1,"queue":false}).fadeTo(s1,posObj[newPos].Opacity);
				}
			}
		}
	  }
	  anim();
	}//end sbScroll2011
		   
});
