/*
	BookSlider v 1.0
	(c) Studentlitteratur 2009, Thomas Frank

*/

(function(){
		  
	var myPath="/html/js/bookSlider.js";
	var myPicPath="/images/general/bookSliderPics/";
	
	// include me two times
	if(!window.bookSliderInited){
		bookSliderInited=1;
		var x='<script src="'+myPath+'"></script>';
		x+=x;
		document.write(x)
		return
	};
	
	bookSliderInited++
	
	// create object instance
	if (bookSliderInited==2){

		bookSliders=window.bookSliders||[];
		
		bookSliders.push({
			instanceNo:bookSliders.length+1,
			currPos:0,
			fullWidth:0,
			wholeStep:385,
			$:function(x){
				return document.getElementById(x)
			},
			createContent:function(d){
				
				var titel=window.bookSliderTitle||"Aktuella titlar"
							
				this.fullWidth=Math.ceil(d.length/2)*this.wholeStep
				
				var x=this.$('bookSlider'+this.instanceNo),y=''
					
				y+='<div onclick="bookSliders['+(this.instanceNo-1)+'].buttonHandler(this,\'prev\',\'click\')" '
				if(!window.iPhone && !window.iPodTouch){
					y+=	'onmouseover="bookSliders['+(this.instanceNo-1)+'].buttonHandler(this,\'prev\',\'over\')" '+
						'onmouseout="bookSliders['+(this.instanceNo-1)+'].buttonHandler(this,\'prev\',\'out\')" '
				}
				y+=' class="printhidden" style="width:48;height:47px;cursor:pointer;position:absolute;margin-top:112px;margin-left:8px" id="bookSliderInner_prev'+this.instanceNo+'">'
				y+='<div class="iepngfix" style="width:48px;height:47px;background:url('+myPicPath+'prev.png)"></div>'
				y+='<div class="iepngfix" style="display:none;width:48px;height:47px;background:url('+myPicPath+'prev_over.png)"></div>'
				y+='<div class="iepngfix" style="display:none;width:48px;height:47px;background:url('+myPicPath+'prev_down.png)"></div>'
				y+='</div>'
				
				y+=y.replace(/prev/g,'next').replace(/margin-left:8px/g,'margin-left:329px')
				
				y+='<div style="position:absolute;z-index:3;margin-top:4px;margin-left:8px;font-family:Verdana,Arial,Helvetica,sans-serif;font-size:12px;font-weight:bold;color:#fff">'+titel+'</div>'
				y='<div>'+y+'</div>'
				
				y+='<div id="bookSliderInner'+this.instanceNo+'" style="width:385px;height:245px;width:'+(this.fullWidth*2)+'px">'
				
				var css='cursor:pointer;margin-top:5px;font-family:Verdana,Arial,Helvetica,sans-serif;font-size:11px;line-height:125%;text-align:center'
				
				var altNames=this.altNames
				
				var bookGen=function(nr){
					var x=''
					x+='<div onclick="location.href=\'/o.o.i.s/2474?artnr='+d[nr].artnr+'\'" style="cursor:pointer;height:140px"><table border="0" height="100%" cellspacing="0" cellpadding="0"><tr><td valign="bottom">'
					x+='<div style="padding-top:6px;padding-left:'+(window.Explorer6?4:8)+'px;margin-left:16px'+(!window.Explorer6?';background:url('+myPicPath+'book_bg.png) no-repeat':'')+'">'
					//x+='<div style="padding-top:6px;padding-left:'+(window.Explorer6?4:8)+'px;margin-left:16px;width:103px'+(!window.Explorer6?';background:url('+myPicPath+'book_bg.png) no-repeat':'')+'">'
					if(window.Explorer6){x+='<div style="width:85px;border-bottom:1px solid #999;border-right:1px solid #999">'}
					x+='<img src="'+d[nr].coverurl+'" style="'+(window.Explorer6?';border:3px solid #fff':'')+'">'
					if(window.Explorer6){x+='</div>'}
					x+='</div>'
					//x+='<div style="overflow:hidden;height:'+(window.Explorer6?7:11)+'px;margin-left:16px;width:103px'+(!window.Explorer6?';background:url('+myPicPath+'book_bg_lower.png) no-repeat':'')+'">'
					x+='<div style="overflow:hidden;height:'+(window.Explorer6?7:11)+'px;margin-left:16px" class="'+(!window.Explorer6?'bookSliderBgBottom':'')+'">'
					x+='&nbsp;'
					x+='</div></td></tr></table>'
					x+='</div>'
					x+='<div onclick="location.href=\'/o.o.i.s/2474?artnr='+d[nr].artnr+'\'" style="'+css+'"><b>'+(altNames[nr].titel||d[nr].titel)+'</b><br/>'+(altNames[nr].forfattare||AuthorNameFlipper(d[nr].forfattare).split(' - ').join('<br>'))+'</div>'
					return x
				}
				
				for(var i=0;i<d.length;i+=2){
					y+='<div style="float:left;width:'+this.wholeStep+'px">'
					if(!d[i+1]){
							y+='<div style="float:left;width:132px;padding-top:34px;margin-left:'+(window.Explorer6?'87px':'127px')+'">'
						y+=bookGen(i)
						y+='</div>'
					}
					else {
						y+='<div style="float:left;width:132px;padding-top:34px;margin-left:50px;overflow:hidden">'
						y+=bookGen(i)
						y+='</div>'
						y+='<div style="float:left;width:132px;padding-top:34px;margin-left:16px;overflow:hidden">'
						y+=bookGen(i+1)
						y+='</div>'
					}
					y+='</div><div style="float:left;width:'+this.wholeStep+'px">&nbsp;</div>'
				}
				
				
				y+='</div>'
				
				x.innerHTML=y;
				
				this.scrollDiv=this.$('bookSliderInner'+this.instanceNo);
				this.fullWidth*=2
				this.wholeStep*=2
				
								
				if(window.Explorer6){
					DD_belatedPNG.fix(".iepngfix")
					this.scrollDiv.style.marginLeft="-45px"
				}
				else {
					this.scrollDiv.style.marginLeft="3px"	
				}
			},
			buttonHandler:function(divObj,direction,action){
				if(this.busy){
					if(action=="over" || action=="out"){this.delayedAction=action}
					return
				}
				this.delayedAction="out";
				var divs=divObj.getElementsByTagName('div');
				divs[0].style.display=(action!="over" && action!="busy")?"block":"none"
				divs[1].style.display=(action=="over")?"block":"none"
				divs[2].style.display=(action=="busy")?"block":"none"
				if(action=="click" && direction=="prev"){this.lastClickBtn=[divObj,direction];this.scrollBookLeft()}
				if(action=="click" && direction=="next"){this.lastClickBtn=[divObj,direction];this.scrollBookRight()}
			},
			scrollBookLeft:function(){
				this.scrollBook(this.currPos+this.wholeStep)
			},
			scrollBookRight:function(){
				this.scrollBook(this.currPos-this.wholeStep)
			},
			scrollBook:function(endPos,step){
				if(!step && this.busy){return};
				if(!step && !this.busy){
					var b=this.lastClickBtn
					this.buttonHandler(b[0],b[1],"busy")
					this.busy=true
				};
				if(!step){step=50};
				if(endPos>this.currPos){this.currPos+=step}
				else if(endPos<this.currPos){this.currPos-=step};
				this.scrollDiv.style.marginLeft=(this.currPos-(window.Explorer6?45:-3))+"px";
				if (this.currPos>=this.wholeStep/2){
					this.currPos=-this.fullWidth+this.currPos;
					endPos=-this.fullWidth+this.wholeStep
				}
				else if(this.currPos<=-this.fullWidth+this.wholeStep/2){
					this.currPos=this.wholeStep/2;
					endPos=0
				};
				if (this.currPos==endPos){
					this.busy=false;
					var b=this.lastClickBtn
					this.buttonHandler(b[0],b[1],this.delayedAction)
					return
				};
				var nstep=step+1, a=arguments.callee, t=this;
				if(Math.abs(this.currPos-endPos)<step){nstep=Math.floor(nstep/2.5)};
				if(Math.abs(this.currPos-endPos)<4){nstep=1};
				setTimeout(function(){a.apply(t,[endPos,nstep])},40)
			}
		});
		
	}
	
	// create content
	if (bookSliderInited==3){
		var a=bookSliderArtNrs.split(","), b=[], c=[];
		
		// slumpa ordning
		var newa=[]
		while(newa.length<a.length){
			var num=Math.floor(Math.random()*a.length)
			if(a[num]!==undefined){newa.push(a[num])}
			a[num]=undefined
		}
		a=newa
		
		
		for(var i=0;i<a.length;i++){
			var x=a[i].split("|")
			b.push(x[0])
			c.push({titel:x[1],forfattare:x[2]})
		}
		bookSliderArtNrs=b.join(",");
		bookSliders[bookSliders.length-1].altNames=c
		var x='<div style="height:245px"><div class="printhidden" style="position:absolute;width:385;height:245px"><img src="'+myPicPath+'book_slider_bg.jpg"></div><div class="screenhidden" style="position:absolute;width:385;height:245px"><img src="'+myPicPath+'book_slider_bg_print.jpg"></div><div style="position:absolute;;overflow:hidden;width:385px;height:245px" id="bookSlider'+bookSliders.length+'"></div></div><script src="/o.o.i.s?id=1&template=../includes/basicBookInfo.i&funcName=bookSliders['+(bookSliders.length-1)+'].createContent&artnrs='+bookSliderArtNrs+'&randi='+Math.random()+'"></script>'
		bookSliderInited=false
		document.write(x)
	}

})();
