function initpage(item_id, section){
	if (item_id > 0){
		getdirs(item_id, section);
		showpageimgs(item_id, section);
		showallimgs(item_id, '', section);
	}
}
//
function getdirs(item_id, section) {
	var handlerFunc = function(t) {
		//alert (t.responseText);
		adddir('root folder', '');
		dirs = t.responseText.split('#');
		l=dirs.length;
		for (i=0; i<l; i++){
		adddir(dirs[i], dirs[i]);
		}
	}
	var errFunc = function(t) {
		alert('Error ' + t.status + ' -- ' + t.statusText);
	}
	//alert (baseUrl);
	new Ajax.Request('common/ajax.func.php', {parameters:'action=imgdirs&item_id='+item_id+'&section='+section, onSuccess:handlerFunc, onFailure:errFunc});
}
//
function showpageimgs(item_id, section) {
document.getElementById('ajax_pageimglist').innerHTML = '';
	var handlerFunc = function(t) {
		//alert (t.responseText);
		document.getElementById('ajax_pageimglist').innerHTML = t.responseText;
	}
	var errFunc = function(t) {
		alert('Error ' + t.status + ' -- ' + t.statusText);
	}
	//alert (baseUrl);
	new Ajax.Request('common/ajax.func.php', {parameters:'action=showpageimgs&item_id='+item_id+'&section='+section, onSuccess:handlerFunc, onFailure:errFunc});
}
//
//
function showallimgs(item_id, folder, section) {
	var handlerFunc = function(t) {
		r = t.responseText.split('###');
		document.getElementById('ajax_imglist').innerHTML = r[0];
		imgs = r[1].split('#');
		l = imgs.length;
		//alert(l);
		if (folder != '') folder += '/';
		for (i=0; i<l; i++){
			document.getElementById('img_'+i).src = '../common/showimage.func.php?img='+folder+imgs[i]+'&thumb=true&thumbsize=115';
		}
	}
	var errFunc = function(t) {
		alert('Error ' + t.status + ' -- ' + t.statusText);
	}
	//alert (baseUrl);
	new Ajax.Request('common/ajax.func.php', {parameters:'action=showimgs&item_id='+item_id+'&folder='+folder+'&section='+section, onSuccess:handlerFunc, onFailure:errFunc});
}
//
//
function imgup(item_id, rank, section){
	saveimgcaptions(item_id, 'action=imgup&item_id='+item_id+'&rank='+rank, section);
}
//
//
function imgdown(item_id, rank, section){
	saveimgcaptions(item_id, 'action=imgdown&item_id='+item_id+'&rank='+rank, section);
}
//
//
function imgdelete(item_id, delete_id, section){
	saveimgcaptions(item_id, 'action=imgdelete&delete_id='+delete_id, section);
}
//
//
function imgadd(item_id, img, section){
	saveimgcaptions(item_id, 'action=imgadd&item_id='+item_id+'&img='+img, section);
}
//
//
function saveimgcaptions(item_id, params, section){
	c = 1;
	modstring='';
	while (document.getElementById('caption['+c+']')) c++;
	//
	//alert (c);
	for (i=1; i<(c); i++){
		caption = document.getElementById('caption['+i+']') ? document.getElementById('caption['+i+']').value : '';
		modstring += '&caption['+i+']='+caption;
	}
	var handlerFunc = function(t) {
		//do ajax call for function
		var handlerFunc = function(t) {
			//alert (t.responseText);
			showpageimgs(item_id, section);
		}
		var errFunc = function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}
		//alert (baseUrl);
		new Ajax.Request('common/ajax.func.php', {parameters:params+'&section='+section, onSuccess:handlerFunc, onFailure:errFunc});
	}
	var errFunc = function(t) {
		alert('Error ' + t.status + ' -- ' + t.statusText);
	}
	//ajax call to save captions
	new Ajax.Request('common/ajax.func.php', {parameters:'action=savecaption&item_id='+item_id+'&section='+section+modstring, onSuccess:handlerFunc, onFailure:errFunc});
}
//
//
function zoom(img, i){
	document.getElementById('zoom_'+i).innerHTML = '<img src="../common/showimage.func.php?img='+img+'&area=75000">';
	document.getElementById('zoom_'+i).style.className = 'zoombox_active';
}
function hidezoom(i){
	document.getElementById('zoom_'+i).innerHTML = '';
	document.getElementById('zoom_'+i).style.className = 'zoombox';
}
function adddir(folder, val)
{
  var newopt = document.createElement('option');
  newopt.text = folder;
  newopt.value = val;
  var sel = document.getElementById('imgdirs');

  try {
    sel.add(newopt, null); // standards compliant; doesn't work in IE
  }
  catch(ex) {
    sel.add(newopt); // IE only
  }
}
// JavaScript Document
function changeImg() {
document.getElementById('currentimg').style.visibility = 'hidden'
document.getElementById('currentimg').style.display = 'none'
document.getElementById('uploader').style.visibility = 'visible'
document.getElementById('uploader').style.display = 'inline'
}
function changeFile() {
document.getElementById('currentfile').style.visibility = 'hidden'
document.getElementById('currentfile').style.display = 'none'
document.getElementById('uploader').style.visibility = 'visible'
document.getElementById('uploader').style.display = 'inline'
}
function previewSelect(loc,id) {
	if (loc=='image'){
		var handlerFunc = function(t) {
			//alert (t.responseText);
			document.getElementById('image').value = t.responseText;
			document.getElementById('imgthumb').innerHTML = '<img src="../common/showimage.func.php?img='+t.responseText+'&amp;area=15000" border="0" alt="page image thumb" />';
		}
		var errFunc = function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}
		//alert (baseUrl);
		new Ajax.Request('common/ajax.func.php', {parameters:'action=findfile', onSuccess:handlerFunc, onFailure:errFunc});
	}
	if (loc=='file'){
		var handlerFunc = function(t) {
			//alert (t.responseText);
			document.getElementById('filename').value = t.responseText;
			document.getElementById('filethumb').innerHTML = '<img src="../images/file_pdf.gif" alt="PDF icon" width="48" height="48" border="0" />&nbsp;' + t.responseText;
		}
		var errFunc = function(t) {
			alert('Error ' + t.status + ' -- ' + t.statusText);
		}
		//alert (baseUrl);
		new Ajax.Request('common/ajax.func.php', {parameters:'action=findfile', onSuccess:handlerFunc, onFailure:errFunc});
	}
}
function selectPressed() {
if (document.getElementById('currentimg')) document.getElementById('currentimg').style.visibility = 'visible';
if (document.getElementById('currentimg')) document.getElementById('currentimg').style.display = 'inline';
if (document.getElementById('currentfile')) document.getElementById('currentfile').style.visibility = 'visible';
if (document.getElementById('currentfile')) document.getElementById('currentfile').style.display = 'inline'
document.getElementById('uploader').style.visibility = 'hidden'
document.getElementById('uploader').style.display = 'none'
}
