Object.extend(String.prototype,{
    ensureEndsWith:function(A){
        return this.endsWith(A)?this:this+A
    },
    px:function(){
        return this.ensureEndsWith("px")
    }
});
Object.extend(Number.prototype,{
    px:function(){
        return this.toString().px()
    }
});
var Window={
    size:function(){
        var C=window.innerWidth||(window.document.documentElement.clientWidth||window.document.body.clientWidth);
        var B=window.innerHeight||(window.document.documentElement.clientHeight||window.document.body.clientHeight);
        var A=window.pageXOffset||(window.document.documentElement.scrollLeft||window.document.body.scrollLeft);
        var D=window.pageYOffset||(window.document.documentElement.scrollTop||window.document.body.scrollTop);
        return{
            width:C,
            height:B,
            x:A,
            y:D
        }
    }
};

var FancyZoomBox={
    directory:"/templates/images",
    zooming:false,
    setup:false,
    init:function(C){
        if(FancyZoomBox.setup){
            return
        }
        FancyZoomBox.setup=true;
        var E=navigator.userAgent.match(/MSIE\s(\d)+/);
        if(E){
            var B=parseInt(E[1]);
            Prototype.Browser["IE"+B.toString()]=true;
            Prototype.Browser.ltIE7=(B<7)?true:false
        }
        var D='<div id="zoom" style="display:none;">                   <table id="zoom_table" style="border-collapse:collapse; width:100%; height:100%;">                     <tbody>                       <tr>                         <td class="tl" style="background:url('+FancyZoomBox.directory+'/tl.png) 0 0 no-repeat; width:20px; height:20px; overflow:hidden;" />                         <td class="tm" style="background:url('+FancyZoomBox.directory+'/tm.png) 0 0 repeat-x; height:20px; overflow:hidden;" />                         <td class="tr" style="background:url('+FancyZoomBox.directory+'/tr.png) 100% 0 no-repeat; width:20px; height:20px; overflow:hidden;" />                       </tr>                       <tr>                         <td class="ml" style="background:url('+FancyZoomBox.directory+'/ml.png) 0 0 repeat-y; width:20px; overflow:hidden;" />                         <td class="mm" style="background:#fff; vertical-align:top; padding:10px;">                           <div id="zoom_content">                           </div>                         </td>                         <td class="mr" style="background:url('+FancyZoomBox.directory+'/mr.png) 100% 0 repeat-y;  width:20px; overflow:hidden;" />                       </tr>                       <tr>                         <td class="bl" style="background:url('+FancyZoomBox.directory+'/bl.png) 0 100% no-repeat; width:20px; height:20px; overflow:hidden;" />                         <td class="bm" style="background:url('+FancyZoomBox.directory+'/bm.png) 0 100% repeat-x; height:20px; overflow:hidden;" />                         <td class="br" style="background:url('+FancyZoomBox.directory+'/br.png) 100% 100% no-repeat; width:20px; height:20px; overflow:hidden;" />                       </tr>                     </tbody>                   </table>                   <a href="#" title="Close" id="zoom_close" style="position:absolute; top:0; left:0;">                     <img src="'+FancyZoomBox.directory+'/closebox.png" alt="Close" style="border:none; margin:0; padding:0;" />                   </a>                 </div>';
        var A=$$("body").first();
        A.insert(D);
        FancyZoomBox.zoom=$("zoom");
        FancyZoomBox.zoom_table=$("zoom_table");
        FancyZoomBox.zoom_close=$("zoom_close");
        FancyZoomBox.zoom_content=$("zoom_content");
        FancyZoomBox.zoom_close.observe("click",FancyZoomBox.hide);
        FancyZoomBox.middle_row=$A([$$("td.ml"),$$("td.mm"),$$("td.mr")]).flatten();
        FancyZoomBox.cells=FancyZoomBox.zoom_table.select("td");
        $$("html").first().observe("click",function(H){
            var G=H.findElement("#zoom"),F=FancyZoomBox.zoom.getStyle("display");
            if(F=="block"&&!G){
                FancyZoomBox.hide(H)
            }
        });
        $(document).observe("keyup",function(G){
            var F=FancyZoomBox.zoom.getStyle("display");
            if(G.keyCode==Event.KEY_ESC&&F=="block"){
                FancyZoomBox.hide(G)
            }
        });
        if(Prototype.Browser.ltIE7){
            FancyZoomBox.switchBackgroundImagesTo("gif")
        }
    },
    show:function(E){
        console.log(E);
        E.stop();
        if(FancyZoomBox.zooming){
            return
        }
        FancyZoomBox.zooming=true;
        var C=E.findElement("a");
        var D=C.content_div;
        var A=(C.zoom_width||D.getWidth())+60;
        var I=(C.zoom_height||D.getHeight())+60;
        var F=Window.size();
        var B=document.viewport.getScrollOffsets()[1];
        var G=Math.min(5, Math.max((F.height/2)-(I/2)+B,0));
       
        var H=(F.width/2)-(A/2);
        FancyZoomBox.curTop=E.pointerY();
        FancyZoomBox.curLeft=E.pointerX();
        FancyZoomBox.moveX=-(FancyZoomBox.curLeft-H);
        FancyZoomBox.moveY=-(FancyZoomBox.curTop-G);
        var vTop = FancyZoomBox.curTop.px();
                
        FancyZoomBox.zoom.hide().setStyle({
            position:"fixed",
            top:vTop,
            left:FancyZoomBox.curLeft.px(),
            zIndex: 200
        });
        new Effect.Parallel([new Effect.Appear(FancyZoomBox.zoom,{
            sync:true
        }),new Effect.Move(FancyZoomBox.zoom,{
            x:FancyZoomBox.moveX,
            y:FancyZoomBox.moveY,
            sync:true
        }),new Effect.Morph(FancyZoomBox.zoom,{
            style:{
                width:A.px(),
                height:I.px()
            },
            sync:true,
            beforeStart:function(J){
                if(Prototype.Browser.IE){
                    FancyZoomBox.middle_row.invoke("setStyle",{
                        height:(I-40).px()
                    })
                }
                FancyZoomBox.fixBackgroundsForIE()
            },
            afterFinish:function(J){
                FancyZoomBox.zoom_content.innerHTML=D.innerHTML;
                FancyZoomBox.unfixBackgroundsForIE();
                FancyZoomBox.zoom_close.show();
                FancyZoomBox.zooming=false
            }
        })],{
            duration:0.5
        })
    },
    hide:function(A){
        A.stop();
        if(FancyZoomBox.zooming){
            return
        }
        FancyZoomBox.zooming=true;
        new Effect.Parallel([new Effect.Move(FancyZoomBox.zoom,{
            x:FancyZoomBox.moveX*-1,
            y:FancyZoomBox.moveY*-1,
            sync:true
        }),new Effect.Morph(FancyZoomBox.zoom,{
            style:{
                width:"1".px(),
                height:"1".px()
            },
            sync:true,
            beforeStart:function(B){
                FancyZoomBox.fixBackgroundsForIE();
                FancyZoomBox.zoom_content.innerHTML="";
                FancyZoomBox.zoom_close.hide()
            },
            afterFinish:function(B){
                FancyZoomBox.unfixBackgroundsForIE();
                FancyZoomBox.zooming=false
            }
        }),new Effect.Fade(FancyZoomBox.zoom,{
            sync:true
        })],{
            duration:0.5
        })
    },
    switchBackgroundImagesTo:function(C){
        FancyZoomBox.cells.each(function(E){
            var D=E.getStyle("background-image").gsub(/\.(png|gif|none)\)$/,"."+C+")");
            E.setStyle("background-image: "+D)
        });
        var B=FancyZoomBox.zoom_close.firstDescendant();
        var A=B.readAttribute("src").gsub(/\.(png|gif|none)$/,"."+C);
        B.writeAttribute("src",A)
    },
    fixBackgroundsForIE:function(){
        if(Prototype.Browser.IE7){
            FancyZoomBox.switchBackgroundImagesTo("gif")
        }
    },
    unfixBackgroundsForIE:function(){
        if(Prototype.Browser.IE7){
            FancyZoomBox.switchBackgroundImagesTo("png")
        }
    }
};

