var Ratio = 0;
var fast = 0;
var change = 0;
var lastchange = 0;
var origHeight = 0;
var origWidth = 0;
var ImageWidth = null;
var ImageHeight = null;
var AspectCheck = null;
var ImageVal = null;
var ImageObject = null;

function myInput(value){
	this.value = value;
}

function LoadImage(ImageSource){
	maxcount = 10000;
	var img_obj = new Image();
	img_obj.src = ImageSource;
	count = 0;
	window.status = "Loading Image.  Please wait...";
	while(count <= maxcount && !(img_obj.height > 0 && img_obj.width > 0)){
		if (count % (maxcount / 100) == 0) {
	    	percentdone = parseInt( (count / maxcount) * 100) ;
		   	window.status = "Loading Image.  Please wait.  " + percentdone + "% Done";
		}
		img_obj.src = ImageSource; 
		count++;
	} 
	//alert("Image Height = " + img_obj.height); 
	//alert("Image Width = " + img_obj.width);
	window.status = "Done";
	return img_obj;
}

function Setup(docImageWidth, docImageHeight, docImageVal, AspectCheckBox){
	if (docImageWidth != null)
		ImageWidth = docImageWidth;
	else
		ImageWidth = new myInput(0);
	if (docImageHeight != null)
		ImageHeight = docImageHeight;
	else
		ImageHeight = new myInput(0);
	ImageVal = docImageVal;
	AspectCheck = AspectCheckBox;
	if (ImageVal != null)
		if (ImageVal.src.length > 0)
			ImageObject = SetImage();
}

function SameImage(){
	if (ImageObject != null && ImageVal != null)
		if (ImageObject.src == ImageVal.src)
			return true;
	return false;
}

function SetImage(){
	if (!SameImage()){
		ImageObject = LoadImage(ImageVal.src);
		Ratio = 0;
		origHeight = 0;
		origWidth = 0;
	}
	return ImageObject;
}


//Preview Image Stuff
function SetSize(iWidth){
	iHeight = 0;
	if (iWidth > ImageObject.width && ImageObject.width > 0){
		iWidth = ImageObject.width;
	}
	if (Ratio > 0) {
		iHeight = parseInt(iWidth * Ratio);
	}
	
	if (iWidth > 0){
		ImageWidth.value = iWidth;
		if (!(iHeight > 0))
			ImageHeight.value = iWidth;
		else
			ImageHeight.value = iHeight;	
		resize();
	}	
}

function resize()  {
	ImageVal.height = ImageHeight.value;
	ImageVal.width = ImageWidth.value;
}

function OrigSize(){
	SetImage();
	if (!(origHeight > 0) || !(origWidth > 0)){
	   	ImageHeight.value = ImageObject.height;
	   	ImageWidth.value = ImageObject.width; 
		origHeight = ImageObject.height;
	   	origWidth = ImageObject.width; 
	} else {
		ImageHeight.value = origHeight;
	   	ImageWidth.value = origWidth; 
	}
}

function SetOrig(iWidth, iHeight){
	if (iWidth > 0 && iHeight > 0){
		if (ImageVal != null) 
			ImageObject.src = ImageVal.src;
		origHeight = iHeight;
   		origWidth = iWidth;
	   	Ratio = iHeight / iWidth;
		if (AspectCheck != null)
			if (AspectCheck.checked != 1)
				AspectCheck.click();
	} //else
		//alert(iWidth + " x " + iHeight);
}

function ResetSize(iWidth){
	OrigSize();
	SetAspect();
	if (iWidth > 0)
		SetSize(iWidth);
	resize();
}

function SetAspect(){
	SetImage();
	if (!(Ratio > 0)){
		Ratio = ImageObject.height / ImageObject.width;
		if (AspectCheck != null)
			if (AspectCheck.checked != 1)
				AspectCheck.click();
	}
	//alert("Ratio = " + Ratio);
}

function AspectRat(){
	SetAspect();
	if (AspectCheck != null)
		if (AspectCheck.checked == 1){
			if (lastchange == 0)
				CheckAspectH();
			else if (lastchange == 1)
				CheckAspectW();
			else
				ResetSize();
		}
}

function CheckAspect(){
if ((ImageHeight.value / ImageWidth.value) == Ratio)
	if (AspectCheck.checked != 1)
		AspectCheck.click();
}

function CheckAspectW(){
	lastchange = 1;
	if (AspectCheck != null)
		if (AspectCheck.checked == 1){
			ImageHeight.value = parseInt(ImageWidth.value * Ratio);
		}
	resize();
}

function CheckAspectH(){
	lastchange = 0;
	if (AspectCheck != null)
		if (AspectCheck.checked == 1){
			ImageWidth.value = parseInt(ImageHeight.value / Ratio);
		}
    resize();
}

function ResetAspect(){
	if (AspectCheck != null)
	  	if (AspectCheck.checked == 1){
  			AspectCheck.click();
  			AspectCheck.click();
	  	} else AspectCheck.click();
}


function clt(){
if (change == 1){clearTimeout(changetime);}
clearTimeout(fast);
change = 0;
}

//Up Width
function upw(){
  ImageWidth.value=parseInt(ImageWidth.value)+1;
  CheckAspectW();
}

function over_upw(){
  upw();
  change = 1;
  changetime = setTimeout("fast_upw()","600");}
  
function fast_upw(){clearTimeout(changetime);
  upw();
  fast = setTimeout("fast_upw()","100");}
  

//Down Width
function downw(){ImageWidth.value=parseInt(ImageWidth.value)-1;
  CheckAspectW();
}
  
function over_downw(){downw();
  change = 1;
  changetime = setTimeout("fast_downw()","600");}
  
function fast_downw(){clearTimeout(changetime);
  downw();
  fast = setTimeout("fast_downw()","100");}
  

//Up Height
function uph(){ImageHeight.value=parseInt(ImageHeight.value)+1;
 CheckAspectH();
 }
 
function over_uph(){uph();
  change = 1;
  changetime = setTimeout("fast_uph()","600");}
  
function fast_uph(){clearTimeout(changetime);
  uph();
  fast = setTimeout("fast_uph()","100");}
  

//Down Height
function over_downh(){downh();
  change = 1;
  changetime = setTimeout("fast_downh()","600");}
  
function fast_downh(){clearTimeout(changetime);
  downh();
  fast = setTimeout("fast_downh()","100");}
  
function downh(){ImageHeight.value=parseInt(ImageHeight.value)-1;
  CheckAspectH();
}
