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){
	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};
  },
  grow:function(element,newsize,callBack) {
    var size = Objects.getSize(element);
  }
}

var Diff = function(vA,vB){return [vB[0]-vA[0],vB[1]-vA[1]];}
var Sizer = function (){}
window.SizerObj=[];
Sizer.prototype = {
	ID:Math.floor(Math.random()*9999),
	factor:5,
	time:50,
	interval:null,
	px:document.layers?'':'px',
	fx: null,
	to:function(e,v1,v2,dir){
		if(typeof e!='String' && e.id=='') e.id='layer-'+(Math.random()*9999999);
		e=$(e);
		if(dir>0 && (v1[0]<v2[0] || v1[1]<v2[1]) ) {
			var B=Diff(v1,v2);
			v1[0]+=Math.ceil(B[0]/this.factor);
			v1[1]+=Math.ceil(B[1]/this.factor);
		} else
		if( dir<=0 && (v1[0]>v2[0] || v1[1]>v2[1]) ) {
		  var B=Diff(v2,v1);
		  v1[0]-=Math.ceil(B[0]/this.factor);
		  v1[1]-=Math.ceil(B[1]/this.factor);
		} else {
		  this.fx = null;
		  if(typeof this.callback == 'function') {
		    this.callback();
			this.callback = null;
		  }
		  this.timer = null;
		  for(var v=0;v<window.SizerObj.lenght;v++) {
			if(window.SizerObj[v].ID == this.ID) {window.SizerObj[v]=null;break;}
		  }
		  return false;
		}
		e.style.width=v1[0]+this.px;
		e.style.height=v1[1]+this.px;
		this.fx = function(){Sizer.to(e,v1,v2,dir);}
		if(this.timer==null) this.timer = setInterval(this.fx,this.time);
	},
	toFx:function(e,factor,limit,dir,callBack){
	  if(factor>1 || factor<-1) factor=factor/100;
	  if(dir>0&&factor<1) factor+=1;
	  var size = [Objects.getWidth(e),Objects.getHeight(e)];
	  if(typeof factor != 'Array') factor = [factor,factor];
	  var newsize = [Math.ceil(size[0]*factor[0]),Math.ceil(size[1]*factor[1])];
	  if(dir<=0) {
		if(newsize[0]<limit[0]) newsize[0]=limit[0];
		if(newsize[1]<limit[1]) newsize[1]=limit[1];
	  } else
	  if(dir>0) {
		if(newsize[0]>limit[0]) newsize[0]=limit[0];
		if(newsize[1]>limit[1]) newsize[1]=limit[1];
	  }
	  var pos = window.SizerObj.length;
	  window.SizerObj[pos] = new Sizer();
	  window.SizerObj[pos].callback = callBack;
	  window.SizerObj[pos].to(e,size,newsize,dir);
	},
	grow:function(e,v1,v2,callBack){
	  var pos = window.SizerObj.length;
	  window.SizerObj[pos] = new Sizer();
	  window.SizerObj[pos].callback = callBack;
	  window.SizerObj[pos].to(e,v1,v2,1);
	},
	reduce:function(e,v1,v2,callBack){
	  e=$(e);
	  var pos = window.SizerObj.length;
	  window.SizerObj[pos] = new Sizer();
	  window.SizerObj[pos].callback = callBack;
	  window.SizerObj[pos].to(e,v1,v2,-1);
	},
	growFx:function(e,fx,xmax,callBack){Sizer.toFx(e,fx,xmax,1);},
	reduceFx:function(e,fx,xmin,callBack){Sizer.toFx(e,fx,xmin,0);}
}
Extend(Sizer,Sizer.prototype);

