var Objects = {
  getHeight: function(object) {
    return $(object).offsetHeight;
  },
  getWidth: function(object) {
    return $(object).offsetWidth;
  },
  getPageviewSize: function() {
	var width, height;
	if (self.innerHeight) {
		width = self.innerWidth;
		height = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		width = document.documentElement.clientWidth;
		height = document.documentElement.clientHeight;
	} else if (document.body) {
		width = document.body.clientWidth;
		height = document.body.clientHeight;
	}
	arrayPageSize = {pageWidth:width,pageHeight:height}
	return arrayPageSize;  
  },
  getPageSize: function() {
	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;
  },
  getPageScroll: function(){
	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;
  },
  getPosition:function(element){
	element=$(element);
	var left=0,top=0;
	if(!element) return {x:left, y:top};
	while (element.offsetParent){ left += element.offsetLeft; top  += element.offsetTop; element = element.offsetParent; }
	left += element.offsetLeft;
	top  += element.offsetTop;
	return {x:left, y:top};
  },
  ShowHide: function(object) {
	object=$(object);
	if(object && object.style) {
	  object.style.display=object.style.display!='block'?'block':'none';
    }
  },
  extend: function(destination,source) {
    for (var property in source) destination[property] = source[property];
	return destination;
  },
  E: function(tag) {
	if(document) return document.createElement(tag);
  }  
}

var GI = {
  filelist: new Array(),
  descriptions: new Array(),
  author: new Array(),
  timeout:5000,
  timer:null,
  zoomTimer:null,
  isPaused:false,
  index:0,
  start:function(i,j,k){
	window.GI.filelist=i;
	window.GI.descriptions=j;
	window.GI.author=k;
	if(window.GI.filelist.length>0) window.GI.show(0);
  },
  show:function(id){
	if(window.GI.filelist[id]<=0 || window.GI.filelist[id]=='undefined' || window.GI.isPaused==true) return false;
	else $('if_loader').src='/gi-load-file.php?id='+window.GI.filelist[id];
  },
  setPic: function(id){	
	if(id=='undefined' || id<0 || id==null) return false;
	for(var i=0;i<window.GI.filelist.length;i++) {
	  if(window.GI.filelist[i]==id) {id=i;break}
	}
	var img = document.createElement('img'),
	container=$('imagem-rotativa');
	while(container.hasChildNodes()) container.removeChild(container.firstChild);
    img.src='/imagem-rotativa/'+window.GI.filelist[id];
	img.alt='carregando... ';
	img.onclick=function(){window.GI.zoom(window.GI.index);}
	container.appendChild(img);	
	if((id)==window.GI.filelist.length) {id=0;}
	window.clearTimeout(window.GI.timer);
	window.GI.timer=setTimeout('window.GI.show('+(id+1)+')',window.GI.timeout);
	window.clearInterval(window.GI.zoomTimer);
	window.GI.zoomTimer=setInterval('window.GI.resizeHeight("sombra")',300);
	$('toolbox-texto').innerHTML = base64_decode(window.GI.descriptions[id]);
	this.index=id;
	id++;	
  },
  next:function(){	
    if(window.GI.index<window.GI.filelist.length-1) {
	  window.clearTimeout(window.GI.timer);
	  window.GI.show(window.GI.index+1);
	}
  },
  prev:function(){
    if(window.GI.index>0) {
	  window.clearTimeout(window.GI.timer);
	  window.GI.show(window.GI.index-1);
	}
  },
  zoom:function(id){
	var i='/imagem-grande/'+window.GI.filelist[id];
	window.GI.openZoom({id:id,src:i,onlyImg:false});
	window.GI.isPaused=true;
  },
  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 = Objects.getPageSize();
		var pageScroll = Objects.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';
	}
  },
  resizeHeight: function (obj) {
	
	if(window.GI.isPaused==false) return false;
	
	obj = $(obj);
	if(obj && obj.id && obj.style) {
		var pageSize = Objects.getPageSize();
		obj.style.height = (pageSize.pageHeight) + 'px';
	}
  },
  closeZoom: function () {
	document.body.removeChild($('zoom'));
	window.GI.isPaused=false;
	if(window.GI.index>=window.GI.filelist.length) {
	  window.clearTimeout(window.GI.timer);
	  window.GI.show(0);
	}
	else {
	  window.clearTimeout(window.GI.timer);
	  window.GI.show(window.GI.index+1);
	}
	$('sombra').style.display='none';
  },
  openZoom: function (obj) {
	var pageSize = Objects.getPageSize();
	if(!obj.onlyImg) obj['onlyImg'] = false;
	var divSombra = $('sombra');
	if(!divSombra) {
	  divSombra=document.createElement('div');
	  divSombra.id = 'sombra';
	  divSombra.className = 'popSombra';
	  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';
	
	if(obj.onlyImg != true) {
	  var spanFonte = document.createElement('div');
	  spanFonte.className = 'fonte';
	  spanFonte.innerHTML = '<strong>'+base64_decode(window.GI.descriptions[obj.id])+'</strong>';
	  var spanAutor = document.createElement('div');
	  spanAutor.className = 'fonte';
	  spanAutor.innerHTML = 'Foto: '+base64_decode(window.GI.author[obj.id]);
	}
	
	var imgZoom = document.createElement('img');
	imgZoom.id = 'img_' + window.GI.filelist[obj.id];
	imgZoom.src = obj.src;
	imgZoom.alt = GI.descriptions[obj.id];
	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() {
		window.GI.closeZoom(this);
		return false;
	}
	divFechar.appendChild(aFechar);
	divZoom.appendChild(divFechar);
	if(obj.onlyImg != true) divZoom.appendChild(spanFonte);
	divZoom.appendChild(imgZoom);
	if(obj.onlyImg != true) divZoom.appendChild(spanAutor);
	document.body.appendChild(divZoom);
	imgZoom.onload = function() {
	  window.GI.resizeZoom(this);
	  return false;
	}
	var pageScroll = Objects.getPageScroll();
	var boxTop = pageScroll.yScroll + ((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";
  }
}