var galleryDivId;
var galleryImages;
var galleryName;
var galleryTitle;
var galleryOrder;
var galleryDeleted;
var galleryWidth;
var galleryModified;
var bufferGallery;

var thumbOver = -1;
var imageSelect = -1;

var gPos = [0, 200];
var gOffset = 0;
var gAnimSpeed = 2;

var tSpace = 20;
var tMinWidth = 80;
var tMaxWidth = 150;
var pMaxWidth = 0;
var overGallery = false;
var influenceMax = 4;

var widthAverage;
var maxWidth;
var galleryWidth;
var startX = 0;

var mouseX = 0;
var mouseY = 0;
var tMouseX = 0;
var tMouseY = 0;

var thumbnails = new Array();
var showGallery = false;
var imagesize = [0, 0];
var bufferImage;

var dragging = 0;
var dragX = 0;
var dragY = 0;
var dragTime;

function getMouseXY(e) {
  if (document.all) { // grab the x-y pos.s if browser is IE
    mouseX = event.clientX + document.body.scrollLeft
    mouseY = event.clientY + document.body.scrollTop
  } else {  // grab the x-y pos.s if browser is NS
    mouseX = e.pageX
    mouseY = e.pageY
  } 
  return true;
}

function getThumb(id){
	return document.getElementById('thumb'+id);
}

function overThumb(id){
	if (id != -1){
		//overGallery = true;
		thumbOver = id;
	} /*else {
		overGallery = false;
	}*/
}

function getThumbId(pos){
	return galleryOrder[pos];
}

function getThumbPos(id){
	var i;
	for (i = 0; i < galleryOrder.length; i++)
		if (galleryOrder[i] == id)
			return i;
}

function getThumbWidth(i, force){
	if (!force){
		return thumbnails[getThumbId(i)][1];
	}
	
	var posX 				= this.startX + i * widthAverage;
	var mouseXArea			= influenceMax * widthAverage;
	
	var x = mouseXArea - Math.abs(posX - mouseX);
	
	if (x >= 0){	//Dans la zone de la souris
		var alpha = Math.atan((this.maxWidth - tMinWidth) / mouseXArea);
		thumbnails[getThumbId(i)][1] = tMinWidth + Math.tan(alpha) * x;
	} else
		thumbnails[getThumbId(i)][1] = tMinWidth;
	
	return thumbnails[getThumbId(i)][1];
}

function getGalleryX(){
	return ((getWidth() - (galleryWidth + tSpace)) / 2) + gPos[0];
}

function getGalleryWidth(){
	var i;
	var total = 0;
	for (i = 0; i < galleryOrder.length;i++){
		total += getThumbWidth(i, true);
	}
	
	total += tSpace * (galleryOrder.length - 1)
	
	return total;
}

function undo(){
	if (galleryModified){
		for (i = 0; i < galleryImages.length; i++){
			document.getElementById('thumb'+i).style.display='block';
			galleryOrder[i] = i;
		}
		galleryModified = false;
		galleryDeleted = null;
	}
}

function moveThumbnail(id, dest){
	var origin = getThumbPos(id);
	
	if (origin < dest)
		for (i = origin; i < dest; i++)	galleryOrder[i] = galleryOrder[i + 1];else
		for (i = origin; i > dest; i--)	galleryOrder[i] = galleryOrder[i - 1];
	
	galleryOrder[dest] = id;
	galleryModified = true;
}

function deleteImage(id){
	moveThumbnail(id, galleryOrder.length - 1);
	galleryOrder = galleryOrder.slice(0, -1);
	document.getElementById('thumb'+id).style.display='none';
	galleryDeleted[galleryDeleted.length] = id;
}

