
Proto.UI.tumb=Class.create({
        initialize: function() 
        {
                      
        	this.options = Object.extend({
        	           duration:0.4,
        	           renderTo:false,
        	           width:100,
        	           height:100,
        	           imgClassName:'sha',
        	           click:function(){},
        	           clickImg:function(){},
        	           clearContainer:true,
        	           data:[]
        	           
                }, arguments[0] || { });
            new Image().src=Proto.path+'../img/loading.gif';
              this.render();   
              return(this);                     
        },
        render:function()
        {
          
          this.container = new Element("div",{style:'cursor:pointer;float:left;margin:10px;background: #f0f0f0 url('+Proto.path+'../img/loading.gif) center no-repeat'});
          if(typeof this.options.data.desc != "undefined")
          {
            this.container.title = this.options.data.desc
          }
          _(this.options.renderTo).insert(this.container);
          this.r = Raphael(this.container, this.options.width, this.options.height);
          this.i = new Image();
          this.i.src = this.options.data.src;
          this.i.className=this.options.imgClassName;
          this.i.style.cursor='pointer';
          this.i.style.zIndex=9997;
          this.i.style.position='absolute';
          _(document.body).insert(this.i);
          Element.hide(this.i);
          if(this.i.complete)
          {
          this.createTmb();
          }
          else
          {
          this.i.onload=this.createTmb.bind(this);
          }
          
          this.i.observe("click",this.options.clickImg.bind(this));

        },
        createTmb:function()
        {
                  this.origWidth=this.i.width;
                  this.origHeight=this.i.height;
                  
                  if(this.i.width<=this.i.height)
                {
                    var pw = this.i.width/(this.i.height/(this.options.height));
                    var ph = this.options.height;
                    var x = ((this.options.width)-pw)/2;
                    var y = 0;
                }
                else
                {
                    var ph = this.i.height/(this.i.width/this.options.width);
                    var pw = this.options.width;     
                    var x = 0;
                    var y = ((this.options.height)-ph)/2;                                              
                }
                
            this.img = this.r.image(this.options.data.src,0,0,pw,ph);
            this.img.click(this.options.click.bind(this));
            this.r.setSize(this.img.attr('width'),this.img.attr('height'));
            this.container.setStyle({width:this.img.attr('width')+'px',height:this.img.attr('height')+'px'});
        },
        resize:function(index)
        {
            this.img.attr({scale:index,x:0,y:0});
            this.r.setSize(this.img.attr('width'),this.img.attr('height'));
            this.container.setStyle({width:this.img.attr('width')+'px',height:this.img.attr('height')+'px'});
            this.img.attr({x:0,y:0});
        }
        
        
});
Proto.UI.galery=Class.create({
        iPhone:(navigator.userAgent.indexOf('iPad')!=-1 || navigator.userAgent.indexOf('iPhone')!=-1),
        initialize: function() 
        {
                      
        	this.options = Object.extend({
        	           duration:0.4,
        	           renderTo:document.body,
        	           background:"#fff",
        	           imgClassName:'sha',
        	           clearContainer:true,
        	           data:[{src:'http://img-fotki.yandex.ru/get/4908/andron-ned.2/0_59291_68203508_XXXL'},
                             {src:'http://img-fotki.yandex.ru/get/4403/sn-rubin.1/0_58e96_3fbd9027_XXXL'},
                             {src:'http://img-fotki.yandex.ru/get/5005/tsk61.1/0_4c082_acd635_XXL'},
                             {src:'http://img-fotki.yandex.ru/get/5603/l-svetlana-s.0/0_5bb3d_cfe3ff0_XXXL'},
                             {src:'http://img-fotki.yandex.ru/get/5005/olgamirovova.0/0_520ff_bdb66cab_XXXL'},
                             {src:'http://img-fotki.yandex.ru/get/5604/pyat-svetlana.31/0_609cc_3d0f3c66_XL'},
                             {src:'http://img-fotki.yandex.ru/get/4513/nnsmelova.2d/0_5cd48_ce0ba6d1_XXL'},
                             {src:'http://img-fotki.yandex.ru/get/5303/le3107na.5a/0_626ef_b06413e6_XXXL'}/**/]
        	           
                }, arguments[0] || { });
              this.active=false;
              this.process=false;  
              this.tmbs=[];
              this.render();    
              
          
              document.body.observe("click",function(e){
                if(this.active){ this.nextPhoto(e);}
              }.bind(this));    
              document.body.observe("touchstart",function(e){
                if(this.active){ this.nextPhoto(e);}
              }.bind(this));                    
        },
        imgClick:function(index)
        {
            if(this.process){return;}
            if(! this.tmbs[index].i.visible() )
            {
                this.imgShow(index);
            }
            else
            {
                this.imgHide(index);
            }
        },
        imgShow:function(index)
        {
            if(this.process){ return; }
            
            if(Prototype.Browser.IE)
            {
            document.documentElement.style.overflow="hidden";
            }
            else
            {
            _(document.body).style.overflow="hidden";
            }
            
            
            this.process=true;
            if( this.active )
            {
                this.imgHide(this.active-1);
            }
            
           
            (function(){
             this.overlay.show(); 
            var vpD=document.viewport.getDimensions();
            var vpO=document.viewport.getScrollOffsets(); 
             
            
            try{Element.clonePosition( this.tmbs[index].i, this.tmbs[index].container );}catch(x){ }
            
            this.tmbs[index].i.show();
            
            var wD = this.tmbs[index].origWidth;
            var hD = this.tmbs[index].origHeight;
            
            if(wD>vpD.width-200 || hD>vpD.height-200)
            {
                
                 if(wD<=hD || hD>vpD.height-200)
                {
                    var wD = wD/(hD/(vpD.height-200));
                    var hD = vpD.height-200;
                }
                else
                {
                    var hD = hD/(wD/(vpD.width-200));
                    var wD = vpD.width-200;                                 
                }
                 
            }

            var topP = (vpD.height - hD)/2 + vpO.top-50;
            var leftP = (vpD.width - wD)/2 + vpO.left;
                      
            new Effect.Morph(this.tmbs[index].i,{
                        style: {
                                    width:wD+'px',
                                    height:hD+'px',
                                    left:leftP+'px',
                                    top:topP+'px'
                                },
                        afterFinish:function(){ 
                        this.showTitles(index);
                        this.active=index+1; this.process=false; 
                        var ofL = wD+5;
                        var ofT = -30;
                        try{
                                Element.clonePosition(this.closeButton, _(this.tmbs[index].i) ,{setWidth:false,setHeight:false,offsetLeft:ofL,offsetTop:ofT});
                               
                            }catch(x){
                           // alert('e')
                            _(this.closeButton).style.top = Number(_(this.tmbs[index].i).style.top.replace('px',''))+ofT+"px";
                            _(this.closeButton).style.left =  Number(_(this.tmbs[index].i).style.left.replace('px',''))+ofL+"px";
                            }
                        this.closeButton.show(); 
                        }.bind(this), 
                        duration: this.options.duration
                    });
                                    
                       }.bind(this)).delay(this.options.duration)             
                                    
            
        },
        imgHide:function(index)
        {
             this.process=true;
            this.closeButton.hide();
            this.overlay.hide();
            this.descBlock.hide();
            this.descBlockW.hide();
                this.leftArrow.hide();
                this.rightArrow.hide();
          
           
                       if(Prototype.Browser.IE)
            {
            document.documentElement.style.overflow="auto";
            }
            else
            {
            _(document.body).style.overflow="auto";
            }
            
            
            var vpO=document.viewport.getScrollOffsets();
                new Effect.Morph(this.tmbs[index].i,{
                        style: {
                                    width:this.tmbs[index].container.getWidth()+'px',
                                    height:this.tmbs[index].container.getHeight()+'px',
                                    left:this.tmbs[index].container.viewportOffset().left+ vpO.left+'px',
                                    top:this.tmbs[index].container.viewportOffset().top+ vpO.top+'px'
                                }, 
                            afterFinish:function(){
                                this.tmbs[index].i.hide();this.active= this.process=false;
                                }.bind(this),
                            duration:this.options.duration
                                    });
        
        },
        nextPhoto:function(e)
        {
                if(this.process){ return; }
                this.process=true;
                 this.leftArrow.hide();
                this.rightArrow.hide();
                var vpD=document.viewport.getDimensions();
            var vpO=document.viewport.getScrollOffsets(); 
                this.closeButton.fade({duration: this.options.duration/3});     
                var pX =  Event.pointerX(e);
if(this.iPhone)
{
pX =  e.targetTouches[0].pageX
}
Event.stop(e);
            if(this.startIndex==false)
        	       {
        	           this.startIndex=this.active-1;
        	           this.clickCnt=0;
        	       }else{this.clickCnt++;}

             if(pX<vpD.width/2)
            {
              
                       	       var ind = indi = this.active-2;
        	       if(ind < 0 )
        	               {
        	                    ind = this.tmbs.length-1;
        	                   indi = this.tmbs.length;
        	               }
        	       if(this.startIndex==ind&&this.clickCnt>=this.tmbs.length-2){
        	                 this.descBlock.hide();
        	                 this.descBlockW.hide();
        	                 this.imgHide(this.active-1);
        	                this.startIndex=this.active=false;
        	                return;
        	                }        	               
            }else
            {
                
        	       var ind = indi =this.active;
        	       if(ind > this.tmbs.length-1 )
        	               {
        	                   ind = 0;
        	                   indi = 1;
        	               }     
        	       if(this.startIndex==indi&&this.clickCnt>=this.tmbs.length-2){
        	                 this.descBlock.hide();
        	                 this.descBlockW.hide();
        	                 this.imgHide(this.active-1);
        	                this.startIndex=this.active=false;
        	                return;
        	                }        	                          
            }
           

            var wD = this.tmbs[ind].origWidth;
            var hD = this.tmbs[ind].origHeight;
            
            if(wD>vpD.width-200 || hD>vpD.height-200)
            {
                
                 if(wD<=hD || hD>vpD.height-200)
                {
                    var wD = wD/(hD/(vpD.height-200));
                    var hD = vpD.height-200;
                }
                else
                {
                    var hD = hD/(wD/(vpD.width-200));
                    var wD = vpD.width-200;                                 
                }
                 
            }

            var topP = (vpD.height - hD)/2 + vpO.top-50;
            var leftP = (vpD.width - wD)/2 + vpO.left;
              
             
                        
            new Effect.Morph(this.descBlockW,{
                        style: {
                                    width:wD+'px',
                                    height:hD+'px',
                                    left:(leftP-35)+'px',
                                    top:(topP-35)+'px'
                                }, 
                        duration: this.options.duration
                    });  
            new Effect.Morph(this.descBlock,{
                        style: {
                                    width:(wD+70)+'px',
                                    left:(leftP-35)+'px',
                                    top:(topP+35+hD)+'px'
                                }, 
                        duration: this.options.duration
                    });
                             
                        new Effect.Fade(_(this.tmbs[this.active-1].i),{
                        duration: this.options.duration
                    });   
                    
this.tmbs[ind].i.setStyle({ width:wD+'px',
                                    height:hD+'px',
                                    left:leftP+'px',
                                    top:topP+'px'}).appear({
                        duration: this.options.duration,
                        afterFinish:function(){  this.showTitles(ind); var ofL = wD+5;
                        var ofT = -30;
                        try{
                                Element.clonePosition(this.closeButton, _(this.tmbs[ind].i) ,{setWidth:false,setHeight:false,offsetLeft:ofL,offsetTop:ofT});
                               
                            }catch(x){
                           // alert('e')
                            _(this.closeButton).style.top = Number(_(this.tmbs[ind].i).style.top.replace('px',''))+ofT+"px";
                            _(this.closeButton).style.left =  Number(_(this.tmbs[ind].i).style.left.replace('px',''))+ofL+"px";
                            }
                        this.closeButton.appear({duration: this.options.duration/3}); 
                                                }.bind(this)
                    });
                                             
            new Effect.Morph(_(this.tmbs[this.active-1].i),{
                        style: {
                                    width:wD+'px',
                                    height:hD+'px',
                                    left:leftP+'px',
                                    top:topP+'px'
                                },
                        afterFinish:function(){ 
try{
 this.tmbs[this.active-1].i.style.opacity=1;
 this.tmbs[this.active-1].i.hide();                       
}catch(x){} // Разобраться!!!
this.process=false;
this.active=ind+1;

                        }.bind(this), 
                        duration: this.options.duration
                    });
            
        },
        prevPhoto:function(e)
        {
        
        },
        showTitles:function(index)
        {
            
            var message="";
            
            
            if(typeof this.options.data[index].source=="string")
            {
            message+=this.options.data[index].desc;
            }
            if(typeof this.options.data[index].source=="string")
            {
            message+="<div style='color:gray;margin-top:10px'>"+this.options.data[index].source+"</div>";
            }
                if(message!=""){ 
                    message="<div style='font-size:1em; padding:15px;background:#000;'>"+message+"<div class='clr'></div></div>"; 
                    this.descBlock.update(message);
                    this.descBlock.show();this.descBlockW.show();
                    var ofL = (Element.getWidth(this.tmbs[index].i)-Element.getWidth(this.descBlock))/2;
                    var ofT = Element.getHeight(this.tmbs[index].i)+30;
                     try{   
                            Element.clonePosition(_(this.descBlockW), _(this.tmbs[index].i) ,{setWidth:true,setHeight:true,offsetLeft:-35,offsetTop:-35});
                            Element.clonePosition(_(this.descBlock), _(this.descBlockW) ,{setWidth:true,setHeight:false,offsetLeft:0,offsetTop:_(this.descBlockW).getHeight()});
                        }catch(x){ 
                        
                        _(this.descBlock).style.top = Number(_(this.tmbs[index].i).style.top.replace('px',''))+ofT+"px";
                        _(this.descBlock).style.left =  Number(_(this.tmbs[index].i).style.left.replace('px',''))+ofL+"px";
                        
                        }
                     
                    }
            
        },
        makeTmb:function(o,index)
        {
            var i = new Proto.UI.tumb({
                    index:index,
                    duration:this.options.duration,
        	       renderTo:this.options.renderTo,
        	       data:o,
        	       imgClassName:this.options.imgClassName,
        	       click:function(){ this.imgClick(index); }.bind(this)
                });
             
                this.tmbs[index]=i;
        },
        render:function()
        {
            if(this.options.clearContainer)
          {
          _(this.options.renderTo).update('');
          }
            this.sliderWrap = new Element("div",{style:'background:#edeef0;border-radius: 2px;padding:7px;height:19px;margin-bottom:13px;margin-top:-10px;'});
            _(this.options.renderTo).insert(this.sliderWrap);
            this.sliderWrap.insert(new Element("img",{src:Proto.path+'../img/gallery/pic_imgbig.png',style:'float:right;margin-left:6px;margin-top:2px;'}))
            this.sliderDiv = new Element("div",{ style:'float:right;background:url('+Proto.path+'../img/gallery/pic_line.png) no-repeat center;position: relative; width:188px;height:12px;margin-top:5px;' });
            this.sliderWrap.insert(this.sliderDiv);
            this.sliderHandle = new Element("div",{ style:'background:url('+Proto.path+'../img/gallery/pic_btn.png) no-repeat center; cursor:move;position: relative;width:12px;height:12px;' });
            this.sliderDiv.insert(this.sliderHandle);
            this.sliderWrap.insert(new Element("img",{src:Proto.path+'../img/gallery/pic_imgsmall.png',style:'float:right;margin-top:6px; margin-right:6px;'}))
            
            this.overlay = new Element('div',{'style':'background:#000; display:none; z-index:9900; position:absolute; left:0px; top:0px; '});
		    Element.insert(_(document.body),this.overlay);
		    this.overlay.setStyle({opacity:0.7,width:'200%', height:'200%'});
		    this.overlay.observe("touchstart",function(e){ Event.stop(e);});
            this.closeButton = new Element("div",{style:'display:none;width:25px;height:25px;position:absolute;z-index:9998; background:url('+Proto.path+'../img/gallery/pic_close.png) no-repeat center;opacity:1;cursor:pointer;'});
            Element.insert(_(document.body),this.closeButton);
            this.closeButton.observe("click",function(e){ Event.stop(e);this.imgHide(this.active-1); }.bind(this));
           document.body.style.height='100%';
		    
		    this.descBlockW=new Element("div",{style:'display:none;position:absolute;top:0px;left:0px;z-index:9991;background:#b2b2b2;padding-left:40px;padding:35px; box-shadow: 0.5em 0.5em 0.5em #888;'});
            document.body.insert(this.descBlockW);  
            this.descBlock=new Element("div",{style:'display:none;position:absolute;bottom:0px;left:0px;height:100px;background:#000;color:#fff;z-index:9991; box-shadow: 0.5em 0.5em 0.5em #888;'});
            Element.insert(_(document.body),this.descBlock);  
            

		    this.descBlockW.observe("touchstart",function(e){ Event.stop(e);});
            
            this.leftArrow=new Element("img",{style:'display:none;position:absolute;z-index:9992;','src':Proto.path+'../img/gallery/pic_left01.png'});
            Element.insert(_(document.body),this.leftArrow);  
            this.rightArrow=new Element("img",{style:'display:none;position:absolute;z-index:9992;','src':Proto.path+'../img/gallery/pic_right01.png'});
            Element.insert(_(document.body),this.rightArrow);  
            
            if(this.iPhone)
            {
                             this.closeButton.observe("touchstart",function(e){ Event.stop(e);this.imgHide(this.active-1); }.bind(this));
                            this.leftArrow.show();
                            this.rightArrow.show();             
            }
            document.body.observe("mousemove",function(e){
            if(!this.active || this.process){ 
                            this.leftArrow.hide();
                            this.rightArrow.hide(); 
                        return;
                }
            var vpD=document.viewport.getDimensions();
            if(Event.pointerX(e)<vpD.width/2)
            {
                Element.clonePosition(_(this.leftArrow), _(this.tmbs[this.active-1].i) ,{setWidth:0,setHeight:0,offsetLeft:-25,offsetTop:_(this.tmbs[this.active-1].i).getHeight()/2-15});
                this.leftArrow.show();
                this.rightArrow.hide();
                //window.clearTimeout(this.hdar);
                 //this.hdar=(function(){ this.leftArrow.hide(); }.bind(this)).delay(1);
               }else
               {
                Element.clonePosition(this.rightArrow, _(this.tmbs[this.active-1].i) ,{setWidth:0,setHeight:0,offsetLeft:_(this.tmbs[this.active-1].i).getWidth()+15,offsetTop:_(this.tmbs[this.active-1].i).getHeight()/2-15});
                
                this.leftArrow.hide();
                this.rightArrow.show();
                
               // window.clearTimeout(this.hdar);
                // this.hdar=(function(){ this.rightArrow.hide(); }.bind(this)).delay(1);
               }
            }.bind(this));
             
            var index=0;
            this.startIndex=false;
            this.options.data.each(function(o){ this.makeTmb(o,index); index++; }.bind(this));
           // this.sliderWrap.observe("touchstart", function(e){Event.stop(e)});
            
            
                new Control.Slider(this.sliderHandle, this.sliderDiv, {
                              range: $R(1, 4),
                              sliderValue: 1,
                              onSlide: function(value) {
                                this.tmbs.each(function(t){t.resize(value)});
                              }.bind(this),
                              onChange: function(value) { 
                                this.tmbs.each(function(t){t.resize(value)});
                              }.bind(this)
                    });
    
            
        }
        
});


