/*
Magnify, by Josh Nathanson
An image magnifier plugin for jQuery
Copyright 2008, see license at end of file
version 1.0.2

name	 magnify
type	 jQuery
param	 options                  hash                    object containing config options
param	 options[lensWidth]       int                     width of lens in px
param	 options[lensHeight]      int                     height of lens in px
param	 options[showEvent]       string                  event upon which to display large image
param 	 options[hideEvent] 	  string                  event upon which to hide large image
param 	 options[stagePlacement]  string                  placement of stage, may be 'right' or 'left'
param	 options[preload]         boolean                 preload large image or not
param	 options[loadingImage]    string                  path to 'loading' image, i.e. ajax spinner
param	 options[stageCss]        object                  addt'l styles for stage                               
param	 options[lensCss]	  	  object				  addt'l styles for lens
param	 options[onBeforeShow]	  function                pass in function to execute before large image is shown
param	 options[onAfterShow]	  function				  pass in function to execute after large image is shown
param	 options[onBeforeHide]	  function				  pass in function to execute before large image is hidden
param 	 options[onAfterHide]	  function				  pass in function to execute after large image is hidden

*** all function params get passed back the settings object as an argument ***

*/


jQuery.fn.magnify=function(d){var f={lensWidth:100,lensHeight:100,showEvent:'mouseover',hideEvent:'mouseout',stagePlacement:'right',preload:true,loadingImage:'',stageCss:{},lensCss:{},onBeforeShow:function(){},onAfterShow:function(){},onBeforeHide:function(){},onAfterHide:function(){}};d?jQuery.extend(f,d):null;return this.each(function(){var c=jQuery("img:first",this),smallimage=new Smallimage(c),a=jQuery(this),lens=new Lens(),largeimage=new Largeimage(a[0].href),largeimageloaded=false,smallimagedata={},stage=null,scale=1,running=false,loader=null,noimage=false,self=this;smallimage.loadimage();c.bind(f.showEvent,activate);function activate(e){if(!running){running=true;f.onBeforeShow.apply(smallimage,[f]);if(!largeimage){largeimage=new Largeimage(a[0].href)}lens.activate(e);lens.setposition(e);jQuery(lens.node).bind('mousemove',lens.setposition).bind(f.hideEvent,deactivate);if(largeimageloaded){stage=new Stage();stage.activate()}else{largeimage.loadimage()}f.onAfterShow.apply(smallimage,[f]);a[0].blur();return false}else deactivate(e)}function deactivate(e){f.onBeforeHide.apply(self,[f]);jQuery(lens.node).unbind('mousemove').unbind('mouseover').remove();jQuery(stage.node).remove();jQuery(largeimage.node).remove();largeimage=null;largeimageloaded=false;f.onAfterHide.apply(self,[f]);running=false;c.unbind(f.showEvent);var s=setTimeout(function(){c.bind(f.showEvent,activate)},50)}function Smallimage(a){this.node=a[0];this.loadimage=function(){this.node.src=c[0].src};this.node.onload=function(){smallimagedata.w=jQuery(this).width();smallimagedata.h=jQuery(this).height();smallimagedata.pos=jQuery(this).offset();smallimagedata.pos.r=smallimagedata.w+smallimagedata.pos.left;smallimagedata.pos.b=smallimagedata.h+smallimagedata.pos.top;if(f.preload){largeimage.loadimage()}};return this}function Largeimage(a){this.url=a;this.node=new Image();this.loadimage=function(){if(!this.node)this.node=new Image();this.node.style.position='absolute';this.node.style.display='none';this.node.style.left='-5000px';loader=new Loader();document.body.appendChild(this.node);this.node.src=this.url};this.node.onload=function(){largeimageloaded=true;this.style.display='block';var w=jQuery(this).width(),h=jQuery(this).height();this.style.display='none';scale=(w/smallimagedata.w+h/smallimagedata.h)/2;jQuery(loader.node).remove();if(running){stage=new Stage();stage.activate()}};this.node.onerror=function(){jQuery(loader.node).remove();noimage=true;if(running){stage=new Stage();stage.activate()}};return this}Largeimage.prototype.setposition=function(){this.node.style.left=Math.ceil(-scale*parseInt(lens.getoffset().left))+'px';this.node.style.top=Math.ceil(-scale*parseInt(lens.getoffset().top))+'px'};function Lens(){this.node=document.createElement("div");jQuery(this.node).css({width:f.lensWidth+'px',height:f.lensHeight+'px',backgroundColor:'white',opacity:0.6,position:'absolute',border:'1px dashed #bbbbbb',zIndex:1000,cursor:'crosshair'}).css(f.lensCss);return this}Lens.prototype.activate=function(){document.body.appendChild(this.node);this.node.style.display='block'};Lens.prototype.setposition=function(e){var a=e.type=='mousemove'?this:this.node,lensleft=e.pageX-f.lensWidth/2,lenstop=e.pageY-f.lensHeight/2,realwidth=parseInt(a.style.width)+parseInt(a.style.borderLeftWidth)+parseInt(a.style.borderRightWidth),realheight=parseInt(a.style.height)+parseInt(a.style.borderTopWidth)+parseInt(a.style.borderBottomWidth),incorner=incorner();if(!incorner){if(againstleft()){a.style.top=lenstop+'px';a.style.left=smallimagedata.pos.left+'px'}else if(againstright()){a.style.top=lenstop+'px';a.style.left=smallimagedata.pos.r-realwidth+'px'}else if(againsttop()){a.style.left=lensleft+'px';a.style.top=smallimagedata.pos.top+'px'}else if(againstbottom()){a.style.left=lensleft+'px';a.style.top=smallimagedata.pos.b-realheight+'px'}else{a.style.top=lenstop+'px';a.style.left=lensleft+'px'}}else{a.style.top=incorner=='topleft'||incorner=='topright'?smallimagedata.pos.top+'px':smallimagedata.pos.b-realheight+'px';a.style.left=incorner=='topleft'||incorner=='bottomleft'?smallimagedata.pos.left+'px':smallimagedata.pos.r-realwidth+'px'}largeimage.setposition();function againstleft(){return lensleft<smallimagedata.pos.left}function againstright(){return lensleft+realwidth>smallimagedata.pos.r}function againsttop(){return lenstop<smallimagedata.pos.top}function againstbottom(){return lenstop+realheight>smallimagedata.pos.b}function incorner(){if(againstbottom()&&againstright())return'bottomright';else if(againstbottom()&&againstleft())return'bottomleft';else if(againsttop()&&againstright())return'topright';else if(againsttop()&&againstleft())return'topleft';else return false}};Lens.prototype.getoffset=function(){var o={};o.left=parseInt(this.node.style.left)-parseInt(smallimagedata.pos.left)+'px';o.top=parseInt(this.node.style.top)-parseInt(smallimagedata.pos.top)+'px';return o};function Stage(){this.node=document.createElement("div");jQuery(this.node).css({position:'absolute',width:Math.round(f.lensWidth*scale)+'px',height:Math.round(f.lensHeight*scale)+'px',zIndex:2000,overflow:'hidden',border:'1px solid #999999',display:'none',backgroundColor:'white'}).css(f.stageCss);this.realheight=parseInt(this.node.style.height)+parseInt(this.node.style.borderTopWidth)+parseInt(this.node.style.borderBottomWidth);this.realwidth=parseInt(this.node.style.width)+parseInt(this.node.style.borderLeftWidth)+parseInt(this.node.style.borderRightWidth);var a=jQuery.browser.safari?document.body.scrollTop:document.documentElement.scrollTop;var b=document.documentElement.clientHeight+a;this.node.style.top=smallimagedata.pos.top+this.realheight>b?b-this.realheight-10+'px':smallimagedata.pos.top+'px';this.node.style.left=f.stagePlacement=='right'?smallimagedata.pos.r+10+'px':smallimagedata.pos.left-this.realwidth-10+'px';return this}Stage.prototype.activate=function(){if(noimage)this.node.appendChild(document.createTextNode('Large Image Not Found.'));else{if(!this.node.firstChild)this.node.appendChild(largeimage.node);largeimage.node.style.display='block';largeimage.setposition()}document.body.appendChild(this.node);jQuery(this.node).fadeIn(300)};function Loader(){this.node=f.loadingImage?new Image():document.createElement("div");jQuery(this.node).appendTo("body").css({top:smallimagedata.pos.top+10+'px',left:smallimagedata.pos.left+10+'px',position:'absolute'});f.loadingImage?this.node.src=f.loadingImage:this.node.appendChild(document.createTextNode(''));return this}})};
/*
+-----------------------------------------------------------------------+
| Copyright (c) 2008 Josh Nathanson                  |
| All rights reserved.                                                  |
|                                                                       |
| Redistribution and use in source and binary forms, with or without    |
| modification, are permitted provided that the following conditions    |
| are met:                                                              |
|                                                                       |
| o Redistributions of source code must retain the above copyright      |
|   notice, this list of conditions and the following disclaimer.       |
| o Redistributions in binary form must reproduce the above copyright   |
|   notice, this list of conditions and the following disclaimer in the |
|   documentation and/or other materials provided with the distribution.|
|                                                                       |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS   |
| "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT     |
| LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT  |
| OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT      |
| LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT   |
| (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  |
|                                                                       |
+-----------------------------------------------------------------------+
*/