var fileList = new Array();
var fileSize = new Array();
var filePos = new Array();
var galleryID = 0;
var actualPage = 0;
var actualFile = 0;
var totalPages = 0;
var numPerPage = 12;
var fadeValue = 0;

function imageGallery(id) {
	if(id>0) {
		window.open('/galerias/'+id,'galeria'+id,'width=770,height=520,status=no,address=no,menus=no,scrollbars=no');
		return;
	}
	return imageGallery;
}

imageGallery.status = new Function();
imageGallery.status = function (msg,type) {
	var o = document.getElementById('status');
	fadeValue = 200;
	imageGallery.fadeStatus();
	o.innerHTML = msg;
	if(type=='w') {o.className = 'status_warning';}
	else if(type=='e') {o.className = 'status_error';}
	else {o.className = 'status_warning';}
}

imageGallery.start = new Function();
imageGallery.start = function(id) {
	galleryID = id;
	var req = Ajax.getTransport();
	if(req) {
		var rnd=parseInt(Math.random()*99999999);
		req.open("GET","/galeria/filelist/"+id,true);
		req.onreadystatechange = function () {imageGallery.updateFiles(req);}
		req.send(null);
	}
}

imageGallery.updateFiles = new Function();
imageGallery.updateFiles = function (request) {
	if( request.readyState == 1) {
		imageGallery.status("Carregando dados...",'w');
	}
	if( request.readyState >= 2 && request.readyState != 4) {
		imageGallery.status("Verificando imagens...",'w');
	}
	if( request.readyState == 4) {
		if(request.status == 200) {
			var xml = request.responseXML;
			var title = xml.getElementsByTagName('title');
			var descr = xml.getElementsByTagName('description');
			var file = xml.getElementsByTagName('file');			
			for(var i=0;i<file.length;i++) {
				var objNode = file[i];
				if(objNode.nodeType == 1) {
					for(var j=0;j<objNode.childNodes.length;j++) {
						var objNode2 = objNode.childNodes[j];
						if(objNode2.nodeType == 1) {
							switch (objNode2.nodeName) {
								case "name": 
									fileList.push(objNode2.firstChild.nodeValue);
								break;
								case "pos":
									filePos.push(objNode2.firstChild.nodeValue);
								break;
								case "dimension":
									fileSize.push(objNode2.firstChild.nodeValue);
								break;
							}
						}
					}
				}
			}
			totalPages = Math.ceil(fileList.length / numPerPage);
			imageGallery.updatePages();
			imageGallery.loadFiles(actualPage);
			imageGallery.status("Atualizando miniaturas...",'w');
		} else {
			imageGallery.status("Falha na galeria!",'e');	
		}
	}
}

imageGallery.loadFiles = new Function();
imageGallery.loadFiles = function (page) {
	actualPage = page;
	if(fileList.length > 0) {
		var start = numPerPage * page;
		var j=0;
		for(var i=start; i<start + numPerPage; i++) {
			j++;
			var obj = document.getElementById('thumb'+j);
			if(fileList[i] != "" && typeof fileList[i] != "undefined") {
//				obj.innerHTML = '<a href="javascript:;" onclick="imageGallery.zoomImage('+i+')" style="background-image:url(/galeria/arquivos/'+galleryID+'/v/100/'+fileList[i]+')"> &nbsp; </a>';
				obj.innerHTML = '<a href="javascript:;" onclick="imageGallery.zoomImage('+i+')"><img src="/galeria/arquivos/'+galleryID+'/'+filePos[i]+'/'+(filePos[i]=='v'?75:56)+'/'+fileList[i]+'" /></a>';
			} else {
				obj.innerHTML = '<img src="/galeria/img/sem_foto.jpg" />';
			}
		}
	}
	imageGallery.status('Página '+(actualPage+1)+' de '+totalPages+'.','w');
	imageGallery.updatePages();
}

imageGallery.updatePages = new Function();
imageGallery.updatePages = function () {
	var o = document.getElementById('footer');
	var html = "";
	for(var i=0; i<totalPages; i++) {
		html += '<a href="javascript:;" onclick="imageGallery.loadFiles('+i+')" class="pageNumber_'+(i==actualPage?'selected':'normal')+'">'+(i+1)+'</a> ';
	}
	o.innerHTML = html;
}

imageGallery.fadeStatus = new Function();
imageGallery.fadeStatus = function () {
	fadeValue-=3;
	if(fadeValue>0) {
		var x = document.getElementById('status');
		if(fadeValue<100) {
			x.style.opacity=parseFloat(fadeValue/100);
			x.style.filter='alpha(Opacity='+fadeValue+')';
		} else {
			x.style.opacity='1.0';
			x.style.filter='alpha(Opacity=100)';
		}
		setTimeout('imageGallery.fadeStatus()',75);
	}
}

imageGallery.zoomImage = new Function();
imageGallery.zoomImage = function (fileID) {
	actualFile=fileID;
	if(filePos[fileID]=='v') {
		var size = 470;
	} else {
		var size = 370;
	}
	AjaxGet('/img_gallery/verfoto.php','id='+galleryID+'&m='+filePos[fileID]+'&s='+size+'&f='+fileList[fileID],'viewer','GET');
/*	//document.getElementById('viewer').innerHTML='<img src="/galeria/arquivos/'+galleryID+'/'+filePos[fileID]+'/'+size+'/'+fileList[fileID]+'" />';
	//document.getElementById('viewer').style.backgroundImage='url(/galeria/arquivos/'+galleryID+'/'+filePos[fileID]+'/500/'+fileList[fileID]+')';*/
}

imageGallery.scroll = new Function();
imageGallery.scroll = function (page) {
	if(page < 0) { imageGallery.status("Você já está no início...",'e'); return;}
	if(page >= totalPages) { imageGallery.status("Este é o final da galeria!",'e');return;}
	if( page >= 0 && page < totalPages ) {
		imageGallery.loadFiles(page);
	}
}

imageGallery.saveImage = new Function();
imageGallery.saveImage = function() {
	if(galleryID > 0 && actualFile>0) {
		window.open('/galeria/download/'+galleryID+'/'+fileList[actualFile],'download');
	}
}
imageGallery.printFile = new Function();
imageGallery.printFile = function(print) {
	if(actualFile>0) {
		document.getElementById('root').style.display='none';
		document.getElementById('print').style.display='block';
		document.getElementById('print').innerHTML = '<img src="/galeria/arquivos/'+galleryID+'/'+filePos[actualFile]+'/'+(filePos[actualFile]=='v'?750:550)+'/'+fileList[actualFile]+'" />';
		document.body.className='body';
		if(print) { setTimeout('window.print();',1500); }
	}
}
imageGallery.unprint = new Function();
imageGallery.unprint = function() {
	document.getElementById('print').style.display='none';
	document.getElementById('print').style.backgroundImage = 'url()';
	document.getElementById('root').style.display='block';
	document.body.className='';
}
imageGallery.send = new Function();
imageGallery.send = function() {
	return;
}
/*window.onload = function () {
	imageGallery.status('Carregando...','w');
}*/