Contribuicoes/BannerAPIComponent: banner.js

File banner.js, 6.0 KB (added by niltonneto, 14 years ago)

Arquivo Javascript para carregar banner.

Line 
1// Caminho da imagem
2var url_path = '/arqs-divulgacao/cipa.jpg';
3// Tamanho da imagem
4var img_height = 350;
5var img_width  = 350;
6// N. de dias que expira
7var expDays = 1;
8
9/*
10Begin moo.fx, simple effects library built with prototype.js (http://prototype.conio.net).
11by Valerio Proietti (http://mad4milk.net) MIT-style LICENSE.
12for more info (http://moofx.mad4milk.net).
1310/24/2005
14v(1.0.2)
15*/
16var Class = {
17  create: function() {
18    return function() {
19      this.initialize.apply(this, arguments);
20    }
21  }
22}
23
24Object.extend = function(destination, source) {
25  for (property in source) {
26    destination[property] = source[property];
27  }
28  return destination;
29}
30
31Function.prototype.bind = function(object) {
32  var __method = this;
33  return function() {
34    return __method.apply(object, arguments);
35  }
36}
37
38function $() {
39  var elements = new Array();
40
41  for (var i = 0; i < arguments.length; i++) {
42    var element = arguments[i];
43    if (typeof element == 'string')
44      element = document.getElementById(element);
45
46    if (arguments.length == 1)
47      return element;
48
49    elements.push(element);
50  }
51
52  return elements;
53}
54
55
56if (!window.Element) {
57  var Element = new Object();
58}
59
60Object.extend(Element, {
61  remove: function(element) {
62    element = $(element);
63    element.parentNode.removeChild(element);
64  },
65
66  hasClassName: function(element, className) {
67    element = $(element);
68    if (!element)
69      return;
70    var a = element.className.split(' ');
71    for (var i = 0; i < a.length; i++) {
72      if (a[i] == className)
73        return true;
74    }
75    return false;
76  },
77
78  addClassName: function(element, className) {
79    element = $(element);
80    Element.removeClassName(element, className);
81    element.className += ' ' + className;
82  },
83 
84  removeClassName: function(element, className) {
85    element = $(element);
86    if (!element)
87      return;
88    var newClassName = '';
89    var a = element.className.split(' ');
90    for (var i = 0; i < a.length; i++) {
91      if (a[i] != className) {
92        if (i > 0)
93          newClassName += ' ';
94        newClassName += a[i];
95      }
96    }
97    element.className = newClassName;
98  },
99 
100  // removes whitespace-only text node children
101  cleanWhitespace: function(element) {
102    element = $(element);
103    for (var i = 0; i < element.childNodes.length; i++) {
104      var node = element.childNodes[i];
105      if (node.nodeType == 3 && !/\S/.test(node.nodeValue))
106        Element.remove(node);
107    }
108  }
109});
110//base
111var fx = new Object();
112fx.Base = function(){};
113fx.Base.prototype = {
114        setOptions: function(options) {
115        this.options = {
116                duration: 500,
117                onComplete: ''
118        }
119        Object.extend(this.options, options || {});
120        },
121
122        go: function() {
123                this.duration = this.options.duration;
124                this.startTime = (new Date).getTime();
125                this.timer = setInterval (this.step.bind(this), 13);
126        },
127
128        step: function() {
129                var time  = (new Date).getTime();
130                var Tpos   = (time - this.startTime) / (this.duration);
131                if (time >= this.duration+this.startTime) {
132                        this.now = this.to;
133                        clearInterval (this.timer);
134                        this.timer = null;
135                        if (this.options.onComplete) setTimeout(this.options.onComplete.bind(this), 10);
136                }
137                else {
138                        this.now = ((-Math.cos(Tpos*Math.PI)/2) + 0.5) * (this.to-this.from) + this.from;
139                        //this time-position, sinoidal transition thing is from script.aculo.us
140                }
141                this.increase();
142        },
143
144        custom: function(from, to) {
145                if (this.timer != null) return;
146                this.from = from;
147                this.to = to;
148                this.go();
149        },
150
151        hide: function() {
152                this.now = 0;
153                this.increase();
154        },
155
156        clearTimer: function() {
157                clearInterval(this.timer);
158                this.timer = null;
159        }
160}
161//fader
162fx.Opacity = Class.create();
163fx.Opacity.prototype = Object.extend(new fx.Base(), {
164        initialize: function(el, options) {
165                this.el = $(el);
166                this.now = 1;
167                this.increase();
168                this.setOptions(options);
169        },
170
171        increase: function() {
172                if (this.now == 1) this.now = 0.9999;
173                if (this.now > 0 && this.el.style.visibility == "hidden") this.el.style.visibility = "visible";
174                if (this.now == 0) this.el.style.visibility = "hidden";
175                if (window.ActiveXObject) this.el.style.filter = "alpha(opacity=" + this.now*100 + ")";
176                this.el.style.opacity = this.now;
177        },
178
179        toggle: function() {
180                if (this.now > 0) this.custom(1, 0);
181                else this.custom(0, 1);
182        }
183});
184/*
185End moo.fx, simple effects library built with prototype.js (http://prototype.conio.net).
186by Valerio Proietti (http://mad4milk.net) MIT-style LICENSE.
187for more info (http://moofx.mad4milk.net).
18810/24/2005
189v(1.0.2)
190*/
191
192fx.buildBanner = function(_img, _width, _height, expDays) {
193        var last_loginid =      GetCookie("last_loginid");
194        if(GetCookie("showBanner_"+last_loginid) == 'false') {
195                return;
196        }
197        var expires = new Date();
198        expires.setTime(expires.getTime() + (expDays*24*60*60*1000));
199        document.cookie = "showBanner_"+last_loginid+"=false"+
200                                                  ";expires=" + expires.toGMTString()+
201                                                  ";path=/";   
202       
203        var screenWidth = document.body.clientWidth ? document.body.clientWidth: document.body.offsetWidth;
204        var screenHeight = document.body.clientHeight ? document.body.clientHeight: document.body.offsetHeight;
205        var _dLeft = (screenWidth/2) - (_width / 2);
206        var _dTop =  (screenHeight/2) - (_height / 2);
207        var _dWidth     = (document.all ? _img.width   : "auto");
208        var _dHeight = (document.all ? _img.height : "auto");   
209        var innerHTML = "<DIV align='center' id ='warning_msg' style='visibility:hidden;background:WHITE;position:absolute;"+
210        "border:1px solid black;left:"+_dLeft+"px;top:"+_dTop+"px;"+"width:"+_dWidth+"px;height:"+_dHeight+"px;padding:10px;"+
211        (!document.all ? "-moz-border-radius: 9px 9px 9px 9px;":"")+
212        "'><img src='"+_img.src+"'/><BR>"+
213        "<div style='width:100%' align='center' noWrap>"+
214        "<a title='Fechar' href='javascript:void(0)' onclick='javascript:myOpacity.toggle()'>FECHAR [X]</a></div>"+
215        "</DIV>";       
216
217        document.body.innerHTML += innerHTML;
218        myOpacity = new fx.Opacity('warning_msg', {duration: 600});     
219        document.getElementById("warning_msg").style.visibility = 'hidden';     
220        myOpacity.now = 0;
221        setTimeout("myOpacity.toggle()",3000);
222}
223
224// Load Banner.
225var _img = new Image();
226_img.src = url_path;
227fx.buildBanner(_img, img_width, img_height, expDays);