var currentImg;
var xmlHttp;
var current_dir='';

function openImageManager(images,mode,options) {
	if(!options) options = { };
	if(!options.path) options.path = g_cfg.the_base_path + '/';
	options.width = options.width || 940;
	if(!mode) mode = 'multi';
	images = escape(images);
	return window.open(options.path+'imgmanage.php?images='+images+'&mode='+mode, 'imgmanager', 
		'height=700,width='+options.width+',resizable=1,'+
		'menubar=0,toolbar=0,location=0,'+
		'directories=0,scrollbars=1,status=0');
}

function useImages(mode) {
	var divs = g('using').childNodes;
	var s = '';
	var i, j = 0;
	var list = new Array();
	for(i = 0; i < divs.length; i++) {
		if(divs[i].tagName && divs[i].tagName.toLowerCase() == 'div') {
			list[j] = divs[i].id.substring(2);
			j++;
		}
	}
	if(mode == 'captions'){
		opener.acceptImagesAndCaptions(list);
	} else {
		opener.acceptImages(list);
	}
	window.close();
}

function useImage() {
	opener.acceptImages(new Array(currentImg));
	window.close();
}

function addItem(fname) {
	var item = g('i_'+fname);
	var usingHTML = g('using_list').innerHTML;
	usingHTML += ","+fname;
	g('using_list').innerHTML = usingHTML;
	g('a_'+fname).href="javascript:delItem('"+(fname)+"')";
	g('using').appendChild(item);
}

function delItem(fname) {
	var item = g('i_'+fname);
	//need to get the list and remove the name of the file from the list.
	//also need to check whether the file should go back on the left hand side or not.
	var using_list = g('using_list').innerHTML;
	var images = using_list.split(',');
	var length = images.length;
	var use_images = '';
	var counter=0;
	for(i = 0; i < length; i++){
		if(images[i] != fname){
			if(counter==0){
				use_images += images[i];
			} else {
				use_images += ','+images[i];
			}
			counter++;
		}
	}
	g('using_list').innerHTML = use_images;
	if(current_dir == ''){
		f_dir = fname.split('/');
		f_len = f_dir.length;
		if(f_len == 1){
			g('a_'+fname).onclick = function(){addItem(fname)};
			g('uploaded').appendChild(item);
			return;
		}
	} else {
		dirs = current_dir.split('/');
		d_len = dirs.length-1;
		f_dir = fname.split('/');
		f_len = f_dir.length-1;
		if(d_len == f_len){
			for(i = 0; i < d_len && i < f_len; i++){
				if(i+1 == d_len && dirs[i] == f_dir[i]){
					g('a_'+fname).onclick = function(){addItem(fname)};
					g('uploaded').appendChild(item);
					return;
				}
				if(dirs[i] != f_dir[i]) break;
			}
		}
	}
	
	g('empty').appendChild(item);
	g('empty').innerHTML = '';
}

function setImg(fname) {
	currentImg = fname;
	g('view_single').src=g_cfg.the_base_path+"/getimage.php?id="+escape(fname)+"&x=200&y=300";
	g('single_filename').innerHTML = fname;
}

function open_dir(id, current,mode) {
		var load_string = '<table><tr><td align="middle" valign="middle" height="150" width="340"><img src="images/imgmanager/loading.gif" /><br/><br/>loading</td></tr></table>';
		g('uploaded').innerHTML = load_string;

		current_dir = id;
		var using_list=g('using_list').innerHTML;
		var url="uimgmanager.php?dir="+id+"&current="+current+"&mode="+mode+"&using="+using_list;
		xmlHttp=GetXmlHttpObject(stateChanged);
		xmlHttp.open("GET", url , true);
		xmlHttp.send(null);
}

// AJAX routines
function stateChanged() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{ 
		//place the code here to output the new file info.
		//get the response from here:  xmlHttp.responseText
		var response = xmlHttp.responseText;
		//alert(response);
		g('uploaded').innerHTML = xmlHttp.responseText;
	} 
}

function GetXmlHttpObject(handler)
{ 
	var objXmlHttp=null

	if (navigator.userAgent.indexOf("Opera")>=0)
	{
		alert("This example doesn't work in Opera") 
		return 
	}
	if (navigator.userAgent.indexOf("MSIE")>=0)
	{ 
		var strName="Msxml2.XMLHTTP"
		if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
	{
		strName="Microsoft.XMLHTTP"
	} 
	try
	{ 
		objXmlHttp=new ActiveXObject(strName)
		objXmlHttp.onreadystatechange=handler 
		return objXmlHttp
	} 
		catch(e)
		{ 
			alert("Error. Scripting for ActiveX might be disabled") 
			return 
		} 
	} 
	if (navigator.userAgent.indexOf("Mozilla")>=0)
	{
		objXmlHttp=new XMLHttpRequest()
		objXmlHttp.onload=handler
		objXmlHttp.onerror=handler 
		return objXmlHttp
	}
} 

