Extend = function(destination,source) {
  for (var property in source) destination[property] = source[property];
  return destination;
}
var $E = function(tag) {if(document) return document.createElement(tag);}
/*var News = {
  list: new Array(),
  getXML: function() {
	Ajax.Get('/news.xml','',null,'GET',News.process);
  },
  process: function() {
	var result = arguments[0].responseXML;
	if(result) {
	  var A = result.getElementsByTagName('noticias');
	  if(A) {
		A=A[0].childNodes;
		for(var i=0;i<A.length;i++) if(A[i].nodeType==1) {
		  var B = A[i].childNodes;
		  var O = {};
		  for(var j=0;j<B.length;j++)
		    if(B[j].nodeType==1) O[News.N(B[j])]=News.V(B[j]);
		  News.list.push(O);
		}
	  }
	}
	window.News.apply();
  },
  apply:function() {
	var N = window.News,L=window.News.list,C = $('lista-destaques');
	C.innerHTML='';
	for(var i=0;i<N.list.length;i++) {
	  var tagLI = $E('li');
	  var imgBox = $E('div');
	  imgBox.className = 'image-box';
	  var tagIMG = $E('img');
	  with(tagIMG) {
		src='/imagem/c/160/100'+L[i].foto;
		alt=L[i].titulo;
		title='Clique para ver a noticia';
		onclick = Function('window.News.applyItem('+L[i].id+');return false;');
	  }
	  imgBox.appendChild(tagIMG);
	  tagLI.appendChild(imgBox);
	  var title = $E('div');
	  title.className = 'titulo';
	  var tagA = $E('a');
	  tagA.innerHTML = L[i].titulo;
	  tagA.title = 'Ir para página da notícia';
	  tagA.href='/noticias/'+L[i].id;
	  title.appendChild(tagA);
	  tagLI.appendChild(title);
	  tagLI.onmouseover = Function('this.className = "li-over"');
	  tagLI.onmouseout = Function('this.className = ""');
	  C.appendChild(tagLI);
	}
	$('barra-lateral').style.backgroundImage='url()';
	$('centro').style.backgroundImage='url()';
	window.News.applyItem(window.News.list[0].id);
	return false;
  },
  applyItem: function(id){	
	var L=window.News.list,index=-1;
	for(var i=0;i<L.length;i++)
	  if(L[i].id==id) { index = i; break; }
	if(index>=0) {
	  var I=L[i];
	  var C = $('centro');
	  C.style.backgroundImage='url(/img/carregando.gif)';
	  C.innerHTML='';
	  var tag = $E('div');
	  tag.className = 'tag';
	  tag.innerHTML = I.tag;
	  C.appendChild(tag);
	  var h = $E('h2');
	  h.innerHTML='<a href="/noticias/'+I.id+'">'+I.titulo+'</a>';
	  C.appendChild(h);
	  var text = $E('div');
	  text.className='texto';
	  text.innerHTML = '<a href="/noticias/'+I.id+'">'+I.descricao+'</a>';
	  C.appendChild(text);
	  var frame = $E('div');
	  frame.className = 'frame-imagem';
	  var autor = $E('div');
	  autor.className = 'autor';
	  autor.innerHTML = I.autor;
	  frame.appendChild(autor);
	  var img = $E('img');
	  img.src = '/imagem/v/300'+I.foto;
	  img.onload = Function("$('centro').style.backgroundImage='url()';");
	  frame.appendChild(img);
	  var legenda = $E('div');
	  legenda.className = 'legenda';
	  legenda.innerHTML = I.legenda;
	  frame.appendChild(legenda);
	  C.appendChild(frame);
	}	
  },
  V:function(obj) {
	return obj.firstChild.nodeValue;
  },
  N:function(obj) {
	return obj.nodeName;
  }
}
*/
var NewsAbas = {
  list: new Array(),
  activeItem:0,
  setTabs:function(list) {
	if(!list || !list.length) return false;
	var div = $('ul-abas');
	div.innerHTML = '';
	for(var tag=0;tag<list.length;tag++) {
	  var item = list[tag],
	  tagLI = $E('li'),
	  tagA = $E('a');
	  tagA.onclick=Function('window.NewsAbas.select('+item[0]+');return false;');
	  tagA.innerHTML = item[1];
	  tagA.href='#';
	  tagLI.appendChild(tagA);
	  tagLI.id = 'li-abas-'+item[0];
	  div.appendChild(tagLI);
	}
  },
  mount:function(array) {
	if(!array || !array.length) return false;
	var aba,lista,tagLI,tagA,tagUL;
	for(var abas=0;abas<array.length;abas++) {
	  aba=0;list=[];tagLI=null;tagA=null;tagUL=null;news=null;
	  aba = array[abas][0];
	  list = array[abas][1];
	  tagUL = $('ul-lista-'+aba);
	  tagUL.innerHTML = '';
	  for(var i=0;i<list.length;i++) {
		news = list[i];
		tagLI = $E('li');
		tagA = $E('a');
		tagA.href='/noticias/'+news[0];
		tagA.innerHTML = '<span>'+news[1]+'</span> '+news[2];
		tagLI.appendChild(tagA);
		tagUL.appendChild(tagLI);		
	  }
	  var vm = $E('li');
	  vm.innerHTML = '<a href="/noticias/tags/'+String(array[abas][2]).toLowerCase()+'">veja mais nesta categoria &raquo;</a>';
	  vm.className = 'veja-mais';
	  tagUL.appendChild(vm);
	  aba=0;
	  list=[];
	  tagUL=null;
	  news=null;
	}
	window.NewsAbas.select(array[0][0]);
  },
  select:function(id) {
	window.NewsAbas.activeItem = id;
	var itemID = 'ul-lista-'+id,
	tabID = 'li-abas-'+id,
	target = $('container').getElementsByTagName('ul');
	for(var i=0;i<target.length;i++)
	  if(itemID==target[i].id) {
	    target[i].style.display='block';
	  } else {
		target[i].style.display='none';
	  }
	target = $('ul-abas').getElementsByTagName('li');
	for(var i=0;i<target.length;i++)
	  if(target[i].id==tabID) {
		target[i].className = 'selecao';
	  } else {
		target[i].className = '';
	  }
  }
}

