// when image changes preview
function jsImageChanged(newValue)
{
	var img = document.getElementById("preview");
	var width = document.getElementById("width");
	var height = document.getElementById("height");
	
	var img = document.getElementById("preview");
	var width = document.getElementById("width");
	var height = document.getElementById("height");
	
	var testImg = new Image();
	testImg.src = "../../../data/imgs/"+newValue;
	
	width.value = testImg.width.toString();
	height.value = testImg.height.toString();
	img.width = width.value;
	img.height = height.value;
	img.src = testImg.src;
	
	origWidth = width.value;
	origHeight = height.value;
}

// open build window for excel and access spreadsheets
function openSqlR( ctrl, first, dir, ctrlWithFile, last )
{
	var file = document.getElementById( ctrlWithFile );
	window.open("WebPointService/Program Files/dialogs/sqlreflection/default.aspx?ctrl="+ctrl+"&first="+first+"&path="+dir+file.value+"&last="+last,'wiz',"width=500, height=500");
}

// when page is loaded
function jsPageLoaded()
{    
	var img = document.getElementById("preview");
	var width = document.getElementById("width");
	var height = document.getElementById("height");
	var caption = document.getElementById("caption");
	var imgs = document.getElementById("uploadedImages");

	try
	{		
		width.value = img.width.toString();
		height.value = img.height.toString();
	} catch(e){}
	
	if (window.dialogArguments != null) 
	{
		var input = window.dialogArguments;
		img.src = "../../../data/imgs/"+input.Url;
		width.value = input.Width.toString();
		height.value = input.Height.toString();
		img.width = input.Width.toString();
		img.height = input.Height.toString();
		
		if ( input.Caption!=null)
		    caption.value = input.Caption;
		else
		    caption.disabled = true;
		
		for (i=0; i<imgs.options.length; i++)
		{
			var preloader = new Image(); preloader.src = "../../../data/imgs/"+imgs.options[i].value;
			
			if (imgs.options[i].value == input.Url)
			{
				imgs.selectedIndex = i;
				break;
			}
		}
		origWidth = input.Height;
		origHeight = input.Width;
	}
}

var origHeight;
var origWidth;

// changes the width then auto-changes the height
function jsWidthChanged(value)
{
	var width = document.getElementById("width");
	var height = document.getElementById("height");
	var img = document.getElementById("preview");
	
	// reset if size is empty
	var testNum = new Number(width.value);
	
	var newWidth = (width.value.length==0 || testNum.toString() == "NaN") ? 5 : new Number(width.value);
	var oldWidth = new Number(origWidth);
	var oldHeight = new Number(origHeight);
	var ratio = oldWidth / newWidth;
	
	// change height
	height.value = new Number(oldHeight / ratio).toFixed(0);
	img.height = (height.value == "NaN") ? 5 : height.value;
	
	// update width
	img.width = newWidth.toString();
}

// reset image size
function jsResetSize()
{
	var img = document.getElementById("preview");
	var width = document.getElementById("width");
	var height = document.getElementById("height");
	
	img.width = origWidth;
	img.height = origHeight;
	width.value = origWidth;
	height.value = origHeight;
}

// exit dialog
function jsExitDialog()
{
	var img = document.getElementById("preview");
	var width = document.getElementById("width");
	var height = document.getElementById("height");
	var caption = document.getElementById("caption");
	
	window.returnValue = new DialogResults(img.src, new Number(width.value), new Number(height.value), caption.value);
	window.close();
	
}

// class
function DialogResults(url, width, height, caption)
{
	this.Url = url;
	this.Width = width;
	this.Height = height;
	this.Caption = caption;
}

// open config
function openImageConfigDialog(imgId, captionId)
{
	var img = document.getElementById(imgId);
	var caption = (captionId==null)? null : document.getElementById(captionId);
	var url = img.src.split("/");
	
	var input = new DialogResults(url[url.length-1], img.width, img.height, ((caption==null)?null:caption.innerHTML) );

	var rtn = window.showModalDialog("WebPointService/Program Files/dialogs/imageconfig.aspx", input, "font-size:10px;dialogWidth:70em;dialogHeight:64em");
	if (rtn==null) return; 
	
	img.src = rtn.Url;
	img.height = rtn.Height.toString();
	img.width = rtn.Width.toString();
	if (caption!=null) caption.innerHTML = rtn.Caption;
}