/*
Copyright (c) 2007-2008 the OTHER media Limited
Licensed under the BSD license, http://ojay.othermedia.org/license.html
Version: 0.3.1
Build:   min
*/
// @require ojay/core-min

(function(e){var h=function(b){return function(){var a=this._0._1;a.setStyle({visibility:'hidden'});this.show('none',{silent:true})[b]().hide('none',{silent:true});a.setStyle({visibility:''});return this}};e.Overlay=new JS.Class({include:[JS.State,e.Observable],extend:{BASE_LAYER:1000,MINIMUM_OFFSET:20,DEFAULT_SIZE:{width:400,height:300},DEFAULT_POSITION:{left:50,top:50},DEFAULT_OPACITY:1,CONTAINER_CLASS:'overlay-container',TRANSITION_TIME:0.4,EASING:'easeOutStrong',Transitions:new JS.Singleton({_7:{},INTERFACE:new JS.Interface(['hide','show']),_b:{hide:function(a){return a},show:function(a){return a}},add:function(a,b){JS.Interface.ensure(b,this.INTERFACE);this._7[a]=b;return this},get:function(a){return this._7[a]||this._b}}),getLayer:function(a){if(a.getLayer)return Number(a.getLayer());if(a.nodeType==e.HTML.ELEMENT_NODE||typeof a=='string')a=e(a);if(a.getStyle)return Number(a.getStyle('zIndex'))||0;return 0}},initialize:function(a){this._0={};a=this._6=a||{};e(document.body).insert(this.getHTML().node,'top');this.setState('INVISIBLE');this.setSize(a.width,a.height);this.setPosition(a.left,a.top);this.setLayer(a.layer);this.setOpacity(a.opacity)},getHTML:function(){var a=this,b=a._0;if(b._1)return b._1;var c=e(e.HTML.div({className:this.klass.CONTAINER_CLASS}));c.setStyle({position:'absolute',overflow:'hidden'}).hide();c.setStyle({padding:'0 0 0 0',border:'none'});(this._6.className||'').trim().split(/\s+/).forEach(c.method('addClass'));return b._1=c},getContainer:function(){return this._0._1},setPosition:function(a,b){if(this.inState('CLOSED'))return this;var c=this.klass.DEFAULT_POSITION;a=this._4(a===undefined?c.left:a);b=this._4(b===undefined?c.top:b);this._8={left:a,top:b};if(this.inState('VISIBLE'))this._0._1.setStyle(this._8);return this},getPosition:function(a){var b=this._8,c=b.left,d=b.top;return a?{left:c,top:d}:{left:parseInt(c),top:parseInt(d)}},setSize:function(a,b){if(this.inState('CLOSED'))return this;var c=this.klass.DEFAULT_SIZE;a=this._4(a===undefined?c.width:a);b=this._4(b===undefined?c.height:b);this._9={width:a,height:b};if(this.inState('VISIBLE'))this._0._1.setStyle(this._9);return this},getSize:function(a){var b=this._9,c=b.width,d=b.height;return a?{width:c,height:d}:{width:parseInt(c),height:parseInt(d)}},getRegion:function(){return!this.inState('INVISIBLE','CLOSED')?this._0._1.getRegion():undefined},setOpacity:function(a){this._5=(a===undefined)?this.klass.DEFAULT_OPACITY:Number(a);if(this._5>1)this._5/=100;if(this.inState('VISIBLE'))this._0._1.setStyle({opacity:this._5});return this},getOpacity:function(){return this._5},positionBehind:function(a){return this.setLayer(this.klass.getLayer(a)-1)},positionInFront:function(a){return this.setLayer(this.klass.getLayer(a)+1)},setLayer:function(a){if(this.inState('CLOSED'))return this;this._a=(a===undefined)?this.klass.BASE_LAYER:Number(a);this._0._1.setStyle({zIndex:this._a});return this},getLayer:function(){return this._a},states:{INVISIBLE:{center:h('center'),show:function(a,b){this.setState('SHOWING');a=this.klass.Transitions.get(a||'none');var c=new JS.MethodChain()._(this).setState('VISIBLE');if((b||{}).silent!==true)c._(this).notifyObservers('show');c._(this);return a.show(this,c)},close:function(a){this._0._1.remove();this.setState('CLOSED');if((a||{}).silent!==true)this.notifyObservers('close');return this}},SHOWING:{},VISIBLE:{center:function(){var a=this.getRegion(),b=e.getVisibleRegion(),c=b.left+(b.getWidth()-a.getWidth())/2,d=b.top+(b.getHeight()-a.getHeight())/2;if(c<this.klass.MINIMUM_OFFSET)c=this.klass.MINIMUM_OFFSET;if(d<this.klass.MINIMUM_OFFSET)d=this.klass.MINIMUM_OFFSET;return this.setPosition(c,d)},hide:function(a,b){this.setState('HIDING');a=this.klass.Transitions.get(a||'none');var c=new JS.MethodChain()._(this).setState('INVISIBLE');if((b||{}).silent!==true)c._(this).notifyObservers('hide');c._(this);return a.hide(this,c)},close:function(a,b){return this.hide(a,b)._(this).close(b)},resize:function(a,b,c,d,f){var g=a,f=f||{};if(typeof g=='object'){f=b||{};a=g.left;b=g.top;c=g.getWidth();d=g.getHeight()}this.setState('RESIZING');return this._0._1.animate({left:{to:a},top:{to:b},width:{to:c},height:{to:d}},f.duration||this.klass.TRANSITION_TIME,{easing:f.easing||this.klass.EASING})._(this).setSize(c,d)._(this).setPosition(a,b)._(this).setState('VISIBLE')._(this)}},HIDING:{},RESIZING:{},CLOSED:{}},_4:function(a){return String(a).replace(/^(-?\d+(?:\.\d+)?)$/g,'$1px')}});e.Overlay.Transitions.add('none',{hide:function(a,b){a.getContainer().hide();b.fire();return a},show:function(a,b){a.getContainer().setStyle({opacity:a.getOpacity()}).setStyle(a.getSize(true)).setStyle(a.getPosition(true)).show();b.fire();return a}}).add('fade',{hide:function(a,b){a.getContainer().animate({opacity:{to:0}},e.Overlay.TRANSITION_TIME).hide()._(b.toFunction());return b},show:function(a,b){a.getContainer().setStyle({opacity:0}).setStyle(a.getSize(true)).setStyle(a.getPosition(true)).show().animate({opacity:{to:a.getOpacity()}},e.Overlay.TRANSITION_TIME)._(b.toFunction());return b}}).add('zoom',{hide:function(a,b){var c=a.getRegion().scale(0.5),d=c.getCenter();a.getContainer().animate({opacity:{to:0},left:{to:c.left},width:{to:c.getWidth()},top:{to:c.top},height:{to:c.getHeight()}},e.Overlay.TRANSITION_TIME,{easing:e.Overlay.EASING}).hide()._(b.toFunction());return b},show:function(a,b){var c=a.getPosition(),d=a.getSize();a.getContainer().setStyle({opacity:0,left:(c.left+d.width/4)+'px',top:(c.top+d.height/4)+'px',width:(d.width/2)+'px',height:(d.height/2)+'px'}).show().animate({opacity:{to:a.getOpacity()},left:{to:c.left},width:{to:d.width},top:{to:c.top},height:{to:d.height}},e.Overlay.TRANSITION_TIME,{easing:e.Overlay.EASING})._(b.toFunction());return b}});e.ContentOverlay=new JS.Class(e.Overlay,{extend:{CONTENT_CLASS:'overlay-content'},initialize:function(a){this.callSuper();this.setContent(this._6.content)},getHTML:function(){var a=this,b=a._0;if(b._2)return b._1;var c=this.callSuper().node,d=new e.HtmlBuilder(c);b._2=e(d.div({className:a.klass.CONTENT_CLASS}));return b._1},setContent:function(a){if(this.inState('CLOSED'))return this;this._0._2.setContent(a||'');return this},getContentElement:function(){return this._0._2},insert:function(a,b){if(this.inState('CLOSED'))return this;this._0._2.insert(a,b);return this},states:{INVISIBLE:{fitToContent:h('fitToContent')},VISIBLE:{fitToContent:function(a){var a=a||{},b=!!a.animate,c=!!a.balance,d=this._0._2.getRegion(),f=this.getRegion();if(c)d.centerOn(f);if(b)return this.resize(d,a);this.setSize(d.getWidth(),d.getHeight());this.setPosition(d.left,d.top);return this}}}});e.Tooltip=new JS.Class(e.ContentOverlay,{initialize:function(a,b){this.callSuper(b);this._0._1.addClass('tooltip');this.setContent(a);this.klass._3.push(this)},extend:{update:function(c,d){var f=YAHOO.util.Event.getXY(d);this._3.forEach(function(a){var b=a.getRegion();width=b?b.getWidth():this.DEFAULT_WIDTH;a.setPosition(f[0]+this.MOUSE_OFFSET-width/2,f[1]+this.MOUSE_OFFSET)},this)},_3:[],DEFAULT_WIDTH:100,MOUSE_OFFSET:20}});e(document).on('mousemove',e.Tooltip.method('update'));e.PageMask=new JS.Class(e.Overlay,{extend:{DEFAULT_COLOR:'000000',DEFAULT_OPACITY:0.5,_3:[],resizeAll:function(){this._3.forEach('setSize')}},initialize:function(a){this.klass._3.push(this);this.callSuper();this.setColor(this._6.color);if(!YAHOO.env.ua.ie)this._0._1.setStyle({position:'fixed'})},setPosition:function(){return this.callSuper(0,0)},setSize:function(){if(!YAHOO.env.ua.ie)return this.callSuper('100%','100%');var a=e(document.body).getRegion(),b=e.getViewportSize();return this.callSuper(Math.max(a.getWidth(),b.width),Math.max(a.getHeight(),b.height))},setColor:function(c){this._c=(arguments.length==3)?Array.from(arguments).map(function(a){var b=Math.round(a%256).toString(16);return(b.length==1?'0':'')+b}).join(''):(c?String(c).replace(/[^0-9a-f]/ig,''):this.klass.DEFAULT_COLOR);this._0._1.setStyle({backgroundColor:'#'+this._c});return this},states:{INVISIBLE:{show:function(){this.setSize();return this.callSuper()}}}});if(YAHOO.env.ua.ie)e(window).on('resize',e.PageMask.method('resizeAll'))})(Ojay);