/*
 * hipic.web.app
 * Copyright(c) 2010, FeiKan.
 * 
 * This code is licensed under BSD license. Use it as you wish, 
 * but keep this copyright intact.
 */


$(function()
{app.ui['window']=function(width,height,closeable)
{var _instance;var _beforeShowHandler,_closeHandler;var _isVisable=false;var _footVisable=true;var _w=width||320;var _h=height||260;var _closeable=closeable===undefined?true:closeable;var _mask,_wnd;var _paneContents;var _init=function()
{_mask=$('.ui-mask');_wnd=$('.ui-window');if(!_mask.length)
{_mask=$('<div class="ui-mask"></div>').appendTo($('body')).hide();}
if(!_wnd.length)
{_wnd=$('<div class="ui-window"><div class="ui-window-head"><em style="display:block;cursor:pointer;float:right;">X</em></div><div class="ui-window-contents"></div><div class="ui-window-foot"><span></span></div></div>').appendTo($('body')).hide();var bc=_wnd.find('em:eq(0)');if(_closeable)
{bc.click(_close);}
else
{bc.hide();}}
_paneContents=_wnd.children('.ui-window-contents').empty();WIN.bind('scroll',_onwindowchange).bind('resize',_onwindowchange);};var _onwindowchange=function()
{if(_beforeShowHandler)
{_beforeShowHandler.call(_instance||undefined);}
if(_mask&&_mask.css('display')==='block')
{_show();}};var _show=function()
{if(!_footVisable)
{_wnd.find('.ui-window-foot').hide();}
else
{_wnd.find('.ui-window-foot').show();}
var h=Math.max($.browser.opera?DOCEL.clientHeight:document.body.clientHeight,WIN.height());var h2=$.browser.opera?document.body.clientHeight:DOCEL.clientHeight;var w=Math.max(BODY.width(),$('#pager').width());if(location.pathname=='/'||location.pathname.toLowerCase()=='/default.htm')
{h+=40;}
_mask.css({'width':w+'px','height':h+'px'}).show();_paneContents.css({'width':_w+'px','height':_h+'px'});_wnd.css({'width':_w+'px'});var x=(BODY.width()-_wnd.width())/2+WIN.scrollLeft();var y=Math.min((h2-_wnd.height())/2+WIN.scrollTop(),WIN.scrollTop()+h2-_wnd.height());_wnd.css({'left':x+'px','top':y+'px'}).fadeIn();_isVisable=true;};var _close=function()
{_paneContents.empty().remove();_mask.remove();_wnd.remove();_paneContents=null;_mask=null;_wnd=null;_isVisable=false;WIN.unbind('scroll',_onwindowchange).unbind('resize',_onwindowchange);if(_closeHandler)
{_closeHandler.call(_instance||undefined);}};_init();return{el:function()
{if(!_instance)
{_instance=this;}
return _paneContents;},getFoot:function()
{return _wnd.find('.ui-window-foot');},show:function()
{if(!_instance)
{_instance=this;}
_show();return this;},close:function()
{_close();return this;},sizeto:function(w,h)
{_w=w;_h=h;if(!_instance)
{_instance=this;}
if(_isVisable)
{_show();}
return this;},footVisable:function(b)
{_footVisable=b;return this;},bind:function(e,h)
{if(!_instance)
{_instance=this;}
switch(e)
{case'resize':_beforeShowHandler=h;break;case'close':_closeHandler=h;break;}
return this;}}}
$.extend(window,{UI_WIN:app.ui['window']});});
