


origImg = {src:document.htmlImg.src, imgWidth:document.editor.imgWidth.value, imgHeight:document.editor.imgHeight.value};
tmpImg = {imgWidth:document.editor.imgWidth.value, imgHeight:document.editor.imgHeight.value, imgRotation:0, imgCrop:"no"};
currentImgPath = "images/";
defaultImg = "yes";

loader = new Image(147,79);
loader.src = "load.gif";

function wChange(){

	if (document.editor.constrain.checked) {
		var newHeight = (tmpImg.imgHeight *  parseInt(document.editor.imgWidth.value)) / tmpImg.imgWidth; 
    	document.editor.imgHeight.value = Math.round(newHeight);
    }
}

function hChange(){
    if (document.editor.constrain.checked) {
		var newWidth = (tmpImg.imgWidth *  parseInt(document.editor.imgHeight.value)) / tmpImg.imgHeight; 
    	document.editor.imgWidth.value = Math.round(newWidth);
	}
}



function submitNewImg(degrees, crop) { 
	
	

	var ajaxImg;
	try { 
		ajaxImg = new XMLHttpRequest(); 
	}
	catch(e) {
		try {
    		ajaxImg = new ActiveXObject(Microsoft.XMLHTTP);
    	}
		catch(e) {
	    	ajaxImg = new ActiveXObject("Msxml2.XMLHTTP");
		}
	}

	ajaxImg.onreadystatechange = function() { 
		if(ajaxImg.readyState == 4) {
        	if(ajaxImg.status == 200){
				//added new Date().getTime(); to the end of url retreived because no matter what I do, browsers provide
				//a cached image when a url matches one of their cached images.
				document.htmlImg.src = ajaxImg.responseText + "?nocache=" + new Date().getTime();
				//alert("hello");
				//alert(ajaxImg.responseText);
			}
            else { 
            	alert("error on resize/rotate");
			} 
		}    
	} 
	ajaxImg.open("POST", "newImgAjax.php",  true); 
	ajaxImg.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
	ajaxImg.setRequestHeader("Cache-Control", "no-cache");
	
	
	var cropper = document.getElementById("cropper");

	if (crop == "yes") {
    		
		if (cropper.style.width == "0px" && cropper.style.height == "0px") {
			return;
		}
		
		tmpImg.imgCrop = "yes";
		document.editor.imgWidth.value = parseInt(cropper.style.width);
		document.editor.imgHeight.value = parseInt(cropper.style.height);	
	}
	document.htmlImg.src = "load.gif";

	tmpImg.imgWidth = document.editor.imgWidth.value;
	tmpImg.imgHeight = document.editor.imgHeight.value;


	if (degrees) {
    	tmpImg.imgRotation = parseInt(tmpImg.imgRotation) + parseInt(degrees);
		imgValue = document.editor.imgWidth.value;
	    document.editor.imgWidth.value = document.editor.imgHeight.value;
		document.editor.imgHeight.value = imgValue;
		tmpImg.imgWidth = document.editor.imgWidth.value;
	    tmpImg.imgHeight = document.editor.imgHeight.value;
		//alert("yep");
	}

	document.editor.crop.disabled = true;
	document.htmlImg.style.cursor = "default";

	currentImgPath = "images/tmp/";	
    ajaxImg.send("imgWidth=" + tmpImg.imgWidth + "&imgHeight=" + tmpImg.imgHeight + "&imgRotation=" + tmpImg.imgRotation +
				 "&cropLeft=" + cursor.downClick.left + "&cropTop=" + cursor.downClick.top +
				 "&cropWidth=" + parseInt(cropper.style.width) + "&cropHeight=" + parseInt(cropper.style.height) +
				 "&imgCrop=" + tmpImg.imgCrop + "&imgPath=" + origImg.src);

}


function submitOrigImg() { 
	document.htmlImg.src = origImg.src;
	document.editor.imgWidth.value = origImg.imgWidth;
	document.editor.imgHeight.value = origImg.imgHeight;
    tmpImg.imgWidth = origImg.imgWidth;
	tmpImg.imgHeight = origImg.imgHeight;
	tmpImg.imgRotation = 0;
	tmpImg.imgCrop = "no";
	document.editor.crop.disabled = false;	
	document.htmlImg.style.cursor = "crosshair";
	currentImgPath = "images/";
}


cursor = {place:{left:0, top:0}, client:{left:0, top:0}, downClick:{left:0, top:0}, upClick:{left:0, top:0}};
epage = {X:0, Y:0};


//for firefox
document.getElementById("cropper").onclick = function(e) {
	if (e) {
    	e.stopPropagation();
	 	
	} 
};

//for firefox
document.htmlImg.onclick = function(e) {
	if (e) {
    	e.stopPropagation();
	}
    
};

function upClick(e) {
	e = e || window.event;
    //alert(document.htmlImg.onmousemove);
	var cropper = document.getElementById("cropper");
	
	if (document.htmlImg.onmousemove == null && cropper.style.display == "block") {
		cropper.style.display = "none";
	
		if (epage.X <= cursor.client.left) {
			//alert(cropper.style.width);
			cursor.downClick.left -= parseInt(cropper.style.width);
		}	
		
		if (epage.Y <= cursor.client.top) {
        	cursor.downClick.top -= parseInt(cropper.style.height);
		}	
	}

	document.htmlImg.onmousemove = null;
	document.getElementById("cropper").onmousemove = null;	
	

}
document.onmouseup = upClick;


