var lukas = {};

lukas.Simulator = function(opt)
{
	var m_this = this, m_button, m_info, m_link, m_price_min, m_price_max, m_price,
		m_price_change_callback, m_active, m_info_text, m_price_base, m_show_info,
		m_enabled, m_basket;

	var m_options = {
		price_min: 500,
		price_max: 10000,
		on_price_change: function() {},
		price_base: 0,
		show_info: false,
		enabled: true,
		basket: false
	};

	function _construct(opt)
	{
		m_button = jQuery(opt.button);
		m_info = jQuery(opt.info);
		m_link = opt.link;
		m_price_min = opt.price_min;
		m_price_max = opt.price_max;
		m_price_change_callback = opt.on_price_change ? opt.on_price_change : function() {};
		m_price_base = opt.price_base;
		m_show_info = opt.show_info;
		m_active = false;
		m_enabled = opt.enabled;
		m_basket = opt.basket;
		m_info_text = "";

		m_button.click(_showSimulator);
	}

	this.setPrice = function(price)
	{
		m_price = price;
		_update();
	}

	this.disable = function()
	{
		m_enabled = false;
		_update();
	}

	this.enable = function()
	{
		m_enabled = true;
		_update();
	}

	this.isEnabled = function()
	{
		return m_enabled
	}

	this.getPriceBase = function()
	{
		return m_price_base;
	}

	this.isActive = function()
	{
		return m_active;
	}

	this.getInfoText = function()
	{
		return m_info_text;
	}

	function _showSimulator()
	{
		window.open(m_link + m_price, "", "width=840,height=600,resizable=yes,scrollbars=yes,status=yes");
		return false;
	}

	function _update()
	{
		if (!m_enabled)
		{
			if (m_basket)
			{
				m_info_text = "Jeden lub więcej produktów w koszyku nie może być kredytowanych w systemie LUKAS Raty.";
			}
			else
			{
				m_info_text = "Zakup tego towaru nie może być finansowany w systemie ratalnym LUKAS Raty.";
			}
			m_info.text(m_info_text);
			m_button.hide();
			m_info.show();
		}
		else if (m_price < m_price_min)
		{
			m_active = false;
			if (m_show_info)
			{
				m_info_text = "Zakupy w systemie ratalnym LUKAS Raty dostępne są od " +
					m_price_min + " zł wartości całego zamówienia.";
			}
			m_info.text(m_info_text);
			m_button.hide();
			m_info.show();
			m_price_change_callback(m_this);
		}
		else if (m_price > m_price_max)
		{
			m_active = false;
			if (m_show_info)
			{
				m_info_text = "Zakupy w systemie ratalnym LUKAS Raty dostępne są do " +
					m_price_max + " zł wartości całego zamówienia.";
			}
			m_info.text(m_info_text);
			m_button.hide();
			m_info.show();
			m_price_change_callback(m_this);
		}
		else
		{
			m_active = true;
			m_info_text = "";
			m_info.hide();
			m_button.show();
			m_price_change_callback(m_this);
		}
	}

	_construct(jQuery.extend(m_options, opt));
}
