

var GoodsPic = new Class({
    options:{
       readingGlass:false,
       readingGlassWidth:false,
       readingGlassHeight:false
    },
	initialize: function(maxW,maxH,options){
        var options=$extend(this.options,options);
        
        this.readingGlass=options.readingGlass;
        
		this.container = $E('.goods-detail-pic-thumbnail');
 
        
		this.imgs = $ES('img', this.container);
		this.imgEl = $E('.goods-detail-pic','goods-viewer').getElement('a');
        
		if(!this.container || !(this.imgs&&this.imgs.length>0)) return;
        
		this.maxHeight=maxH||$E('.goods-detail-pic','goods-viewer').getSize().y;
		this.maxWidth=maxW||$E('.goods-detail-pic','goods-viewer').getSize().x;
        
        
		this.imgs.each(function(img, index){
            var _this=this;
            img.addEvent('load',function(i){
             
             
            
            });
			img.getParent('a').addEvent('click', function(e){
                 e?e.stop():null;
                 this.blur();
                 var box=this.getParent('td');
                 var prt=this.getParent('table');
                 
                 if(box.hasClass('current'))return;
                 var tc=prt.retrieve('tc');
                 if(tc)
                 tc.removeClass('current');
                 box.addClass('current');
                _this.changeImg(img);
                 prt.store('tc',box);
            });
		},this);
        
       
        
        this.imgs[0].getParent('a').fireEvent('click');
      
 
        
        if(this.readingGlass){
           this.readingGlassBox=new Element('div',{
              styles:{
                'width':options.readingGlassWidth||this.maxWidth,
                'height':options.readingGlassHeight||this.maxHeight,
                'position':'absolute',
                'zIndex':200,
                'display':'none',
                'background':'#fff',
                'overflow':'hidden',
                'border':'1px solid #333'
              },
              'class':'readingGlassBox'
           }).inject(document.body);
           this.bindreadingGlass(this.imgEl);
        }
      
	},
	changeImg: function($image,callback){
         callback=callback||$empty;
         var imgsrc=$image.src,alt=$image.alt,flahView=$image.getParent('a').get('href');
	     var imgEL=this.imgEl.empty();
         var iep=imgEL.getParent();
         var that=this;
         var imgElInject=function(img){
                  imgEL.empty().adopt(img);
				  var efto={'margin-top':0};
				  if(imgEL.getSize().y<iep.getSize().y){
				     efto=$merge(efto,{'margin-top':(iep.getSize().y-imgEL.getSize().y)/2});
				  }
				  imgEL.effects({link:'cancel'}).start(efto);
         };
         if($image.retrieve('imgView')){
           return imgElInject($image.retrieve('imgView'));
         }
		 var imgSrc=$image.get('b_src')||imgsrc;
         var imgC_Src=$image.get('c_src')||imgsrc;

		 iep.addClass('goods-detail-pic_loading');

	     var _IMG= new Asset.image(imgSrc,{
		     alt:alt,
		     onload:function(){
				  var img=$(this);
				  if(img.$e)return;
				  img.zoomImg(that.maxWidth,that.maxHeight);
				  iep.removeClass('goods-detail-pic_loading');
                  imgEL.empty();
			      imgElInject(img.set('c_src',imgC_Src));
                  $image.store('imgView',img);
                  img.getParent('a').set('href',flahView);
                  callback.call(that,img);
				  img.$e=true;
			},onerror:function(){
			     iep.removeClass('goods-detail-pic_loading');
			     imgEL.setHTML('<span style="diaplay:block;font-family:Verdana;margin:10px;">NO PHOTO</span><hr/><h4>'+alt+'</h4>');
			}});
	},
    bindreadingGlass:function($imgEL){
        var _this=this;
        $imgEL.addEvent('mouseenter',function(enterEvent){
            var $img=this.getElement('img');
            if(!$img)return;
            this.setStyle('cursor','crosshair'); 
            if(window.ie6){$$('select').setStyle('visibility','hidden')}
            _this.readingGlassBox.empty().addClass('goods-detail-pic_loading').setStyle('display','').setStyles({
              'left':$E('.goodsname','goods-viewer').getPosition().x,
              'top':$E('.goodsname','goods-viewer').getPosition().y+23
           });
            if($img.retrieve('readingGlassIMG')){
              $img.retrieve('readingGlassIMG').inject(_this.readingGlassBox.empty());
              _this.readingGlassBox.removeClass('goods-detail-pic_loading');
              $imgEL.fireEvent('mousemove',enterEvent);
              return;
            }
            new Asset.image($img.get('c_src'),{
		     onload:function(){
				  if(this.$e)return;
                  this.inject(_this.readingGlassBox.empty());
                  _this.readingGlassBox.removeClass('goods-detail-pic_loading');
                  $img.store('readingGlassIMG',this);
                  $imgEL.fireEvent('mousemove',enterEvent);
                  this.$e=true;
			}});
        });
       $imgEL.addEvent('mousemove',function(e){
                      var pageXY=e.page;
                      var imgSize=this.getSize();
                      var imgPos=this.getPosition();
                      var curXY={
                                  'x':(pageXY.x-imgPos.x).limit(0,imgSize.x),
                                  'y':(pageXY.y-imgPos.y).limit(0,imgSize.y)
                                 };
                      var inverse={x:(curXY.x/imgSize.x).toFloat(),y:(curXY.y/imgSize.y).toFloat()};
                      var rgboxScrollSize=_this.readingGlassBox.getScrollSize();
                     
                      _this.readingGlassBox.scrollTo(
                      (rgboxScrollSize.x*inverse.x).toInt()-((_this.options.readingGlassWidth||_this.maxWidth)/2).toInt(),
                      (rgboxScrollSize.y*inverse.y).toInt()-((_this.options.readingGlassHeight||_this.maxHeight)/2).toInt()
                      );
                  });
       $imgEL.addEvent('mouseleave',function(){
             this.setStyle('cursor','pointer');
             if(window.ie6){$$('select').setStyle('visibility','visible')}
            _this.readingGlassBox.setStyle('display','none').removeClass('goods-detail-pic_loading');
           
        });
    }
});

