var GSImageUploadHandler, GSImageUploadSelection, GSLastSelected, GSImgWidth, GSImgHeight, GSMaxWidth, GSMaxHeight;

function ImageUpload() 
{
	this.image 			= new Array();
	this.selection 		= -1;
	this.selected_obj 	= null;
}
	
ImageUpload.prototype.addImage = function(url, w, h) 
{
	var img = new Array();
	img['url'] = url;
	img['imgW'] = w;
	img['imgH'] = h;
	img['type'] = '';
	this.image.push(img);
}
	
ImageUpload.prototype.validate = function() 
{
	var ret = new Array();
	if (this.image[this.selection]) ret = this.image[this.selection];
	var ctrls = new Array('browse', 'upload');
	var i;
	for (i in ctrls) 
	{
		if (document.getElementById(ctrls[i]).style.display == 'block') ret['type'] = ctrls[i];
	}
	switch (ret['type'])
	{
		case 'browse':
			ret['title'] 	= '';
		break;
		case 'upload':
			ret['title'] 	= '';
		break;
	}
	return ret;
}
	
ImageUpload.prototype.ctrlCellSelect = function(obj) 
{
	if (this.selected_obj) RemoveClass('ctrlCellActive', this.selected_obj);
	AddClass('ctrlCellActive', obj);
	this.selection = obj.id;
	this.selected_obj = obj;
	document.getElementById('ok_button').disabled=false;
}

ImageUpload.prototype.ctrlCellHover = function(obj, status) 
{
	if (obj.id != this.selection)
	{
		if (status) AddClass('ctrlCellActive', obj);
		else RemoveClass('ctrlCellActive', obj);
	}
}

ImageUpload = new ImageUpload();

function GSImageUpload_SetImgDim(imgObj) {
	GSImgWidth = imgObj.offsetWidth;
	GSImgHeight = imgObj.offsetHeight;
	}

function GSImageUpload_ComputeImgDim() {
	var result = new Array();
	if (GSImgWidth <= GSMaxWidth) {
		result['width'] = GSImgWidth;
		result['height'] = GSImgHeight;
		}
	else {
		result['width'] = GSMaxWidth;
		result['height'] = Math.ceil(GSMaxWidth / (GSImgWidth/GSImgHeight));
		}
	return result;
	}

function GSImageUpload_ValidateSel(forceURL) {
	if (!forceURL) {
		if (window.opener.GSImageUploadHandler) {
			if (GSLastSelected == "url") {
				window.opener.GSImageUploadHandler(document.getElementById('ImageUpload_URL').value);
				window.close();
				}
			else if (GSLastSelected == "file") {
				var iuForm = document.getElementById("IUForm");
				if (iuForm) {
					iuForm.IUReturn.value = 1;
					iuForm.submit();
					}
				}
			else if (GSLastSelected == "selection" && GSImageUploadSelection) {
				imgDim = GSImageUpload_ComputeImgDim();
				window.opener.GSImageUploadHandler(GSImageUploadSelection.id, imgDim);
				window.close();
				}
			}
		}
	else {
		imgDim = GSImageUpload_ComputeImgDim();
		window.opener.GSImageUploadHandler(forceURL, imgDim);
		window.close();
		}
	}

function GSImageUpload(folder_id, handler, maxWidth, maxHeight) {
	GSImageUploadHandler = handler;
	var top = (screen.height-450)/2;
	var left = (screen.width-600)/2;
	//var IUUrl = "<?php echo $GLOBALS["CONFIG"]["PATHS"]["PUBLIC"]["COMPONENTS"]["URL"]."ImageUpload/controls/assistant.control.php?fid="; ?>" + folder_id;
	var IUUrl = 'fid='+folder_id;
	if (maxWidth) IUUrl = IUUrl +  "&mW=" + maxWidth;
	if (maxHeight) IUUrl = IUUrl +  "&mH=" + maxHeight;
	Blacksmith.openControl('ImageUpload', 'edit', IUUrl, handler, 800, 600);
	/*if (handler) IUUrl = IUUrl +  "&h=1";
	if (maxWidth) IUUrl = IUUrl +  "&mW=" + maxWidth;
	if (maxHeight) IUUrl = IUUrl +  "&mH=" + maxHeight;*/
	//window.open(IUUrl,"ImageUploadAssistant","top="+top+",left="+left+",width="+600+",height="+450+", toolbar=no, status=yes, scrollbars=no, resizable=no, directories=no, menubar=no, location=no"); 
	}
	
function GSImageUpload_Select(obj, imgW, imgH) {
	if (GSImageUploadSelection) {
		var PrevSelect = GSImageUploadSelection;
		}
	GSImageUploadSelection = obj;
	GSImgWidth = imgW;
	GSImgHeight = imgH;
	GSImageUpload_Focus('selection');
	if (PrevSelect) RollOver(PrevSelect, false, GSImageUploadSelection);
	}

function GSImageUpload_HighlightSelection() {
	if (GSImageUploadSelection) RollOver(GSImageUploadSelection, true, GSImageUploadSelection);
	}