var ComboBox = function(){}
ComboBox.prototype = {
  fnList: new Array(),
  create: function(target,list,callback) {
	target=$(target);
	var id=target.id;
    if(!list || !list.length || !list[0].length) return false;
	if(!target) return false;
	target.className = 'js-combobox';
	var pos = Objects.getPosition(target);
	var combo = document.createElement('div');
	var comboButton = document.createElement('a');
	comboButton.className = 'js-combobox-button';
	comboButton.onclick = function(){ComboBox.Show_Hide('combolayer_'+id); /*if(!window.document.all) $('combolayer_'+id).focus();*/ return false;}
	comboButton.href='#';
	combo.appendChild(comboButton);
	var viewport = document.createElement('div');
	viewport.className = 'js-combobox-viewport';	
	viewport.onclick = comboButton.onclick;//function(){ComboBox.Show_Hide('combolayer_'+id); return false;}
	viewport.id = 'viewport_'+id;
	viewport.style.width = (Objects.getWidth(target)-35) + 'px';
	viewport.innerHTML = list[1][0];
	combo.appendChild(viewport);	
	window.ComboBox.fnList[id]=callback;
	var comboList = document.createElement('ul');
	comboList.className = 'js-combo-list';	
	comboList.id = 'combolayer_'+id;
//	comboList.onblur = Function('window.ComboBox.Hide("'+comboList.id+'");');	// exit on loose 'focus'	
	comboList.style.left = pos.x + 'px';
	comboList.style.width = (Objects.getWidth(target)-2) + 'px';
//	if(list[0].length <= 8)
	comboList.style.height='auto';
	comboList.style.top = (Objects.getHeight(target) + pos.y) + 'px';
	var tagLI,tagA,a,b;
	for(var i=0;i<list[0].length;i++) {
	  tagLI = document.createElement('li');
	  tagA = document.createElement('a');
	  a=list[0][i],b=list[1][i];
	  tagA.onclick = Function('var v=$("viewport_'+id+'");v.innerHTML="'+b+'";v.title=v.innerHTML;window.ComboBox.Show_Hide("combolayer_'+id+'");window.ComboBox.fnList["'+id+'"]("'+a+'");return false;');
	  tagA.href='#';
	  tagA.innerHTML = b;
	  tagA.title = b;
	  tagLI.appendChild(tagA);
	  comboList.appendChild(tagLI);
	}
	document.body.appendChild(comboList);
	comboList = null;
	target.innerHTML = '';
	target.appendChild(combo);
  },
  Show_Hide: function(obj) {
	obj = $(obj);
    if(!obj) return false;
	if(obj.style.display!='block') {
	  obj.style.display='block';
	  var posY = Objects.getPosition(obj).y,
	  size=Objects.getPageviewSize(),
	  scroll = Objects.getPageScroll(),
	  height = Objects.getHeight(obj);
	  scroll = scroll.yScroll;
	  while(posY > (size.pageHeight + scroll - height) && posY > 0) {
        posY--;
	  }
	  obj.style.top = posY+'px';
	} else {obj.style.display='none';}
  },
  Hide: function(obj) {
	obj = $(obj);
    if(!obj) return false;
	obj.style.display='none';
  }
}
Extend(ComboBox,ComboBox.prototype);

var GI = {
  filelist: new Array(),
  descriptions: new Array(),
  timeout:5000,
  timer:null,
  zoomTimer:null,
  isPaused:false,
  index:0,
  start:function(i,j){
	window.GI.filelist=i;
	window.GI.descriptions=j;
	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);
	this.index=id;
	if(++id>=window.GI.filelist.length) {id=0;}
	window.clearTimeout(window.GI.timer);
	window.GI.timer=setTimeout('window.GI.show('+id+')',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]);
  },
  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});
	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();
	
	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 && obj.onlyImg != true) {
	  var spanFonte = document.createElement('div');
	  spanFonte.className = 'fonte';
	  spanFonte.innerHTML = '<strong>'+base64_decode(window.GI.descriptions[obj.id])+'</strong>';
	}
	
	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 && obj.onlyImg != true) divZoom.appendChild(spanFonte);
	divZoom.appendChild(imgZoom);
	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";
  }
}
