
function openImageUploader(fn, imageW, imageH, thumbnailW, thumbnailH)
{	
	var url = "/controls/assetBrowser.php?fn=" + fn;
	
	if (imageW) url += "&iw=" + imageW;
	if (imageH) url += "&ih=" + imageH;
	if (thumbnailW) url += "&tw=" + thumbnailW;
	if (thumbnailH) url += "&th=" + thumbnailH;
	return openDialog(url, 300, 200);
}

function openSendtoFriend(articleId)
{
	var url = "/controls/SendToFriend.php?id="+articleId;
	openDialog(url, 550, 500);
}

function openPrintView(articleId)
{
	var url = "/controls/PrintView.php?id="+articleId;
	openDialog(url, 750, 550, 1);
}

var curImageControlId;
var curThumbnailControlId;

function openAssetBrowser(imageControlId, thumbnailControlId, imageW, imageH, thumbnailW, thumbnailH)
{
	curImageControlId = imageControlId;
	curThumbnailControlId = thumbnailControlId;
	openImageUploader("assetSaved", imageW, imageH, thumbnailW, thumbnailH);
}

function assetSaved(url, tnUrl)
{
	document.getElementById(curImageControlId).value = url;
	document.getElementById(curThumbnailControlId).value = tnUrl;
}

function assetFailure()
{
	alert("Could not upload file. \nIt may be too large or of the incorrect file type.");	
}

var curGalleryId;
function addGalleryPhoto(galleryId, imageW, imageH, thumbnailW, thumbnailH)
{	
	curGalleryId = galleryId;
	openImageUploader("galleryPhotoReady", imageW, imageH, thumbnailW, thumbnailH);
}

function galleryPhotoReady(imageUrl, thumbnailUrl)
{
	var curGallery = document.getElementById(curGalleryId);
	var key = curGalleryId + "_element_" + curGallery.childNodes.length;
	curGallery.innerHTML += "<img class='unselectedPhoto' id='"+key+"' src='"+thumbnailUrl+"' onclick='selectPhoto(this)'>";
	curGallery.innerHTML += "<input type='hidden' id='"+key+"_thumbnail' name='"+curGalleryId+"_thumbnails[]' value='"+thumbnailUrl+"'>";
	curGallery.innerHTML += "<input type='hidden' id='"+key+"_image' name='"+curGalleryId+"_images[]' value='"+imageUrl+"'>";
}

function saveGalleryState()
{
	var galleryImages = document.getElementById(curGalleryId + "_images");
	var galleryThumbnails = document.getElementById(curGalleryId + "_thumbnails");
	var galleryAltered = document.getElementById(curGalleryId + "_altered");
	
	var photos = getGalleryPhotos(curGalleryId);
		
	galleryAltered.value = "1";
}

function selectPhoto(image)
{
	if (image.selected)
	{
		image.className = "unselectedPhoto";
		image.selected = false;
	}
	else
	{
		image.className = "selectedPhoto";
		image.selected = true;
	}
}

function removeSelectedPhotos(galleryId)
{
	curGalleryId = galleryId;
	var images = getGalleryPhotos(galleryId);
	
	for(i=0; i<images.length; i++)
	{
		var image = images[i];
		if (image.selected)
		{
			var imageHidden = document.getElementById(image.id+"_images");
			var thumbnailHidden = document.getElementById(image.id+"_thumbnails");			
			
			image.parentNode.removeChild(imageHidden);
			image.parentNode.removeChild(thumbnailHidden);
			image.parentNode.removeChild(image);			
		}
	}
}

function getGalleryPhotos(galleryId)
{
	var curGallery = document.getElementById(galleryId);
	var images = new Array();  
	
	for(i=0; i<curGallery.childNodes.length; i++)
	{
		var node = curGallery.childNodes[i];
		if (node.tagName == 'IMG')
		{
			images.push(node);
		}
	}
	
	return images;
}

function postBack(controlId, eventName)
{
	var control = document.getElementById(controlId);
	control.form.eventControl.value = controlId;
	control.form.eventName.value = eventName;
	control.form.submit();
}

function openDialog(url, width, height, scrollbars)
{
	if (scrollbars>0){
	var attributes = "directories=0,location=0,menubar=0,scrollbars=1";
	}else { var attributes = "directories=0,location=0,menubar=0,scrollbars=0";}
	attributes += ",width=" + width + ",height=" + height;
	return window.open(url, "_blank", attributes);
}