// JavaScript Document
//Image functions

function getImageFolder(number)
{
	var pathToFile = "/~ecoagric/functions/cms_images.php?ajax=listFolderContents&folder="+number;
	ajaxGetContents('innerHTML',pathToFile, 'pictureFrame');
}

function getImageFolders()
{
	var pathToFile = "/~ecoagric/functions/cms_images.php?ajax=getFolders";
	ajaxGetContents('innerHTML',pathToFile, 'pictureFrame');
}

function deleteImage(id, folder)
{
	var check = confirm("Are you sure that you would like to delete this image? This action cannot be undone and if any pages reference this image, the page will attempt to display a non-existant image, which could affect page layout.");	
	if (check)
	{
		var pathToFile = "/~ecoagric/functions/cms_images.php?ajax=deleteImage&imageID="+id;
		var returnValue = ajaxGetContents('javascript',pathToFile, 'pictureFrame');	
		getImageFolder(folder);
	}
}

function addImageFolder()
{
	var check = confirm('Please be careful about adding new folders. Once a folder has been created it cannot be deleted, so please be judicious in your folder management. \r\n\r\nIf you would still like to create a new folder, select OK.');
	if (check)
	{
		var name = prompt("Please provide a UNIQUE name for the folder. (do not include special characters)");
		while (name==''){prompt("You must provide a UNIQUE name for the folder. (do not include special characters)");};
		var pathToFile = "/~ecoagric/functions/cms_images.php?ajax=addFolder&folderName="+name;
		var returnValue = ajaxGetContents('javascript',pathToFile, 'pictureFrame');	
		getImageFolders();
	}
}

function getImageDetails(imageID)
{
	alert(imageID);	
}

function addFormattedImage()
{
	var pathToFile = "/~ecoagric/cms/images_upload_formatted.php";
	ajaxGetContents('innerHTML',pathToFile, 'imageManage');
}


//EDITING FUNCTIONS
function getCursorPosition(e){
	
	isIE=document.all;
	isNN=!document.all&&document.getElementById;
	isN4=document.layers;
	
//get current cursor position
  var x,y;
	if (self.pageYOffset) // all except Explorer
	{
		x = self.pageXOffset;
		y = self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}
  
  
  currentX=isIE ? event.clientX : e.clientX;
  currentY=isIE ? event.clientY : e.clientY;

	var coordinates = new Array();
	coordinates['left']=(currentX+x);
	coordinates['top']=(currentY+y);
	
	return (coordinates);
}

function scaleImage(image,x,y)
{
	var theImage = document.getElementById(image);
	
	if (x<10){alert('You cannot shrink the image smaller than 10 pixels'); return false;};
	if (y<10){alert('You cannot shrink the image smaller than 10 pixels'); y.value=10;return};
	
	theImage.style.width=x + 'px';
	theImage.style.height=y +'px';
}

function keepImageProportions(width, height, ratio)
{
	if (document.getElementById('constrain').checked!=true)
	{
		if (width != null && width<10){alert('You cannot shrink the image smaller than 10 pixels'); keepImageProportions(parseFloat(width)+1, height, ratio); return;};
		if (height != null && height<10){alert('You cannot shrink any image dimension  smaller than 10 pixels'); keepImageProportions(width, parseFloat(height)+1, ratio); return;};
		return;
	};
	if (width != null)
	{
		if (width<10){alert('You cannot shrink the image smaller than 10 pixels'); return;};
		var newHeight =  Math.round(parseFloat(width)/parseFloat(ratio));
		if (newHeight<10){alert('You cannot shrink the image smaller than 10 pixels'); keepImageProportions(parseFloat(width)+1, height, ratio); return;};
		document.getElementById('width').value = width;
		document.getElementById('height').value = newHeight;
	}
	else
	{
		if (height<10){alert('You cannot shrink the image smaller than 10 pixels'); return;};
		var newWidth =  Math.round(parseFloat(height)*parseFloat(ratio));
		if (newWidth<10){alert('You cannot shrink the image smaller than 10 pixels'); keepImageProportions(width, parseFloat(height)+1, ratio); return;};
		document.getElementById('height').value = height;
		document.getElementById('width').value = newWidth;
	}
}

function forceScaleImage(x, y, image, constrain){ //if constrain = true, constrain by width
	var theImage = document.getElementById(image);
	var ratio = document.getElementById('ratio').value;
	if (constrain){
		document.getElementById('constrain').checked=true;
		y = Math.round(parseInt(x/ratio));
		document.getElementById('width').value=x;
		document.getElementById('height').value=y;
		theImage.style.width=document.getElementById('width').value + 'px';
		theImage.style.height=document.getElementById('height').value + 'px';
	} else {
		document.getElementById('constrain').checked=false;
		document.getElementById('width').value=x;
		document.getElementById('height').value=y;
		theImage.style.width= x + 'px';
		theImage.style.height= y + 'px';
	}
}