var absPath = '/';


// Image Catgories -----------------------------------
function loadImgCats (content_id)
{
	execAjaxImgCats (content_id)
/*	
	new Effect.BlindUp 
	(
		'imgCats', {duration: .5, afterFinish: execAjaxImgCats (content_id)}
	);
*/	
}
function execAjaxImgCats (content_id)
{
	new Ajax.Updater 
	(
		'imgCats',
		absPath + 'img_categories/load/' + content_id, 
		{
			asynchronous:true,
			evalScripts:true,
			evalJS:true,
			onComplete:function (request, json) 
			{
				new Effect.BlindUp 
				(
					'imgCats', {duration: .2, afterFinish: refreshContentImgCats}
				);
				refreshContentImgCats ();
			}, 
			requestHeaders:['X-Update', 'imgCats']
		}
	);
}
function refreshContentImgCats ()
{
	Effect.toggle ('new_imgCat', 'blind');
	new Effect.BlindDown ('imgCats', {duration: .5});
}
// Images Add Btn ------------------------------------
function execAjaxImagesContent (img_category_id)
{
	new Ajax.Updater 
	(
		'imgs_' + img_category_id,
		absPath + 'images/load_imgs_content/' + img_category_id,
		{
			asynchronous:true,
			evalScripts:true,
			evalJS:true,
			onComplete:function (request, json) { }, 
			requestHeaders:['X-Update', 'imgs_' + img_category_id]
		}
	);
}
// Images --------------------------------------------
function loadImages (img_category_id)
{
	execAjaxImages (img_category_id)
	
	/*
	new Effect.BlindUp 
	(
		'imgsList_' + img_category_id, {duration: .5, afterFinish: execAjaxImages (img_category_id)}
	);
	*/
}
function execAjaxImages (img_category_id)
{
	new Ajax.Updater 
	(
		'imgsList_' + img_category_id,
		absPath + 'images/load/' + img_category_id,
		{
			asynchronous:true,
			evalScripts:true,
			evalJS:true,
			onLoading:function ()
			{
				new Effect.BlindUp ('imgsList_' + img_category_id, {duration: .2});
			},
			onComplete:function (request, json) 
			{
				if ($('new_imgFormBox_' + img_category_id) != undefined) Effect.toggle ('new_imgFormBox_' + img_category_id, 'blind');		
				new Effect.BlindDown ('imgsList_' + img_category_id);
			}, 
			requestHeaders:['X-Update', 'imgsList_' + img_category_id]
		}
	);
}
// Image upload popup
function imgUpload_popUp (id)
{
	var x = Math.ceil((screen.width - 290)/2);
	var y = Math.ceil((screen.height - 180)/2);

	var imgUpload_win = window.open('../../images/add/' + id,'popup','menubar=no,toolbar=no,location=no,status=no, scrollbars=no,resizable=no,width=290,height=180,top='+y+',left='+x+'');
	imgUpload_win.focus ();
}

function validateFields (form, imgCat_id)
{
	var fields_error = false;
	var allowed_files = 'jpg,gif,pdf,doc,docx';


	if (form.ImageTitle != undefined)
	{
		var title = form.ImageTitle.value;	

		if (title == '')
		{
			fields_error = true;
			$('img_title_error_' + imgCat_id).innerHTML = 'Bitte eine &Uuml;berschrift eingeben.';
		}
		else $('img_title_error_' + imgCat_id).innerHTML = ''
	}
		

	var file = form.ImageFile.value		
	file = file.lastIndexOf ('\\') == -1 ? file : file.substr (file.lastIndexOf ('\\') + 1);
	
	if (file == '')
	{
		fields_error = true;
		$('img_file_error_' + imgCat_id).innerHTML = 'Bitte eine Datei ausw&auml;hlen.';
	}
	else
	{
		if (file.lastIndexOf ('.') == -1)
		{
			fields_error = true;
			$('img_file_error_' + imgCat_id).innerHTML = 'Bitte eine g&uuml;ltige Datei ausw&auml;hlen.';
		}
		else
		{
			var ext = file.substr (file.lastIndexOf ('.') + 1).toLowerCase ();
			
			if (allowed_files.indexOf (ext) == -1)
			{
				fields_error = true;
				$('img_file_error_' + imgCat_id).innerHTML = 'Bitte einen g&uuml;ltigen Dateityp ausw&auml;hlen.';
			}
			else $('img_file_error_' + imgCat_id).innerHTML = '';
		}
	}


	if (fields_error) return false;
	else return true;
}

// -----------------------------------------------------
function removeEl (el)
{
	var parent = $(el).parentNode;

	Effect.SlideUp (el, {duration: .3, afterFinish: function () { parent.removeChild ($(el)); }});
}

function enableBtn (btnBox, btn)	// there is a copy into js/micoxUploader.js
{
	$(btn).show ();
	$(btnBox).removeChild ($(btn + '_disable'));
}
function disableBtn (btnBox, btn, caption)
{
	var btnClass = $(btn).className;
	var new_aTag = Builder.node ('a', {id: btn + '_disable', className: btnClass + '_disable', href: 'javascript:void(0)'}, caption);

	$(btn).hide ();
	$(btnBox).insertBefore (new_aTag, $(btn));
}
function setBtnCaption (el, caption)
{
	$(el).innerHTML = caption;
}
// -----------------------------------------------------
function toggleView (el, div)
{
	if (el.className == 'btn_expand') expand (el, div);
	else contract (el, div);
}
function expand (el, div2expand)
{
	el.className = 'btn_contract';
	el.title = 'Ansicht schließen';
	
	Effect.BlindDown (div2expand);
}
function contract (el, div2contract)
{
	el.className = 'btn_expand';
	el.title = 'Dateien ansehen';

	Effect.BlindUp (div2contract);
}


var updateImg_title = null;
function updateImg_saving (el, span_title)
{
	updateImg_title = $(span_title).innerHTML;

	$(el).hide ();
	$(span_title).className = 'title inplaceeditor-saving';
	$(span_title).innerHTML = 'Bitte warten...';
}
function updateImg_loaded (el, span_title)
{
//	Effect.toggle (el, 'blind');

	$(span_title).className = 'title';
	$(span_title).innerHTML = updateImg_title;

	updateImg_title = null;
}






