f4a3427b96ee22181db8232e92931d0d1670e08c
[ldapsaisie.git] / trunk / includes / js / LSinfosBox.js
1 var LSinfosBox = new Class({
2     initialize: function(options){
3       // Default options
4       this._options = {
5         closeBtn:       0,
6         name:           '',
7         fxDuration:     500,
8         opacity:        0.8,
9         autoClose:      3000
10       };
11       
12       // Load options from argument
13       if ($type(options)=='object') {
14         $each(options,function(val,name) {
15           if ($type(this._options[name])) {
16             this._options[name]=val;
17           }
18         },this);
19       }
20       
21       this.build();
22       
23       this.opened=0;
24     },
25     
26     build: function() {
27       var classes;
28       if (this._options.name!='') {
29         classes='LSinfosBox '+this._options.name;
30       }
31       else {
32         classes='LSinfosBox'
33       }
34       
35       this.core = new Element('div');
36       this.core.addClass(classes);
37       this.core.addEvent('dblclick',this.close.bind(this));
38       
39       if(this._options.closeBtn) {
40         this.closeBtn = new Element('span');
41         this.closeBtn.addClass(classes);
42         this.closeBtn.set('html','X');
43         this.closeBtn.addEvent('click',this.close.bind(this));
44         this.closeBtn.injectInside(this.core);
45       }
46       
47       this.content = new Element('p');
48       this.content.addEvent(classes);
49       this.content.injectInside(this.core);
50       
51       this.fx = new Fx.Tween(
52         this.core,
53         {
54             property: 'opacity',
55             duration: this._options.fxDuration,
56             fps:      30
57         }
58       );
59       
60       this.core.inject(document.body,'top');
61     },
62     
63     open: function() {
64       this.core.setStyle('top',getScrollTop()+10);
65       
66       if (this._options.autoClose>0) {
67         this.closeTime = (new Date()).getTime();
68         this.autoClose.delay((this._options.autoClose+this._options.fxDuration),this,this.closeTime);
69       }
70       
71       if (this.opened) {
72         return true;
73       }
74       
75       this.fx.start(0,this._options.opacity);
76       this.opened = 1;
77       
78     },
79     
80     close: function(withoutEffect) {
81       if (this.opened) {
82         this.opened = 0;
83         if (withoutEffect==1) {
84           this.fx.set(0);
85         }
86         else {
87           this.fx.start(this._options.opacity,0);
88         }
89       }
90     },
91     
92     autoClose: function(time) {
93       if (time==this.closeTime) {
94         this.close();
95         this.closeTime=0;
96       }
97     },
98     
99     addInfo: function(html) {
100       if (this.content.innerHTML=='') {
101         this.content.set('html',html);
102       }
103       else {
104         var ul = this.content.getLast("ul");
105         if (!$type(ul)) {
106           ul = new Element('ul');
107           var c_li = new Element('li');
108           c_li.set('html',this.content.innerHTML);
109           c_li.injectInside(ul);
110           this.content.empty();
111           ul.injectInside(this.content);
112         }
113         var li = new Element('li');
114         li.set('html',html);
115         li.injectInside(ul);
116       }
117       this.open();
118     },
119     
120     display: function(html) {
121       if ($type(html)) {
122         this.content.empty();
123         this.content.set('html',html);
124       }
125       this.open();
126     }
127 });