function downClick(e) {
	e = e || window.event;
	if (document.editor.crop.disabled == true) {
    	//alert(
		return;
	}
	
	var cropper = document.getElementById("cropper");
	cropper.style.width = "0px";
	cropper.style.height = "0px";
	cropper.style.display = "block";
	
	var leftp = 0;
	var topp = 0;
	var element = document.htmlImg;
	var leftp = element.offsetLeft - 1;
	var topp = element.offsetTop - 1;	
	while (element.offsetParent) {
    	leftp += element.offsetParent.offsetLeft;
		topp += element.offsetParent.offsetTop;
    	element = element.offsetParent;
	} 	

	
    if (e.pageX || e.pageY) {
		cursor.downClick.left = e.pageX - leftp;
		cursor.downClick.top = e.pageY - topp;
    	cursor.client.left = e.pageX;
		cursor.client.top = e.pageY;
		document.htmlImg.onmousemove = move;
		cropper.onmousemove = move;
		cropper.style.left = e.pageX  + "px";
		cropper.style.top = e.pageY  + "px";
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft - 2;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop - 2;

		cursor.downClick.left = posx - leftp;
		cursor.downClick.top = posy - topp;
    	cursor.client.left = posx;
		cursor.client.top = posy;		
		cropper.style.left = posx + "px";
		cropper.style.top = posy + "px";
		
		document.htmlImg.onmousemove = move;
		cropper.onmousemove = move;
		//alert(cropper.style.height);
	}
}
document.htmlImg.onmousedown = downClick;
document.getElementById("cropper").onmousedown = downClick;
document.htmlImg.ondragstart = function(){return false;};
document.htmlImg.ondrag = function(){return false;};
document.htmlImg.ondragend = function(){return false;};

function move(e) {
	e = e || window.event;
	
	var cropNums = document.getElementById("cropnums");
	


		var cropper = document.getElementById("cropper");
		
    if (e.pageX || e.pageY) {
    	epage.X = e.pageX;
		epage.Y = e.pageY; 
	}
	else if (e.clientX || e.clientY) {
    	epage.X = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft - 2;
		epage.Y = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop - 2;
	}
	

	if (epage.X <= cursor.client.left) {
    	
        	cropper.style.left = epage.X  + "px";
			cropper.style.width = (cursor.client.left - epage.X) + "px";	
		    //cursor.downClick.left -= 
	}

	 if (epage.Y <= cursor.client.top) {
    	  	cropper.style.top = epage.Y  + "px";
			cropper.style.height = (cursor.client.top - epage.Y) + "px";			
			
	}


	if (epage.X > cursor.client.left) {
        	cropper.style.left = cursor.client.left  + "px";
			cropper.style.width = (epage.X - cursor.client.left - 1) + "px";				
	}

	 if (epage.Y > cursor.client.top) {
        cropper.style.top = cursor.client.top  + "px";
		cropper.style.height = (epage.Y - cursor.client.top - 1) + "px";					
	}
	
	cropNums.innerHTML = cropper.style.width + " " + cropper.style.height;

}



document.htmlImg.onmouseover = function() {
	if (window.sidebar) {
	document.onmousedown = function() {return false;};
	}
	
};


document.htmlImg.onmouseout = function() {
	if (window.sidebar) {
		document.onmousedown = null;
	}
	
};


document.editor.save.onclick = function() {
//window.location = "http://www.nickmudge.info/image/test.php";

		window.location = "?save=yes&filename=" + document.editor.uploadfilename.value + 
						  "&path=" + currentImgPath;

};


if (document.editor.upload) {
	
    defaultImg = "no";
	document.editor.upload.onchange = function() {
		var lastChars = document.editor.upload.value.toLowerCase();
		//alert(lastChars);
		if (lastChars.indexOf(".gif") != -1 || lastChars.indexOf(".jpg") != -1 || lastChars.indexOf(".png") != -1 || lastChars.indexOf(".jpeg") != -1) {
			document.htmlImg.src = "load.gif";
			document.editor.submit();
			return;
		}
		alert("Images must be jpg, gif or png and have one of those file extensions.");
		document.editor.upload.value = "";
	};

	document.editor.httpsubmit.onclick = function() {
		var lastChars = document.editor.httpupload.value.toLowerCase();
		//alert(lastChars);
		if (lastChars.indexOf(".gif") != -1 || lastChars.indexOf(".jpg") != -1 || lastChars.indexOf(".png") != -1 || lastChars.indexOf(".jpeg") != -1) {
			document.htmlImg.src = "load.gif";
			var winlocation = window.location;
			window.location = winlocation + "?img=" + document.editor.httpupload.value;
			
			return;
		}
		alert("Images must be jpg, gif or png and have one of those file extensions.");
		
	};



}

if (defaultImg == "no") {
	document.editor.remove.disabled = true;	
	document.editor.save.disabled = true;	
}
else {
	document.editor.remove.disabled = false;
	document.editor.save.disabled = false;	
}


document.editor.remove.onclick = function() {
	document.htmlImg.src = "load.gif";
	document.editor.deleteFile.value = origImg.src; 
    document.editor.submit();

};

document.editor.crop.onclick = function() {

	submitNewImg(0,'yes');
};



