//
// Loads a thumbnail of the specified filename into the image_gallery space. 
// Used when showing form errors and when an image has been successfully uploaded.
//
function loadThumbnail( dir, filename, topDocument ){
	var galleryContainer = topDocument.getElementById( "image_gallery" );

	// Create all the tags
	//////////
	var imageContainer = topDocument.createElement('div');
	var imageTag = topDocument.createElement('img');
	var brTag = topDocument.createElement('br');
	var removeLink = topDocument.createElement('a');
	var removeText = topDocument.createTextNode("del");
	var editLink = topDocument.createElement('a');
	var editText = topDocument.createTextNode("add caption");

	// Style the tags
	//////////
	imageContainer.style.width = "100px";
	imageContainer.style.height = "120px";
	imageContainer.style.margin = "10px";
	imageContainer.style.textAlign = "center";
	imageContainer.id = filename;

	imageTag.src = dir +"thumbs/"+ filename;

	removeLink.href = "javascript:smoothRemove( '" + filename + "' );";
	editLink.href = "javascript:openPopup( '"+dir+"', 'addCaptionBox', '"+filename+"' )";

	// Link the tags together
	//////////
	removeLink.appendChild( removeText );
	editLink.appendChild( editText );

	imageContainer.appendChild( imageTag );
	imageContainer.appendChild( brTag );
	imageContainer.appendChild( editLink );
	imageContainer.appendChild( topDocument.createTextNode(" | "));
	imageContainer.appendChild( removeLink );

	galleryContainer.appendChild( imageContainer );

}


function loadThumbnailForReview( dir, filename, topDocument, shortcaption, fullcaption ){
	var galleryContainer = topDocument.getElementById( "image_gallery" );

	// Create all the tags
	//////////
	var imageContainer = topDocument.createElement('div');
	var imageLink = topDocument.createElement('a');
	var imageTag = topDocument.createElement('img');
	var brTag = topDocument.createElement('br');
	var viewLink = topDocument.createElement('a');
	var viewText = topDocument.createTextNode(shortcaption);
	
	// Style the tags
	//////////
	imageContainer.style.width = "100px";
	imageContainer.style.height = "110px";
	imageContainer.style.margin = "10px";
	imageContainer.style.textAlign = "center";
	imageContainer.className = "image";
	imageLink.id = filename;

	imageTag.src = dir +"thumbs/"+ filename;
	imageTag.alt = fullcaption;	

	viewLink.href = "javascript:openPopupForReview( '"+dir+"', 'fullImageBox', '"+filename+"' );"
	imageLink.href = "javascript:openPopupForReview( '"+dir+"', 'fullImageBox', '"+filename+"' );"

	// Link the tags together
	//////////
	viewLink.appendChild( viewText );
	imageLink.appendChild( imageTag );

	imageContainer.appendChild( imageLink );
	imageContainer.appendChild( brTag );
	imageContainer.appendChild( viewLink );

	galleryContainer.appendChild( imageContainer );


}

function smoothRemove( id ){
	var myNode = document.getElementById( id );
	var strWidth  = myNode.style.width;
	var width = strWidth.substring(0, strWidth.length - 2);

	if( width > 10 ){
		myNode.style.overflow = "hidden";
		myNode.style.width = (width - 10) + "px";
		setTimeout("smoothRemove( '" + id + "' );", 25); // .1 sec
	}
	else{
		removeGalleryImage( id )
	}
}

function removeGalleryImage( id ){
	myNode = document.getElementById( id );
	myParentNode = myNode.parentNode;
	myParentNode.removeChild( myNode );
	document.getElementById("deleteImages").value += id + "|";
}

function openPopup( dir, elmntId , filename){
	var image_id = filename.substring(0,filename.length-4);
	document.getElementById( 'imageLarge' ).src = dir+filename;
	document.getElementById( 'captionImageId' ).value = image_id;
	document.getElementById( 'captionRuid' ).value = document.getElementById( 'ruid' ).value;
	document.getElementById( 'captionSubmit' ).value = "";
	document.getElementById("whiteout").style.visibility = 'visible';

	AjaxRequest.get(
		{
			'url':'/review/ajax_addImageCaption.jsp?captionaction=getcaption&image_id='+image_id,
			'onSuccess':function(req){ var ajresp = req.responseText; 
							document.getElementById( "captionText" ).innerHTML = ajresp;
							document.getElementById( "captionSubmit" ).value = ajresp;
							location.hash = 'editImageCaption';
							document.getElementById("popupBox").style.visibility = 'visible';
							document.getElementById( elmntId ).style.display = 'block';
						},
			'onError':function(req){ alert(req.responseText)}
		}
	);
}