var FancyZoom=Class.create({
    initialize:function(A){
        this.options=arguments.length>1?arguments[1]:{};

        FancyZoomBox.init();
        this.element=$(A);
        if(this.element){
            //this.element.content_div=$(this.element.readAttribute("href").gsub(/^#/,""));
            this.element.content_div = $(this.element.readAttribute('rel'));
            this.element.content_div.hide();
            this.element.zoom_width=this.options.width;
            this.element.zoom_height=this.options.height;
            if (this.element.readAttribute('target') != '_lightbox') is.element.observe("click",FancyZoomBox.show);
        }
    },
    show:function(){
        if(FancyZoomBox.zooming){
            return
        }
        FancyZoomBox.zooming=true;
        var C=this.element;
        var D=C.content_div;
        var A=(C.zoom_width||D.getWidth())+60;
        var I=(C.zoom_height||D.getHeight())+60;
        var F=Window.size();
        var B=document.viewport.getScrollOffsets()[1];
        var G=Math.max((F.height/2)-(I/2)+B,0);
        var H=(F.width/2)-(A/2);
        //FancyZoomBox.curTop=E.pointerY();
        //FancyZoomBox.curLeft=E.pointerX();

        FancyZoomBox.curTop=50;
        FancyZoomBox.curLeft=50;
        FancyZoomBox.moveX=-(FancyZoomBox.curLeft-H);
        FancyZoomBox.moveY=-(FancyZoomBox.curTop-G);
        FancyZoomBox.zoom.hide().setStyle({
            position:"fixed",
            top:FancyZoomBox.curTop.px(),
            left:FancyZoomBox.curLeft.px(),
            zIndex: 200
        });
        new Effect.Parallel([new Effect.Appear(FancyZoomBox.zoom,{
            sync:true
        }),new Effect.Move(FancyZoomBox.zoom,{
            x:FancyZoomBox.moveX,
            y:FancyZoomBox.moveY,
            sync:true
        }),new Effect.Morph(FancyZoomBox.zoom,{
            style:{
                width:A.px(),
                height:I.px()
            },
            sync:true,
            beforeStart:function(J){
                if(Prototype.Browser.IE){
                    FancyZoomBox.middle_row.invoke("setStyle",{
                        height:(I-40).px()
                    })
                }
                FancyZoomBox.fixBackgroundsForIE()
            },
            afterFinish:function(J){
                FancyZoomBox.zoom_content.innerHTML=D.innerHTML;
                FancyZoomBox.unfixBackgroundsForIE();
                FancyZoomBox.zoom_close.show();
                FancyZoomBox.zooming=false
            }
        })],{
            duration:0.5
        });
    }
});