/*
function myload(div, url)
{
	$(div).css("opacity", 0.3);
	$(div).load(url, null, function(){ $(div).css("opacity", 1.0); });
}

function calculateProvision(div, url, input)
{
	clearInterval(window.__copen);
	window.__cdiv = div;
	window.__curl = url;
	window.__cinput = input;
	window.__copen = setInterval(calculateProvision_, 500);
}

function calculateProvision_()
{
	myload(__cdiv, __curl+$('#'+window.__cinput).val());
	clearInterval(window.__copen);
}
*/
function uploadFile(content_id)
{
	if ($("#fileToUpload").val() == "") return false;

	/*
	prepareing ajax file upload
	url: the url of script file handling the uploaded files
		    fileElementId: the file type of input element id and it will be the index of  $_FILES Array()
	dataType: it support json, xml
	secureuri:use secure protocol
	success: call back function when the ajax complete
	error: callback function when the ajax failed

	    */
	$.ajaxFileUpload
	(
	{
		url:'index.php?c=item&m=uploadFile&cid='+content_id+'&ajax=1',
		secureuri:false,
		fileElementId:'fileToUpload',
		dataType: 'json',
		success: function (data, status)
		{
			//data = data[0];
			if(typeof(data.error) != undefined)
			{
				if(data.error != '')
				{
					alert(data.error);
				} else {
					//console.log(data,"2");
					//if (data.msg) alert(data.msg);
					switch(data.type){
						case 'image':
							addImageToForm(content_id, data.fid, data.mini1_filename, data.filename, data.original_filename, data.fileextension, data.delete_confirm_message);
							break;
						case 'datafile':
							addDataFileToForm(content_id, data.fid, data.original_filename, data.filename, data.fileextension, data.delete_confirm_message);
							break;
					}
				}
			}
		},
		error: function (data, status, e)
		{
			alert(e);
		}
	}
	)

	return false;

}

function uploadEmbed(content_id, messages)
{
	//starting setting some animation when the ajax starts and completes
	/*
    $("#loading_embed")
    .ajaxStart(function(){
	$(this).show();
    })
    .ajaxComplete(function(){
	$(this).hide();
    });

    alert($("#embedToUpload").val()+'\n\n'+$("#embedToUploadTitle").val());
    */
	var title = $("#embedToUploadTitle").val();
	var link = $("#embedToUpload").val();
	if (messages.length == 0){
		messages.enter_title = "Enter title";
		messages.enter_link = "Enter link";
	}

	if (title == ""){
		alert(messages.enter_title);
		$("#embedToUploadTitle").focus();
		return false;
	}
	if (link == ""){
		alert(messages.enter_link);
		$("#embedToUpload").focus();
		return false;
	}

	$.ajax
	(
	{
		type: 'POST',
		url:'index.php?c=item&m=uploadEmbed',
		dataType: 'json',
		success: function (data, status)
		{
			if(typeof(data.error) != 'undefined')
			{
				if(data.error != '')
				{
					alert(data.error);
				}else
				{
					addEmbedToForm(data.id, link, title, data.delete_confirm_message)
				}
			}
		},
		error: function (data, status, e)
		{
			alert(e);
		}
	}
	)


	return false;

}

function restoreUploadedFiles(content_id){

	$.ajax
	(
	{
		type: 'POST',
		url:'index.php?c=item&m=restore_uploaded_files',
		data: 'content_id='+content_id,
		dataType: 'json',
		success: function (data, status)
		{
			//alert(typeof(data)+'|'+data.toSource());
			if(typeof(data.error) != 'undefined')
			{
				if(data.error != '')
				{
					alert(data.error);
				}else
				{
					/*
			$.each(data.images,function(){
			    addImageToForm(content_id, this.fid, this.mini1_filename, this.filename, this.fileextension, data.image_delete_confirm_message);
			});
			$.each(data.datafiles,function(){
			    addDataFileToForm(content_id, this.fid, this.filename, this.fileextension, data.datafile_delete_confirm_message);
			});
			*/
					//alert(data.toSource());
					if (data.files){
						$.each(data.files,function(){
							//alert(this.toSource());
							switch(this.type){
								case 'image':
									addImageToForm(content_id, this.fid, this.mini1_filename, this.filename, this.original_filename, this.extension, this.delete_confirm_message);
									break;
								case 'datafile':
									addDataFileToForm(content_id, this.fid, this.original_filename, this.filename, this.extension, this.delete_confirm_message);
									break;
							}
						});
					}
				}
			}

		//alert($("#uploaded_item_images > *").length);
		},
		error: function (data, status, e)
		{
			alert(e);
		}
	}
	)


}