function openPopupForReview( dir, elmntId , filename){
	var x = document.getElementsByTagName("select");
	for (i = 0; i < x.length; i++) x[i].style.visibility = 'hidden';

	var imagecontain = document.getElementById( filename );
	var childn = "";
	
/*	for(i =0 ; i < imagecontain.childNodes.length; i++){
		childn += '\n imagecontain.childNodes['+i+']: ' + imagecontain.childNodes[i].alt;
	}
	alert(childn);*/
	document.getElementById( 'imageLarge' ).src = dir+filename;
	document.getElementById( "captionText" ).innerHTML = document.getElementById( filename ).childNodes[0].alt; 
	document.getElementById("whiteout").style.visibility = 'visible';
	//location.hash = 'fullImage';
	document.getElementById("popupBox").style.visibility = 'visible';
	document.getElementById( elmntId ).style.display = 'block';
	window.location = '#';
}


function openPopupForComplex( dir, elmntId , filename, captiontext){
	var x = document.getElementsByTagName("select");
	for (i = 0; i < x.length; i++) x[i].style.visibility = 'hidden';

	var imagecontain = document.getElementById( filename );
	var childn = "";
/*	for(i =0 ; i < imagecontain.childNodes.length; i++){
		childn += '\n imagecontain.childNodes['+i+']: ' + imagecontain.childNodes[i].alt;
	}
	alert(childn);*/
	document.getElementById( 'imageLargeComplex' ).src = dir+filename;
	document.getElementById( "captionTextComplex" ).innerHTML = captiontext;
	document.getElementById("whiteout").style.visibility = 'visible';
	//location.hash = 'fullImage';
	document.getElementById("popupBoxComplex").style.visibility = 'visible';
	document.getElementById( elmntId ).style.display = 'block';

}


function submitCaption( form ){
	// Verify form
	//////
	if( form.captionSubmit.value == "" ){
		alert("Please type in the caption you would like to submit.");
		return;
	}
	
	if( form.captionSubmit.length > 254){
		alert("Please edit your caption, it may be too long");
	}

 	var status = AjaxRequest.submit(
		form
		,{
			'onSuccess':function(req){ document.getElementById( "captionText" ).innerHTML = req.responseText; }
		}
	);

	closePopup("popupBox");
  return status;	
}

function closePopup( elmntId ){
	document.getElementById("whiteout").style.visibility = 'hidden';
	document.getElementById(elmntId).style.visibility = 'hidden';
	var x = document.getElementsByTagName("select");
	for (i = 0; i < x.length; i++) x[i].style.visibility = 'visible';
	//document.getElementById( elmntId ).style.display = 'none';
}

////////////////////////////////////////////////////////////////////////

	


function switchFromTo( fromId, toId ){
	document.getElementById( fromId ).style.display = 'none';
	document.getElementById( toId ).style.display = 'block';
}

function submitPhoto( form ){
	// Verify form
	//////
	if( form.legalConfirmation.checked == false ){
		alert("You must agree to the terms above to submit this photo.");
		return;
	}
	
	if( form.filename.value == "" ){
		alert("You have upload a photo first!");
		return;
	}
	
	// Submit form
	/////
	AjaxRequest.submit( form );
	switchFromTo('addPhotoBox', 'addPhotoBoxDone');
	
	// Reset form
	//////
	document.addPhotoForm.legalConfirmation.checked = false;
	document.addPhotoForm.filename.value = '';
	document.getElementById("photoUploadIframe").src = "/iframe_uploadComplexPhoto?complexId=512391010078701";
}


	
function removePhoto(){
	if( document.addPhotoForm.filename.value == '' ) return;
	document.getElementById("photoUploadIframe").src = "iframe_uploadComplexPhoto?complexId=512391010078701&remove=" + document.addPhotoForm.filename.value;
}