function placeThumbnails(){
	var i;
	var posX;
	var offsetX = 0;
	
	this.widthAverage 		= ((tMinWidth + tSpace) * galleryOrder.length) / galleryOrder.length;
	this.maxWidth 			= blendValue(tMinWidth, tMaxWidth, pMaxWidth);
	this.galleryWidth 		= galleryOrder.length * widthAverage;
	this.startX 			= (getWidth() - galleryWidth + tMinWidth) / 2 + gPos[0];
	
	
	galleryWidth = getGalleryWidth();
	
	for (i = 0; i < galleryOrder.length; i++){		
		
		var id = getThumbId(i);
		
		var width = getThumbWidth(i);
		var thumb = getThumb(id);
		thumbnails[id][2]	= Math.round((getGalleryX() + offsetX));
		thumbnails[id][3]	= Math.round(gPos[1] - width / 2);
		
		if (imageSelect != id){
			thumb.style.left 	= thumbnails[id][2] + 'px';
			thumb.style.top		= thumbnails[id][3] + 'px';
			thumb.style.width 	= width + 'px';
			thumb.style.height 	= width + 'px';
			fadeObject(thumb, getPercentage(tMinWidth - (tMaxWidth - tMinWidth)/2, tMaxWidth, width) + 20);
		}
			
		offsetX += width + tSpace;
	}
}

function animThumbnails(){
	var tPercent = pMaxWidth;
	
	/**/
	if ((thumbOver != -1 && Math.abs(mouseY - gPos[1]) < tMaxWidth /2) || (thumbOver == -1 && Math.abs(mouseY - gPos[1]) < tMinWidth /2))
		pMaxWidth += 20; else
		pMaxWidth -= 20;
	
	if (pMaxWidth < 0) {
		pMaxWidth = 0;
		thumbOver = -1;
	} else
	if (pMaxWidth > 100)
		pMaxWidth = 100;
	
	
	extra = galleryWidth + 200 - getWidth() ;
	
	if (extra > 0){
		gPos[0] = - (extra / getWidth())* (mouseX - getWidth()/2) + tSpace / 2;
	}
	
	if (showGallery){
		placeThumbnails();
		setTimeout('animThumbnails();', 5);
	}
}

function getImageSize(data, id, error){
	
	imagesize = explode(data);
	if (imagesize[0] > 1)
	loadImage(2);
}

function loadImage(state, width, height){
	var image = document.getElementById('fullsizeImage');
	
	if (state == 1){
		bufferImage = null;
		bufferImage = new Image();
		bufferImage.src = bufferGallery[1][imageSelect];
		document.getElementById('loading').style.display='block';
		document.getElementById('loading').innerHTML = 'Chargement de l\'image en cours...';
		state = 2;
		//makeRequest('admin.php?action=getimagesize&url='+bufferGallery[1][imageSelect], 'getImageSize', 1, false);
	}
	
	if (state == 2){
		if (bufferImage.complete){
			imagesize[0] = bufferImage.width;
			imagesize[1] = bufferImage.height;
			imagesize[2] = this.startX + getThumbPos(imageSelect) * widthAverage - imagesize[0] / 2;
			if (imagesize[2] < 5)	imagesize[2] = 5;
			if (imagesize[2] + imagesize[0] > getWidth())
				imagesize[2] = getWidth() - imagesize[0] - 5;
			document.getElementById('loading').style.display='none';
			image.style.height 	= 1 + 'px';
			image.style.display = 'block';
			image.style.visibility = '';
			image.src = bufferImage.src;
			loadImage(3, 1, 0);
		} else
			setTimeout('loadImage(2);', 20);
	} 
	
	if (state == 3){
		image.style.width 	= width + 'px';
		image.style.height 	= height + 'px';
		image.style.left	= imagesize[2] + 'px';
		image.style.top = 250 - (getPercentage(0, imagesize[1], height)*2) + 'px';
		
		if (width < imagesize[0]){
			width += Math.ceil((imagesize[0] - width) / 2);
		}else
			
		if (height < imagesize[1])
			height += Math.ceil((imagesize[1] - height) / 2);else
			return;
			
		setTimeout('loadImage(3, '+width+', '+height+');', 15);
	}
}


