/*
Copyright 2010, KISSY UI Library v1.1.5
MIT Licensed
build time: Sep 19 17:41
*/
KISSY.add("imagezoom",function(g,l){function m(a,b){var c=this,d;if(!(c instanceof m))return new m(a,b);if(c.image=a=g.get(a)){c.config=b=g.merge(v,b);if(!b.bigImageSrc)if((d=e.attr(a,"data-ks-imagezoom"))&&u.test(d))b.bigImageSrc=d;b.offset=g.makeArray(b.offset);if(b.preload)(new Image).src=b.bigImageSrc;n(a,function(){c._init()})}}function n(a,b){a.complete?b():q.on(a,"load",b)}function o(a){return e.create(w,{"class":a,style:"position:absolute"})}var k=document,e=g.DOM,q=g.Event,w="<div>",u=/^.+\.(jpg|png|gif)$/i,
r=Math.round,p=["top","right","bottom","left","inner"],v={type:"standard",bigImageSrc:"",bigImageSize:[800,800],position:"right",offset:10,preload:true,timeout:120,timeoutMsg:"\u56fe\u7247\u6682\u4e0d\u53ef\u7528",zoomSize:["auto","auto"],lensIcon:true,zoomCls:""};g.augment(m,g.EventTarget,{_init:function(){this._renderUI();this._bindUI()},_renderUI:function(){var a=this.config,b=this.image;this._imgRegion=g.merge(e.offset(b),{width:b.clientWidth,height:b.clientHeight});a.lensIcon&&this._renderIcon()},_renderIcon:function(){var a=
this._imgRegion,b=o("ks-imagezoom-icon");k.body.appendChild(b);e.offset(b,{left:a.left+a.width-e.width(b),top:a.top+a.height-e.height(b)});this.lensIcon=b},_bindUI:function(){var a=this,b;q.on(a.image,"mouseenter",function(){b=g.later(function(){var c=a.config.bigImageSrc;if(a.viewer){if(a._cacheBigImageSrc&&a._cacheBigImageSrc!==c){e.attr(a.bigImage,"src",c);a._cacheBigImageSrc=c;a._updateViewerOnLoad()}}else a._createViewer();a.show()},100)});q.on(a.image,"mouseleave",function(){if(b){b.cancel();
b=l}})},_createViewer:function(){var a=this.config,b;this._renderLens();b=o("ks-imagezoom-viewer "+a.zoomCls);a=e.create("<img>",{src:a.bigImageSrc});b.appendChild(a);k.body.appendChild(b);this.bigImage=a;this.viewer=b;this._setViewerRegion();this._updateViewerOnLoad()},_updateViewerOnLoad:function(){var a=this,b=a.config,c,d=a.bigImage;if(!d.complete){c=g.later(function(){d.complete||a._showTimeoutMsg();c=l},b.timeout*1E3);n(d,function(){if(c){c.cancel();c=l}a._setViewerRegion()})}},_renderLens:function(){var a=
o("ks-imagezoom-lens");e.hide(a);k.body.appendChild(a);this.lens=a},_setViewerRegion:function(){var a=this.config,b=this.viewer,c=this._imgRegion,d=a.zoomSize,h,f,i;this._bigImageSize=f=(i=this.bigImage)?{width:a.bigImageSize[0],height:a.bigImageSize[1]}:{width:i.clientWidth,height:i.clientHeight};i=d[0];if(i==="auto")i=c.width;d=d[1];if(d==="auto")d=c.height;h=r(i*c.width/f.width);f=r(d*c.height/f.height);this._lensSize=[h,f];var j=this.lens;e.width(j,h);e.height(j,f);e.offset(this.lens,{left:c.left+
(c.width-h)/2,top:c.top+(c.height-f)/2});h=c.left+(a.offset[0]||0);f=c.top+(a.offset[1]||0);switch(a.position){case p[0]:f-=d;break;case p[1]:h+=c.width;break;case p[2]:f+=c.height;break;case p[3]:h-=i;break;case p[4]:i=c.width;d=c.height;e.css(b,"cursor","move")}e.offset(b,{left:h,top:f});a=d;e.width(b,i);e.height(b,a)},_onMouseMove:function(a){var b=this.viewer,c=this.lens,d=this._imgRegion,h=d.left,f=d.top,i=d.width;d=d.height;var j=this._lensSize,s=j[0],t=j[1];if(a.pageX>h&&a.pageX<h+i&&a.pageY>
f&&a.pageY<f+d){j=a.pageX-s/2;a=a.pageY-t/2;if(j<=h)j=h;else if(j>=i+h-s)j=i+h-s;if(a<=f)a=f;else if(a>=d+f-t)a=d+f-t;c&&e.offset(c,{left:j,top:a});b.scrollLeft=r((j-h)*this._bigImageSize.width/i);b.scrollTop=r((a-f)*this._bigImageSize.height/d)}else this.hide()},_showTimeoutMsg:function(){var a=this.config,b=this.viewer,c=g.get("p",b);if(!c){c=e.create("<p>");b.appendChild(c)}e.html(c,a.timeoutMsg)},show:function(){e.show(this.config.position!==p[4]?[this.lens,this.viewer]:this.viewer);e.hide(this.lensIcon);
q.on(k.body,"mousemove",this._onMouseMove,this)},hide:function(){e.hide([this.lens,this.viewer]);e.show(this.lensIcon);q.remove(k.body,"mousemove",this._onMouseMove,this)},set:function(a,b){if(a==="bigImageSrc")if(b&&u.test(b)){this._cacheBigImageSrc=this.config.bigImageSrc;this.config.bigImageSrc=b}}});g.ImageZoom=m});
KISSY.add("autorender",function(g){g.ImageZoom.autoRender=function(l,m){l="."+(l||"KS_Widget");g.query(l,m).each(function(n){var o=n.getAttribute("data-widget-type"),k;if(o==="ImageZoom")try{if(k=n.getAttribute("data-widget-config"))k=k.replace(/'/g,'"');new g[o](n,g.JSON.parse(k))}catch(e){}})}},{host:"imagezoom"});
