/*  Window.Growl, version 2.0: http://icebeat.bitacoras.com
 *  Daniel Mota aka IceBeat <daniel.mota@gmail.com>
 *  
 *  Mod for usage with Prototype/Script.aculo.us by silvanji <silvan@liip.ch>
--------------------------------------------------------------------------*/
var Gr0wl = {};

Gr0wl.Base = Class.create({
	
    queue:[],
    
	options: {
		image: '',
		title: 'Growl.Smoke<br/>Script.aculo.us mod',
		text: 'http://blog.var.cc/static/growl/',
		duration: 1.5
	},
	
	initialize: function(image) {
	  this.image = new Element('img',{src:image});
		this.create();
  },
	
	create: function(styles) {
	  this.image.setStyle('position:absolute;display:none');
		Element.setOpacity(this.image, 0.0);
		this.block = new Element('div');
		
    // Verlinden Steve, add css class
    this.block = new Element('div', {'class': styles.div_class});
		
    this.block.setOpacity(0.0);

		h3 = new Element('h3');
		h3.insert(new Element('span', {'class': styles.msg_span_class}));
		
		p = new Element('p');
		p.insert(new Element('strong'));
		p.insert(new Element('br'));

		span = new Element('span', {'class': styles.footer_span_class});
		
		this.block.insert(h3);
		this.block.insert(p);
		this.block.insert(span);
	},
	
    show: function(options) {
	    
      options = Object.extend(this.options, options);
	    var elements = [this.block.cloneNode(true)];
        
      document.body.appendChild(elements[0]);
      elements[0].setStyle(options.position);
		
      elements[0].down().down().update(options.msg);
			elements[0].down().next().down().update(options.title);
			elements[0].down().next().insert(options.text);
		
     var fxOpts = {duration:0.8, from:0.0, sync:true};
     new Effect.Event({
         beforeStart: function() {
             new Effect.Parallel([
                 new Effect.Opacity(elements[0], Object.extend(fxOpts, {to:0.9}))
             ], {duration:0.8, queue:options.queue}
             
             );
         },
         
         duration: options.duration,
         queue: options.queue
     });
     
     this.hide(elements);
     
    },
	
	hide: function(elements) {
        var fxOpts = {
            duration:0.8, 
            to:0.0,
            afterFinish: function(o) {
                o.element.remove();
            },
            sync:true
        };
        
        new Effect.Parallel([
            new Effect.Opacity(elements[0], Object.extend(fxOpts, {from:0.9}))
        ], {
            duration:0.8, 
            queue:'end', 
            afterFinish:(function() {
                this.queue.shift();
            }).bind(this)
           }
        );
    },
	
	getScrollTop: function() {
        var scrollTop = document.body.scrollTop;
        if (scrollTop == 0) {
            if (window.pageYOffset) {
                scrollTop = window.pageYOffset;
            } else {
                scrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
            }
        }
        return scrollTop;
	},
    
    getScrollLeft: function() {
        var scrollLeft = document.body.scrollLeft;
        if (scrollLeft == 0) {
            if (window.pageXOffset) {
                scrollLeft = window.pageXOffset;
            } else {
                scrollLeft = (document.body.parentElement) ? document.body.parentElement.scrollLeft : 0;
            }
        }
        return scrollLeft;
    }
	
});


Gr0wl.Smoke = Class.create(Gr0wl.Base, {
	
	
	
	create: function($super, oArgs) {
		$super({
			div: 'width:298px;height:73px;',
			img: 'float:left;margin:12px;',
			h3: 'margin:0;padding:10px 0px;font-size:13px;',
			p: 'margin:0px 10px;font-size:12px;'
		});
	},
	
	show: function($super, options) {
		var last = this.queue.last();
		if (!last) {
		  last = 0;
		}
		scrollTop = this.getScrollTop();
		delta = scrollTop+10+(last*83);
		options = Object.extend(options, {
            position: {'top':delta+'px', 'right':'10px', 'display':'block'},
            queue: 'parallel'
        }); 
        
        this.queue.push(last+1);
		$super(options);
	},
	
	hide: function($super, elements) {
		$super(elements,{'opacity': 0 });
	}
	
});