function base64_decode(data) {
var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var o1, o2, o3, h1, h2, h3, h4, bits, i = ac = 0, dec = "", tmp_arr = [];
do {
  h1 = b64.indexOf(data.charAt(i++));
  h2 = b64.indexOf(data.charAt(i++));
  h3 = b64.indexOf(data.charAt(i++));
  h4 = b64.indexOf(data.charAt(i++));
  bits = h1<<18 | h2<<12 | h3<<6 | h4;
  o1 = bits>>16 & 0xff;
  o2 = bits>>8 & 0xff;
  o3 = bits & 0xff;
  if (h3 == 64) {tmp_arr[ac++] = String.fromCharCode(o1);}
  else if (h4 == 64) {tmp_arr[ac++] = String.fromCharCode(o1, o2);}
  else {tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);}
} while (i < data.length);

dec = tmp_arr.join('');
dec = utf8_decode(dec);
return dec;
}
function utf8_decode ( str_data ) {
    var tmp_arr = [], i = ac = c = c1 = c2 = 0;
    while ( i < str_data.length ) {
        c = str_data.charCodeAt(i);
        if (c < 128) {
            tmp_arr[ac++] = String.fromCharCode(c); 
            i++;
        } else if ((c > 191) && (c < 224)) {
            c2 = str_data.charCodeAt(i+1);
            tmp_arr[ac++] = String.fromCharCode(((c & 31) << 6) | (c2 & 63));
            i += 2;
        } else {
            c2 = str_data.charCodeAt(i+1);
            c3 = str_data.charCodeAt(i+2);
            tmp_arr[ac++] = String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }
    }
    return tmp_arr.join('');
}

/**************/

function comboServicos(value) {
  open(value,'_self');
  return false;
}
function comboSecretarias(value) {
  open('/secretarias/'+value,'_self');
  return false;
}
function comboCidade(value) {
  var re=new RegExp('/([a-z])/','gi');
  if(re.test(value)){var link=value;} else {var link='/a-cidade/'+value;}
  open(link,'_self');
  return false;
}
function comboDiversos(value) {
  open(value,'_self');
  return false;
}

function buscar_focus(o) {
  o.className='input';
  if(o.value=='pesquisa') o.value='';
}
function buscar_blur(o) {
  o.className='input blur';
  if(o.value=='') o.value='pesquisa';
}

