/* Плагин jQuery.msgbox, Версия 0.6 Beta, Автор: Балтабек Бекенов (cybri0nix19@gmail.com) http://cyb.kz */
(function(jQuery){jQuery.tools=function()
{}
jQuery.msgbox=function(options)
{options=options||{};options.animate=options.animate==undefined?true:options.animate;jQuery.msgbox.params={id:'#msgbox',closeByEsc:true,closeByBgClick:true,animate:options.animate?true:false,outSpeed:options.animate?200:-1,inSpeed:options.animate?200:-1,bgcolor:'#444',opacity:'0.7',transparent:false,visible:true,skin:'<div class="bg"></div><div class="msgbox_loading"></div><div class="wind" style="display:none;"><div class="wrap"><div class="title msgbox_move_handle" title="Нажмите Esc для закрытия окна">Заголовок</div><div class="message"><div class="text"></div><div class="buttons"></div><div class="minwidth"></div></div></div></div>'};jQuery.extend(jQuery.msgbox.params,options);jQuery.msgbox.actions={afterClose:options?options.afterClose||null:null,beforeShow:options?options.beforeShow||null:null,showCompleted:false};jQuery.extend(jQuery.msgbox.actions,jQuery.msgbox.actions);var bg='.bg',win='.wind',caption='.title',btns='.buttons',msg_content='.message .text';jQuery.extend(jQuery.msgbox,{box:jQuery.msgbox.params.id,bg:bg,win:win,caption:caption,btns:btns,msg_content:msg_content});this.buttons.items=Array();this.buttons.styles=Array();this.buttons.titles=Array();this.buttons.classes=Array();this.buttons.performers=Array();if(!jQuery(jQuery.msgbox.box).length)
{jQuery.msgbox_view=jQuery.msgbox.insert(jQuery.msgbox.params.id);}
jQuery.msgbox.general={loaded_img_count:0,wait_img_counter:null,after_wait_img:null,opened:false,closeTimer:null,manualSetTitle:false,manualSetMessage:false,manualRenderButtons:false,win_obj:null,bg_obj:null,caption_obj:null,box_obj:null,btns_obj:null};with(jQuery.msgbox.general)
{box_obj=jQuery.msgbox_view;bg_obj=box_obj.find(jQuery.msgbox.bg);win_obj=box_obj.find(jQuery.msgbox.win);caption_obj=win_obj.find(jQuery.msgbox.caption);btns_obj=win_obj.find(jQuery.msgbox.btns);}
jQuery.extend(jQuery.msgbox.general,jQuery.msgbox.general);return this;};jQuery.extend(jQuery.msgbox,{insert:function(id)
{jQuery('body').find(jQuery.msgbox.box).remove();id=id.replace(/#/g,'');var box=jQuery('<div />');box.attr('id',id).attr('style','display:none').appendTo('body').html(jQuery.msgbox.params.skin);return jQuery('#'+id);},wait_image:function()
{jQuery(jQuery.msgbox.msg_content+' img').each(function()
{jQuery(this).load(function()
{jQuery.msgbox.general.loaded_img_count--;});jQuery.msgbox.general.loaded_img_count++;});jQuery.msgbox.is_image_loading_complete();},is_image_loading_complete:function()
{if(jQuery.msgbox.general.loaded_img_count)
{jQuery.msgbox.general.loaded_img_counter=setTimeout(jQuery.msgbox.is_image_loading_complete,100);}
else
{clearTimeout(jQuery.msgbox.general.loaded_img_counter);jQuery.msgbox.setPos(jQuery.msgbox.win,{x:jQuery.msgbox.wParams.left,y:jQuery.msgbox.wParams.top});if(jQuery.msgbox.general.after_wait_img)
jQuery.msgbox.general.after_wait_img();}},loading:function(is_loading,callback)
{var l=jQuery('.msgbox_loading');var callback=callback||null;if(!jQuery.msgbox.wParams.waitImage)
{if(callback)callback();return false;}
if(is_loading)
{jQuery.msgbox.setPos('.msgbox_loading',{x:'center',y:'center'});l.fadeIn(150,function()
{if(callback)callback();});}
else
l.fadeOut(100,function()
{if(callback)callback();});},exec:function()
{return this;},show:function(options,_callback,_and_params,_and_callback)
{jQuery.msgbox.actions.showCompleted=false;if(typeof options=="string"&&options)
{var buff=options;options={};if(typeof _callback=="object"&&_callback)
{options=_callback;}
if(typeof _and_params=="object"&&_and_params)
{options=_and_params;}
var buff_callback=_callback;if(typeof _and_params=="function")
{_callback=_and_params;}
if(typeof _and_callback=="function")
{_callback=_and_callback;}
if(typeof buff_callback=="string")
{options.showCaption=true;options.caption=buff;options.message=buff_callback;}
else
{options.showCaption=false;options.message=buff;}}
_callback=typeof _callback!="function"?0:_callback;options.inSpeed=options.inSpeed!=undefined&&options.inSpeed==0?-1:options.inSpeed;options.outSpeed=options.outSpeed!=undefined&&options.outSpeed==0?-1:options.outSpeed;jQuery.msgbox.wParams={caption:'',message:'',autoClose:0,showCaption:true,transparent:false,outSpeed:200,inSpeed:100,autoPosWhenScroll:true,minOpacityWhenScroll:0.2,moveby:'',movable:true,width:'auto',height:'auto',top:'center',left:'center',waitImage:false,moveHandle:'.msgbox_move_handle',skin:'',mode:''}
jQuery.extend(jQuery.msgbox.wParams,options);function renderContent(options,f)
{with(jQuery.msgbox)
{with(general)
{if(!manualSetTitle)setTitle(options.caption);if(!manualSetMessage)setMessage(options.message);if(!manualRenderButtons)renderButtons();}
setWinSize({width:jQuery.msgbox.wParams.width,height:jQuery.msgbox.wParams.height});jQuery.msgbox.setPos(jQuery.msgbox.win,{x:jQuery.msgbox.wParams.left,y:jQuery.msgbox.wParams.top});jQuery.msgbox.general.after_wait_img=function()
{if(actions.beforeShow)
actions.beforeShow();jQuery.msgbox.loading(false,function()
{jQuery('head').append('<link href="msgbox_visuals/skins/'+jQuery.msgbox.wParams.skin+'/main.css" type="text/css" rel="stylesheet" />');jQuery(win).fadeIn(jQuery.msgbox.wParams.inSpeed,function()
{if(jQuery.touch!=undefined)
if(jQuery.msgbox.wParams.movable)
{jQuery.touch.set
(jQuery.msgbox.wParams.moveHandle,'#msgbox .wind',{moveby:(jQuery.msgbox.wParams.moveby!=undefined?jQuery.msgbox.wParams.moveby:'')});}
actions.showCompleted=true;if(jQuery.msgbox.wParams.autoClose)
general.closeTimer=setTimeout("jQuery.msgbox.close();clearTimeout(jQuery.msgbox.general.closeTimer);",jQuery.msgbox.wParams.autoClose);if(f)f();jQuery.msgbox.general.opened=true;});});}
jQuery.msgbox.loading(true);if(jQuery.msgbox.wParams.waitImage)
jQuery.msgbox.wait_image();else
jQuery.msgbox.general.after_wait_img();}}
jQuery.msgbox_view.find(jQuery.msgbox.win).fadeOut('fast',function()
{jQuery.msgbox.general.bg_obj.show();jQuery.msgbox.general.box_obj.show();if(jQuery.msgbox.wParams.transparent)
{jQuery.msgbox.general.win_obj.addClass('transparent');jQuery.msgbox.general.caption_obj.hide();}
else jQuery.msgbox.general.win_obj.removeClass('transparent');if(jQuery.msgbox.params.transparent)
jQuery.msgbox.general.bg_obj.css({background:'none'})
else
jQuery.msgbox.general.bg_obj.css({background:jQuery.msgbox.params.bgcolor});if(jQuery.msgbox.wParams.showCaption)
jQuery.msgbox.general.caption_obj.show()
else
jQuery.msgbox.general.caption_obj.hide();if(!jQuery.msgbox.visible())
{jQuery.msgbox.general.bg_obj.css({opacity:0});if(jQuery.msgbox.params.visible)
{jQuery.msgbox.general.bg_obj.animate({opacity:jQuery.msgbox.params.opacity},jQuery.msgbox.params.inSpeed,function()
{renderContent(jQuery.msgbox.wParams,_callback);});if(jQuery.browser.msie)
{jQuery.msgbox_view.find(jQuery.msgbox.bg).css({height:3500,width:2000});}}
else
{jQuery.msgbox.general.bg_obj.hide();renderContent(jQuery.msgbox.wParams,_callback);}}
else{if(!jQuery.msgbox.params.visible)
jQuery.msgbox.general.bg_obj.fadeOut(jQuery.msgbox.params.outSpeed);jQuery.msgbox.general.win_obj.fadeOut(jQuery.msgbox.wParams.outSpeed,function()
{jQuery.msgbox.general.bg_obj.animate({opacity:jQuery.msgbox.params.opacity},jQuery.msgbox.params.inSpeed,function()
{renderContent(jQuery.msgbox.wParams,_callback);});});}
if(jQuery.msgbox.params.closeByBgClick)
jQuery.msgbox.general.bg_obj.bind('click',function()
{jQuery.msgbox.close();});jQuery(window).bind('resize',function()
{jQuery.msgbox.setPos(jQuery.msgbox.box).setPos(jQuery.msgbox.win,{x:jQuery.msgbox.wParams.left,y:jQuery.msgbox.wParams.top})}).bind('scroll',function()
{if(jQuery.msgbox.wParams.autoPosWhenScroll&&(jQuery.msgbox.general.win_obj.css('opacity')==1))
{jQuery.msgbox.general.win_obj.css({opacity:jQuery.msgbox.wParams.minOpacityWhenScroll});setTimeout("jQuery.msgbox.setPos(jQuery.msgbox.win,{x:jQuery.msgbox.wParams.left,y:jQuery.msgbox.wParams.top});jQuery.msgbox.general.win_obj.animate({opacity:1.0},200);",300);};});});if(jQuery.msgbox.params.closeByEsc)
jQuery(document).bind('keydown',function(ev)
{if(ev.keyCode==27)
{jQuery.msgbox.close();return false;}});return this;},close:function(options)
{jQuery.msgbox.general.win_obj.fadeOut(jQuery.msgbox.wParams.outSpeed,function()
{jQuery.msgbox.general.bg_obj.animate({opacity:0},jQuery.msgbox.params.outSpeed,function()
{jQuery(document).unbind('keydown');jQuery(window).unbind('scroll').unbind('resize');jQuery.msgbox.general.bg_obj.unbind('click');for(i in jQuery.msgbox.buttons.items)
{jQuery(jQuery.msgbox.btns+' > #btn'+i).unbind('click');}
jQuery.msgbox.general.box_obj.hide();clearTimeout(jQuery.msgbox.general.closeTimer);clearTimeout(jQuery.msgbox.general.loaded_img_counter);jQuery(jQuery.msgbox.wParams.moveHandle).unbind('mousedown');if(jQuery.msgbox.actions.afterClose)
jQuery.msgbox.actions.afterClose();jQuery.msgbox.general.opened=false;return this;});});},setMessage:function(nText)
{jQuery(jQuery.msgbox.win+' .message .text').html(nText||' ');return this;},setTitle:function(nTitle)
{jQuery(jQuery.msgbox.win+' .title').html(nTitle||' ');return this;},buttons:{items:Array(),obj:Array(),titles:Array(),performers:Array(),classes:Array(),styles:Array()},setButton:function(options,callback,_and_params)
{var buff=options;var buff_callback=callback;if(typeof options=="string")
{options={};if(typeof callback=="object"&&callback)
{options=callback;}
if(typeof _and_params=="function")
{callback=_and_params;}
options.caption=buff;}
callback=typeof callback!="function"?null:callback;if(options!=undefined)
with(jQuery.msgbox.buttons)
{items.push(options.caption||'');performers.push(callback||null);titles.push(options.title||'');classes.push(options.xclass||'');styles.push(options.xstyle||'');}
return this;},renderButtons:function()
{jQuery.msgbox.general.btns_obj.empty().hide();with(jQuery.msgbox.buttons)
{if(items.length)
{for(i in items)
{var _class=classes[i]!=undefined?classes[i]:'';jQuery.msgbox.general.btns_obj.append('<span class="button '+_class+'" id="btn'+i+'">'+items[i]+'</span>');jQuery.msgbox.general.btns_obj.find('#btn'+i).attr('title',titles[i]).unbind('click').bind('click',{id:i},function(ev)
{if(performers[ev.data.id])
performers[ev.data.id](obj[ev.data.id]);});if(typeof styles[i]=="object")
jQuery.msgbox.general.btns_obj.find('#btn'+i).css(styles[i])
else
jQuery.msgbox.general.btns_obj.find('#btn'+i).attr('style',styles[i]);delete items[i];}
jQuery.msgbox.general.btns_obj.show();}
else
{jQuery.msgbox.general.btns_obj.hide();}}
return this;},mode:function(options)
{return this;},visible:function()
{return jQuery.msgbox.general.bg_obj.css('opacity')==jQuery.msgbox.params.opacity?true:false;},setWinSize:function(options)
{with(jQuery.msgbox.general.win_obj)
{if(options.width)
css({width:options.width});if(options.height)
children('.message').children('.text').css({height:options.height});}
return this;},setPos:function(el,options)
{var l=t=0;var w_width=jQuery(window).width();var el_width=jQuery(el).width();if(!options||!options.x)options={x:'center'}
if((options.x=='left'||options.x=='right'||options.x=='center'))
{switch(options.x)
{case'left':l=jQuery(window).scrollLeft()+4;break;case'right':l=w_width-el_width-16;break;default:l=parseInt((w_width-el_width)/2);}}
var w_height=jQuery.browser.opera?window.innerHeight:jQuery(window).height();var el_height=jQuery(el).height();if(!options||!options.y)options={y:'center'}
if(options.y=='top'||options.y=='bottom'||options.y=='center')
{switch(options.y)
{case'top':t=jQuery(window).scrollTop()+4;break;case'bottom':t=w_height-el_height+jQuery(window).scrollTop()-4;break;default:t=parseInt((w_height-el_height)/2)+jQuery(window).scrollTop();}}
if(l)jQuery(el).css({left:l});if(t)jQuery(el).css({top:t});return this;},setSkin:function(content)
{jQuery.msgbox.params.skin=content;jQuery.msgbox.insert(jQuery.msgbox.params.id);return this;}});})(jQuery);jQuery.extend(jQuery.msgbox);