/*! * jqzoom evolution library v2.2 - javascript image magnifier * http://www.mind-projects.it * * copyright 2011, engineer marco renzi * licensed under the bsd license. * * redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 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. * * neither the name of the organization nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * date: 11 april 2011 22:16:00 */ (function ($) { //global variables var isie6 = ($.browser.msie && $.browser.version < 7); var body = $(document.body); var window = $(window); var jqzoompluging_disabled = false; //disabilita globalmente il plugin $.fn.jqzoom = function (options) { return this.each(function () { var node = this.nodename.tolowercase(); if (node == 'a') { new jqzoom(this, options); } }); }; jqzoom = function (el, options) { var api = null; api = $(el).data("jqzoom"); if (api) return api; var obj = this; var settings = $.extend({}, $.jqzoom.defaults, options || {}); obj.el = el; el.rel = $(el).attr('rel'); //anchor element el.zoom_active = false; el.zoom_disabled = false; //to disable single zoom instance el.largeimageloading = false; //tell us if large image is loading el.largeimageloaded = false; //tell us if large image is loaded el.scale = {}; el.timer = null; el.mousepos = {}; el.mousedown = false; $(el).css({ 'outline-style': 'none', 'text-decoration': 'none' }); //base image var img = $("img:eq(0)", el); el.title = $(el).attr('title'); el.imagetitle = img.attr('title'); var zoomtitle = ($.trim(el.title).length > 0) ? el.title : el.imagetitle; var smallimage = new smallimage(img); var lens = new lens(); var stage = new stage(); var largeimage = new largeimage(); var loader = new loader(); //preventing default click,allowing the onclick event [exmple: lightbox] $(el).bind('click', function (e) { e.preventdefault(); return false; }); //setting the default zoomtype if not in settings var zoomtypes = ['standard', 'drag', 'innerzoom', 'reverse']; if ($.inarray($.trim(settings.zoomtype), zoomtypes) < 0) { settings.zoomtype = 'standard'; } $.extend(obj, { create: function () { //create the main objects //create zoompad if ($(".zoompad", el).length == 0) { el.zoompad = $('
').addclass('zoompad'); img.wrap(el.zoompad); } //creating zoompup if ($(".zoompup", el).length == 0) { lens.append(); } //creating zoomwindow if ($(".zoomwindow", el).length == 0) { stage.append(); } //creating preload if ($(".zoompreload", el).length == 0) { loader.append(); } //preloading images if (settings.preloadimages || settings.zoomtype == 'drag' || settings.alwayson) { obj.load(); } obj.init(); }, init: function () { //drag option if (settings.zoomtype == 'drag') { $(".zoompad", el).mousedown(function () { el.mousedown = true; }); $(".zoompad", el).mouseup(function () { el.mousedown = false; }); document.body.ondragstart = function () { return false; }; $(".zoompad", el).css({ cursor: 'default' }); $(".zoompup", el).css({ cursor: 'move' }); } if (settings.zoomtype == 'innerzoom') { $(".zoomwrapper", el).css({ cursor: 'crosshair' }); } $(".zoompad", el).bind('mouseenter mouseover', function (event) { img.attr('title', ''); $(el).attr('title', ''); el.zoom_active = true; //if loaded then activate else load large image smallimage.fetchdata(); if (el.largeimageloaded) { obj.activate(event); } else { obj.load(); } }); $(".zoompad", el).bind('mouseleave', function (event) { obj.deactivate(); }); $(".zoompad", el).bind('mousemove', function (e) { //prevent fast mouse mevements not to fire the mouseout event if (e.pagex > smallimage.pos.r || e.pagex < smallimage.pos.l || e.pagey < smallimage.pos.t || e.pagey > smallimage.pos.b) { lens.setcenter(); return false; } el.zoom_active = true; if (el.largeimageloaded && !$('.zoomwindow', el).is(':visible')) { obj.activate(e); } if (el.largeimageloaded && (settings.zoomtype != 'drag' || (settings.zoomtype == 'drag' && el.mousedown))) { lens.setposition(e); } }); var thumb_preload = new array(); var i = 0; //binding click event on thumbnails var thumblist = new array(); thumblist = $('a').filter(function () { var regex = new regexp("gallery[\\s]*:[\\s]*'" + $.trim(el.rel) + "'", "i"); var rel = $(this).attr('rel'); if (regex.test(rel)) { return this; } }); if (thumblist.length > 0) { //getting the first to the last var first = thumblist.splice(0, 1); thumblist.push(first); } thumblist.each(function () { //preloading thumbs if (settings.preloadimages) { var thumb_options = $.extend({}, eval("(" + $.trim($(this).attr('rel')) + ")")); thumb_preload[i] = new image(); thumb_preload[i].src = thumb_options.largeimage; i++; } $(this).click(function (e) { thumblist.each(function () { $(this).removeclass('zoomthumbactive'); }); e.preventdefault(); obj.swapimage(this); return false; }); }); }, load: function () { if (el.largeimageloaded == false && el.largeimageloading == false) { var url = $(el).attr('href'); el.largeimageloading = true; largeimage.loadimage(url); } }, activate: function (e) { cleartimeout(el.timer); //show lens and zoomwindow lens.show(); stage.show(); }, deactivate: function (e) { switch (settings.zoomtype) { case 'drag': //nothing or lens.setcenter(); break; default: img.attr('title', el.imagetitle); $(el).attr('title', el.title); if (settings.alwayson) { lens.setcenter(); } else { stage.hide(); lens.hide(); } break; } el.zoom_active = false; }, swapimage: function (link) { el.largeimageloading = false; el.largeimageloaded = false; var options = new object(); options = $.extend({}, eval("(" + $.trim($(link).attr('rel')) + ")")); if (options.smallimage && options.largeimage) { var smallimage = options.smallimage; var largeimage = options.largeimage; $(link).addclass('zoomthumbactive'); $(el).attr('href', largeimage); img.attr('src', smallimage); lens.hide(); stage.hide(); obj.load(); } else { alert('error :: missing parameter for largeimage or smallimage.'); throw 'error :: missing parameter for largeimage or smallimage.'; } return false; } }); //sometimes image is already loaded and onload will not fire if (img[0].complete) { //fetching data from sallimage if was previously loaded smallimage.fetchdata(); if ($(".zoompad", el).length == 0) obj.create(); } /*========================================================, | smallimage |---------------------------------------------------------: | base image into the anchor element `========================================================*/ function smallimage(image) { var $obj = this; this.node = image[0]; this.findborder = function () { var bordertop = 0; bordertop = image.css('border-top-width'); btop = ''; var borderleft = 0; borderleft = image.css('border-left-width'); bleft = ''; if (bordertop) { for (i = 0; i < 3; i++) { var x = []; x = bordertop.substr(i, 1); if (isnan(x) == false) { btop = btop + '' + bordertop.substr(i, 1); } else { break; } } } if (borderleft) { for (i = 0; i < 3; i++) { if (!isnan(borderleft.substr(i, 1))) { bleft = bleft + borderleft.substr(i, 1) } else { break; } } } $obj.btop = (btop.length > 0) ? eval(btop) : 0; $obj.bleft = (bleft.length > 0) ? eval(bleft) : 0; }; this.fetchdata = function () { $obj.findborder(); $obj.w = image.width(); $obj.h = image.height(); $obj.ow = image.outerwidth(); $obj.oh = image.outerheight(); $obj.pos = image.offset(); $obj.pos.l = image.offset().left + $obj.bleft; $obj.pos.t = image.offset().top + $obj.btop; $obj.pos.r = $obj.w + $obj.pos.l; $obj.pos.b = $obj.h + $obj.pos.t; $obj.rightlimit = image.offset().left + $obj.ow; $obj.bottomlimit = image.offset().top + $obj.oh; }; this.node.onerror = function () { alert('problems while loading image.'); throw 'problems while loading image.'; }; this.node.onload = function () { $obj.fetchdata(); if ($(".zoompad", el).length == 0) obj.create(); }; return $obj; }; /*========================================================, | loader |---------------------------------------------------------: | show that the large image is loading `========================================================*/ function loader() { var $obj = this; this.append = function () { this.node = $('
').addclass('zoompreload').css('visibility', 'hidden').html(settings.preloadtext); $('.zoompad', el).append(this.node); }; this.show = function () { this.node.top = (smallimage.oh - this.node.height()) / 2; this.node.left = (smallimage.ow - this.node.width()) / 2; //setting position this.node.css({ top: this.node.top, left: this.node.left, position: 'absolute', visibility: 'visible' }); }; this.hide = function () { this.node.css('visibility', 'hidden'); }; return this; } /*========================================================, | lens |---------------------------------------------------------: | lens over the image `========================================================*/ function lens() { var $obj = this; this.node = $('
').addclass('zoompup'); this.append = function () { $('.zoompad', el).append($(this.node).hide()); if (settings.zoomtype == 'reverse') { this.image = new image(); this.image.src = smallimage.node.src; // fires off async $(this.node).empty().append(this.image); //$( this.node ).css({'opacity' : 1}); } }; this.setdimensions = function () { this.node.w = (parseint((settings.zoomwidth) / el.scale.x) > smallimage.w) ? smallimage.w : parseint((settings.zoomwidth) / el.scale.x); this.node.h = (parseint((settings.zoomheight) / el.scale.y) > smallimage.h) ? smallimage.h : parseint((settings.zoomheight) / el.scale.y); this.node.top = (smallimage.oh - this.node.h - 2) / 2; this.node.left = (smallimage.ow - this.node.w - 2) / 2; //centering lens this.node.css({ top: 0, left: 0, width: this.node.w + 'px', height: this.node.h + 'px', position: 'absolute', display: 'none', borderwidth: 1 + 'px' }); if (settings.zoomtype == 'reverse') { this.image.src = smallimage.node.src; $(this.node).css({ 'opacity': 1 }); $(this.image).css({ position: 'absolute', left: -(this.node.left + 1 - smallimage.bleft) + 'px', top: -(this.node.top + 1 - smallimage.btop) + 'px' }); } }; this.setcenter = function () { //calculating center position this.node.top = (smallimage.oh - this.node.h - 2) / 2; this.node.left = (smallimage.ow - this.node.w - 2) / 2; //centering lens this.node.css({ top: this.node.top, left: this.node.left }); if (settings.zoomtype == 'reverse') { $(this.image).css({ position: 'absolute', left: -(this.node.left + 1 - smallimage.bleft) + 'px', top: -(this.node.top + 1 - smallimage.btop) + 'px' }); } //centering large image largeimage.setposition(); }; this.setposition = function (e) { el.mousepos.x = e.pagex; el.mousepos.y = e.pagey; var lensleft = 0; var lenstop = 0; function overleft(lens) { return el.mousepos.x - (lens.w) / 2 < smallimage.pos.l; //el.mousepos.x - (lens.w) / 2 - smallimage.bleft < } function overright(lens) { return el.mousepos.x + (lens.w) / 2 > smallimage.pos.r; //+ smallimage.bleft } function overtop(lens) { return el.mousepos.y - (lens.h) / 2 < smallimage.pos.t; // + smallimage.btop; } function overbottom(lens) { return el.mousepos.y + (lens.h) / 2 > smallimage.pos.b; // + smallimage.btop; } lensleft = el.mousepos.x + smallimage.bleft - smallimage.pos.l - (this.node.w + 2) / 2; lenstop = el.mousepos.y + smallimage.btop - smallimage.pos.t - (this.node.h + 2) / 2; if (overleft(this.node)) { lensleft = smallimage.bleft - 1; } else if (overright(this.node)) { lensleft = smallimage.w + smallimage.bleft - this.node.w - 1; } if (overtop(this.node)) { lenstop = smallimage.btop - 1; } else if (overbottom(this.node)) { lenstop = smallimage.h + smallimage.btop - this.node.h - 1; } this.node.left = lensleft; this.node.top = lenstop; this.node.css({ 'left': lensleft + 'px', 'top': lenstop + 'px' }); if (settings.zoomtype == 'reverse') { $(this.image).css({ position: 'absolute', left: -(this.node.left + 1 - smallimage.bleft) + 'px', top: -(this.node.top + 1 - smallimage.btop) + 'px' }); } largeimage.setposition(); }; this.hide = function () { img.css({ 'opacity': 1 }); this.node.hide(); }; this.show = function () { if (settings.zoomtype != 'innerzoom' && (settings.lens || settings.zoomtype == 'drag')) { this.node.show(); } if (settings.zoomtype == 'reverse') { img.css({ 'opacity': settings.imageopacity }); } }; this.getoffset = function () { var o = {}; o.left = $obj.node.left; o.top = $obj.node.top; return o; }; return this; }; /*========================================================, | stage |---------------------------------------------------------: | window area that contains the large image `========================================================*/ function stage() { var $obj = this; this.node = $("
"); this.ieframe = $(''); this.setposition = function () { this.node.leftpos = 0; this.node.toppos = 0; if (settings.zoomtype != 'innerzoom') { //positioning switch (settings.position) { case "left": this.node.leftpos = (smallimage.pos.l - smallimage.bleft - math.abs(settings.xoffset) - settings.zoomwidth > 0) ? (0 - settings.zoomwidth - math.abs(settings.xoffset)) : (smallimage.ow + math.abs(settings.xoffset)); this.node.toppos = math.abs(settings.yoffset); break; case "top": this.node.leftpos = math.abs(settings.xoffset); this.node.toppos = (smallimage.pos.t - smallimage.btop - math.abs(settings.yoffset) - settings.zoomheight > 0) ? (0 - settings.zoomheight - math.abs(settings.yoffset)) : (smallimage.oh + math.abs(settings.yoffset)); break; case "bottom": this.node.leftpos = math.abs(settings.xoffset); this.node.toppos = (smallimage.pos.t - smallimage.btop + smallimage.oh + math.abs(settings.yoffset) + settings.zoomheight < screen.height) ? (smallimage.oh + math.abs(settings.yoffset)) : (0 - settings.zoomheight - math.abs(settings.yoffset)); break; default: this.node.leftpos = (smallimage.rightlimit + math.abs(settings.xoffset) + settings.zoomwidth < screen.width) ? (smallimage.ow + math.abs(settings.xoffset)) : (0 - settings.zoomwidth - math.abs(settings.xoffset)); this.node.toppos = math.abs(settings.yoffset); break; } } this.node.css({ 'left': '101%', 'top': this.node.toppos + 'px' }); return this; }; this.append = function () { $('.zoompad', el).append(this.node); this.node.css({ position: 'absolute', display: 'none', zindex: 5001 }); if (settings.zoomtype == 'innerzoom') { this.node.css({ cursor: 'default' }); var thickness = (smallimage.bleft == 0) ? 1 : smallimage.bleft; $('.zoomwrapper', this.node).css({ width: smallimage.w + 'px', borderwidth: thickness + 'px' }); $('.zoomwrapperimage', this.node).css({ width: '100%', height: smallimage.h + 'px' }); $('.zoomwrappertitle', this.node).css({ width: '100%', position: 'absolute' }); } else { $('.zoomwrapper', this.node).css({ width: math.round(settings.zoomwidth) + 'px' }); $('.zoomwrapperimage', this.node).css({ width: '100%', height: math.round(settings.zoomheight) + 'px' }); $('.zoomwrappertitle', this.node).css({ width: '100%', position: 'absolute' }); } //zoom title $('.zoomwrappertitle', this.node).hide(); if (settings.title && zoomtitle.length > 0) { $('.zoomwrappertitle', this.node).html(zoomtitle).show(); } $obj.setposition(); }; this.hide = function () { switch (settings.hideeffect) { case 'fadeout': this.node.fadeout(settings.fadeoutspeed, function () {}); break; default: this.node.hide(); break; } this.ieframe.hide(); }; this.show = function () { switch (settings.showeffect) { case 'fadein': this.node.fadein(); this.node.fadein(settings.fadeinspeed, function () {}); break; default: this.node.show(); break; } if (isie6 && settings.zoomtype != 'innerzoom') { this.ieframe.width = this.node.width(); this.ieframe.height = this.node.height(); this.ieframe.left = this.node.leftpos; this.ieframe.top = this.node.toppos; this.ieframe.css({ display: 'block', position: "absolute", left: this.ieframe.left, top: this.ieframe.top, zindex: 99, width: this.ieframe.width + 'px', height: this.ieframe.height + 'px' }); $('.zoompad', el).append(this.ieframe); this.ieframe.show(); }; }; }; /*========================================================, | largeimage |---------------------------------------------------------: | the large detailed image `========================================================*/ function largeimage() { var $obj = this; this.node = new image(); this.loadimage = function (url) { //showing preload loader.show(); this.url = url; this.node.style.position = 'absolute'; this.node.style.border = '0px'; this.node.style.display = 'none'; this.node.style.left = '-5000px'; this.node.style.top = '0px'; document.body.appendchild(this.node); this.node.src = url; // fires off async }; this.fetchdata = function () { var image = $(this.node); var scale = {}; this.node.style.display = 'block'; $obj.w = image.width(); $obj.h = image.height(); $obj.pos = image.offset(); $obj.pos.l = image.offset().left; $obj.pos.t = image.offset().top; $obj.pos.r = $obj.w + $obj.pos.l; $obj.pos.b = $obj.h + $obj.pos.t; scale.x = ($obj.w / smallimage.w); scale.y = ($obj.h / smallimage.h); el.scale = scale; document.body.removechild(this.node); $('.zoomwrapperimage', el).empty().append(this.node); //setting lens dimensions; lens.setdimensions(); }; this.node.onerror = function () { alert('problems while loading the big image.'); throw 'problems while loading the big image.'; }; this.node.onload = function () { //fetching data $obj.fetchdata(); loader.hide(); el.largeimageloading = false; el.largeimageloaded = true; if (settings.zoomtype == 'drag' || settings.alwayson) { lens.show(); stage.show(); lens.setcenter(); } }; this.setposition = function () { var left = -el.scale.x * (lens.getoffset().left - smallimage.bleft + 1); var top = -el.scale.y * (lens.getoffset().top - smallimage.btop + 1); $(this.node).css({ 'left': left + 'px', 'top': top + 'px' }); }; return this; }; $(el).data("jqzoom", obj); }; //es. $.jqzoom.disable('#jqzoom1'); $.jqzoom = { defaults: { zoomtype: 'standard', //innerzoom/standard/reverse/drag zoomwidth: 300, //zoomwindow default width zoomheight: 300, //zoomwindow default height xoffset: 10, //zoomwindow x offset, can be negative(more on the left) or positive(more on the right) yoffset: 0, //zoomwindow y offset, can be negative(more on the left) or positive(more on the right) position: "right", //zoomwindow default position preloadimages: true, //image preload preloadtext: 'loading zoom', title: true, lens: true, imageopacity: 0.4, alwayson: false, showeffect: 'show', //show/fadein hideeffect: 'hide', //hide/fadeout fadeinspeed: 'slow', //fast/slow/number fadeoutspeed: '2000' //fast/slow/number }, disable: function (el) { var api = $(el).data('jqzoom'); api.disable(); return false; }, enable: function (el) { var api = $(el).data('jqzoom'); api.enable(); return false; }, disableall: function (el) { jqzoompluging_disabled = true; }, enableall: function (el) { jqzoompluging_disabled = false; } }; })(jquery);