function addImageToForm(content_id, fid, mini_filename, filename, original_filename, fileextension, delete_confirm_message){
	$("#fileToUpload").val(null);
    
	//$("#uploaded_item_images").append("<div id='"+fid+"' style='float:left'><div style='display:block;text-align:center;margin: 2px; padding:2px;'><img src='"+mini_filename+"' alt='img' /></div><div style='display:block;text-align:center;'><img src='images/24-em-cross.png' alt='delete' style='cursor:pointer' title='delete' id='delete_"+fid+"' /></div></div>");
	//$("#uploaded_item_images").append(html);

	target_form_id = "form_add_item";
	var target = document.getElementById(target_form_id);

	var input = document.createElement("input");
	input.setAttribute("name", "attachments[images]["+fid+"][filename]");
	input.setAttribute("id", "file_"+fid);
	input.setAttribute("type", "hidden");
	input.setAttribute("value", filename);
	target.appendChild(input);

	input = document.createElement("input");
	input.setAttribute("name", "attachments[images]["+fid+"][mini_filename]");
	input.setAttribute("id", "file_mini_"+fid);
	input.setAttribute("type", "hidden");
	input.setAttribute("value", mini_filename);
	target.appendChild(input);

	input = document.createElement("input");
	input.setAttribute("name", "attachments[images]["+fid+"][original_filename]");
	input.setAttribute("id", "file_original_"+fid);
	input.setAttribute("type", "hidden");
	input.setAttribute("value", original_filename);
	target.appendChild(input);

	/*
    $("#form_1").append("<input type='hidden' name='attachments[images]["+fid+"][filename]' id='file_"+fid+"' value='"+filename+"' />");
    $("#form_1").append("<input type='hidden' name='attachments[images]["+fid+"][mini_filename]' id='file_mini_"+fid+"' value='"+mini_filename+"' />");
    $("#form_1").append("<input type='hidden' name='attachments[images]["+fid+"][original_filename]' id='file_original_"+fid+"' value='"+original_filename+"' />");
    */
	$.ajax
	(
	{
		type: 'POST',
		url:'index.php?c=item&m=getUploadedImageTemplate',
		data: 'fid='+fid+'&mini_filename='+mini_filename+'&ajax=1',
		dataType: 'json',
		async: false,
		success: function (data, status)
		{

			//alert(data[0].html);
			data = data[0];
			$("#uploaded_item_images").append(data.html);
			$("#delete_"+fid).bind('click', function(e){
				deleteFile(content_id, fid, fileextension, 'image', delete_confirm_message);
			});
		},
		error: function (data, status, e)
		{
			alert(e);
		}
	}
	)
	$("#uploaded_item_images_header").show();
}

function getUploadedImageTemplate(fid, mini_filename){
}

function addDataFileToForm(content_id, fid, original_filename, filename, fileextension, delete_confirm_message){
	$("#fileToUpload").val(null);
	//$("#uploaded_item_datafiles").append("<div id='"+fid+"' style='float:left'><div style='display:block;text-align:center;margin: 2px; padding:2px;'>"+original_filename+"</div><div style='display:block;text-align:center;'><img src='images/24-em-cross.png' alt='delete' style='cursor:pointer' title='delete' id='delete_"+fid+"' /></div></div>");
	/*
    $("#form_1").append("<input type='hidden' name='attachments["+fid+"][filename]' id='file_"+fid+"' value='"+filename+"' />");
    $("#form_1").append("<input type='hidden' name='attachments["+fid+"][type]' id='file_type_"+fid+"' value='datafile' />");
    $("#form_1").append("<input type='hidden' name='attachments["+fid+"][original_filename]' id='file_original_"+fid+"' value='"+original_filename+"' />");
    */

	var target_form_id = "form_add_item";
	var target = document.getElementById(target_form_id);

	var input = document.createElement("input");
	input.setAttribute("name", "attachments[datafiles]["+fid+"][filename]");
	input.setAttribute("id", "file_"+fid);
	input.setAttribute("type", "hidden");
	input.setAttribute("value", filename);
	target.appendChild(input);

	input = document.createElement("input");
	input.setAttribute("name", "attachments[datafiles]["+fid+"][original_filename]");
	input.setAttribute("id", "file_original_"+fid);
	input.setAttribute("type", "hidden");
	input.setAttribute("value", original_filename);
	target.appendChild(input);

	//$("#form_1").append("<input type='hidden' name='attachments[datafiles]["+fid+"][filename]' id='file_"+fid+"' value='"+filename+"' />");
	//$("#form_1").append("<input type='hidden' name='attachments[datafiles]["+fid+"][type]' id='file_type_"+fid+"' value='datafile' />");
	//$("#form_1").append("<input type='hidden' name='attachments[datafiles]["+fid+"][original_filename]' id='file_original_"+fid+"' value='"+original_filename+"' />");
	$.ajax
	(
	{
		type: 'POST',
		url:'index.php?c=item&m=getUploadedDatafileTemplate',
		data: 'fid='+fid+'&original_filename='+original_filename+'&fileextension='+fileextension+'&ajax=1',
		dataType: 'json',
		success: function (data, status)
		{

			//alert(data[0].html);
			data = data[0];
			$("#uploaded_item_datafiles").append(data.html);
			$("#delete_"+fid).bind('click', function(e){
				deleteFile(content_id, fid, fileextension, 'datafile', delete_confirm_message);
			});
		},
		error: function (data, status, e)
		{
			alert(e);
		}
	}
	)
	//$("#uploaded_item_datafiles_header").show();
	$("#uploaded_item_images_header").show();
}

