/**
	notify = new IntNotify({
		type: 'notify', // lub alert lub confirm
		title: 'czesc',
		text:	'Jakis tekst',
		is_modal: true,
		
		confirm_ok: function()
		{
			alert('dobrze');
		  $(this).IntNotify.get().close();
		},
		
		confirm_cancel: function()
		{
			alert('zle');
		}
	});
	
	notify.close();
*/

var IntNotify = function(options)
{
	if(options.type == undefined)
	{
		options.type = 'notify';
	}
	
	if(options.title == undefined)
	{
		options.title = '';
	}
	
	if(options.text == undefined)
	{
		options.text = '';
	}
	
	if(options.is_modal == undefined)
	{
		options.is_modal = true;
	}
	
	if(options.draggable == undefined)
	{
		options.draggable = false;
	}
	
	if(options.resisable == undefined)
	{
		options.resizable = false;
	}
	
	if(options.confirm_ok == undefined)
	{
		options.confirm_ok = function(){ $(this).IntNotify.get().close(); };
	}
	
	if(options.confirm_cancel == undefined)
	{
		options.confirm_cancel = function(){ $(this).IntNotify.get().close(); };
	}
	
	if(options.confirm_ok_label == undefined)
	{
		options.confirm_ok_label = "TAK";
	}
	
	if(options.confirm_cancel_label == undefined)
	{
		options.confirm_cancel_label = "NIE";
	}
	
	if(options.alert_label == undefined)
	{
		options.alert_label = "OK";
	}
	
	if(IntNotify.rewrite_base == undefined)
	{
		IntNotify.rewrite_base = IntLib.getRB();
		this.rewrite_base = IntNotify.rewrite_base; 
	}
	
	this.options = options;
	
	this.show();
	
	$.extend(this, this.popup);
};

$.fn.IntNotify = function(){};
$.fn.IntNotify.obj;
$.fn.IntNotify.set = function(v)
{
	this.obj = v;
};

$.fn.IntNotify.get = function(v)
{
	return this.obj;
};

/**
 * vars
 */
IntNotify.prototype.options = null;
IntNotify.prototype.rewrite_base = '/';
IntNotify.prototype.popup = null;

/**
 * Methods
 */
IntNotify.prototype.show = function()
{
	switch(this.options.type)
	{
		case 'alert':
			this.buildAlert();
			this._show();
		break;
		
		case 'confirm':
			this.buildConfirm();
			this._show();
		break;
		
		default:
		case 'notify':
			this.buildNotify();
			this._show();
		break;
	}
};

IntNotify.prototype._show = function()
{
	if(this.options.autoshow != false)
		this.popup.dialog('open');
};

IntNotify.prototype.showNotify = function()
{
	this.popup.dialog('open');
};

IntNotify.prototype.close = function()
{
	this.popup.dialog('destroy');
};

IntNotify.prototype.addHTML = function(v)
{
	this.popup.html(v);
};

IntNotify.prototype.buildAlert = function()
{
	this.buildPopup();
	
	
	this.addButton(this.options.alert_label, function() 
	{ 
		$(this).dialog('close'); 
		
		if($(this).IntNotify.get().options.alert_ok != undefined)
		{
			$(this).IntNotify.get().options.alert_ok(this);
		}
	});
};

IntNotify.prototype.buildNotify = function()
{
	this.buildPopup();
	this.popup.dialog('option', 'closeOnEscape', false);
};

IntNotify.prototype.buildConfirm = function()
{
	this.buildPopup();
	
	this.addButton(this.options.confirm_cancel_label, this.options.confirm_cancel);
	this.addButton(this.options.confirm_ok_label, this.options.confirm_ok);
};

IntNotify.prototype.addButton = function(label, callback)
{
	var buttons = this.popup.dialog('option', 'buttons') || {};
	var button = {};
	button[label] = callback;
	
	$.extend(buttons, button);
	
	this.popup.dialog('option', 'buttons', buttons);
};

IntNotify.prototype.buildPopup = function()
{
	
	this.options.text = this.options.text.replace('#loading', '<div style="text-align: center; padding: 10px"><img src="'+this.rewrite_base+'images/jquery_ui/ui-anim_basic_16x16.gif" alt="loading" /></div>');
	
	var  obj = $('<div>'+this.options.text+'</div>');

	var options = {
		modal: this.options.is_modal,
		draggable: this.options.draggable,
		resizable: this.options.resizable,
		autoOpen: false,
		title: this.options.title,
		open: function()
		{
			$('.ui-dialog-titlebar-close').remove();
		},
		width: this.options.width,
		height: this.options.height
	};
	
	$.extend(options, this.options);
	
	obj.dialog(options);	
	
	this.popup = obj;
	this.popup.IntNotify.set(this);
};

