/*
Script: Notimoo.js
	Notimoo - Notification system using Mootools (1.2)

License:
	MIT-style license.
	Copyright (c) 2009 PaquitoSoft

	Permission is hereby granted, free of charge, to any
	person obtaining a copy of this software and associated
	documentation files (the "Software"), to deal in the
	Software without restriction, including without limitation
	the rights to use, copy, modify, merge, publish,
	distribute, sublicense, and/or sell copies of the
	Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:
	
	The above copyright notice and this permission notice
	shall be included in all copies or substantial portions of
	the Software.
	
	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
	KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
	WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
	PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
	OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
	OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
	OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
	SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Author:
	PaquitoSoft (http://paquitosoft.com/).

Code & Documentation:
	PaquitoSoft blog (http://paquitosoft.com/?page_id=53)

Inspiration:
    - Growl by Growl team (http://growl.info/)
	- Window.Growl by Daniel Mota (http://icebeat.bitacoras.com/mootools/growl/)

*/
var Shotimoo=new Class({elements:[],Implements:[Options,Events],scrollTimeOut:null,options:{parent:"",height:50,width:300,visibleTime:5000,locationVType:"top",locationHType:"right",locationVBase:10,locationHBase:10,notificationsMargin:5,opacityTransitionTime:750,closeRelocationTransitionTime:750,scrollRelocationTransitionTime:500,notificationOpacity:0.95},initialize:function(a){this.options.parent=$(document.body);if(a){if(a.parent){a.parent=$(a.parent)}this.setOptions(a)}var b=this;this.options.parent.addEvent("scroll",function(){$clear(this.scrollTimeOut);this.scrollTimeOut=(function(){b._relocateActiveNotifications(b.TYPE_RELOCATE_SCROLL)}).delay(200)},this);window.addEvent("scroll",function(){$clear(b.scrollTimeOut);b.scrollTimeOut=(function(){b._relocateActiveNotifications(b.TYPE_RELOCATE_SCROLL)}).delay(200)});this.elements.push(this.createNotificationElement(this.options))},createNotificationElement:function(){var c=new Element("div",{"class":"shotimoo"});c.setStyle(this.options.locationVType,this.options.locationVBase);c.setStyle(this.options.locationHType,this.options.locationHBase);c.adopt(new Element("span",{"class":"shotimootitle"}));c.adopt(new Element("div",{"class":"shotimoomessage"}));c.setStyle("width",this.options.width);c.setStyle("height",this.options.height);c.store("working",false);c.set("tween",{link:"chain",duration:this.options.opacityTransitionTime});c.set("opacity",0);var b=new Fx.Tween(c,{property:this.options.locationVType,link:"chain",duration:this.options.closeRelocationTransitionTime});c.store("baseTween",b);var a=new Fx.Tween(c,{property:this.options.locationVType,link:"chain",duration:this.options.scrollRelocationTransitionTime});c.store("scrollTween",a);c.addEvent("click",function(d){d.stop();if(this.options.shSetCookie && this.options.shId) {var shCookie = Cookie.write( 'shotimoo_clrd_' + this.options.shId, true, {path:'/',duration:1});}this.close(c)}.bind(this));return c},show:function(b){var c=this;var a=this._applyScrollPosition(this.options.locationVBase);var d=this.elements.filter(function(f){var e=f.retrieve("working");if(e){a=f.getStyle(this.options.locationVType).toInt()+f.getSize().y+this.options.notificationsMargin}return !e},this).getLast();if(!d){d=this.createNotificationElement();this.elements.push(d)}d.setStyle(this.options.locationVType,a);d.store("working",true);if(b.width){d.setStyle("width",b.width)}if(b.title){d.getElement("span.shotimootitle").set("html",b.title)}d.getElement("div.shotimoomessage").set("html",b.message);if(b.customClass){d.addClass(b.customClass)}d.getElements("a").addEvent("click",function(e){e.stopPropagation()});this.options.parent.adopt(d);this._checkSize(d);d.get("tween").start("opacity",this.options.notificationOpacity).chain(function(){if((b.sticky)?!b.sticky:true){(function(){c.close(d)}).delay((b.visibleTime)?b.visibleTime:c.options.visibleTime,c);}c.fireEvent("show",d)})},close:function(c){var b=this;var a=b.elements;c.get("tween").start("opacity",0).chain(function(){if(a.length>1){a.elements=a.erase(c);c.destroy()}b._resetNotificationElement(c);b._relocateActiveNotifications(b.TYPE_RELOCATE_CLOSE);b.fireEvent("close",c)})},_relocateActiveNotifications:function(b){var d=this._applyScrollPosition(this.options.locationVBase);for(var a=0;a<this.elements.length;a++){var c=this.elements[a];if(c.retrieve("working")){if(this.TYPE_RELOCATE_CLOSE==b){c.retrieve("baseTween").start(d)}else{c.retrieve("scrollTween").start(d)}d+=c.getSize().y+this.options.notificationsMargin}}},_checkSize:function(b){var d=b.getStyle("height").toInt();var c=b.getElement("span.shotimootitle").getSize().y;var a=b.getElement("div.shotimoomessage").getSize().y;if(a>(d-c)){b.setStyle("height",d+(a-(d-c)))}},_resetNotificationElement:function(a){a.store("working",false);a.setStyle(this.options.locationVType,this.options.locationVBase);a.setStyle("height",this.options.height);a.setStyle("width",this.options.width)},_applyScrollPosition:function(a){if(this.options.locationVType=="top"){a+=this.options.parent.getScroll().y}else{a-=this.options.parent.getScroll().y}return a},TYPE_RELOCATE_CLOSE:1,TYPE_RELOCATE_SCROLL:2});
