/*
Copyright (c) 2009, Yahoo! Inc. All rights reserved.
Code licensed under the BSD License:
http://developer.yahoo.net/yui/license.txt
version: 2.8.0r4
*/
(function(){var k=YAHOO.util.Dom,Event=YAHOO.util.Event,Lang=YAHOO.lang;var m=function(a,b){var c={element:a,attributes:b||{}};m.superclass.constructor.call(this,c.element,c.attributes)};m._instances={};m.getCropperById=function(a){if(m._instances[a]){return m._instances[a]}return false};YAHOO.extend(m,YAHOO.util.Element,{CSS_MAIN:'yui-crop',CSS_MASK:'yui-crop-mask',CSS_RESIZE_MASK:'yui-crop-resize-mask',CSS_RESIZE_SUB_MASK:'yui-crop-resize-sub-mask',_image:null,_active:null,_resize:null,_resizeEl:null,_resizeMaskEl:null,_wrap:null,_mask:null,_createWrap:function(){this._wrap=document.createElement('div');this._wrap.id=this.get('element').id+'_wrap';this._wrap.className=this.CSS_MAIN;var b=this.get('element');this._wrap.style.width=b.width?b.width+'px':k.getStyle(b,'width');this._wrap.style.height=b.height?b.height+'px':k.getStyle(b,'height');var c=this.get('element').parentNode;c.replaceChild(this._wrap,this.get('element'));this._wrap.appendChild(this.get('element'));Event.on(this._wrap,'mouseover',this._handleMouseOver,this,true);Event.on(this._wrap,'mouseout',this._handleMouseOut,this,true);Event.on(this._wrap,'click',function(a){Event.stopEvent(a)},this,true)},_createMask:function(){this._mask=document.createElement('div');this._mask.className=this.CSS_MASK;this._wrap.appendChild(this._mask)},_createResize:function(){this._resizeEl=document.createElement('div');this._resizeEl.className=YAHOO.util.Resize.prototype.CSS_RESIZE;this._resizeEl.style.position='absolute';if(this.get('initSizeAddPercWidth')>0){var a=(100-(this.get('initSizeAddPercWidth')*2));var b=(100-(this.get('initSizeAddPercHeight')*2));this._resizeEl.innerHTML='<div class="'+this.CSS_RESIZE_MASK+'"><div class="'+this.CSS_RESIZE_SUB_MASK+'" style="top: '+this.get('initSizeAddStartFromTop')+'px; left: '+this.get('initSizeAddStartFromLeft')+'px; width: '+a+'%; height: '+b+'%;"></div></div>'}else{this._resizeEl.innerHTML='<div class="'+this.CSS_RESIZE_MASK+'"></div>'}this._resizeMaskEl=this._resizeEl.firstChild;this._wrap.appendChild(this._resizeEl);this._resizeEl.style.top=this.get('initialXY')[1]+'px';this._resizeEl.style.left=this.get('initialXY')[0]+'px';this._resizeMaskEl.style.height=Math.floor(this.get('initHeight'))+'px';this._resizeMaskEl.style.width=Math.floor(this.get('initWidth'))+'px';this._resize=new YAHOO.util.Resize(this._resizeEl,{knobHandles:true,handles:'all',draggable:true,status:this.get('status'),minWidth:this.get('minWidth'),minHeight:this.get('minHeight'),ratio:this.get('ratio'),autoRatio:this.get('autoRatio'),height:this.get('initHeight'),width:this.get('initWidth')});this._setBackgroundImage(this.get('element').getAttribute('src',2));this._setBackgroundPosition(-(this.get('initialXY')[0]),-(this.get('initialXY')[1]));this._resize.on('startResize',this._handleStartResizeEvent,this,true);this._resize.on('endResize',this._handleEndResizeEvent,this,true);this._resize.on('dragEvent',this._handleDragEvent,this,true);this._resize.on('beforeResize',this._handleBeforeResizeEvent,this,true);this._resize.on('resize',this._handleResizeEvent,this,true);this._resize.dd.on('b4StartDragEvent',this._handleB4DragEvent,this,true)},_handleMouseOver:function(a){var b='keydown';if(YAHOO.env.ua.gecko||YAHOO.env.ua.opera){b='keypress'}if(!this._active){this._active=true;if(this.get('useKeys')){Event.on(document,b,this._handleKeyPress,this,true)}}},_handleMouseOut:function(a){var b='keydown';if(YAHOO.env.ua.gecko||YAHOO.env.ua.opera){b='keypress'}this._active=false;if(this.get('useKeys')){Event.removeListener(document,b,this._handleKeyPress)}},_moveEl:function(a,b){var t=0,l=0,region=this._setConstraints(),resize=true;switch(a){case'down':t=-(b);if((region.bottom-b)<0){resize=false;this._resizeEl.style.top=(region.top+region.bottom)+'px'}break;case'up':t=(b);if((region.top-b)<0){resize=false;this._resizeEl.style.top='0px'}break;case'right':l=-(b);if((region.right-b)<0){resize=false;this._resizeEl.style.left=(region.left+region.right)+'px'}break;case'left':l=b;if((region.left-b)<0){resize=false;this._resizeEl.style.left='0px'}break}if(resize){this._resizeEl.style.left=(parseInt(this._resizeEl.style.left,10)-l)+'px';this._resizeEl.style.top=(parseInt(this._resizeEl.style.top,10)-t)+'px';this.fireEvent('moveEvent',{target:'keypress'})}else{this._setConstraints()}this._syncBackgroundPosition()},_handleKeyPress:function(a){var b=Event.getCharCode(a),stopEvent=false,inc=((a.shiftKey)?this.get('shiftKeyTick'):this.get('keyTick'));switch(b){case 0x25:this._moveEl('left',inc);stopEvent=true;break;case 0x26:this._moveEl('up',inc);stopEvent=true;break;case 0x27:this._moveEl('right',inc);stopEvent=true;break;case 0x28:this._moveEl('down',inc);stopEvent=true;break;default:}if(stopEvent){Event.preventDefault(a)}},_handleB4DragEvent:function(){this._setConstraints()},_handleDragEvent:function(){this._syncBackgroundPosition();this.fireEvent('dragEvent',arguments);this.fireEvent('moveEvent',{target:'dragevent'})},_handleBeforeResizeEvent:function(a){var b=k.getRegion(this.get('element')),c=this._resize._cache,ch=this._resize._currentHandle,h=0,w=0;if(a.top&&(a.top<b.top)){h=(c.height+c.top)-b.top;k.setY(this._resize.getWrapEl(),b.top);this._resize.getWrapEl().style.height=h+'px';this._resize._cache.height=h;this._resize._cache.top=b.top;this._syncBackgroundPosition();return false}if(a.left&&(a.left<b.left)){w=(c.width+c.left)-b.left;k.setX(this._resize.getWrapEl(),b.left);this._resize._cache.left=b.left;this._resize.getWrapEl().style.width=w+'px';this._resize._cache.width=w;this._syncBackgroundPosition();return false}if(ch!='tl'&&ch!='l'&&ch!='bl'){if(c.left&&a.width&&((c.left+a.width)>b.right)){w=(b.right-c.left);k.setX(this._resize.getWrapEl(),(b.right-w));this._resize.getWrapEl().style.width=w+'px';this._resize._cache.left=(b.right-w);this._resize._cache.width=w;this._syncBackgroundPosition();return false}}if(ch!='t'&&ch!='tr'&&ch!='tl'){if(c.top&&a.height&&((c.top+a.height)>b.bottom)){h=(b.bottom-c.top);k.setY(this._resize.getWrapEl(),(b.bottom-h));this._resize.getWrapEl().style.height=h+'px';this._resize._cache.height=h;this._resize._cache.top=(b.bottom-h);this._syncBackgroundPosition();return false}}},_handleResizeMaskEl:function(){var a=this._resize._cache;this._resizeMaskEl.style.height=Math.floor(a.height)+'px';this._resizeMaskEl.style.width=Math.floor(a.width)+'px'},_handleResizeEvent:function(a){this._setConstraints(true);this._syncBackgroundPosition();this.fireEvent('resizeEvent',arguments);this.fireEvent('moveEvent',{target:'resizeevent'})},_syncBackgroundPosition:function(){this._handleResizeMaskEl();this._setBackgroundPosition(-(parseInt(this._resizeEl.style.left,10)),-(parseInt(this._resizeEl.style.top,10)))},_setBackgroundPosition:function(l,t){var a=parseInt(k.getStyle(this._resize.get('element'),'borderLeftWidth'),10);var b=parseInt(k.getStyle(this._resize.get('element'),'borderTopWidth'),10);if(isNaN(a)){a=0}if(isNaN(b)){b=0}var c=this._resize.getWrapEl().firstChild;var d=(l-a)+'px '+(t-b)+'px';this._resizeMaskEl.style.backgroundPosition=d},_setBackgroundImage:function(a){var b=this._resize.getWrapEl().firstChild;this._image=a;b.style.backgroundImage='url('+a+'#)'},_handleEndResizeEvent:function(){this._setConstraints(true)},_handleStartResizeEvent:function(){this._setConstraints(true);var h=this._resize._cache.height,w=this._resize._cache.width,t=parseInt(this._resize.getWrapEl().style.top,10),l=parseInt(this._resize.getWrapEl().style.left,10),maxH=0,maxW=0;switch(this._resize._currentHandle){case'b':maxH=(h+this._resize.dd.bottomConstraint);break;case'l':maxW=(w+this._resize.dd.leftConstraint);break;case'r':maxH=(h+t);maxW=(w+this._resize.dd.rightConstraint);break;case'br':maxH=(h+this._resize.dd.bottomConstraint);maxW=(w+this._resize.dd.rightConstraint);break;case'tr':maxH=(h+t);maxW=(w+this._resize.dd.rightConstraint);break}if(maxH){}if(maxW){}this.fireEvent('startResizeEvent',arguments)},_setConstraints:function(a){var b=this._resize;b.dd.resetConstraints();var c=parseInt(b.get('height'),10),width=parseInt(b.get('width'),10);if(a){c=b._cache.height;width=b._cache.width}var d=k.getRegion(this.get('element'));var e=b.getWrapEl();var f=k.getXY(e);var g=f[0]-d.left;var h=d.right-f[0]-width;var i=f[1]-d.top;var j=d.bottom-f[1]-c;if(i<0){i=0}b.dd.setXConstraint(g,h);b.dd.setYConstraint(i,j);return{top:i,right:h,bottom:j,left:g}},getCropCoords:function(){var a={top:parseInt(this._resize.getWrapEl().style.top,10),left:parseInt(this._resize.getWrapEl().style.left,10),height:this._resize._cache.height,width:this._resize._cache.width,image:this._image};return a},reset:function(){this._resize.resize(null,this.get('initHeight'),this.get('initWidth'),0,0,true);this._resizeEl.style.top=this.get('initialXY')[1]+'px';this._resizeEl.style.left=this.get('initialXY')[0]+'px';this._syncBackgroundPosition();return this},getEl:function(){return this.get('element')},getResizeEl:function(){return this._resizeEl},getWrapEl:function(){return this._wrap},getMaskEl:function(){return this._mask},getResizeMaskEl:function(){return this._resizeMaskEl},getResizeObject:function(){return this._resize},init:function(a,b){m.superclass.init.call(this,a,b);var c=a;if(!Lang.isString(c)){if(c.tagName&&(c.tagName.toLowerCase()=='img')){c=k.generateId(c)}else{return false}}else{var d=k.get(c);if(d.tagName&&d.tagName.toLowerCase()=='img'){}else{return false}}m._instances[c]=this;this._createWrap();this._createMask();this._createResize();this._setConstraints()},initAttributes:function(b){m.superclass.initAttributes.call(this,b);this.setAttributeConfig('initialXY',{writeOnce:true,validator:YAHOO.lang.isArray,value:b.initialXY||[10,10]});this.setAttributeConfig('keyTick',{validator:YAHOO.lang.isNumber,value:b.keyTick||1});this.setAttributeConfig('shiftKeyTick',{validator:YAHOO.lang.isNumber,value:b.shiftKeyTick||10});this.setAttributeConfig('useKeys',{validator:YAHOO.lang.isBoolean,value:((b.useKeys===false)?false:true)});this.setAttributeConfig('status',{validator:YAHOO.lang.isBoolean,value:((b.status===false)?false:true),method:function(a){if(this._resize){this._resize.set('status',a)}}});this.setAttributeConfig('minHeight',{validator:YAHOO.lang.isNumber,value:b.minHeight||50,method:function(h){if(this._resize){this._resize.set('minHeight',h)}}});this.setAttributeConfig('minWidth',{validator:YAHOO.lang.isNumber,value:b.minWidth||50,method:function(w){if(this._resize){this._resize.set('minWidth',w)}}});this.setAttributeConfig('ratio',{validator:YAHOO.lang.isBoolean,value:b.ratio||false,method:function(r){if(this._resize){this._resize.set('ratio',r)}}});this.setAttributeConfig('autoRatio',{validator:YAHOO.lang.isBoolean,value:((b.autoRatio===false)?false:true),method:function(a){if(this._resize){this._resize.set('autoRatio',a)}}});this.setAttributeConfig('initHeight',{writeOnce:true,validator:YAHOO.lang.isNumber,value:b.initHeight||(this.get('element').height/4)});this.setAttributeConfig('initWidth',{validator:YAHOO.lang.isNumber,writeOnce:true,value:b.initWidth||(this.get('element').width/4)});this.setAttributeConfig('initSizeAddPercWidth',{validator:YAHOO.lang.isNumber,writeOnce:true,value:b.initSizeAddPercWidth||0});this.setAttributeConfig('initSizeAddPercHeight',{validator:YAHOO.lang.isNumber,writeOnce:true,value:b.initSizeAddPercHeight||0});this.setAttributeConfig('initSizeAddStartFromTop',{validator:YAHOO.lang.isNumber,writeOnce:true,value:b.initSizeAddStartFromTop||0});this.setAttributeConfig('initSizeAddStartFromLeft',{validator:YAHOO.lang.isNumber,writeOnce:true,value:b.initSizeAddStartFromLeft||0})},destroy:function(){this._resize.destroy();this._resizeEl.parentNode.removeChild(this._resizeEl);this._mask.parentNode.removeChild(this._mask);Event.purgeElement(this._wrap);this._wrap.parentNode.replaceChild(this.get('element'),this._wrap);for(var i in this){if(Lang.hasOwnProperty(this,i)){this[i]=null}}},toString:function(){if(this.get){return'ImageCropper (#'+this.get('id')+')'}return'Image Cropper'}});YAHOO.widget.ImageCropper=m})();YAHOO.register("imagecropper",YAHOO.widget.ImageCropper,{version:"2.8.0r4",build:"2449"});
