/*
 * Copyright (c) 2009 by Florian Moser @ novalab - new media engineering - http://www.novalab.ch/
 */
var Magnismo=Class.create({initialize:function(a){this.options=Object.extend({zoomInDuration:0.5,zoomInFx:Effect.Transitions.spring,fps:100,zoomOutDuration:0.4,zoomOutFx:Effect.Transitions.spring,leafthroughDuration:0.6,leafthroughFX:Effect.Transitions.sinoidal,loadTimeoutDelay:30,loadingIcon:null,album:false},a||{});this.counter=0;this.covers=new Array();this.useMagnismo=false;$$("a.Magnismo").each(this.initializeLink.bind(this));if(!this.useMagnismo){return}this.bgLayer=new Element("div",{className:"Magnismo_bgLayer"}).setStyle({position:"fixed",left:0,top:0,width:"100%",height:"100%"});this.bgLayer.hide();document.body.insert(this.bgLayer);this.panel=new Element("div",{className:"Magnismo_panel"}).setStyle({position:"absolute"});if(this.options.album){this.panel.addClassName("Magnismo_panelAlbum")}else{this.panel.addClassName("Magnismo_panelNormal")}this.panel.update(new Element("div",{className:"Magnismo_panelContainer"}).setStyle({position:"relative"}));this.panel.insert(new Element("div",{className:"Magnismo_panelClose"}).setStyle({position:"absolute"}));if(this.options.album){this.panelHandleBack=new Element("div",{className:"Magnismo_handleBack"}).observe("click",this.onHandleBackClick.bind(this));this.panel.insert(this.panelHandleBack.setStyle({position:"absolute"}));this.panelHandleForward=new Element("div",{className:"Magnismo_handleForward"}).observe("click",this.onHandleForwardClick.bind(this));this.panel.insert(this.panelHandleForward.setStyle({position:"absolute"}))}this.panel.offsetWidthLeft=null;this.panel.offsetWidthTop=null;this.panel.hide();document.body.insert(this.panel);this.currentImage=new Element("img",{className:"Magnismo_image"}).setStyle({position:"absolute"});this.currentImage.hide();document.body.insert(this.currentImage);this.activeCover=null;this.semaphore=false;this.lastEffect=null;this.imageOpen=false;this.nextImage=null;this.nextCover=null;this.loadingBox=new Element("div",{className:"Magnismo_loadingBox",style:"position: fixed;"});if(this.options.loadingIcon!=null){this.loadingBox.update(new Element("img",{src:this.options.loadingIcon}))}this.loadingBox.hide();document.body.insert(this.loadingBox)},initializeLink:function(b){if(!b.readAttribute("href")||b.readAttribute("href")==""){return}var d=b.readAttribute("href").substr(b.readAttribute("href").length-3);if(d!="png"&&d!="gif"&&d!="jpg"){return}var e=new Element("div",{className:"Magnismo_cover"}).setStyle({position:"absolute"});var c=b.down();if(c&&c.tagName=="IMG"){e.clonePosition(c)}else{e.clonePosition(b)}document.body.insert(e);e.href=b.readAttribute("href");e.title=b.readAttribute("title");e.imageLoaded=false;e.periodicalExecuter=null;e.destinationImage=null;var a=e.cumulativeOffset();e.sourcePositionLeft=a.left;e.sourcePositionTop=a.top;b.observe("click",function(f){Event.stop(f)});e.observe("click",this.onCoverClick.bind(this));this.covers[this.counter]=e;e.coverID=this.counter;this.counter++;this.useMagnismo=true},onCoverClick:function(a){if(this.semaphore||a.element()==this.activeCover){return}this.observeAnywhere=this.onAnywhereClick.bind(this);document.observe("click",this.observeAnywhere);document.observe("keydown",this.observeAnywhere);this.semaphore=true;Event.stop(a);this.activeCover=a.element();this.bgLayer.show();if(this.activeCover.imageLoaded){this.animationZoomIn()}else{this.preloadImage(this.activeCover.coverID,true,this.animationZoomIn.bind(this))}},animationZoomIn:function(){this.preloadNeighbours();this.currentImage.writeAttribute({src:this.activeCover.destinationImage.readAttribute("src")});this.currentImage.clonePosition(this.activeCover);this.currentImage.show();var b=100/this.activeCover.coverWidth*this.activeCover.destinationImageWidth;var a=100/this.activeCover.coverHeight*this.activeCover.destinationImageHeight;this.lastEffect=new Effect.Parallel([new Effect.Move(this.currentImage,{sync:true,x:this.getImagePositionLeft(this.activeCover.coverID),y:this.getImagePositionTop(this.activeCover.coverID),mode:"absolute"}),new Effect.Opacity(this.currentImage,{sync:true,from:0.3,to:1}),new Effect.Scale(this.currentImage,b,{sync:true,scaleY:false,scaleMode:{originalWidth:this.activeCover.coverWidth}}),new Effect.Scale(this.currentImage,a,{sync:true,scaleX:false,scaleMode:{originalHeight:this.activeCover.coverHeight}})],{duration:this.options.zoomInDuration,fps:this.options.fps,afterFinish:this.onAnimationZoomInEnd.bind(this),transition:this.options.zoomInFx})},animationZoomOut:function(){var b=100/this.activeCover.destinationImageWidth*this.activeCover.coverWidth;var a=100/this.activeCover.destinationImageHeight*this.activeCover.coverHeight;this.lastEffect=new Effect.Parallel([new Effect.Move(this.currentImage,{sync:true,x:this.activeCover.sourcePositionLeft,y:this.activeCover.sourcePositionTop,mode:"absolute"}),new Effect.Opacity(this.currentImage,{sync:true,from:1,to:0.3}),new Effect.Scale(this.currentImage,b,{sync:true,scaleY:false,scaleMode:{originalWidth:this.activeCover.destinationImageWidth}}),new Effect.Scale(this.currentImage,a,{sync:true,scaleX:false,scaleMode:{originalHeight:this.activeCover.destinationImageHeight}})],{duration:this.options.zoomOutDuration,transition:this.options.zoomOutFx,fps:this.options.fps,afterFinish:this.resetMagnismo.bind(this)})},onAnimationZoomInEnd:function(a){this.lastEffect=null;this.imageOpen=true;this.semaphore=false;this.panel.down().update(this.currentImage.remove());this.currentImage.setStyle({position:"relative",left:0,top:0});if(this.activeCover.title!=""){this.panel.down().insert(new Element("p").update(this.activeCover.title))}if(this.panel.offsetWidthLeft==null||this.panel.offsetWidthTop==null){this.panel.offsetWidthLeft=0;if(!isNaN(parseInt(this.panel.getStyle("padding-left")))){this.panel.offsetWidthLeft+=parseInt(this.panel.getStyle("padding-left"))}if(!isNaN(parseInt(this.currentImage.getStyle("margin-left")))){this.panel.offsetWidthLeft+=parseInt(this.currentImage.getStyle("margin-left"))}if(!isNaN(parseInt(this.panel.down().getStyle("padding-left")))){this.panel.offsetWidthLeft+=parseInt(this.panel.down().getStyle("padding-left"))}if(!isNaN(parseInt(this.panel.down().getStyle("margin-left")))){this.panel.offsetWidthLeft+=parseInt(this.panel.down().getStyle("margin-left"))}this.panel.offsetWidthTop=0;if(!isNaN(parseInt(this.panel.getStyle("padding-top")))){this.panel.offsetWidthTop+=parseInt(this.panel.getStyle("padding-top"))}if(!isNaN(parseInt(this.currentImage.getStyle("margin-top")))){this.panel.offsetWidthTop+=parseInt(this.currentImage.getStyle("margin-top"))}if(!isNaN(parseInt(this.panel.down().getStyle("padding-top")))){this.panel.offsetWidthTop+=parseInt(this.panel.down().getStyle("padding-top"))}if(!isNaN(parseInt(this.panel.down().getStyle("margin-top")))){this.panel.offsetWidthTop+=parseInt(this.panel.down().getStyle("margin-top"))}}this.panel.setStyle({left:this.getPanelPositionLeft(this.activeCover.coverID)+"px",top:this.getPanelPositionTop(this.activeCover.coverID)+"px"});this.refreshHandles();this.panel.show()},refreshHandles:function(){if(!this.options.album||this.activeCover==null){return}if(this.activeCover.coverID>0){this.panelHandleBack.setStyle({height:this.activeCover.destinationImageHeight+"px"});this.panelHandleBack.show()}else{this.panelHandleBack.hide()}if(this.activeCover.coverID<(this.covers.length-1)){this.panelHandleForward.setStyle({height:this.activeCover.destinationImageHeight+"px"});this.panelHandleForward.show()}else{this.panelHandleForward.hide()}},preloadNeighbours:function(){if(!this.options.album||this.activeCover==null){return}if(this.activeCover.coverID>0){this.preloadImage(this.activeCover.coverID-1,false)}if(this.activeCover.coverID<(this.covers.length-1)){this.preloadImage(this.activeCover.coverID+1,false)}},resetMagnismo:function(){this.currentImage.hide();this.activeCover=null;this.bgLayer.hide();this.nextCover=null;this.lastEffect=null;document.stopObserving("click",this.observeAnywhere);document.stopObserving("keydown",this.observeAnywhere)},showLoadingBox:function(){this.loadingBox.setStyle({left:Math.round((document.viewport.getWidth()-this.loadingBox.getWidth())/2)+"px",top:Math.round((document.viewport.getHeight()-this.loadingBox.getHeight())/2)+"px"});this.loadingBox.show();document.body.setStyle({cursor:"wait"})},hideLoadingBox:function(){this.loadingBox.hide();document.body.setStyle({cursor:"auto"})},onAnywhereClick:function(b){if(b.type=="keydown"&&b.keyCode!=27){return}if(this.lastEffect!=null){this.lastEffect.cancel();this.lastEffect=null}for(var a=0;a<this.covers.length;a++){this.abortImageLoading(a)}if(this.imageOpen){this.closePanel();this.imageOpen=false;this.animationZoomOut()}else{this.semaphore=false;this.resetMagnismo()}},closePanel:function(){if(this.currentImage.up().nodeName!="DIV"){return}this.panel.insert({after:this.currentImage.remove()});this.currentImage.setStyle({position:"absolute",left:this.getImagePositionLeft(this.activeCover.coverID)+"px",top:this.getImagePositionTop(this.activeCover.coverID)+"px"});this.panel.hide()},abortImageLoading:function(a){this.stopLoadTimeoutExecuter(a);if(this.covers[a].destinationImage!=null){this.covers[a].destinationImage.stopObserving("load");this.covers[a].destinationImage.stopObserving("error")}if(this.imageOpen===false&&this.covers[a]==this.activeCover){this.semaphore=false;this.hideLoadingBox();this.resetMagnismo()}},onPEAbort:function(a,b){this.abortImageLoading(b)},stopLoadTimeoutExecuter:function(a){if(this.covers[a].periodicalExecuter!=null){this.covers[a].periodicalExecuter.stop();this.covers[a].periodicalExecuter=null}},getImagePositionLeft:function(b){var a=Math.round((document.viewport.getWidth()-this.covers[b].destinationImageWidth)/2)+document.viewport.getScrollOffsets().left;if(a<0){a=0}return a},getImagePositionTop:function(b){var a=Math.round((document.viewport.getHeight()-this.covers[b].destinationImageHeight)/2)+document.viewport.getScrollOffsets().top;if(a<0){a=0}return a},getPanelPositionLeft:function(b){var a=this.getImagePositionLeft(b)-this.panel.offsetWidthLeft;if(a<0){a=0}return a},getPanelPositionTop:function(b){var a=this.getImagePositionTop(b)-this.panel.offsetWidthTop;if(a<0){a=0}return a},onHandleForwardClick:function(a){a.stop();if(!this.options.album||this.activeCover==null||this.lastEffect!=null||this.covers[this.activeCover.coverID+1]==undefined||this.covers[this.activeCover.coverID+1]==this.nextCover){return}this.nextCover=this.covers[this.activeCover.coverID+1];if(this.nextCover.imageLoaded){this.animationLeafthrough()}else{this.preloadImage(this.nextCover.coverID,true,this.animationLeafthrough.bind(this))}},onHandleBackClick:function(a){a.stop();if(!this.options.album||this.activeCover==null||this.lastEffect!=null||this.covers[this.activeCover.coverID-1]==undefined||this.covers[this.activeCover.coverID-1]==this.nextCover){return}this.nextCover=this.covers[this.activeCover.coverID-1];if(this.nextCover.imageLoaded){this.animationLeafthrough()}else{this.preloadImage(this.nextCover.coverID,true,this.animationLeafthrough.bind(this))}},animationLeafthrough:function(){this.nextImage=new Element("img",{src:this.nextCover.destinationImage.readAttribute("src"),className:"Magnismo_image"}).setStyle({width:this.activeCover.destinationImageWidth+"px",height:this.activeCover.destinationImageHeight+"px",position:"absolute",left:0,top:0});this.currentImage.insert({before:this.nextImage});var a,b;a=100*this.nextCover.destinationImageWidth/this.activeCover.destinationImageWidth;b=100*this.nextCover.destinationImageHeight/this.activeCover.destinationImageHeight;this.lastEffect=new Effect.Parallel([new Effect.Move(this.panel,{sync:true,x:this.getPanelPositionLeft(this.nextCover.coverID),y:this.getPanelPositionTop(this.nextCover.coverID),mode:"absolute"}),new Effect.Scale(this.currentImage,a,{sync:true,scaleY:false,scaleMode:{originalWidth:this.activeCover.destinationImageWidth}}),new Effect.Scale(this.currentImage,b,{sync:true,scaleX:false,scaleMode:{originalHeight:this.activeCover.destinationImageHeight}}),new Effect.Scale(this.nextImage,a,{sync:true,scaleY:false,scaleMode:{originalWidth:this.activeCover.destinationImageWidth}}),new Effect.Scale(this.nextImage,b,{sync:true,scaleX:false,scaleMode:{originalHeight:this.activeCover.destinationImageHeight}}),new Effect.Opacity(this.currentImage,{sync:true,from:1,to:0}),new Effect.Opacity(this.nextImage,{sync:true,from:0,to:1}),new Effect.Scale(this.panel.down(".Magnismo_handleBack"),b,{sync:true,scaleX:false,scaleMode:{originalHeight:this.activeCover.destinationImageHeight}}),new Effect.Scale(this.panel.down(".Magnismo_handleForward"),b,{sync:true,scaleX:false,scaleMode:{originalHeight:this.activeCover.destinationImageHeight}})],{duration:this.options.leafthroughDuration,fps:this.options.fps,afterFinish:this.onAnimationLeafthroughEnd.bind(this),transition:this.options.leafthroughFX})},onAnimationLeafthroughEnd:function(a){this.activeCover=this.nextCover;this.nextCover=null;this.currentImage=new Element("img",{src:this.activeCover.href,className:"Magnismo_image"}).setStyle({position:"relative",left:0,top:0});this.nextImage=null;this.panel.down().update(this.currentImage);if(this.activeCover.title!=""){this.panel.down().insert(new Element("p").update(this.activeCover.title))}this.lastEffect=null;this.refreshHandles();this.preloadNeighbours()},preloadImage:function(b,a){if(this.covers[b]==undefined||this.covers[b].imageLoaded==true){return}if(arguments[2]){onLoadCallback=arguments[2]}else{onLoadCallback=null}if(a){this.showLoadingBox()}this.covers[b].periodicalExecuter=new PeriodicalExecuter(this.onPEAbort.bindAsEventListener(this,b),this.options.loadTimeoutDelay);this.covers[b].destinationImage=new Element("img");this.covers[b].destinationImage.observe("load",this.onImageLoadSuccess.bindAsEventListener(this,b,onLoadCallback));this.covers[b].destinationImage.observe("error",this.onImageLoadFailure.bindAsEventListener(this,b));this.covers[b].destinationImage.writeAttribute({src:this.covers[b].href})},onImageLoadFailure:function(a,b){this.stopLoadTimeoutExecuter(b);if(this.covers[b]==this.activeCover||this.covers[b]==this.nextCover){this.closePanel();this.imageOpen=false;this.hideLoadingBox();this.semaphore=false;this.resetMagnismo();alert("File Not Found")}},onImageLoadSuccess:function(d,f,e){this.covers[f].imageLoaded=true;this.stopLoadTimeoutExecuter(f);this.hideLoadingBox();var a=new Element("img",{src:this.covers[f].destinationImage.readAttribute("src"),className:"Magnismo_image"}).hide();document.body.insert(a);var b=a.getDimensions();this.covers[f].destinationImageWidth=b.width;this.covers[f].destinationImageHeight=b.height;a.remove();var c=this.covers[f].getDimensions();this.covers[f].coverWidth=c.width;this.covers[f].coverHeight=c.height;if(e!=null){e()}}});
