function onClickDownload(contentId, curPageURL) {	
	$("#mcontent-wrapper").each(function() {;		
		var p = $("#thumb_" + contentId);
		var position = p.position()
		var left = position.left - 5;
		var top = position.top - 5;
		$(this).css('position', 'absolute').css('left',  left + 'px').css('top', top + 'px');
	});
	
	var postOptions = {
		type: "POST",
		dataType: "json",
		url: "/mcontent/wallpaper/content",
		data: JSON.stringify({
		action: "download",
		id: contentId
		}),
		processData: false,
		success: function(result) {
			if(result.errorCode == 200) {
				var image = document.getElementById("mcontent-download-image");
				MContent.ui.dialog.openDialog();
				image.src = result.src;
			} 
			else {
				if(result.errorCode == 401) {
					alert("กรุณา Login ก่อน ทำการดาวน์โหลดคอนเทนท์");
					window.location.href = "/login.html?url=" + curPageURL;
				}
				else {
					alert("ไม่สามารถดาวน์โหลดได้");
				}  
			}   
		},
		error: function(xmlHttpRequest, textStatus, errorThrown) {
			alert("ไม่สามารถดาวน์โหลดได้ " + "\n" + "Error with message " + textStatus  + ", " + errorThrown);
		}
	};
	$.ajax(postOptions);
}

registerNS("MContent.ui.dialog");
MContent.ui.dialog.openDialog = function() {
	$("#mcontent-wrapper").fadeIn(300);
	$.modal("<div></div>", 
				{containerId: 'mcontent-modal-container', 
				overlayId: 'mcontent-modal-overlay' , 
				close: false});		
}
		
MContent.ui.dialog.closeDialog = function(){	
	$("#mcontent-wrapper").hide();
	$.modal.close(); 
}

$(document).ready(function() {
	$("#mcontent-wrapper #close_cmd").bind("click", function(event) {
		event.preventDefault();
	    MContent.ui.dialog.closeDialog();
	});
});