var jconfirm = {
	message: jQuery('#popup_message'),
	popup: jQuery('#popup_control'),
	success: jQuery('#popup_success'),
	type: 'confirm',
	action: 'jconfirm.popup_hide()',
	
    jconfirm: function (message, action)
    {
    	jconfirm.action = action;
    	if (jconfirm.type != 'confirm')
    	{
    		jconfirm.popup.remove();
    		jconfirm.popup.length = 0;
			jconfirm.type = 'confirm';
    	}
		if (jconfirm.popup.length == 0)
		{
			jconfirm.create_popup_control(message, action);
		}
		else
		{			
			jconfirm.success.unbind("click").click(function () { eval(jconfirm.action);});
			jconfirm.message.html(message);
			jconfirm.popup.show();
			window.scroll(0,0);
		}
    },
    
    jmessage: function (message, action)
    {    	
    	if (jconfirm.type != 'message')
    	{
    		jconfirm.popup.remove();
    		jconfirm.popup.length = 0;
			jconfirm.type = 'message';
    	}
    	if (jconfirm.popup.length == 0)
		{
			jconfirm.create_popup_control(message, action, 'message');
		}
		else
		{			
			jconfirm.success.unbind("click").click(function () { eval(jconfirm.action);});
			jconfirm.message.html(message);
			jconfirm.popup.show();
			window.scroll(0,0);
		}
    },
    
    create_popup_control: function(message, action, type)
    {
		if (type == undefined)
		{
			type = 'confirm';
		}
		if (action != undefined)
		{
			jconfirm.action = action;
		} else {
			jconfirm.action = 'jconfirm.popup_hide()';
		}
		jQuery.post(site_url+'ajax/popup_block.php', { message: message, type: type}, jconfirm.add_popup_to_page, "html");
	},

	add_popup_to_page: function(data)
	{
		jQuery("body").append(data);
		jconfirm.message = jQuery('#popup_message');
		jconfirm.popup = jQuery('#popup_control');
		jconfirm.success = jQuery('#popup_success');
		jconfirm.success.unbind("click").click(function () { eval(jconfirm.action);});
		jconfirm.popup.show();
		window.scroll(0,0);
	},

	popup_hide: function()
	{
		jconfirm.popup.hide();
	},
	
	show_json_message : function(json)
	{
		var data = json;
		jconfirm.jmessage(data.message, data.action);
	}
}