var VotingDialog = new VotingDialogImpl();
function VotingDialogImpl() {
	this.stack = new Array();
};
VotingDialogImpl.prototype.create =  function(name,id,pos) {
	var dialog = $('<div id="VotingDialog_'+this.stack.length+'"></div>');
	this.stack.push(dialog);
	dialog.css('position','absolute');
	dialog.css('display','none');
	$('body').prepend(dialog);
	$.get('/votingDialog.do'
			,'id='+id
			,function(html){
				dialog.html(html);
				var img = new Image();
				$(img).load(function () {
					$('#imageLoading',dialog).replaceWith(img);
					var width = dialog.width();
					var height = dialog.height();
					var left = pos[0]-width/2;
					var top = pos[1]-height/2;
					if(top < 10) {
						top = 10;
					} else if(top+height > $(window).height()-10) {
						top = $(window).height()-10-height;
					}
					if(left < 10) {
						left = 10;
					} else if(left+width > $(window).width()-10) {
						left = $(window).width()-10-width;
					}
					dialog.css('left',left);
					dialog.css('top',top);
					dialog.css('display','');
				}).attr('src','/getUserImage.do?name='+name+'&w=500&h=400'); 
				$('[name="close"]:first',dialog).click(function(){
					dialog.hide().remove();
				});
			});
	dialog.draggable( 
	        { 
	            opacity: 0.7
//	            ,handle:    '#layer1_handle' 
	        } 
	    );
};

document.onkeyup  = checkKey;
function checkKey(evt) {
	var e = evt || window.event;
	var keycode = e.keyCode;
//	alert(keycode);
	if(keycode == 27) {
		var dialog = VotingDialog.stack.pop();
		if(dialog) {
			dialog.hide().remove();
		}
	}
}