Gr0wl.Bezel = Class.create(Gr0wl.Base, {
	
    
	create: function($super) {
		this.i=0;
		$super({
			div: 'width:211px;height:206px;text-align:center;background: green;padding: 20px;',
			img: 'margin-top:25px;',
			h3: 'margin:0;padding:0px;padding-top:22px;font-size:14px;',
			p: 'margin:15px;font-size:12px;'
		});
	},
	
	show: function($super, options) {
		var top = this.getScrollTop()+(document.body.offsetHeight /2)-105,
		left = this.getScrollLeft()+(document.body.offsetWidth / 2)-103;
        options = Object.extend(options, {
            position: {'top':top+'px', 'left':left+'px', 'display':'block'},
            queue: 'end'
        });
        $super(options);
    },
	
	hide: function($super, elements) {
		$super(elements, { 'opacity': 0, 'margin-top': [0,50] });
	}
	
});

Gr0wl.success = Class.create(Gr0wl.Base, {
	
    
	create: function($super) {
		this.i=0;
		$super({
			div_class: 'gnotice',
			msg_span_class: 'ok',
			footer_span_class: 'deco'
		});
	},
	
	show: function($super, options) {
		var top = this.getScrollTop()+(document.body.offsetHeight /4)-50,
		left = this.getScrollLeft()+(document.body.offsetWidth / 2)-200;
        options = Object.extend(options, {
            position: {'top':'50%', 'left':'50%', 'display':'block'},
            queue: 'end'
        });
        $super(options);
    },
	
	hide: function($super, elements) {
		$super(elements, { 'opacity': 0, 'margin-top': [0,50] });
	}
	
});


Gr0wl.notice = Class.create(Gr0wl.Base, {
	
    
	create: function($super) {
		this.i=0;
		$super({
			div_class: 'gnotice',
			msg_span_class: 'ok',
			footer_span_class: 'deco'			
		});
	},
	
	show: function($super, options) {
		var top = this.getScrollTop()+(document.body.offsetHeight /4)-50,
		left = this.getScrollLeft()+(document.body.offsetWidth / 2)-200;
        options = Object.extend(options, {
            position: {'top':'50%', 'left':'50%', 'display':'block'},
            queue: 'end'
        });
        $super(options);
    },
	
	hide: function($super, elements) {
		$super(elements, { 'opacity': 0, 'margin-top': [0,50] });
	}
	
});

Gr0wl.warning = Class.create(Gr0wl.Base, {
	
    
	create: function($super) {
		this.i=0;
		$super({
			div_class: 'gnotice',
			msg_span_class: 'nok',
			footer_span_class: 'deco'
		});
	},
	
	show: function($super, options) {
		var top = this.getScrollTop()+(document.body.offsetHeight /4)-50
		left = this.getScrollLeft()+(document.body.offsetWidth / 2)-200;
        options = Object.extend(options, {
            position: {'top':'50%', 'left':'50%', 'display':'block'},
            queue: 'end'
        });
        $super(options);
    },
	
	hide: function($super, elements) {
		$super(elements, { 'opacity': 0, 'margin-top': [0,50] });
	}
	
});

Gr0wl.error = Class.create(Gr0wl.Base, {
	
    
	create: function($super) {
		this.i=0;
		$super({
			div_class: 'gnotice',
			msg_span_class: 'nok',
			footer_span_class: 'deco'
		});
	},
	
	show: function($super, options) {
		var top = this.getScrollTop()+(document.body.offsetHeight /4)-50
		left = this.getScrollLeft()+(document.body.offsetWidth / 2)-200;
        options = Object.extend(options, {
            position: {'top':'50%', 'left':'50%', 'display':'block'},
            queue: 'end'
        });
        $super(options);
    },
	
	hide: function($super, elements) {
		$super(elements, { 'opacity': 0, 'margin-top': [0,50] });
	}
	
});

var Growl =  {
    initialize: function() {
        this.oBezel = new Gr0wl.Bezel('');
        this.Bezel =  this.oBezel.show.bind(this.oBezel);
        
        this.osuccess = new Gr0wl.success('');
        this.success =  this.osuccess.show.bind(this.osuccess);
        
        this.onotice = new Gr0wl.notice('');
        this.notice =  this.onotice.show.bind(this.onotice);
        
				this.owarning = new Gr0wl.warning('');
        this.warning =  this.owarning.show.bind(this.owarning);

        this.oerror = new Gr0wl.error('');
        this.error =  this.oerror.show.bind(this.oerror);

        this.oSmoke = new Gr0wl.Smoke('');
        this.Smoke = this.oSmoke.show.bind(this.oSmoke);
    }
};


