Mouse Orbit after changing Position sets camera back to last position

Hi,

I am really stuck on this code and I know I am doing something stupid. I have a camera that is is getting the mouse input and a target to use for a mouse orbit. There is another script on objects that when clicked sets a flag for moving the camera with a lerp to the object.

This all works fine except when I try and do a manual mouse orbit again because it snaps the camera back to the last position I was at during the last mouse orbit.

I have tried to offset it and a couple of other stabs at the dark but having no luck so have given up. It would be great if someone could give a hand and I do hope that all makes sense.

Edit: Also this is it running which will do a better job explaining it. http://3dsteve.co.uk/camerawebbuild/camerawebbuilt.html

function LateUpdate () 
{
    if(Input.GetMouseButton(0) && !selectanatomy.moving && GUIUtility.hotControl == 0) 
    {
        x +=  Input.GetAxis("Mouse X") * xSpeed * 0.02;
        y -=  Input.GetAxis("Mouse Y") * ySpeed * 0.02;
 
 		y = ClampAngle(y, yMinLimit, yMaxLimit);
 
		var rotation = Quaternion.Euler(y, x, 0.0);
		var position = rotation * Vector3(0.0, 0.0, -distance) + target.transform.position;
		 
		transform.rotation = rotation;
		transform.position = position;
    }
    
	if(selectanatomy.moving)	//object clicked sets this to true
	{
		transform.position = Vector3.Lerp(transform.position, targetposition.transform.position, Time.deltaTime * 5);
		
    	renablecamera();
	}
}

function renablecamera()
{
	yield WaitForSeconds (1.0);

	//I THINK THIS IS WHERE I NEED TO RESET THE ROTATION OF THE CAMERA TO A NEW X AND Y POSITION BASED ON WHERE THE TARGET POSITION IS 

	selectanatomy.moving = false;
}
 
static function ClampAngle (angle : float, min : float, max : float) {
    if (angle < -360)
        angle += 360;
    if (angle > 360)
        angle -= 360;
    return Mathf.Clamp (angle, min, max);
}

In Start() function, you have to get Camera Original Rotation:

origintalRotation = transform.localRotation;

Then,

In Update() function, set the rotation to the Camera

Quaternion xQuaternion = Quaternion.AngleAxis(rotationX, Vector3.up);

Quaternion  yQuaternion = Quaternion.AngleAxis(rotationy, Vector3.left);

transform.localRotation = originalRotation * xQuaternion * yQuaternion