//-----------------------------------------------------------------------------
// XET Toolkit 
// Copyright (c) 2006-2007 Xavier Amado (xavier@theadfirm.com || xamado@gmail.com)
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Uploader v0.5
// The uploader handles showing an url in a popup box in the middle of the page
// which should be a file uploader, done by the developer. When the file has 
// been uploaded the custom uploader can call this uploader's upload completed
// callback that will handle passing the value back to the page using the 
// provided callback. 
//-----------------------------------------------------------------------------

var onUploadedCallback = 0;

function xet_file_uploader_show(callback, uploaderURL)
{
	onUploadedCallback = callback;
	
	var darkBackground = document.createElement("div");
	darkBackground.setAttribute("id", "darkbackground");
	darkBackground.setAttribute("class", "darkbackground");
	darkBackground.setAttribute("className", "darkbackground");
	document.body.appendChild(darkBackground);
	
	var contentDiv = document.createElement("div");
	contentDiv.setAttribute("id", "fileselector");	
	contentDiv.setAttribute("class", "fileselector");
	contentDiv.setAttribute("className", "fileselector");

	// Append after setting the position		
	document.body.appendChild(contentDiv);
	
	// Calculate the position for the fileselector
	var leftMargin = (xet_browser_get_width() - contentDiv.clientWidth) / 2;
	var topMargin = (xet_browser_get_height() - contentDiv.clientHeight) / 2;
	contentDiv.style.left = leftMargin + "px";
	contentDiv.style.top = topMargin + "px";
	
	var iframe = document.createElement("iframe");
	iframe.setAttribute("src", configBaseURL + uploaderURL);
	contentDiv.appendChild(iframe);

	var closeBtn = document.createElement("input");
	closeBtn.setAttribute("type", "button");
	closeBtn.setAttribute("value", "Close");
	closeBtn.setAttribute("onmousedown", "xet_file_uploader_close();");	
	contentDiv.appendChild(closeBtn);
}

function xet_file_uploader_close()
{
	var fileSelector = document.getElementById("fileselector");
	var darkBackground = document.getElementById("darkbackground");
	var parent = fileSelector.parentNode;
	parent.removeChild(fileSelector);
	parent.removeChild(darkBackground);
}

function xet_file_uploader_on_upload_completed(filename)
{
	// Close the selector
	xet_file_uploader_close();
	
	// Update the thumbnail image
	onUploadedCallback(filename);
}

//-----------------------------------------------------------------------------
// Getting the browser width/height is a pain in the butt, this helps.
//-----------------------------------------------------------------------------

function xet_browser_get_width() 
{
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function xet_browser_get_height() 
{
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) 
{
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

