//////////////////////////////
// Displaying Large Pictures
//////////////////////////////

var lastPicture;

function togglePicture(picture) {
  //Hide the last picture
	var lastP = document.getElementById(lastPicture);
	var justHidden=false;
  if(lastP!=null && lastP.style.visibility == "visible") {
	  lastP.style.visibility = "hidden";    
	  justHidden = (picture == lastPicture);
	}
  //If not just hidden and if not visible then make visible
  if(!justHidden && document.getElementById(picture).style.visibility!="visible") {
	  document.getElementById(picture).style.visibility="visible";
	}
	lastPicture = picture;
}

function hidePicture(picture) {
  document.getElementById(picture).style.visibility="hidden";
}

///////////////////////
// Website Management
///////////////////////

var img;
var painting;
var longName;

// The id of the current option (filename) is passed in
function selectGivenPicture(picture) {
  //if the option exists it is selected
  if (document.getElementById(picture)) {
	  painting=document.getElementById(picture).selected="Selected";
		selectPicture();
	//otherwise if options exist then the first is selected
  } else if (document.getElementById("currentName").options[0]) {
	  document.getElementById("currentName").options[0].selected="Selected";
		selectPicture();
	}	
}

//Displays the selected picture on the manage page
function selectPicture() {
  directory=document.getElementById("directory").value;
  painting=document.getElementById("currentName").value;
	longName=directory+"/"+painting;
  document.getElementById("newName").value=painting;
	
	//If no image hide and don't proceed
	if(painting == "currently no paintings") {
	  document.getElementById('image').style.visibility="hidden";
	  document.getElementById('message').innerHTML="No Paintings";
		return;
  }
  document.getElementById('image').style.visibility="visible";
	document.getElementById('message').innerHTML="";
	
	img = new Image();
  img.src = longName;
	
  //Amazing Code that waits for image to be loaded
  if(img.complete) imageLoaded(); 
  else img.onload = imageLoaded; //functions are first class citizens
}

//finishes up selecting the picture after the image is loaded
function imageLoaded() {
  //Resize the image if width is less than 300
	if(img.width<300) {
	  var num = ((300-img.width)/2) + 10;
	  document.getElementById('image').style.padding="10px "+num+"px";
		document.getElementById('image').style.width=img.width+"px";
	} else {
	  document.getElementById('image').style.padding="10px";
	  document.getElementById('image').style.width="300px";
	}
	//Check if not currently displaying the picture
	if(painting.indexOf("!")==0) {
		$("#image").addClass("hiding");
		document.getElementById('message').innerHTML="Not Displayed";
		document.getElementById('hidePicture').checked = true;
	} else {
		$("#image").removeClass("hiding");
		document.getElementById('message').innerHTML="";
		document.getElementById('hidePicture').checked = false;
  }		
	//Load the Picture
	document.getElementById('image').src=longName;
}

//Allows only JPEGS to be uploaded 
function enableUpload() {
  file = document.getElementById("uploadFile").value;
	if((file.length - 4) ==  file.indexOf(".jpg") || (file.length - 4) ==  file.indexOf(".JPG")) { 
		document.getElementById("upload").disabled = false;
	} else {
	  document.getElementById("upload").disabled = true;
	}
}

function setFocus(element) {
  document.getElementById(element).focus();
}

function changeDirectory() {
  document.getElementById("directoryChanged").value = "true";
	document.manage.submit();
}

//couldn't be named hidePicture because name already taken
function hidePainting() {
	if(document.getElementById("currentName").value == document.getElementById("newName").value) {
	  name = document.getElementById("newName").value;
		if((name.length - 4) ==  name.indexOf(".jpg")) {
		  document.getElementById("hidingPainting").value = "true";
		  document.manage.submit();
    }
	}
}
