/*
 * jQuery Pines Notify (pnotify) Plugin 1.0
 *
 * Copyright (c) 2009 Hunter Perrin
 *
 * Licensed (along with all of Pines) under the GNU Affero GPL:
 *	  http://www.gnu.org/licenses/agpl.html
 */
(function(d){var m,k,j,l;d.extend({pnotify_remove_all:function(){var i=j.data("pnotify");i&&i.length&&d.each(i,function(){this.pnotify_remove&&this.pnotify_remove()})},pnotify_position_all:function(){k&&clearTimeout(k);k=null;var i=j.data("pnotify");if(i&&i.length){d.each(i,function(){var c=this.opts.pnotify_stack;if(c){if(!c.nextpos1)c.nextpos1=c.firstpos1;if(!c.nextpos2)c.nextpos2=c.firstpos2;if(!c.addpos2)c.addpos2=0;if(this.css("display")!="none"){var a,b,e={},h;switch(c.dir1){case "down":h="top";
break;case "up":h="bottom";break;case "left":h="right";break;case "right":h="left";break}a=parseInt(this.css(h));if(isNaN(a))a=0;if(typeof c.firstpos1=="undefined"){c.firstpos1=a;c.nextpos1=c.firstpos1}var g;switch(c.dir2){case "down":g="top";break;case "up":g="bottom";break;case "left":g="right";break;case "right":g="left";break}b=parseInt(this.css(g));if(isNaN(b))b=0;if(typeof c.firstpos2=="undefined"){c.firstpos2=b;c.nextpos2=c.firstpos2}if(c.dir1=="down"&&c.nextpos1+this.height()>l.height()||
c.dir1=="up"&&c.nextpos1+this.height()>l.height()||c.dir1=="left"&&c.nextpos1+this.width()>l.width()||c.dir1=="right"&&c.nextpos1+this.width()>l.width()){c.nextpos1=c.firstpos1;c.nextpos2+=c.addpos2+10;c.addpos2=0}if(c.animation&&c.nextpos2<b)switch(c.dir2){case "down":e.top=c.nextpos2+"px";break;case "up":e.bottom=c.nextpos2+"px";break;case "left":e.right=c.nextpos2+"px";break;case "right":e.left=c.nextpos2+"px";break}else this.css(g,c.nextpos2+"px");switch(c.dir2){case "down":case "up":if(this.outerHeight(true)>
c.addpos2)c.addpos2=this.height();break;case "left":case "right":if(this.outerWidth(true)>c.addpos2)c.addpos2=this.width();break}if(c.nextpos1)if(c.animation&&(a>c.nextpos1||e.top||e.bottom||e.right||e.left))switch(c.dir1){case "down":e.top=c.nextpos1+"px";break;case "up":e.bottom=c.nextpos1+"px";break;case "left":e.right=c.nextpos1+"px";break;case "right":e.left=c.nextpos1+"px";break}else this.css(h,c.nextpos1+"px");if(e.top||e.bottom||e.right||e.left)this.animate(e,{duration:500,queue:false});switch(c.dir1){case "down":case "up":c.nextpos1+=
this.height()+10;break;case "left":case "right":c.nextpos1+=this.width()+10;break}}}});d.each(i,function(){var c=this.opts.pnotify_stack;if(c){c.nextpos1=c.firstpos1;c.nextpos2=c.firstpos2;c.addpos2=0;c.animation=true}})}},pnotify:function(i){j||(j=d("body"));l||(l=d(window));var c,a;if(typeof i=="string"){a=d.extend({},d.pnotify.defaults);a.pnotify_text=i}else a=d.extend({},d.pnotify.defaults,i);if(a.pnotify_before_init)if(a.pnotify_before_init(a)===false)return null;var b=d("<div />",{"class":"ui-widget ui-helper-clearfix ui-pnotify "+
a.pnotify_addclass,css:{display:"none"},mouseenter:function(){if(c=="out"&&a.pnotify_mouse_reset){b.stop(true);b.css("height","auto").animate({width:a.pnotify_width,opacity:a.pnotify_opacity},"fast")}a.pnotify_hide&&a.pnotify_mouse_reset&&b.pnotify_cancel_remove();a.pnotify_closer&&b.closer.show()},mouseleave:function(){a.pnotify_hide&&a.pnotify_mouse_reset&&b.pnotify_queue_remove();b.closer.hide();d.pnotify_position_all()}});b.opts=a;if(a.pnotify_shadow)b.shadow_container=d("<div />",{"class":"ui-widget-shadow ui-pnotify-shadow"}).prependTo(b);
b.container=d("<div />",{"class":"ui-corner-all ui-pnotify-container "+(a.pnotify_type=="error"?"ui-state-error":"ui-state-highlight")}).appendTo(b);b.pnotify_version="1.0.0";b.pnotify=function(g){var f=a;if(typeof g=="string")a.pnotify_text=g;else a=d.extend({},a,g);b.opts=a;if(a.pnotify_shadow!=f.pnotify_shadow)if(a.pnotify_shadow)b.shadow_container=d("<div />",{"class":"ui-widget-shadow ui-pnotify-shadow"}).prependTo(b);else b.children(".ui-pnotify-shadow").remove();if(a.pnotify_addclass===false)b.removeClass(f.pnotify_addclass);
else a.pnotify_addclass!==f.pnotify_addclass&&b.removeClass(f.pnotify_addclass).addClass(a.pnotify_addclass);if(a.pnotify_title===false)b.title_container.hide("fast");else a.pnotify_title!==f.pnotify_title&&b.title_container.html(a.pnotify_title).show("fast");if(a.pnotify_text===false)b.text_container.hide("fast");else if(a.pnotify_text!==f.pnotify_text){if(a.pnotify_insert_brs)a.pnotify_text=a.pnotify_text.replace("\n","<br />");b.text_container.html(a.pnotify_text).show("fast")}b.pnotify_history=
a.pnotify_history;a.pnotify_type!=f.pnotify_type&&b.container.toggleClass("ui-state-error ui-state-highlight");if(a.pnotify_notice_icon!=f.pnotify_notice_icon&&a.pnotify_type=="notice"||a.pnotify_error_icon!=f.pnotify_error_icon&&a.pnotify_type=="error"||a.pnotify_type!=f.pnotify_type){b.container.find("div.ui-pnotify-icon").remove();if(a.pnotify_error_icon&&a.pnotify_type=="error"||a.pnotify_notice_icon)d("<div />",{"class":"ui-pnotify-icon"}).append(d("<span />",{"class":a.pnotify_type=="error"?
a.pnotify_error_icon:a.pnotify_notice_icon})).prependTo(b.container)}a.pnotify_width!==f.pnotify_width&&b.animate({width:a.pnotify_width});a.pnotify_min_height!==f.pnotify_min_height&&b.container.animate({minHeight:a.pnotify_min_height});a.pnotify_opacity!==f.pnotify_opacity&&b.fadeTo(a.pnotify_animate_speed,a.pnotify_opacity);if(a.pnotify_hide)f.pnotify_hide||b.pnotify_queue_remove();else b.pnotify_cancel_remove();b.pnotify_queue_position();return b};b.pnotify_queue_position=function(){k&&clearTimeout(k);
k=setTimeout(d.pnotify_position_all,10)};b.pnotify_display=function(){b.parent().length||b.appendTo(j);if(a.pnotify_before_open)if(a.pnotify_before_open(b)===false)return;b.pnotify_queue_position();if(a.pnotify_animation=="fade"||a.pnotify_animation.effect_in=="fade")b.show().fadeTo(0,0).hide();else a.pnotify_opacity!=1&&b.show().fadeTo(0,a.pnotify_opacity).hide();b.animate_in(function(){a.pnotify_after_open&&a.pnotify_after_open(b);b.pnotify_queue_position();a.pnotify_hide&&b.pnotify_queue_remove()})};
b.pnotify_remove=function(){if(b.timer){window.clearTimeout(b.timer);b.timer=null}if(a.pnotify_before_close)if(a.pnotify_before_close(b)===false)return;b.animate_out(function(){if(a.pnotify_after_close)if(a.pnotify_after_close(b)===false)return;b.pnotify_queue_position();a.pnotify_remove&&b.detach()})};b.animate_in=function(g){c="in";var f;f=typeof a.pnotify_animation.effect_in!="undefined"?a.pnotify_animation.effect_in:a.pnotify_animation;if(f=="none"){b.show();g()}else if(f=="show")b.show(a.pnotify_animate_speed,
g);else if(f=="fade")b.show().fadeTo(a.pnotify_animate_speed,a.pnotify_opacity,g);else if(f=="slide")b.slideDown(a.pnotify_animate_speed,g);else if(typeof f=="function")f("in",g,b);else b.effect&&b.effect(f,{},a.pnotify_animate_speed,g)};b.animate_out=function(g){c="out";var f;f=typeof a.pnotify_animation.effect_out!="undefined"?a.pnotify_animation.effect_out:a.pnotify_animation;if(f=="none"){b.hide();g()}else if(f=="show")b.hide(a.pnotify_animate_speed,g);else if(f=="fade")b.fadeOut(a.pnotify_animate_speed,
g);else if(f=="slide")b.slideUp(a.pnotify_animate_speed,g);else if(typeof f=="function")f("out",g,b);else b.effect&&b.effect(f,{},a.pnotify_animate_speed,g)};b.pnotify_cancel_remove=function(){b.timer&&window.clearTimeout(b.timer)};b.pnotify_queue_remove=function(){b.pnotify_cancel_remove();b.timer=window.setTimeout(function(){b.pnotify_remove()},isNaN(a.pnotify_delay)?0:a.pnotify_delay)};b.closer=d("<div />",{"class":"ui-pnotify-closer",css:{cursor:"pointer",display:"none"},click:function(){b.pnotify_remove();
b.closer.hide()}}).append(d("<span />",{"class":"ui-icon ui-icon-circle-close"})).appendTo(b.container);if(a.pnotify_error_icon&&a.pnotify_type=="error"||a.pnotify_notice_icon)d("<div />",{"class":"ui-pnotify-icon"}).append(d("<span />",{"class":a.pnotify_type=="error"?a.pnotify_error_icon:a.pnotify_notice_icon})).appendTo(b.container);b.title_container=d("<div />",{"class":"ui-pnotify-title",html:a.pnotify_title}).appendTo(b.container);typeof a.pnotify_title!="string"&&b.title_container.hide();if(a.pnotify_insert_brs&&
typeof a.pnotify_text=="string")a.pnotify_text=a.pnotify_text.replace("\n","<br />");b.text_container=d("<div />",{"class":"ui-pnotify-text",html:a.pnotify_text}).appendTo(b.container);typeof a.pnotify_text!="string"&&b.text_container.hide();typeof a.pnotify_width=="string"&&b.css("width",a.pnotify_width);typeof a.pnotify_min_height=="string"&&b.container.css("min-height",a.pnotify_min_height);b.pnotify_history=a.pnotify_history;var e=j.data("pnotify");if(e==null||typeof e!="object")e=[];e=a.pnotify_stack.push==
"top"?d.merge([b],e):d.merge(e,[b]);j.data("pnotify",e);a.pnotify_after_init&&a.pnotify_after_init(b);if(a.pnotify_history){var h=j.data("pnotify_history");if(typeof h=="undefined"){h=d("<div />",{"class":"ui-pnotify-history-container ui-state-default ui-corner-bottom",mouseleave:function(){h.animate({top:"-"+m+"px"},{duration:100,queue:false})}}).append(d("<div />",{"class":"ui-pnotify-history-header",text:"Redisplay"})).append(d("<button />",{"class":"ui-pnotify-history-all ui-state-default ui-corner-all",
text:"All",hover:function(){d(this).toggleClass("ui-state-hover")},click:function(){d.each(e,function(){this.pnotify_history&&this.pnotify_display&&this.pnotify_display()});return false}})).append(d("<button />",{"class":"ui-pnotify-history-last ui-state-default ui-corner-all",text:"Last",hover:function(){d(this).toggleClass("ui-state-hover")},click:function(){for(var g=1;!e[e.length-g]||!e[e.length-g].pnotify_history;){if(e.length-g===0)return false;g++}e[e.length-g].pnotify_display&&e[e.length-
g].pnotify_display();return false}})).appendTo(j);m=d("<span />",{"class":"ui-pnotify-history-pulldown ui-icon ui-icon-grip-dotted-horizontal",mouseenter:function(){h.animate({top:"0"},{duration:100,queue:false})}}).appendTo(h).offset().top+2;h.css({top:"-"+m+"px"});j.data("pnotify_history",h)}}a.pnotify_stack.animation=false;b.pnotify_display();return b}});d.pnotify.defaults={pnotify_addclass:"",pnotify_history:true,pnotify_width:"300px",pnotify_min_height:"16px",pnotify_type:"notice",pnotify_notice_icon:"ui-icon ui-icon-info",
pnotify_error_icon:"ui-icon ui-icon-alert",pnotify_animation:"fade",pnotify_animate_speed:"slow",pnotify_opacity:1,pnotify_shadow:false,pnotify_closer:true,pnotify_hide:true,pnotify_delay:8E3,pnotify_mouse_reset:true,pnotify_remove:true,pnotify_insert_brs:true,pnotify_stack:{dir1:"down",dir2:"left",push:"bottom"}}})(jQuery);