function addEmbedToForm(id, embed, title, delete_confirm_message){
	$("#embedToUpload").val(null);
	$("#embedToUploadTitle").val(null);
	$("#uploaded_item_embed").append("<div id='"+id+"' style='float:left'><div style='display:block;text-align:center;margin: 2px; padding:2px;'>"+embed+"</div><div style='display:block;text-align:center;'><img src='images/24-em-cross.png' alt='delete' style='cursor:pointer' title='delete' id='delete_"+id+"' /></div></div>");
	$("#form_1").append("<input type='hidden' name='attachments[embeds]["+id+"][filename]' id='embed_"+id+"' value='"+embed+"' />");
	$("#form_1").append("<input type='hidden' name='attachments[embeds]["+id+"][original_filename]' id='embed_original_"+id+"' value='"+title+"' />");
	$("#delete_"+id).bind('click', function(e){
		deleteEmbed(id, delete_confirm_message);
	});
}

function deleteFile(content_id, fid, ext, type, confirm_message){
	if (confirm(confirm_message)){
		//starting setting some animation when the ajax starts and completes
		/*
	$("#loading")
	.ajaxStart(function(){
	    $(this).show();
	})
	.ajaxComplete(function(){
	    $(this).hide();
	});
*/
		$.ajax
		(
		{
			type: 'POST',
			url:'index.php?c=item&m=deleteAttachment',
			data: 'content_id='+content_id+'&fid='+fid+'&ext='+ext+'&type='+type,
			dataType: 'json',
			success: function (data, status)
			{
				if(typeof(data.error) != 'undefined')
				{
					if(data.error != '')
					{
						alert(data.msg);
					}else
					{
						$("#"+fid).remove();
						$("#file_"+fid).remove();
						$("#file_type_"+fid).remove();
						$("#file_original_"+fid).remove();
						$("#file_mini_"+fid).remove();
						/*
			if ($("#uploaded_item_images > *").length == 0) $("#uploaded_item_images_header").hide();
			if ($("#uploaded_item_datafiles > *").length == 0) $("#uploaded_item_datafiles_header").hide();
			*/
						if ($("#uploaded_item_images > *").length == 0 && $("#uploaded_item_datafiles > *").length == 0) $("#uploaded_item_images_header").hide();

						alert(data.msg);

					}
				}
			},
			error: function (data, status, e)
			{
				alert(e);
			}
		}
		)
	}

	return false;

}

function deleteEmbed(id, confirm_message){
	if (confirm(confirm_message)){
		$("#loading_embed")
		.ajaxStart(function(){
			$(this).show();
		})
		.ajaxComplete(function(){
			$(this).hide();
		});



		$.ajax
		(
		{
			type: 'POST',
			url:'index.php?c=item&m=getMessage',
			data: 'key=item.embed.delete_message',
			dataType: 'json',
			success: function (data, status)
			{
				if(typeof(data.error) != 'undefined')
				{
					if(data.error != '')
					{
						alert(data.error);
					}else
					{
						$("#"+id).remove();
						$("#embed_"+id).remove();
						$("#embed_original_"+id).remove();
						alert(data.msg);
					}
				}
			},
			error: function (data, status, e)
			{
				alert(e);
			}
		}
		)

	}

	return false;


}

function calculateProvision(price){

	$.ajax
	(
	{
		type: 'POST',
		url:'index.php?c=item&m=calculate_provision',
		data: 'price='+price,
		dataType: 'json',
		success: function (data, status)
		{
			if(typeof(data.error) != 'undefined')
			{
				if(data.error != '')
				{
					alert(data.error);
				}else
				{
					$("#provision_amount_info").html('Koszt dodania ogłoszenia: <b>'+data.provision_add+'</b> zł<br />Prowizja od sprzedaży przedmiotu: <b>'+data.provision_sell+'</b>');
				}
			}
		},
		error: function (data, status, e)
		{
			alert(e);
		}
	}
	)
}


function getAlternativeContent(content_id, language){
	$.ajax
	(
	{
		type: 'POST',
		url:'index.php?c=item&m=getAlternativeContent',
		data: 'content_id='+content_id+'&language='+language,
		dataType: 'html',
		success: function (data, status)
		{
			$("#item_description_alternative_content").html(data);
		},
		error: function (data, status, e)
		{
			alert(e);
		}
	}
	)

	return false;

}


function sortItems(column, direction){
    
}

