function toggleVisibility(namedElement) 
{
	$(namedElement).toggle();
}

function busyArea(n) 
{
	n.innerHTML = '<div style="background:transparent url(/js/ced/img/loading.gif) no-repeat center center"><div style="opacity:0.4;filter:alpha(opacity=40)">'+n.innerHTML+'</div></div>'
}

function getImage(imageId, houseId, userId, timeout) 
{
	busyArea($("houseImage"))

	window.setTimeout(function() 
	{
		ImageService.getImage(imageId, 
	        function (response) 
	        {
	        	$("houseImage").innerHTML = response;
	        }
	    );
	    
	    if (userId > 0)
	    {
		    ImageService.updateComment(imageId, userId,
		    	function (response)
		    	{
		    		if (response != null)
		    		{
			  	        var html = "<a class=\"editComment\" href=\"javascript:toggleVisibility('commentImage" + imageId + "');\">" + housebook_images_notes + "</a>";
			        	html += "<div id=\"commentImage" + imageId + "\" class=\"commentImage\" style=\"display: none;\">";
						html += "<textarea id=\"imageComment\" onkeyup=\"countChars('imageComment', 'charsImage', 500);\">" + response + "</textarea>";
						html += "<p class=\"charsLeft\">" + housebook_chars_remaining + " <span id=\"charsImage\">?</span></p>";
			    		html += "<input type=\"button\" value=\"" + housebook_save + "\" onclick=\"javascript:saveImageComment(" + imageId + ", " + userId + ", 'imageComment', ''); toggleVisibility('commentImage" + imageId + "');\" />";
			    		html += "</div>";
			    		
			    		$("houseImage").innerHTML += html;
			    	}
		    	}
		    );
		}
	}, timeout);
}

function saveImageComment(imageId, userId, from, to)
{
	var comment = $(from).value;
	CommentService.saveImageComment(imageId, userId, comment,
		function (response)
		{
			if (to != '')
				$(to).innerHTML = response;
		}
	);
}

function saveHouseComment(houseId, userId, from, to)
{
	var comment = $(from).value;
	CommentService.saveHouseComment(houseId, userId, comment,
		function (response)
		{
			if (to != '')
			{
				$(to).innerHTML = response;
			}
		}
	);
}

function removeHouseComment(houseId, userId)
{
	CommentService.removeHouseComment(houseId, userId);
}

function removeImageComment(imageId, userId)
{
	CommentService.removeImageComment(imageId, userId);
}

function printPreview(url)
{
	if ($('printOptions') != null)
	{
		var inputs = $('printOptions').getElementsBySelector('input');    
		inputs.each(function(item) {
			if(item.checked)
			{
				url += '&' + item.name + '=' + item.value;
			}
		});
	}

	//alert(url);
	window.open(url, 'Print', 'config=height=500,width=920,scrollbars=1,menubar=1');
}

function countChars(elementId, charsElement, limit)
{
	var remaining = limit - $(elementId).value.length;
	if (remaining < 0)
	{
		$(elementId).value = $(elementId).value.substring(0, limit);
		remaining = 0;
	}
	
	$(charsElement).innerHTML = remaining;
}