﻿// JavaScript functions for Files module user controls.

/* FileGalleries/PhotoGalleriesView.ascx */
function photoGalleriesView_file(url, caption)
{
	this.url = url;
	this.caption = caption;
}

function photoGalleriesView_increment(prefix,amount)
{
	var field = document.getElementById(prefix + "_CurrentIndex");
	if (field)
	{
		var files = eval(prefix + "_Files");
		var total = parseInt(document.getElementById(prefix + "_GalleryFileCount").value);
		var index = parseInt(field.value);
		
		//ensure a numeric index is present
		if (isNaN(index)) {index = 0;}
		
		//adjust index
		var newIndex = index + amount;
		if (newIndex >= 0 && newIndex < total)
		{
			var file = files[newIndex];
			
			//set current image & caption
			document.getElementById(prefix + "_CurrentPhoto").src = file.url;
			document.getElementById(prefix + "_PhotoIndex").firstChild.nodeValue = (newIndex + 1);
			document.getElementById(prefix + "_PhotoCaption").firstChild.nodeValue = file.caption;
			
			//if available, set next image
			if ((newIndex + 1) < total)
				document.getElementById(prefix + "_NextPhoto").src = files[newIndex+1].url;
			
			//set new current index
			field.value = newIndex;
			
			//show photo area, hide end of gallery area
			document.getElementById(prefix + "_PhotoArea").style.display = "block";
			document.getElementById(prefix + "_EndOfGallery").style.display = "none";
		}
		else if (newIndex == total) //end of gallery
		{
			//hide photo area, show end of gallery area
			document.getElementById(prefix + "_PhotoArea").style.display = "none";
			document.getElementById(prefix + "_EndOfGallery").style.display = "block";
		}
	}
}

function photoGalleriesView_restart(prefix)
{
	var field = document.getElementById(prefix + "_CurrentIndex");
	if (field)
	{
		field.value = "0";
		photoGalleriesView_increment(prefix,0);
	}
}

/* FileLibraryManager.ascx */
function fileLibraryManager_image_view(prefix,fileName,fileCaption)
{
	var path = document.getElementById(prefix + "_FilesPath");
	var img = document.getElementById(prefix + "_Image");
	var caption = document.getElementById(prefix + "_ImageCaption");
	
	if (path && img && caption)
	{
		img.src = path.value + fileName;
		caption.innerHTML = fileCaption;	
		modalDialog_toggle(prefix + "_ImageDialog",true);
	}	
}

function fileLibraryManager_imageDialog_hide(prefix)
{
	modalDialog_toggle(prefix + "_ImageDialog",false);
}

/* FileSelector.ascx */
function fileSelector_clear_onClick(prefix)
{
	fileSelector_onSelect(prefix, 0, "", "");
}

function fileSelector_onSelect(prefix, fileLeafID, fileName, originalFileName)
{
	var delegate = document.getElementById(prefix + "_ClientEventHandlerName").value;

	document.getElementById(prefix + "_FileDisplay").value = originalFileName;
	document.getElementById(prefix + "_FileLeafID").value = fileLeafID;
	document.getElementById(prefix + "_FileName").value = fileName;
	document.getElementById(prefix + "_OriginalFileName").value = originalFileName;
	
	//try to call delegate
	if (delegate.length > 0)
	{
		try {eval(delegate + "(" + fileLeafID + ",'" + fileName + "')");}
		catch(ex) { /*do nothing*/; }
	}
}

/* Files/FileFields.ascx */
function fileFields_autoResize_onClick(prefix,inEditMode)
{
	var autoResizeFieldsContainerEl = document.getElementById("AutoResizeFieldsContainer");
	var autoResizeEl = document.getElementById(prefix + "_AutoResize");
	var autoResizeFieldsEl = document.getElementById("AutoResizeFields");
	var replaceFileEl = document.getElementById(prefix + "_ReplaceFile");
	var replaceFileFieldsEl = document.getElementById("ReplaceFileFields");

	if (autoResizeFieldsContainerEl && autoResizeEl && autoResizeFieldsEl)
	{
		if (!inEditMode || (inEditMode && replaceFileEl.checked))
		{
			if (autoResizeEl && autoResizeFieldsEl)
			{
				autoResizeFieldsEl.style.display = autoResizeEl.checked ? "block" : "none";
				ValidatorEnable(document.getElementById(prefix + "_AutoResizeWidthValidator"),autoResizeEl.checked)
				ValidatorEnable(document.getElementById(prefix + "_AutoResizeHeightValidator"),autoResizeEl.checked)
			}
			autoResizeFieldsContainerEl.style.display = "block";
		}
		else
			autoResizeFieldsContainerEl.style.display = "none";
	}
}

function fileFields_replaceFile_onClick(prefix,inEditMode)
{
	var replaceFileEl = document.getElementById(prefix + "_ReplaceFile");
	var replaceFileFieldsEl = document.getElementById("ReplaceFileFields");

	if (replaceFileEl && replaceFileFieldsEl)
	{
		replaceFileFieldsEl.style.display = replaceFileEl.checked ? "block" : "none";
		ValidatorEnable(document.getElementById(prefix + "_EditUploadFileValidator"),replaceFileEl.checked)
	}
	fileFields_autoResize_onClick(prefix,inEditMode);
}

function fileFields_onLoad(prefix,inEditMode)
{
	fileFields_autoResize_onClick(prefix,inEditMode);
	fileFields_replaceFile_onClick(prefix,inEditMode);
}

/* Images/ImageViewModalDialog */
function imageViewModalDialog_show(prefix,fileName,fileCaption)
{
	var path = document.getElementById(prefix + "_FilesPath");
	var img = document.getElementById(prefix + "_Image");
	var caption = document.getElementById(prefix + "_ImageCaption");
	
	if (path && img && caption)
	{
		img.src = path.value + fileName;
		caption.innerHTML = fileCaption;	
		modalDialog_toggle(prefix + "_Dialog",true);
	}	
}