var manualSize = 0;

var minWidth = 230;
var minHeight = 338;
var t_minWidth = 50;
var t_minHeight = 72;
var maxWidth = 1280;
var maxHeight = 1280;
var oWidth = 0;
var oHeight = 0;

var action = getQueryVariable("action");
var imagePath = getQueryVariable("id");
var error = getQueryVariable("error");
var pID = getQueryVariable("pID");
var gender = getQueryVariable("gender");

function saveVariables(){
	
	var g = $("#gender").val();
	if ( $("#imageBasket").find("img").length === 0 ){
		alert("Please upload a photograph to continue");
		return false
	}
	else if ( g == "" ){
		alert("Please select a gender");
		return false
	}
	else{
		var x = "";
		var theImage = $("#theImage");
		var iWidth = theImage.width();
		var iHeight = theImage.height();
		var wDiff = Math.floor(iWidth - minWidth);
		var hDiff = Math.floor(iHeight - minHeight);
		var p = $("#theImage").position();

		$("#iWidth").val(iWidth);
		$("#iHeight").val(iHeight);
		$("#t_minWidth").val(t_minWidth);
		$("#t_minHeight").val(t_minHeight);
		$("#xPos").val(wDiff - p.left);
		$("#yPos").val(hDiff - p.top);
		$("#cWidth").val(minWidth);
		$("#cHeight").val(minHeight);
		$("#pID1").val(pID);
		return true
	}

}

function setupImage(){
	var theImage = $("#theImage");
	var iWidth = theImage.width();
	var iHeight = theImage.height();
	if ( manualSize == 0 ){
		oWidth = iWidth;
		oHeight = iHeight;
	}
	var wDiff = Math.floor(iWidth - minWidth);
	var hDiff = Math.floor(iHeight - minHeight);
	var pLeft = wDiff * -1;
	var pTop = hDiff * -1;
	var pWidth = iWidth + wDiff;
	var pHeight = iHeight + hDiff;
	$("#imageWorkArea").css({
		position:"absolute",
		left: pLeft,
		top: pTop,
		width: pWidth,
		height: pHeight
	});
	var iLeft = parseInt(theImage.css("left"));
	var iTop = parseInt(theImage.css("top"));
	if ( iLeft > 0 ){
		theImage.css("left","0px");
	}
	if ( iTop > 0 ){
		theImage.css("top","0px");
	}
}

function upload(){
	$("#image").val(imagePath);
	$("#imageBasket").append("<div id=\"imageWorkArea\"><img id=\"theImage\" src=\"/_images_temp/"+imagePath+"\" alt=\"\" /></div>");
	$("#theImage").load(function(){
		setupImage();
		$("#theImage").draggable({
			containment: "parent",
			cursor:"move"
		});
	});
	
}

function save(){
	window.parent.updateProfile(pID,imagePath,gender);
	window.close();
}

function dFunction(){
	
}

$(document).ready(function(){
	
	//setupPage
	
	$("#imageBasket").css({
		width: minWidth,
		height: minHeight
	});
	
	//upload form
	
	$("#maxWidth").val(maxWidth);
	$("#maxHeight").val(maxHeight);
	$("#minWidth").val(minWidth);
	$("#minHeight").val(minHeight);
	$("#pID2").val(pID);
	
	$("#imageField").change(function(){
		$("#imageBasket").css("background","url(/_images/loading_on_white.gif) #ffffff no-repeat center center");
		$("#formUpload").submit();
	})
	
	switch(action){
		case "upload":
			upload();
			break;
		case "save":
			save();
			break;
		case "error":
			$("#message").text(error);
			dFunction();
			break;
		default:
			dFunction();
	}
	
	$("#zoomPlus").click(function(){
		manualSize = 1;
		var iWidth = $("#theImage").width();
		var iHeight = $("#theImage").height();
		if ( iWidth * 1.2 < oWidth && iHeight * 1.2 < oHeight){
			$("#theImage").css({
				width: iWidth * 1.2,
				height: iHeight * 1.2
			})
			setupImage();
		}
	});
	
	$("#zoomMinus").click(function(){
		manualSize = 1;
		var iWidth = $("#theImage").width();
		var iHeight = $("#theImage").height();
		if ( iWidth * 0.8  >= minWidth && iHeight * 0.8 >= minHeight){
			$("#theImage").css({
				width: iWidth * 0.8,
				height: iHeight * 0.8
			})
			setupImage();
		}
	});
	
});
