var currentGallery=0;

function showPhoto(url) {
	document.getElementById('fond').style.width = screen.width;
	document.getElementById('fond').style.height = screen.height;
	document.getElementById('fond').style.display = "block";
	document.getElementById('picimg').onload = function() {
		document.getElementById('pic').style.left = document.body.offsetWidth/2-document.getElementById('picimg').width/2;
		document.getElementById('pic').style.top = document.body.offsetHeight/2-document.getElementById('picimg').height/2;
		document.getElementById('pic').style.width = document.getElementById('picimg').width+2;
		document.getElementById('pic').style.height = document.getElementById('picimg').height+2;
	}
	document.getElementById('picimg').src=url;
}

function hidePhoto() {
	document.getElementById('fond').style.display = "none";
	document.getElementById('picimg').src='/images/loading.png';
}

function showPhotoInside(id, caption, viewed) {
	var container = document.getElementById('image_holder');
	document.getElementById('rid').value = id;
	setTimeout("getComments(0,'media')",1000);
	container.innerHTML = '';
	container.style.textAlign='center';
	var imageObject = new Image();
	imageObject.src = '/gallery/thumbnail_280_'+id+'.png';
	document.getElementById('phototag').innerHTML =  "<b>Caption: </b>"+caption+"<br /><b>Viewed: </b>"+viewed+" times";
	container.appendChild(imageObject);
}

function showEtiquette(txt) {
	/* var posx = 0;
   var posy = 0;
	var e = window.event; */
	document.getElementById('etiquette').innerHTML = txt;
	document.getElementById('etiquette').style.display = "block";
	/* if (e.pageX || e.pageY) {
		posx = e.pageX;
      posy = e.pageY;
   } else if (e.clientX || e.clientY) {
      posx = e.clientX + document.body.scrollLeft;
      posy = e.clientY + document.body.scrollTop;
   }
	document.getElementById('etiquette').style.top = posy;
	document.getElementById('etiquette').style.left = posx; */
}

function hideEtiquette() {
	document.getElementById('etiquette').style.display = "none";
}

function updateEtiquettePosition(e) {
	var posx = 0;
   var posy = 0;
	if(!e) var e = window.event;
	if (e.pageX || e.pageY) {
		posx = e.pageX;
      posy = e.pageY;
   } else if (e.clientX || e.clientY) {
      posx = e.clientX + document.body.scrollLeft;
      posy = e.clientY + document.body.scrollTop;
   }
	document.getElementById('etiquette').style.top = posy;
	document.getElementById('etiquette').style.left = posx;
}

function clearGallery() {
	document.getElementById('gallery').innerHTML = '';
}

function galleryPage(page) {
	AJAXRequest("POST","/mmedit.php","cmd=view_gallery&id="+guid+"&page="+page+"&gallery="+currentGallery,galleryPageHandler,true);
}

function galleryPageHandler(xmlObject) {
	var id;
	var caption;
	var photoObj;
	var imageObject;
	var photos = xmlObject.responseXML.getElementsByTagName('photo');
	clearGallery();
	for(i=0;i<photos.length;i++) {
		id = photos[i].getAttribute('id');
		views = photos[i].getAttribute('views');
		caption = photos[i].getAttribute("caption");
		photoObj = document.createElement('li');
		photoObj.setAttribute("id", 'row'+id);
		photoObj.setAttribute("class", 'box');
		 /* imageObject = document.createElement('img');
		imageObject.src = '/gallery/thumbnail_60_'+id+'.png';
		//imageObject.onload = function() {
			//imageObject.setAttribute('width',180);
			//imageObject.setAttribute('height',180);
			/* if(navigator.userAgent.indexOf("MSIE")==-1) {
				imageObject.setAttribute('onClick','showPhotoInside(\'/images/galleries/'+photos[i].getAttribute("file")+'\')');
			} else {
				photoObj.setAttribute('onclick','javascript:showPhotoInside('+id+',\''+caption+'\')');
			/* }
 			photoObj.style.cursor='pointer';
 			photoObj.onClick='showPhotoInside('+id+',\''+caption+'\')';
			imageObject.setAttribute('alt',caption);
			photoObj.appendChild(imageObject); */
			//photoObj.innerHTML += '<br /><span style="font-size: x-small; cursor: text">'+caption+'</span>';
			photoObj.innerHTML = '<img src="/gallery/thumbnail_60_'+id+'.png" style="cursor:pointer" alt="'+caption+'" onClick="showPhotoInside('+id+',\''+caption+'\','+views+')" />';
			document.getElementById('gallery').appendChild(photoObj);
		//};
	}
	document.getElementById('pagination').innerHTML = xmlObject.responseXML.getElementsByTagName('pages')[0].firstChild.nodeValue;
}

function showAlbum(id,title) {
	document.getElementById('album_name').innerHTML = title;
	if(currentGallery != 0)
		document.getElementById('album'+currentGallery).style.backgroundColor = 'transparent';
	currentGallery = id;
	document.getElementById('album'+id).style.backgroundColor = '#fff';
	galleryPage(0);
}