/*
* My Box script © 2007 John Davenport Scheuer
*  *********** Annotated Version ***********
* A kinder, gentler, yet versatile 'Box' script
*   This notice must remain for legal use
*/

if (!document.createElement||(!window.addEventListener&&!window.attachEvent)) //create a dummy object & function for non-supporting browsers
var mybox = {show:function(){return true;}};
else { //create the mybox object, and first hide the content (if any) that will be added via id
document.write('<style type="text/css"> #boxcontent { position:absolute;top:-80000px;left:-80000px;visibility:hidden; } <\/style>');
var mybox = {  //define the mybox object and its objects (functions, variables and elements)
loading_image:'css/loading.gif', //define path to loading image for the purpose of preloading it
ie6ha:0, content:[], http_request:null, reged_ids:[],
loadingtext:document.createTextNode('Aguarde . . .'),
loadingt:document.createElement('div'),
loading:document.createElement('div'),
olay:document.createElement('div'),
box:document.createElement('div'),
kid1:document.createElement('div'),
close:document.createElement('div'),
link:document.createElement('a'),
text:document.createTextNode('FECHAR'),
x:document.createElement('span'),
xtext:document.createTextNode('\xa0x'),
ie_selects:function(state){ //hides and reveals selects for IE 6 and earlier
for (var i = 0, s=document.getElementsByTagName('select'); i < s.length; i++)
s[i].style.visibility=state? state : '';
},
reg_id:function(obj){ //get a page element's id or create one for it and get that, if it has none
var n;
return obj.id || (obj.id=mybox.reged_ids[n=mybox.reged_ids.length]='my$box_'+n);
},
capit:function(t,c){ //function to add captions to the box, is passed caption [t]ext and (except w/Ajax) [c]ontent element
var m=mybox, cb=c&&c.tagName.toLowerCase()=='img';
m.cap=document.createElement('div');
m.cap.appendChild(document.createTextNode(t));
if(m.ie6ha&&c&&!cb)
m.cap.style.width=Math.max(250, c.offsetWidth)+'px';
else if(c&&!m.ie6ha||cb)
m.cap.style.width=c.offsetWidth+'px';
m.close.id='cap';
m.box.className='cap '+m.box.className;
m.close.insertBefore(m.cap,m.close.firstChild);
},
set_cook:function(d){ //function to set optional cookie, if used with optional onload display
if(d){var t=new Date(); t.setTime(t.getTime()+d*86400000)};
var p=mybox.allpages?'; path=/':'', e=d?'; expires='+t.toGMTString():'';
document.cookie=(p?'mybox_$all_$cookie_1':window.location.pathname)+'='+(d&&d==-1?'': '1')+e+p;
},
read_cook:function(){ //function to read optional cookie, if used with optional onload display
var c, i=0, n=(mybox.allpages?'mybox_$all_$cookie_1':window.location.pathname)+'=', a=document.cookie.split(';');
for(i ;i < a.length;i++){c=a[i];
while (c.charAt(0)==' ') c=c.substring(1,c.length);
if (c.indexOf(n)==0) return c.substring(n.length,c.length);}
return null;
},
begin:function(){ //set attributes for and append mybox page elements to each other and append/insert them to the page
var m=mybox, d=document.body;
m.loadingt.appendChild(m.loadingtext);
m.loading.appendChild(m.loadingt);
m.loading.id='loading';
m.x.appendChild(m.xtext);
m.link.appendChild(m.text);
m.link.appendChild(m.x);
m.link.href='javascript:close();';
if(window.opera||/Apple/.test(navigator.vendor||typeof document.documentElement.style.KhtmlOpacity=='string'))
m.link.style.fontWeight='normal';  //this and the above are kludgy, but it works out (a minor visual effect)
m.link.title="Fechar";
m.ad_event(m.link, 'click', m.hide);
m.close.className='close';
m.close.appendChild(m.link);
m.olay.id='theolay';
m.ad_event(m.olay, 'click', m.hide);
m.box.id='thebox';
m.kid1.className='id';
m.box.appendChild(m.kid1);
m.box.appendChild(m.close);
d.insertBefore(m.box, d.firstChild);
d.insertBefore(m.loading, d.firstChild);
d.insertBefore(m.olay, d.firstChild);
if(!document.all&&!/Apple/.test(navigator.vendor)&&window.removeEventListener){ //another kludge, fortunately browsers that
m.tframe=document.createElement('iframe'); // follow this 'by mistake' are not harmed by it. This section and some later code
m.tframe.style.position='absolute'; // overcomes early Mozilla's "continuous loading image bug" by creating another load
m.tframe.style.top='-1000px'; // event for that browser in a hidden iframe.  This apparently cancels the 'fake' one for the image.
m.tframe.style.left='-1000px';
m.tframe.style.visibility='hidden';
m.tframe.name='moz_img_kludge';
d.appendChild(m.tframe);
} //end early Mozilla iframe kludge setup
m.init();
},
init:function(){ //initialize existing page links to the script if they have one of the recognized rel attributes
var m=mybox, i=j=0, thelinks=document.links, re=new RegExp('^(img|iframe|ajax|id)$'),
lre=new RegExp('\\bloadit\\b'), rel, rev, ti, ref, lid, mid, c;
for (i; i < thelinks.length; i++){
if(re.test(rel=thelinks[i].rel)&&(!thelinks[i].id||!m[thelinks[i].id+'boxnum'])){ //tests if a link qualifies & if it hasn't been initialized already
lid=m.reg_id(thelinks[i]), ti=thelinks[i].title, rev=thelinks[i].rev; //registers its rel, rev, id, and title attributes to variables
if (rel=='id'){ //begin script adds id
m.content[j]=document.getElementById(rev).parentNode.removeChild(document.getElementById(rev));
m[rev+'cap']=ti||'';
} //end script adds id
else { //if not id activated, we now need to register the href
ref=thelinks[i].href;
if(document.images&&rel=='img'){ //begin script adds image
m.content[j]=document.createElement('div')
mid=m.reg_id(m.content[j]);
m[mid+'im']=document.createElement('img');
m[mid+'im'].className='boximage';
m.ad_event(m[mid+'im'], 'load', m.img_load);
m[mid+'savedsrc']=ref;
m[mid+'im'].alt=rev||'';
m[mid+'im'].title=ti||'';
m[mid+'im'].style.display='block';
m.content[j].appendChild(m[mid+'im']);
} //end script adds image
else if (rel=='iframe'){ //begin script adds iframe
m.content[j]=document.createElement('div');
mid=m.reg_id(m.content[j]);
m[mid+'ifm']=document.createElement('iframe');
m[mid+'ifm'].src=m.ie6ha? 'about:blank' : ref;
if(rev)
for (var k = 0, a=rev.split('::'); k < a.length; k++)
m[mid+'ifm'].setAttribute(a[k++], a[k], 0);
m.ad_event(m[mid+'ifm'], 'load', m.ifm_load);
m[mid+'savedsrc']=ref;
m.content[j].appendChild(m[mid+'ifm']);
m[mid+'cap']=ti||'';
} //end script adds iframe
else //script adds ajax (the one line below)
m.content[j]=[ref, ti||''];
}
if(lre.test(c=thelinks[i].className)){ //Check to see if there is content to dispaly onload using the class name 'loadit'
m.loadit=m.loadit||typeof m.loadit=='number'? m.loadit : j; //make sure there is no other already set by any other method
var z=0;
for (z,c=c.split(' '); z < c.length; z++){ //check for cookie instructions in the class name, if they are not set elsewhere, use them
m.days=typeof m.days=='number'? m.days : /days-{0,1}\d+/.test(c[z])? parseInt(c[z].replace(/days/,'')) : '';
m.allpages=typeof m.allpages=='number'? m.allpages : /allpages/.test(c[z])? 1 : '';
}
}
m[lid+'boxnum']=j++; //set the identifying object for each added content type, this will hold a reference to the activating link
m.ad_event(thelinks[i], 'click', m.a_init); //assign the link it its onclick event
}
}
if (typeof m.loadit!='undefined'&&(m.days==-1||!m.read_cook()||typeof mybox.days!='number')){ //check for onload content and cookie,
// load the content if it exists and if the cookie (if it exists) permits
setTimeout(function(){var l,c;(l=m.loadit)&&l[0]? m.show.apply(null, l) : (c=m.content[l])&&c[0]? m.show.apply(null, c) : m.show(l)},100);
if (m.days==-1||typeof m.days=='number'&&!m.read_cook()) //Set the cookie if need be
m.set_cook(m.days);
}
},
img_load:function(){mybox.show(mybox.n, 1)}, //function fired when image loads (varies by browser if only once, or every time)
ifm_load:function(){var l=mybox.loading.style;l.zIndex='';l.display='none';}, //function fired when iframe loads, dismisses loading div
a_init:function(e){ //function assigned to each rel= link that qualified in the init process
if (e&&e.preventDefault)
e.preventDefault();
return mybox.show(mybox[this.id+'boxnum']);
},
hide:function(e){ //function to remove box contents and stop displaying overlay, loading div and the box, 
if(e&&e.preventDefault) //also cleans up settings for a number of elements in the box
e.preventDefault();
var s='style',d='display',w='width',h='height',m=mybox;
m.http_request=null;
if(m.cap&&m.cap.parentNode==m.close)
m.close.removeChild(m.cap);
m.cap=null;
document.body[s][h]=document.body[s][w]='';
m.kid1.innerHTML='';
if(m[m.box.firstChild.id+'ifm'])
window.frames[0].location.replace('about:blank'); //set any iframe initialized content to a blank page
if(m.box.firstChild!=m.kid1)
m.box.replaceChild(m.kid1,m.box.firstChild);
m.loading[s][d]=m.olay[s][d]=m.box[s][d]='none';
m.loading[s].zIndex=m.close[s][w]=m.close.id=m.olay[s][h]=m.olay[s][w]=m.box[s].top=m.box[s].left='';
m.box[s].visibility='hidden';
if(m.ie6ha) //if ie6- show selects
m.ie_selects();
return false;
},
show:function(n){ //function to manage the appearance of the loading div, show the box, and insert its contents
var m=mybox, mid;
if(!arguments[1]||typeof arguments[1]=='string'||m.content[n][0]) //in case we are launching a box from an already displayed box
m.hide(); //clean up the launching box
if(m.cap&&m.cap.parentNode==m.close) //make sure no old caption is hanging around
m.close.removeChild(m.cap);
m.cap=null;
m.box.className='text'; //set default class for box (used with id and Ajax methods)
if(m.ie6ha) //if IE in quirks mode or ie6- remove scrpt added bottom border width style & hide selects
m.box.style.borderBottomWidth='',m.ie_selects('hidden');
m.loading.style.display=m.olay.style.display=m.box.style.display='block'; //display all box elements
m.boxLoc(m.loading); //position the loading divison
if(typeof n!='number'||(mid=m.content[n][0])) //if we have one of the two methods for Ajax content
return m.makeRequest.apply(this, mid? m.content[n]:arguments); //pass its data to the Ajax function and return
if(!m.content[n]||!m.content[n].tagName) //if not Ajax and not another valid registered content
return m.hide(); //return and clean up the beginning box
m.box.replaceChild(m.content[n],m.box.firstChild); //if we get here, we have valid content, insert it into the box
mid=m.content[n].id; //register its id to this function for later use
if(m[mid+'im']){ //begin if it is an image
m.n=n;
m.box.className=''; //removes class .text (minimum dimensions)
if(!m[mid+'im'].src){ //if the src for the image hasn't loaded yet, keep things hidden and exit until the image is loaded
m.box.style.visibility='';
m.box.style.left=m.box.style.top='-2000px';
m[mid+'im'].src=m[mid+'savedsrc'];
return false;
}
if(m.tframe){ //iframe loading kludge for early Mozilla if initialized
m[mid+'im'].removeEventListener('load', m.img_load, false);
window.frames['moz_img_kludge'].location.replace('about:blank');
}
if(m[mid+'im'].alt) //if the image has a caption
m.capit(m[mid+'im'].alt, m[mid+'im']);
} //end if it is an image
if (m[mid+'ifm']){ //begin if it is an iframe
m.loading.style.zIndex=20000;
m.box.className='plain'; //lends a default white background while iframe loads
window.frames[0].location.replace(m[mid+'savedsrc']);
} //end if it is an iframe
if (m[mid+'cap']) //if iframe or id loaded conent and a caption
m.capit(m[mid+'cap'], m.content[n]); //grab the caption and add it to the box
m.box.style.visibility='';
m.close.style.width='';
m.boxLoc(m.box); //position the box
if (!m[mid+'ifm']) //if not an iframe, get rid of the loading div (iframe dismisses it onload)
m.loading.style.display='none';
return false;
},
iedb:function(){ //document.body or document.documentElement - the eternal question
return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
},
iegetWidth:function(){ //this function is called from an expression in the mybox_ie.css stylesheet for IE quirks mode and ie6-
var m=mybox;
if(!m.ie6ha) //it sets a flag that is also a height adjustment function for the script to use in determning what IE mode to use
m.ie6ha=function(){return Math.max(m.iedb().offsetHeight, m.iedb().scrollHeight)-m.iedb().clientHeight>4? 0 : 4;};
return '';
},
boxLoc:function(obj){ //positions either the loading division or the box as well as setting the size of the overlay
var m=mybox, w=window, ow='offsetWidth', oh='offsetHeight', sw='scrollWidth', sh='scrollHeight', cw='clientWidth', iw='innerWidth';
if(obj==m.loading)
m.olay.style.height=m.olay.style.width='';
if(obj==m.box)
m.box.style.top=m.box.style.left='';
m.olay.style.height=Math.max(m.iedb()[oh], m.iedb()[sh])-(m.ie6ha? m.ie6ha() : 0)+'px';
m.olay.style.width=m.ie6ha?m.iedb()[sw]:Math.max(m.iedb()[ow], m.iedb()[sw])+'px';
var portH=w.innerHeight || m.iedb().clientHeight;
var portW=w[iw]&&m.iedb()[cw]? Math.min(m.iedb()[cw],w[iw]) : w[iw]? w[iw] : m.iedb()[cw];
var tS=self.pageYOffset || m.iedb().scrollTop || 0;
obj.style.top=(obj==m.loading||(obj[oh]<portH&&(!obj.firstChild||obj.firstChild[oh]<portH))? portH/2-obj[oh]/2 + tS : tS + 20)+'px';
tS=self.pageXOffset || m.iedb().scrollLeft || 0;
obj.style.left=(obj==m.loading||(obj[ow]<portW&&(!obj.firstChild||obj.firstChild[ow]<portW))? portW/2-obj[ow]/2 + tS : tS + 20)+'px';
if(m.iedb()[sh] < m.box[oh]+m.box.offsetTop+29||m.iedb()[sw] < m.box[ow]+m.box.offsetLeft+29){
m.olay.style.height=m.olay.style.width='';
if(m.iedb()[sh] < m.box[oh]+m.box.offsetTop+29)
document.body.style.height=m.box[oh]+m.box.offsetTop+(m.ie6ha||document.body.filters?25:10)+'px';
if(m.iedb()[sw] < m.box[ow]+m.box.offsetLeft+29)
document.body.style.width=m.box[ow]+m.box.offsetLeft+25+'px';
m.olay.style.height=Math.max(m.iedb()[oh], m.iedb()[sh])-(m.ie6ha? m.ie6ha() : 0)+'px';
m.olay.style.width=m.ie6ha?m.iedb()[sw]:Math.max(m.iedb()[ow], m.iedb()[sw])+'px';
}
},
makeRequest:function(url, cap) { //begin Ajax request function
var m=mybox;
m.close.style.width='';
if (cap)
m.capit(cap);
if (window.XMLHttpRequest&&(!window.ActiveXObject||/http/.test(window.location.href))) { // Mozilla, Safari, IE 7 live . . .
m.http_request = new XMLHttpRequest();
}
else if ((typeof createRequest == 'object') || (typeof createRequest == 'function'))
m.http_request = createRequest();
else if (window.ActiveXObject) { // IE6- (v7 locally)
/*@cc_on @*/
/*@if(@_jscript_version >= 5)
try {
m.http_request = new ActiveXObject('Msxml2.XMLHTTP');
} catch(e) {
try {
m.http_request = new ActiveXObject('Microsoft.XMLHTTP');
} catch(e) {
m.http_request = null;
}
}
@end @*/
}
if (!m.http_request)
return;
m.http_request.open('GET', url, true);
m.http_request.onreadystatechange = function() {
m.box.style.visibility='hidden';
if (m.http_request.readyState == 4 && (m.http_request.status == 200||!/http/.test(window.location.href))){ //if the content is ready
m.kid1.innerHTML = m.http_request.responseText; //load it into the display divsion
if(!document.images||!m.kid1.getElementsByTagName('img')[0]){ //if it contains no images that can be displayed
m.boxLoc(m.box); //position the box and show it
m.box.style.visibility='';
m.loading.style.display='none';
}
else { //if it contains images that can be displayed, make sure they are loaded before displaying
m.box.style.left=m.box.style.top='-2000px';
m.il=0;
for (var i = 0, ims=m.kid1.getElementsByTagName('img'); i < ims.length; i++)
m.ad_event(ims[i], 'load', m.count_images);
}
} //end 'if content ready'
if(m.cap) //if we have a caption, set its width to that of the loaded content
m.cap.style.width=(m.ie6ha? Math.max(250, m.kid1.offsetWidth):m.kid1.offsetWidth)+'px';
};
m.http_request.send(null);
return false;
},
count_images:function(e){ //function used by Ajax loaded images to make sure they are all loaded
var i=0, ims=mybox.kid1.getElementsByTagName('img');
for (i ; i < ims.length; i++)
if(ims[i]==this)
mybox.il++;
if(mybox.il==ims.length){ //if all images in the Ajax added content are loaded
mybox.boxLoc(mybox.box);
mybox.box.style.visibility='';
mybox.loading.style.display='none';
if(mybox.tframe) //apply the early Mozilla loading kludge if it was setup during initialization
window.frames['moz_img_kludge'].location.replace('about:blank');
}
},
events:[], //array to store events for later removal in IE
ad_event:function(el,etype,f){ //function to add events
if ( typeof window.addEventListener != "undefined" )
el.addEventListener( etype, f, false );
else if ( typeof window.attachEvent != "undefined" ){
var t = function() {
return f.apply(el);
};
mybox.events.push({'element': el, 'event': 'on'+etype, 'handler': t});
el.attachEvent( "on"+etype, t );
}
}
};
if(document.images){ //if image support, preload the loading image
mybox.loadingim=new Image(),
mybox.loadingim.src=mybox.loading_image;
}
mybox.ad_event(window, 'load', mybox.begin); //set onload event
mybox.ad_event(window, 'resize', function(){mybox.boxLoc(mybox.box);}); //set onresize event
if(mybox.events.length>0&&window.detachEvent) // begin extensive cleanup for IE
window.attachEvent('onunload', function(){
for(var i = 0, e = mybox.events; i < e.length;   i++) {
e[i].element.detachEvent(e[i].event, e[i].handler);
e[i].element[e[i].event] = null;
};
var m=mybox, kill_n=function(obj){
if(obj.parentNode)
obj.parentNode.removeChild(obj);
obj=null;
};
kill_n(m.loadingtext);
m.loading.id='';
kill_n(m.loadingt);
kill_n(m.xtext);
kill_n(m.text);
kill_n(m.x);
m.link.href='';
m.link.title="";
kill_n(m.link);
m.olay.id='';
m.box.id='';
m.kid1.className='';
kill_n(m.kid1);
kill_n(m.close);
kill_n(m.box);
kill_n(m.loading);
kill_n(m.olay);
var kill=function(obj){
for (var p in obj){
for (var z in p)
kill_n(z);
kill_n(p);
}
obj=null;
};
for (var j = 0; j < mybox.content.length; j++)
kill(mybox.content[j]);
for (var n in mybox)
kill(n);
mybox=null;
m=null;
}); // end extensive cleanup for IE
} //<-- Final closing brace mybox.js