/*
Copyright (c) 2007-2008 the OTHER media Limited
Licensed under the BSD license, http://ojay.othermedia.org/license.html
*/
// @require ojay/core-min
(function(g){var h=function(b){return function(){var a=this._0._1;a.setStyle({visibility:'hidden'});this.show('none')[b]().hide('none');a.setStyle({visibility:''});return this}};g.Overlay=JS.Class({include:[JS.State,JS.Observable],extend:{BASE_LAYER:10000,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:JS.Singleton({_2:{},INTERFACE:new JS.Interface(['hide','show']),_3:{hide:function(a){return a},show:function(a){return a}},add:function(a,b){JS.Interface.ensure(b,this.INTERFACE);this._2[a]=b;return this},get:function(a){return this._2[a]||this._3}}),getLayer:function(a){if(a.getLayer)return Number(a.getLayer());if(a.nodeType==g.HTML.ELEMENT_NODE||typeof a=='string')a=g(a);if(a.getStyle)return Number(a.getStyle('zIndex'))||0;return 0}},initialize:function(a){this._0={};a=this._4=a||{};g(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=g(g.HTML.div({className:this.klass.CONTAINER_CLASS}));c.setStyle({position:'absolute',overflow:'hidden'}).hide();c.setStyle({padding:'0 0 0 0',border:'none'});(this._4.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._5(a===undefined?c.left:a);b=this._5(b===undefined?c.top:b);this._6={left:a,top:b};if(this.inState('VISIBLE'))this._0._1.setStyle(this._6);return this},getPosition:function(a){var b=this._6,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._5(a===undefined?c.width:a);b=this._5(b===undefined?c.height:b);this._7={width:a,height:b};if(this.inState('VISIBLE'))this._0._1.setStyle(this._7);return this},getSize:function(a){var b=this._7,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._8=(a===undefined)?this.klass.DEFAULT_OPACITY:Number(a);if(this._8>1)this._8/=100;if(this.inState('VISIBLE'))this._0._1.setStyle({opacity:this._8});return this},getOpacity:function(){return this._8},positionBehind:function(a){return this.setLayer(a.getLayer()-1)},positionInFront:function(a){return this.setLayer(a.getLayer()+1)},setLayer:function(a){if(this.inState('CLOSED'))return this;this._9=(a===undefined)?this.klass.BASE_LAYER:Number(a);this._0._1.setStyle({zIndex:this._9});return this},getLayer:function(){return this._9},states:{INVISIBLE:{center:h('center'),show:function(a){this.setState('SHOWING');a=this.klass.Transitions.get(a||'none');var b=new JS.MethodChain()._(this).setState('VISIBLE')._(this);return a.show(this,b)},close:function(){this._0._1.remove();this.setState('CLOSED');return this}},SHOWING:{},VISIBLE:{center:function(){var a=this.getRegion(),b=g.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){this.setState('HIDING');a=this.klass.Transitions.get(a||'none');var b=new JS.MethodChain()._(this).setState('INVISIBLE')._(this);return a.hide(this,b)},close:function(a){return this.hide(a)._(this).close()},resize:function(a,b,c,d,e){var f=a,e=e||{};if(typeof f=='object'){e=b||{};a=f.left;b=f.top;c=f.getWidth();d=f.getHeight()}this.setState('RESIZING');return this._0._1.animate({left:{to:a},top:{to:b},width:{to:c},height:{to:d}},e.duration||this.klass.TRANSITION_TIME,{easing:e.easing||this.klass.EASING})._(this).setState('VISIBLE')._(this)}},HIDING:{},RESIZING:{},CLOSED:{}},_5:function(x){return String(x).replace(/^(-?\d+(?:\.\d+)?)$/g,'$1px')}});g.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}},g.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()}},g.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()}},g.Overlay.TRANSITION_TIME,{easing:g.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',fontSize:'50%'}).show().animate({opacity:{to:a.getOpacity()},left:{to:c.left},width:{to:d.width},top:{to:c.top},height:{to:d.height},fontSize:{to:100,unit:'%'}},g.Overlay.TRANSITION_TIME,{easing:g.Overlay.EASING})._(b.toFunction());return b}});g.ContentOverlay=JS.Class(g.Overlay,{extend:{CONTENT_CLASS:'overlay-content'},initialize:function(a){this.callSuper();this.setContent(this._4.content)},getHTML:function(){var a=this,b=a._0;if(b._10)return b._1;var c=this.callSuper().node,d=new g.HtmlBuilder(c);b._10=g(d.div({className:a.klass.CONTENT_CLASS}));return b._1},setContent:function(a){if(this.inState('CLOSED'))return this;this._0._10.setContent(a||'');return this},getContentElement:function(){return this._0._10},insert:function(a,b){if(this.inState('CLOSED'))return this;this._0._10.insert(a,b);return this},states:{INVISIBLE:{fitToContent:h('fitToContent')},VISIBLE:{fitToContent:function(){var a=this._0._10.getRegion();return this.setSize(a.getWidth(),a.getHeight())}}}});g.Tooltip=JS.Class(g.ContentOverlay,{initialize:function(a,b){this.callSuper(b);this._0._1.addClass('tooltip');this.setContent(a);this.klass._11.push(this)},extend:{update:function(c,d){var e=YAHOO.util.Event.getXY(d);this._11.forEach(function(a){var b=a.getRegion();width=b?b.getWidth():100;a.setPosition(e[0]+20-width/2,e[1]+20)})},_11:[]}});g(document).on('mousemove',g.Tooltip.method('update'));g.PageMask=JS.Class(g.Overlay,{extend:{DEFAULT_COLOR:'000000',DEFAULT_OPACITY:0.5,_11:[],resizeAll:function(){this._11.forEach('setSize')}},initialize:function(a){this.klass._11.push(this);this.callSuper();this.setColor(this._4.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=g(document.body).getRegion(),b=g.getViewportSize();return this.callSuper(Math.max(a.getWidth(),b.width),Math.max(a.getHeight(),b.height))},setColor:function(b){this._12=(arguments.length==3)?Array.from(arguments).map(function(x){var a=Math.round(x%256).toString(16);return(a.length==1?'0':'')+a}).join(''):(b?String(b).replace(/[^0-9a-f]/ig,''):this.klass.DEFAULT_COLOR);this._0._1.setStyle({backgroundColor:'#'+this._12});return this},states:{INVISIBLE:{show:function(){this.setSize();return this.callSuper()}}}});if(YAHOO.env.ua.ie)g(window).on('resize',g.PageMask.method('resizeAll'))})(Ojay);