function ajaxFileUpload( pageId )
{
	if($("#tx_modernboard_attachmentTitle").attr("value") == null || $("#tx_modernboard_attachmentTitle").attr("value") == "")
	{
		alert('Sie haben keinen Titel angegeben!');
		return false;
	}
	
	// show/hide loading
	$("#tx_modernboard_attachmentLoading")
	.ajaxStart(function(){
		$(this).show();
	})
	.ajaxComplete(function(){
		$(this).hide();
	});
	// show/hide form
	$("#tx_modernboard_attachmentForm")
	.ajaxStart(function(){
		$(this).hide();
	})
	.ajaxComplete(function(){
		$(this).show();
	});
	
	// start upload
	$.ajaxFileUpload
	(
		{
			url:'index.php?eID=tx_modernboard_pi1&action=add_attachment',
			secureuri:false,
			fileElementId:'tx_modernboard_attachmentInput',
			titleElementId:'tx_modernboard_attachmentTitle',
			keywordElementId:'tx_modernboard_attachmentKeywords',
			dataType: 'json',
			success: function (data, status)
			{
				if(typeof(data.error) != 'undefined')
				{
					if(data.error != '')
					{
						alert(data.error);
					}else
					{
						reloadAttachmentPool(pageId);
					}
				}
			},
			error: function (data, status, e)
			{
				alert(e);
			}
		}
	)

	return false;
	
}

function reloadAttachmentPool(pageId)
{
	$.get("index.php?id="+pageId+"&view=ATTACHMENT_POOL&type=73", function(data)
	{
		$("#tx_modernboard_attachmentPoolAllWrap").empty();
		$("#tx_modernboard_attachmentPoolAllWrap").html(data);
	});
	
}

function ajaxFileDelete(attachmentUid, pageId)
{
    $.ajax({
		type: "POST",
		url: "index.php?eID=tx_modernboard_pi1&action=delete_attachment",
		data: "attachmentUid="+attachmentUid,
		success: function(msg)
		{
			if(msg == 1)
			{
				reloadAttachmentPool(pageId);
			}
            else
            {
            	alert(msg);
            }
		}
	});
}

jQuery.fn.addAttItem = function(formWrapClassName, maxItems, id, image, title) {
	if(this.length >= maxItems)
	{
		alert("max item ("+maxItems+") selected");
		return false;
	}
	var item_exists = false;
	this.each( function() {
		if( $(this).attr("id") == "att_item_"+id )
		{
			alert("item "+id+" allready exists");
			item_exists = true;
			return false;
		}      
	});
	if( item_exists == false )
	{
		$("."+formWrapClassName+" .att_item_wrap .att_item_dummy").clone(true) 
			.appendTo("."+formWrapClassName+" .att_item_wrap")
			.removeClass("att_item_dummy")
			.addClass("att_item")
			.attr("id", "att_item_"+id)
			.show();
		$("."+formWrapClassName+" #att_item_"+id+" .att_title").html(title);
		$("."+formWrapClassName+" #att_item_"+id+" .att_image").attr("src", image);
		$("."+formWrapClassName+" #att_item_"+id+" .att_id").attr("value", id);
		$("."+formWrapClassName+" #att_item_"+id+" .att_deleteLink").click(function(){
			removeAttachmentItem(formWrapClassName, id)
		});
	}
}

function addAttachmentItem(formWrapClassName, maxItems, id, image, title)
{
	$("."+formWrapClassName+" .att_item_wrap .att_item").addAttItem(formWrapClassName, maxItems, id, image, title);
	tb_remove();
}

function removeAttachmentItem(formWrapClassName, id)
{
	$("."+formWrapClassName+" #att_item_"+id).fadeOut("slow", function ()
	{
		$("."+formWrapClassName+" #att_item_"+id).remove();	
	});
}

function addSelectedAttachmentsToForm(formObj, formWrapClassName)
{
	$("."+formWrapClassName+" .att_item_wrap .att_item .att_id").each( function() {
		$(this).clone(true).appendTo(formObj);
	});
	return true;
}

function getSelectedAttachmentsAsPostString(formWrapClassName)
{
	var out = '';
	$("."+formWrapClassName+" .att_item_wrap .att_item .att_id").each( function() {
		out += '&tx_modernboard_pi1[form][attachment_id][]='+$(this).attr("value");
	});
	return out;
}