Proto.UI.books=[];
Proto.UI.book=Class.create({
        process:false,
        initialize: function() 
        {
            if(navigator.userAgent.indexOf('ndroid')!=-1){return;}          
        	this.options = Object.extend({
        	           duration:0.4,
        	           container:false,
        	           pages:[],
        	           leftCover:false,
        	           rightCover:false,
        	           pagesBackgroundColor:"#eeeef0",
        	           coverBackgroundColor:"#eeeef0",
        	           width:800,
        	           height:500,
        	           cornerHeight:110,
        	           cornerWidth:160,
        	           onChange:function(){},
        	           statFunction:function(){},
        	           index:0//1 based, i.e. first page = 1
                }, arguments[0] || { });
                this.options.index--;
              this.pages =  [];
              this.length = this.options.pages.length;
              this.container=_(this.options.container);
              this.index=this.options.index;
              this.render();  
              Proto.UI.books.push(this);        
              return (this)            
        },
        render:function()
        {
            this.width=this.options.width/2;
            this.container.style.background=this.options.coverBackgroundColor;
            this.container.style.width=this.options.width+"px";
            this.container.style.height=this.options.height+"px";
            this.container.style.overflow="hidden";
            
            try{var lbg = _(this.options.leftCover).getStyle('background-color') || this.options.coverBackgroundColor; 
            var rbg = _(this.options.rightCover).getStyle('background-color') || this.options.coverBackgroundColor; }catch(x){
            var lbg = this.options.coverBackgroundColor; 
            var rbg = this.options.coverBackgroundColor; 
            }
            
            this.lcvr = new Element("div",{id:_(this.options.leftCover).identify()+'_wrappper',style:"overflow:hidden;background:"+lbg+";width:"+this.width+"px;height:"+this.options.height+"px; z-index:10; position:absolute;left:0px;top:0px;"});
            this.lcvr.update(_(this.options.leftCover).innerHTML);
            _(this.options.leftCover).remove();
            this.container.insert(this.lcvr);
            
            this.rcvr = new Element("div",{id:_(this.options.rightCover).identify()+'_wrappper',style:"overflow:hidden;background:"+rbg+";width:"+this.width+"px;height:"+this.options.height+"px; z-index:10; position:absolute;left:"+this.width+"px;top:0px;"});
            this.rcvr.update(_(this.options.rightCover).innerHTML);
            _(this.options.rightCover).remove();
            this.container.insert(this.rcvr);
            this.corners=[];
            
            
            
            for (var i = 0, length = this.options.pages.length; i < length; i++) 
             {
                       
                       var bg = this.options.pages[i].getStyle('background-color') || this.options.pagesBackgroundColor; 
                        var clr="#000";
                        this.options.pages[i].style.backgroundColor=bg;
                        this.options.pages[i].style.top="0px";
                       this.options.pages[i].style.width=this.width+"px";
                       this.options.pages[i].style.height=this.options.height+"px";
                        this.options.pages[i].style.overflow="hidden";
            
                       this.options.pages[i].style.position="static";
                       
                        this.options.pages[i].style.height=(this.options.height-this.options.cornerHeight)+"px";
                        
                        var wrapper = new Element("div",{id:_(this.options.pages[i]).identify()+'_wrappper',style:'display:none;overflow:hidden;z-index:'+((Proto.chet(i))?100-i:100+i)+';top:0px;left:0px;margin:0px;padding:0px;position:absolute;width:'+this.width+'px;height:'+this.options.height+"px"});
                        this.options.pages[i].wrap(wrapper);
                         this.options.pages[i].style.display="";
                        var botTbl = new Element("table",{style:'position:absolute;bottom:0px;border-spacing:0px;border-collapse:collapse;border:0px;margin:0px;padding:0px;width:'+this.width+'px;height:'+this.options.cornerHeight+"px"});
                        wrapper.insert(botTbl);
                        var tblb = new Element("tbody",{style:'margin:0px;padding:0px;'});
                        botTbl.insert(tblb);
                        var tbltr = new Element("tr",{style:'margin:0px;padding:0px;'});
                        tblb.insert(tbltr);
                        
                        
                        wrapper.absolutize();
                        
                        
                        if( !Proto.chet(i))
                        {
                            /*left*/
                            try{ 
                               clr = this.options.pages[i-1].getStyle('background-color');
                                
                                
                            }catch(e)
                            {
                                clr = this.options.pagesBackgroundColor;
                                
                            } 
                        var td = new Element("td",{style:'margin:0px;padding:0px;width:'+this.options.cornerWidth+'px;height:'+this.options.cornerHeight+'px'});
                        tbltr.insert(td);
                        var td1 = new Element("td",{style:'text-align:left;margin:0px;padding:0px;background:'+bg+';height:'+this.options.cornerHeight+'px;width:'+(this.width-this.options.cornerWidth)+'px'});
                        tbltr.insert(td1);
                       // td1.update("<b>"+(i+1)+"</b>");
                        var cCn = new Element("div",{ style:'margin:0px;padding:0px;width:'+this.options.cornerWidth+'px;height:'+this.options.cornerHeight+'px'});
                        td.insert(cCn);
                        var bgCorner = false;
                       
                        try{var bgCorner = Element.select(_(this.options.pages[i]),".bgCorner")[0].src;}catch(x){ }
                       
                        var crn=new Proto.UI.Corner({
                            container:cCn.identify(),
                            dura:this.options.duration*500,
                            type:'left',
                            background:bg,
                            bgImg:bgCorner ,
                            color:clr
                            
                        });bgCorner = false;
                        this.corners.push(crn);
                        cCn.observe("click",function(){this.backwd();}.bind(this));
                        wrapper.observe("mouseover",crn.open.bind(crn));
                        wrapper.observe("mouseout",crn.close.bind(crn));
                        wrapper.style.left = 0+"px";
                                               
                        }
                        else
                        {
                            /*right*/
                         try{ 
                                clr = this.options.pages[i+1].getStyle('background-color');
                                
                            }catch(e)
                            {
                                clr = this.options.pagesBackgroundColor
                            }
                       
                        var td1 = new Element("td",{style:'text-align:right;margin:0px;padding:0px;background:'+bg+';height:'+this.options.cornerHeight+'px;width:'+(this.width-this.options.cornerWidth)+'px'});
                        tbltr.insert(td1);       
                        var td = new Element("td",{style:'margin:0px;padding:0px;width:'+this.options.cornerWidth+'px;height:'+this.options.cornerHeight+'px; padding:0px;margin:0px;'});
                        tbltr.insert(td);
                        var cCn = new Element("div",{ style:'margin:0px;padding:0px;width:'+this.options.cornerWidth+'px;height:'+this.options.cornerHeight+'px'});
                        td.insert(cCn);
                       // td1.update("<b>"+(i+1)+"</b>");
                       
                        try{var bgCorner = Element.select(_(this.options.pages[i]),".bgCorner")[0].src;}catch(x){ }
                        
                        var crn = new Proto.UI.Corner({
                            container:cCn.identify(),
                            dura:this.options.duration*500,
                            bgImg:bgCorner ,
                            background:bg,
                            color:clr

                        });bgCorner = false;
                        this.corners.push(crn);
                        cCn.observe("click",function(){this.fwd();}.bind(this));
                        wrapper.observe("mouseover",crn.open.bind(crn));
                        wrapper.observe("mouseout",crn.close.bind(crn));
                           wrapper.style.left = this.width+"px";
                            wrapper.hide();
                        }
                        
                       this.pages[i]=wrapper;
                }
                
               
            var pozT=this.options.height/5-40;
            this.center = new Element("div",{style:"background: transparent url("+Proto.path.replace(/js\/$/,"")+"img/book.png) center repeat-y;width:56px;position:absolute;z-index:9000;top:0px;left:"+((this.options.width/2)-56/2+1)+"px;height:"+this.options.height+"px"});
            this.container.insert(this.center);
            
            this.center2 = new Element("div",{style:"display:none;background: transparent url("+Proto.path.replace(/js\/$/,"")+"img/book.png) center repeat-y;width:56px;position:absolute;z-index:9000;top:0px;left:"+((this.options.width/2)-56/2+1)+"px;height:"+this.options.height+"px"});
            this.container.insert(this.center2);
            
            this.sha = new Element("div",{style:"outline:1px green solid;display:none;background:url("+Proto.path.replace(/js\/$/,"")+"img/book.png) right top repeat-y;width:"+56+"px;position:absolute;z-index:9000;top:0px;left:0px;height:"+(this.options.height-this.options.cornerHeight+30)+"px"});
            this.container.insert(this.sha);
            
            this.topStaple = new Element("div",{style:"background-image:url("+Proto.path.replace(/js\/$/,"")+"img/book1.png); background-position: center; background-repeat:no-repeat ; width:52px;position:absolute;top:"+pozT+"px;left:0px;height:80px"});
            this.center.insert(this.topStaple);
            this.botStaple = new Element("div",{style:"background-image:url("+Proto.path.replace(/js\/$/,"")+"img/book1.png); background-position: center; background-repeat:no-repeat ; width:52px;position:absolute;top:"+(pozT*5)+"px;left:0px;height:80px"});
            this.center.insert(this.botStaple);
            if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent)) {
            this.fix6ie(this.center,Proto.path.replace(/js\/$/,"")+"img/book.png");
            this.fix6ie(this.center2,Proto.path.replace(/js\/$/,"")+"img/book.png");
          //  this.fix6ie(this.topStaple,Proto.path.replace(/js\/$/,"")+"img/book1.png");
          //  this.fix6ie(this.botStaple,Proto.path.replace(/js\/$/,"")+"img/book1.png");
          
            }
            this.showPage(this.options.index);

        },
        fix6ie:function(e,src){
            
            //filter: expression(fixPNG(this));
            var scale_mode = 'scale';
            e.runtimeStyle.backgroundImage = "none";
            e.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"
					+ src + "',sizingMethod='" + scale_mode + "')";
            
        },
        showPage:function(num)
        {
            this.pages.each(Element.hide);
            
                                   this.options.onChange.call();
                                   
                      if(!Proto.chet(num))
                {
                    /*left*/
                    try{ this.pages[num+1].show(); }catch(e){ }
                    try{this.pages[num-2].show();}catch(e){ }
                    try{this.pages[num+3].show();}catch(e){ }
                    try{this.pages[num].show();}catch(e){ }
                   try{this.corners[num].open.bind(this.corners[num]).call();}catch(e){ }
                   try{this.corners[num].open.bind(this.corners[num+1]).call();}catch(e){ }
                   
                  // this.options.statFunction(num+1,num+2);
                }
                else
                {
                    try{ this.pages[num-1].show(); }catch(e){ }
                    try{this.pages[num+2].show();}catch(e){ }
                    try{this.pages[num-3].show();}catch(e){ }
                     try{this.pages[num].show();}catch(e){ }
                     try{this.corners[num].open.bind(this.corners[num]).call();}catch(e){ }
                    // this.options.statFunction(num+1,num);
                }    
                
                if(num == this.length/2 || num+1 == this.length/2 )
                {
                this.topStaple.show();
                this.botStaple.show();
                }        
                else
                {
                 this.topStaple.hide();
                this.botStaple.hide();
                }  
        },
        fwd:function()
        {   
                if(this.proc){return;}
                                   this.options.onChange.call();
                if(!Proto.chet(this.index))
                {
                this.index++;
                }
                this.options.statFunction(this.index+2,this.index+3);
                this.proc=true;
                this.sha.style.left=this.width*2+"px";
                this.corners.each(function(c){c.close.bind(c).call()});
                //this.corners[this.index+1].open.bind(this.corners[this.index+1]).delay(this.options.duration*0.5);
                (function(){
                this.center2.show();
               
                this.center.style.zIndex=100; 
                    this.pages[this.index+1].show();
                    this.pages[this.index+1].select('div')[0].addClassName('booksha');
                    
                    new Effect.Tween(null, 0,this.width,
                        { 
                            duration:this.options.duration,
                            afterFinish:function()
                                { 
                                    this.index++;this.center2.hide();this.center.style.zIndex=1000;
                                    this.pages[this.index].select('div')[0].removeClassName('booksha');
                                   this.sha.hide();
                                   this.proc=false;
                                }.bind(this)
                        },function(p)
                        {
                            var oldw = (this.width-p);
                            this.pages[this.index].style.width=((oldw>0)?oldw:0)+"px";
                            this.pages[this.index+1].style.width= p+"px";
                            this.pages[this.index+1].style.left=(this.width-p)*2+"px";
                            var c2l = (this.width-p)*2+p-56/2+1;
                            this.center2.style.left=c2l+"px";
                            if(c2l<=this.options.width+this.options.width/3)
                            {
                                
                               // this.center2.fade({duration:this.options.duration*0.3});
                            }
                            this.sha.style.left=(this.width-p)*2-56/2+1+"px"; 
                        }.bind(this));
                }.bind(this)).delay(this.options.duration*0.6);
        },
        backwd:function()
        {
               if(this.proc){return;}
                this.options.onChange.call();
               if(Proto.chet(this.index))
                {
                this.index--;
                }
                 this.options.statFunction(this.index,this.index-1);
                this.proc=true;
                this.sha.style.left=this.width*2+"px";
                
                this.corners.each(function(c){c.close.bind(c).call()});(function(){
                this.center2.show();
                
                this.center.style.zIndex=100;
                    this.pages[this.index-1].show();
                    this.pages[this.index-1].select('div')[0].addClassName('booksha');
                    new Effect.Tween(null, this.width,0,
                        { 
                            duration:this.options.duration,
                            afterFinish:function()
                                { 
                                    this.index--;
                                    this.center.style.zIndex=1000;
                                    this.pages[this.index].select('div')[0].removeClassName('booksha');
                                    this.sha.hide();this.center2.hide(); this.proc=false;
                                   
                                }.bind(this)
                        },function(p)
                        {
                            var oldw = (this.width-p);
                            this.pages[this.index-1].style.width=((oldw>0)?oldw:0)+"px";
                            this.pages[this.index-1].scrollLeft=0;
                            this.pages[this.index].scrollLeft=0;
                            this.pages[this.index].style.width= p+"px";
                            this.pages[this.index].style.left=(this.width-p)*2+"px";
                            var c2l=(this.width-p)*2+p-56/2+1;
                            this.center2.style.left=c2l+"px";
                            if(c2l>=this.width)
                            {
                                this.center2.fade({duration:this.options.duration*0.3});
                            }
                            
                            this.sha.style.left=(this.width-p)*2-56/2+1+"px"; 
                            
                        }.bind(this));
                }.bind(this)).delay(this.options.duration*0.6);
        }
        
});