function showImage(id){
	document.getElementById('fullsizeImage').style.visibility ='hidden';
	document.getElementById('displayImage').style.display = 'block';
	document.getElementById('fullsizeImage').src = '';
	showGallery = false;
	imageSelect = id;
	setTimeout('loadImage(1);', 10);
}

function closeImage(){
	document.getElementById('displayImage').style.display='none';
	document.getElementById('fullsizeImage').style.display='none';
	showGallery = true;
	animThumbnails();
	imageSelect = -1;
}

function dragImage(id){
	var i;
	
	if (id == -1){
		if (dragging){
			var thumb = getThumb(imageSelect);
			if (pMaxWidth < 100){
				if (1 == 1 || confirm('Voulez-vous vraiment effacer cette image ? ('+imageSelect+')')){
					deleteImage(imageSelect);
				} 
			}
			
			fadeObject('imagethumb' + imageSelect, 100);
			document.getElementById('thumb'+imageSelect).style.backgroundImage = '';
			imageSelect = -1;
			thumb.style.zIndex = 5;
			setTimeout('imageSelect = -1;', 10);
		} else 
		if (imageSelect != -1) {
			setTimeout('showImage(\'' + imageSelect + '\');', 10);
		}
		
		dragging = false;
		tMouseX = 0; tMouseY = 0;
		clearTimeout(dragTime);
	} else {
		
		if (imageSelect == -1)
			imageSelect = id;
		
		if (!tMouseX){
			tMouseX = mouseX;
			tMouseY = mouseY;
		}
		
		if (!dragging && (tMouseX != mouseX || tMouseY != mouseY)){
			var thumb = getThumb(id);
			thumb.style.zIndex = 6;
			dragX = thumbnails[id][2] - tMouseX;
			dragY = thumbnails[id][3] - tMouseY;
			
			dragging = true;
		}
		
		if (dragging){
			var thumb = getThumb(id);
			if (pMaxWidth < 100){
				fadeObject('imagethumb' + id, 50);
				document.getElementById('thumb'+id).style.backgroundImage = 'url(images/rouge.png)';
			} else {
				fadeObject('imagethumb' + id, 100);
				document.getElementById('thumb'+id).style.backgroundImage = '';
			}
			thumb.style.left 	= mouseX + dragX + 'px';
			thumb.style.top 	= mouseY + dragY + 'px';
			
			for (i = 0; i < galleryOrder.length; i++){
				var tID = getThumbId(i);
				if (tID != id && mouseX + dragX/2 > thumbnails[tID][2] && mouseX + dragX/2 < thumbnails[tID][2] + thumbnails[tID][1]){
					moveThumbnail(id, i);
					break;
				}
			}
		}
		
		dragTime = setTimeout('dragImage('+id+');', 1);
	}
}


function initGallery(id, folder, images, title){
	var content;
	
	galleryDivId 	= id;
	galleryImages 	= images;
	galleryTitle	= title;
	bufferGallery 	= new Array();
	galleryOrder	= new Array();
	galleryDeleted	= new Array();
	
	bufferGallery[0] 	= new Array();
	bufferGallery[1] 	= new Array();
	
	content = document.getElementById(id);
	content.innerHTML = '';
	
	var i;
	for (i = 0; i < images.length; i++){
		bufferGallery[0][i] = ['galleries/'+folder+'/thumbnails/'+images[i]];
		bufferGallery[1][i] = ['galleries/'+folder+'/images/'+images[i]];
		
		content.innerHTML += '<a onmousedown="dragImage('+i+');" onmouseup="dragImage(-1)"  onmouseover="overThumb('+i+');"><div id="thumb'+i+'" class="content">' +
			'' +
				'<img ondrag="return false" onmousedown = "return false;" id="imagethumb'+i+'" src="'+bufferGallery[0][i][0]+'" width="100%" height="100%">' +
			''+
		'</div></a>';
		thumbnails[i] = [0, tMinWidth];
		galleryOrder[i] = i;
		//content.onmouseout = function(){overThumb(-1);};
	}
	
	gOffset = 0;
}

document.onmousemove = getMouseXY;
