$(document).ready(function() {
	
	$(document).on('click', '.export', function() {
		var id = $(this).data('id');
		var type = $(this).data('type');

		response = AjaxSubmit({
			type: type,
			id: id,
			href: "/index.php?m=bildarchiv&sm=image&op=set_export&image_id="+id+"&act="+type
		});
		
		if ( response.success )
		{
			if ( type == "add_export")
			{
				$(this).find('img').attr("src","/images/bildarchiv/partner_active.png");
				$(this).find('img').attr("title","Vom Export entfernen");
				$(this).data('type',"drop_export");
			} else {
				$(this).find('img').attr("src","/images/bildarchiv/partner_inactive.png");
				$(this).data('type',"add_export");
				$(this).find('img').attr("title","Zum Export hinzufügen");
			}
		}
			
		/*
		$.ajax(
		{
			url:"/index.php?m=bildarchiv&sm=image&op=set_export&image_id="+id+"&act="+type,
			method:"POST",
			data: {
				act: type,
				id: id
			},
			success:function(data)
			{
				alert(data);	 
				//alert("ja");
				//$(this).data('type',10);
				
				
			},
			error:function(data)
			{
				alert("nein");
			}
		});
		*/
		return false;
	});	
	
	
	$(document).on('click', '.highlight', function() {

		var id = $(this).data('id');
		var type = $(this).data('type');

		response = AjaxSubmit({
			type: type,
			id: id,
			href: "/index.php?m=bildarchiv&sm=image&op=set_highlight&image_id="+id+"&act="+type
		});
		
		if ( response.success )
		{
			if ( type == "add_highlight")
			{
				$(this).find('img').attr("src","/images/bildarchiv/highlight_active.png");
				$(this).find('img').attr("title","Kein Highlight mehr");
				$(this).data('type',"drop_highlight");
			} else {
				$(this).find('img').attr("src","/images/bildarchiv/highlight.png");
				$(this).data('type',"add_highlight");
				$(this).find('img').attr("title","Zum Highlight machen");
			}
		}
		return false;
	});	
})


function AjaxSubmit(params) {

	var returnvar = '';
	var url = '/ajr';

	if (params.href)
	{
		url = params.href;
		delete params.href;
	}

	$.ajax(
	{
		type: "POST",
		url: url,
		async: false,
		data: params,
		datatype: "JSON",
		
		success: function(data) 
		{
			//alert(data);
			if (data)
			{
				if (data.slice(0, 1) == '{')
				{
					returnvar = jQuery.parseJSON(data);
				} else {
					returnvar = jQuery.parseJSON( '{"error":"parse_error"}' );
				}
				//} else { returnvar = jQuery.parseJSON( '{"error":"parse_error"}' ); }
			}
		},
		error: function(err) 
		{			
			returnvar = jQuery.parseJSON( '{"error":"error_request"}' );
		}
	});
	return returnvar;
	
}