Object.extend = function(destination, source) {
  for (property in source) {
    destination[property] = source[property];
  }
  return destination;
}

function shMenu(obj,cmd) {
	if(typeof obj == "string") {
		obj = document.getElementById(obj);
	}
	obj.style.display=cmd;
}
function galeria(id)
{
	A('galeria_read.php','id='+id,'conteudo','GET');	
	document.getElementById('id_').value=id;
	//if(id>0) window.open('galeria_read.php?id='+id,'galeria'+id,'width=800,height=580,left=0,top=0,scrollbars=yes,status=no,address=no,menu=no');
}
function A(p,v,t,m) {
	AjaxGet(p,v,t,m);
}

var GI = function (){}
Object.extend(GI,{
  resizeZoom: function (obj) {
	var largura = obj.width;
	var altura = obj.height;
	obj.style.visibility='visible';
	var box = obj.parentNode;
	if(box.id=='zoom') {
		var pageSize = getPageSize();
		var pageScroll = getPageScroll();
		var boxTop = pageScroll.yScroll + ((pageSize.windowHeight - altura - 33) / 2);
		var boxLeft = ((pageSize.pageWidth - largura - 40) / 2);
		box.style.top = (boxTop < 0) ? "0px" : boxTop + "px";
		box.style.left = (boxLeft < 0) ? "0px" : boxLeft + "px";
		box.style.width=obj.width + 'px';
		box.style.height='auto';
	}
	GI.resizeHeight('sombra');
  },
  resizeHeight: function (obj) {
	obj = $(obj);
	if(obj && obj.id && obj.style) {
		var pageSize = getPageSize();
		obj.style.height = (pageSize.pageHeight) + 'px';
	}
  },
  closeZoom: function () {
	document.body.removeChild($('zoom'));
	$('sombra').style.display='none';
  },
  openZoom: function (obj) {
	var pageSize = getPageSize();
	
	var divSombra = $('sombra');
	if(!divSombra) {
	  divSombra=document.createElement('div');
	  divSombra.id = 'sombra';
	  divSombra.className = 'popSombra';
	  divSombra.onclick=function(){GI.closeZoom();}
	  divSombra.style.height = (pageSize.pageHeight + 'px');	  
	  document.body.appendChild(divSombra);
	}
	divSombra.style.display='block';
	var divZoom = document.createElement('div');
	divZoom.id = 'zoom';
	divZoom.className = 'popZoom';
	divZoom.style.width='400px';
	divZoom.style.height='200px';

	var imgZoom = document.createElement('img');
	imgZoom.id = 'img_';
	imgZoom.src = obj;
	imgZoom.alt = 'Fotos Felice';
	imgZoom.style.visibility='hidden';
	
	var divFechar = document.createElement('div');
	divFechar.className = 'fechar';
	
	var aFechar = document.createElement('a');
	aFechar.title = 'fechar';
	aFechar.className = 'fechar';
	aFechar.innerHTML = 'fechar';
	aFechar.href = '#';
	aFechar.onclick = function() {
		GI.closeZoom();
		return false;
	}
	divFechar.appendChild(aFechar);
	divZoom.appendChild(divFechar);	
	divZoom.appendChild(imgZoom);
	document.body.appendChild(divZoom);
	imgZoom.onload = function() {
	  GI.resizeZoom(this);
	  return false;
	}
	var pageScroll = getPageScroll();
	var boxTop = ((pageSize.windowHeight - 233) / 2);
	var boxLeft = ((pageSize.pageWidth - 440) / 2);
	divZoom.style.top = (boxTop < 0) ? "0px" : boxTop + "px";
	divZoom.style.left = (boxLeft < 0) ? "0px" : boxLeft + "px";
	GI.resizeHeight('sombra');
  }
});
function getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY){
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	if(yScroll < windowHeight) pageHeight = windowHeight;
	else pageHeight = yScroll;
	if(xScroll < windowWidth) pageWidth = windowWidth;
	else pageWidth = xScroll;
	arrayPageSize = {pageWidth:pageWidth,pageHeight:pageHeight,windowWidth:windowWidth,windowHeight:windowHeight}
	return arrayPageSize;
}

function getPageScroll(){
	var yScroll;
	if (self.pageYOffset) yScroll = self.pageYOffset;
	else if (document.documentElement && document.documentElement.scrollTop) yScroll = document.documentElement.scrollTop;
	else if (document.body) yScroll = document.body.scrollTop;
	arrayPageScroll = {yScroll:yScroll};
	return arrayPageScroll;
}
function show(obj){
  GI.openZoom(obj.href);
}
window["GI"]=GI;

function trim(str){return str.replace(/(^\s*)|(\s*$)/g,'');}

function valida_form(list){
  var retorno = true,
  aviso = document.getElementById('box-aviso');
  for(var i=0;i<list.length;i++){
	var field = document.getElementById('field-'+list[i]);
	if(trim(field.value)=='') {field.className='wrong';retorno=retorno&&false;} else {field.className='';}
  }
  aviso.style.display=retorno==true?'none':'block';
  return retorno;
}

function showFormMsg(title,msg) {
  var c = $('conteudo');
  var b = document.createElement('div');
  var t = document.createElement('div');
  var m = document.createElement('div');
  b.className = 'msg-box';
  t.innerHTML = title;
  t.className='msg-title';
  c.innerHTML = '';
  b.appendChild(t);
  m.innerHTML = msg;
  m.className='msg-value';  
  b.appendChild(m);
  c.appendChild(b);
}

function busca_noticias() {
  var q = $('q-busca-noticias');
  if(q) {
	var k=trim(q.value);
	if(k!='') {
	  AjaxGet('/noticias-busca.php','q='+k,'news-blank','GET');
	} else {
	  alert('Preencha o campo primeiro!');
	  q.focus();
	}
  }
}

function str_filter(str) {
  L=new Array('?','!',',','.',';','"',"'");
  str = str.replace('\"','"');
  str = str.replace("\'","'");
  str = str.replace('\\\\','\\');
  str=str.toLowerCase();
  str=str.replace(/\s{2}gi/,' ');
  str=str.replace(/\s/gi,'-');
  for(var i=160;i<=255;i++) {
    str=str.replace(RegExp(String.fromCharCode(i)),'');
  }
  for(var j=0;j<L.length;j++) {
    str=str.replace(RegExp('(['+L[j]+'])'),'',str);
  }
  return str;
}

var popul = function() {
  var root = document.getElementsByTagName('table');
  for(var i=0;i<root.length;i++) {
    if(root[i].className=='field-table') {root=root[i];break;}
  }
  if(root && root.className=='field-table') {
    var L = root.getElementsByTagName('div'),list=[];
	for(var i=0;i<L.length;i++) {
	  if(L[i].className=='form-field') list.push(L[i]);
	}
	if(list && list.length>0) {
	  for(var i=0;i<list.length;i++) {
	    var lab = list[i].getElementsByTagName('label')[0];
		var inp = list[i].getElementsByTagName('input')[0];
		inp.name = str_filter(lab.innerHTML);
		inp.value = lab.innerHTML;
	  }
	}
  }
}