////////////////





Proto.UI.galeryLite=Class.create({
        iPhone:(navigator.userAgent.indexOf('iPad')!=-1 || navigator.userAgent.indexOf('iPhone')!=-1),
        initialize: function() 
        {
                      
        	this.options = Object.extend({
        	           duration:0.4,
        	           renderTo:document.body,
        	           background:"#fff",
        	           imgClassName:'sha',
        	           clearContainer:true,
        	           data:[{src:'http://img-fotki.yandex.ru/get/4908/andron-ned.2/0_59291_68203508_XXXL'},
                             {src:'http://img-fotki.yandex.ru/get/4403/sn-rubin.1/0_58e96_3fbd9027_XXXL'},
                             {src:'http://img-fotki.yandex.ru/get/5005/tsk61.1/0_4c082_acd635_XXL'},
                             {src:'http://img-fotki.yandex.ru/get/5603/l-svetlana-s.0/0_5bb3d_cfe3ff0_XXXL'},
                             {src:'http://img-fotki.yandex.ru/get/5005/olgamirovova.0/0_520ff_bdb66cab_XXXL'},
                             {src:'http://img-fotki.yandex.ru/get/5604/pyat-svetlana.31/0_609cc_3d0f3c66_XL'},
                             {src:'http://img-fotki.yandex.ru/get/4513/nnsmelova.2d/0_5cd48_ce0ba6d1_XXL'},
                             {src:'http://img-fotki.yandex.ru/get/5303/le3107na.5a/0_626ef_b06413e6_XXXL'}/**/]
        	           
                }, arguments[0] || { });
              this.active=false;
              this.process=false;  
              this.tmbs=[];
              this.render();    
              
          
              document.body.observe("click",function(e){
                if(this.active){ this.nextPhoto(e);}
              }.bind(this));    
              document.body.observe("touchstart",function(e){
                if(this.active){ this.nextPhoto(e);}
              }.bind(this));                    
        },
        imgClick:function(index)
        {
           
                       
            Proto.go(this.options.data[index].url);
        },
        makeTmb:function(o,index)
        {
            var i = new Proto.UI.tumb({
                    index:index,
                    duration:this.options.duration,
        	       renderTo:this.options.renderTo,
        	       data:o,
        	       imgClassName:this.options.imgClassName,
        	       click:function(){ this.imgClick(index); }.bind(this)
                });
             
                this.tmbs[index]=i;
        },
        render:function()
        {
            if(this.options.clearContainer)
          {
          _(this.options.renderTo).update('');
          }
            var index=0;
            this.startIndex=false;
            this.options.data.each(function(o){ this.makeTmb(o,index); index++; }.bind(this));
        }
        
});