var Menu = Class({
  list:['cidade','secretarias'],
  show:function(item) {
    var layer=$('menu-'+item);
	if(!layer) return false;
	var ref = $('link-'+item),
	    pos = {p:Objects.getPosition(ref),height:Objects.getHeight(ref)},
		fun = Function("var i=arguments[0]; if(i=='"+item+"') return false; $('menu-'+i).style.display='none';$('link-'+i).className='';");
	this.list.foreach(fun);
	
	if(layer.style.display!='block') {
	  layer.style.display='block';
	  layer.style.top = (pos.p.y + pos.height-1)+'px';
	  layer.style.left = Objects.getPosition('menu-topo').x + 'px';
	  ref.className = 'selected';
	} else {
	  ref.className = '';
	  layer.style.display='none';
	}
  }
});

function contaCaracter(opt_boxcontado, opt_contarcorpo, opt_valormax) {
  var boxcontado = opt_boxcontado ? opt_boxcontado : "contado";
  var contarcorpo = opt_contarcorpo ? opt_contarcorpo : "contador";
  var valormax = opt_valormax ? opt_valormax : 150;
	
  var field = document.getElementById(boxcontado);
  if (field && field.value.length >= valormax) {
	field.value = field.value.substring(0, valormax);
  }
  var txtField = document.getElementById(contarcorpo);
  if (txtField) {  
	txtField.innerHTML = opt_valormax - field.value.length;
  }
}


var Overlay = {
  show:function(){
	var lay = $('site-overlay');
    if(!lay) {
	  lay = document.createElement('div');
	  lay.id='site-overlay';
	  document.body.appendChild(lay);
	}
	lay.style.width='100%';
	var ps = Objects.getPageScroll().yScroll*1;
	lay.style.height = (Objects.getPageSize().pageHeight+ps)+'px';
	lay.style.display='block';
	lay.style.zIndex='4500';
	var ls = document.getElementsByTagName('select');
	if(ls.length) {
	  for(var i=0;i<ls.length;i++) ls[i].style.visibility='hidden';
	}
  },
  hide:function() {
    $('site-overlay').style.display='none';
	var ls = document.getElementsByTagName('select');
	if(ls.length) {
	  for(var i=0;i<ls.length;i++) ls[i].style.visibility='visible';
	}
  }
}

function agenda_detalhe(id,ref) {
  Overlay.show();
  var layer = document.createElement('div');
  layer.id='agenda-detalhes';
  var buttonClose = document.createElement('a');
  buttonClose.innerHTML = '<img src="/img/fechar-grande.gif" alt="Fechar" />';
  buttonClose.className = 'aclose';
  buttonClose.href='#';
  buttonClose.onclick=function(){var v=$('agenda-detalhes'); if(v) v.parentNode.removeChild(v); Overlay.hide(); return false;}
  var content = document.createElement('div');
  content.id='agenda-ajax';
  content.className = 'content';
  layer.appendChild(buttonClose);
  layer.appendChild(content);  
  document.body.appendChild(layer);
  var sz = Objects.getPageviewSize(),
  scr = Objects.getPageScroll().yScroll;
  var left = sz.pageWidth-Objects.getWidth(layer),
  top = scr + 50;
  left = Math.ceil(left/2);
  layer.style.left = left+'px';
  layer.style.top = top+'px';
  RDA.Ajax.Get('/agenda/'+id,'','agenda-ajax','GET',function(req){$('agenda-detalhes').style.height='auto';});
  return false;
}

function trim(str){return str.replace(/(^\s*)|(\s*$)/g,'');}
function re(r,v) {return new RegExp(r,'gi').test(v);}

var Newsletter = {
  register: function() {
	var name=$('news-nome').value;
	var email=$('news-email').value;
	var r_e='^(\\w|[-.])+@(\\w|[-.])+\.(\\w|[-.])+$';
	if(trim(name)!='' && trim(email)!='') {
	  if(!re(r_e,email)) {
	    alert('Opa!\n\nSeu e-mail não está correto.\nVerifique se não há nada faltando...');
	  } else {
	    RDA.Ajax.Get('/assinar-newsletter','nome='+name+'&email='+email,'','POST');
	  }
	} else {
	  alert('Opa!\n\nVocê não preencheu os campos ainda...\n\nPreencha com seu nome e email.');
	}
  },
  unregister:function() {
	var email=$('news-email').value;
  }
}
