var attachWorks;
function showAttach (icn)
{
	icon = $(icn);
	post = icon.parents('.composePost');
	cell = post.find('.attachWorks');
	attachWorks = cell;
	cell.show();
	label = cell.find('.labelSelectedChange');
	text = cell.find('.textSelectedChange');
	inputDiv = post.find('.divInputAttach');
	button = inputDiv.find('.buttonSubmit');
	button.unbind();
	button.click (checkLink);
	divPicUpload = cell.find('.divPicUpload');
	divPicUpload.hide();
	divVideoUpload = cell.find('.divVideoUpload');
	divVideoUpload.hide();
	input = inputDiv.find('.inputAttach');
	input.val('');
	
	if (icon.hasClass('iconVideo'))
	{
		label.html(instVidLab);
		text.html(instVid);
		button.html(attachButtonSubmit);
		button.unbind();
		button.click(checkYoutubeLink);
		divVideoUpload.show();
		input.focus();

	} else if (icon.hasClass('iconLink'))
	{
		label.html(instLinLab);
		text.html(instLin);
		button.html(attachButtonSubmit);
		button.attr('attachtype','link');
		input.focus();
	} else if (icon.hasClass('iconPicture'))
	{
		label.html(instImgLab);
		text.html(instImg);
		button.html(attachButtonSubmit);
		button.attr('attachtype','imageLink');
		divPicUpload.show();
		input.focus();
	}
}

function cancelAttachWorks (btn)
{
	button= $(btn);
	button.parents('.attachWorks').hide();
}

var savedButtonForBinding;
function checkLink()
{
	var button = $(this);
	savedButtonForBinding = button;
	var type = button.attr('attachtype');
	var text = attachWorks.find('.inputAttach').val();
	
	var inputDiv = attachWorks.find('.divInputAttach');
	var loading = $('#templates  .squareLoading').clone(true);
	loading.addClass('loadingCloned');
	loading.css('display','inline');
	loading.css('margin','0px');
	inputDiv.append(loading);
	button.unbind();
	
	$.ajax({url:'attachment_check.php', type: 'POST', data: ({input:text,type:type}), dataType: 'json', success: addAttachmentRequest, error: connectionFail});
}

function addAttachmentRequest (response,status)
{
	$('.loadingCloned').remove();
	if (status == 'success')
	{
		if (response['success'])
		{
			addAttachment(response);
			return;
		} else {
			errorToConsole ('error 031: query failed');
			return;
		}
	}
	errorToConsole ('error 030: query failed');
}

function addAttachment(array)
{
	var valid = array['linkValid'];
	var title = array['title'];
	var thumbnail = array['thumbnail'];
	var link = array['link'];
	var type = array['type'];
	var errorMessage = array['errorMessage'];
	var divAttachments = attachWorks.parents('.composePost').find('.divAttachments');
	if (valid == 'valid')
	{
		
		var pAttachment = $('#templates > .pAttachment').clone(true);
		var spanType = pAttachment.find('.spanAttachmentType');
		var spanText = pAttachment.find('.spanAttachmentText');
		var checkbox = pAttachment.find('.checkboxAttachment');
		
		var typeToUse = type;
		if (typeToUse == 'imageSrc')
		{
				typeToUse = 'image';
		} else if (typeToUse == 'imageLink')
		{
				typeToUse = 'external image';
		}
		spanType.text(typeToUse);
		spanText.text(title);
		checkbox.attr('attachmentlink',link);
		checkbox.attr('attachmenttype',type);
		checkbox.attr('attachmentthumb',thumbnail);
		checkbox.attr('attachmenttitle',title);
		divAttachments.append(pAttachment);
		if (type != "imageSrc")
		{
			attachWorks.hide();
		}
	}
	else
	{
		messageSpan = attachWorks.find('.newTagError');
		messageSpan.html(errorMessage);
		messageSpan.show();
		messageSpan.fadeOut(4000);
		
		savedButtonForBinding.click(checkLink);
		return;
	}
}

function attachWorksHide()
{
	attachWorks.hide();
}

