function gbppdb_notepad_button(conf)
{
	var _this = this;
	this.cdef = function (val,def){return(val==null)?def:val};
	
	this.icon_add = this.cdef(conf.icon_add,'add.png');
	this.text_add = this.cdef(conf.text_add,'add Product to Notepad');
	this.icon_delete = this.cdef(conf.icon_delete,'delete.png');
	this.text_delete = this.cdef(conf.text_delete,'delete Product from Notepad');
	this.parent = this.cdef(conf.parent,null);
	this.product_id = this.cdef(conf.product_id,0);
	this.requestURL = this.cdef(conf.requestURL,'index.php?eID=tx_gbppdb');
	this.id = null;
	this.inprogress = false;
	this.isInit = false;
	
	this.init = function()
	{
		this.id = 'notepadbutton_'+this.product_id;
		document.write('<img id="'+_this.id+'" src="" alt="" title ="" />');
		var t=this;
		$('#'+_this.id).click(function()
				{
				
				$.ajax({
					type: "POST",
				   	url: _this.requestURL,
				   	data: 'tx_gbppdb_eID_handle[controller]=NotePadWidgetAjax&tx_gbppdb_eID_handle[action]=p_toggle&tx_gbppdb_eID_handle[uid]='+_this.product_id,
				   	success: function(msg)
				   	{
						if(!_this.inprogress)_this.getStatus();
				   	}
				});
			}
		);
		this.getStatus();
	};
	
	this.getStatus = function()
	{
		if(this.inprogress)return;
		this.inprogress = true;
		var t=this;
		$.ajax(
		{
				type: "POST",
			   	url: this.requestURL,
			   	data: 'tx_gbppdb_eID_handle[controller]=NotePadWidgetAjax&tx_gbppdb_eID_handle[action]=p_status&tx_gbppdb_eID_handle[uid]='+_this.product_id,
				success: function(msg){
					var json = eval('('+msg+')');
					$('#'+_this.id).attr(
						{
							"alt":(json.active?_this.text_delete:_this.text_add),
							"title":(json.active?_this.text_delete:_this.text_add),
							"src":(json.active?_this.icon_delete:_this.icon_add)
						}
					)
					if((notepad) && _this.isInit)notepad.update();
					if(!_this.isInit)_this.isInit = true;
					_this.inprogress = false;
				}
		});
	};
	
	this.init();
}

function gbppdb_notepad()
{
	var _this = this;
	this.inprogress = false;
	
	this.init = function(conf)
	{
		this.setDefaults(conf);
		var content = '<span id="'+this.id+'"></span>';
		document.write(this.Wrap(content,this.listWrap));
		this.update();
	};
	
	this.update = function()
	{
		if(this.inprogress)return;
		this.inprogress = true;
		$.ajax({
			type:"POST",
			url: this.requestURL,
		   	data: 'L='+this.L+'&tx_gbppdb_eID_handle[controller]=NotePadWidgetAjax&tx_gbppdb_eID_handle[action]=item_list',
			success: function (msg)
			{
				var json = eval(msg);
				$('#'+_this.id_notepadlink).hide();
				$('#'+_this.id).hide();
				$('#'+_this.id).empty();
				_this.inprogress = false;
				if(!json)return;
				for(var i=0;i<json.length;i++)
				{
					var item = '<span id="'+_this.id+'_'+i+'" rel="'+json[i].uid+'" ><a href="'+_this.isoL+'products/product/'+json[i].url+'" >'+json[i].name+'</a></span>';
					$('#'+_this.id).append(_this.Wrap(item,_this.wrap));
					/*$('#'+_this.id+'_'+i).click(function(){
						location.href = +$(this).attr('rel');
					});*/
				}
				$('#'+_this.id).fadeIn(200);
				if(json.length)$('#'+_this.id_notepadlink).fadeIn(200);
			}
		});
	};
	
	this.Wrap = function(content,wrapper)
	{
		return wrapper.split('|').join(content);
	}
	
	this.setDefaults = function(conf)
	{
		this.requestURL = conf.requestURL || 'index.php?eID=tx_gbppdb';
		this.wrap = conf.wrap || '|';
		this.listWrap = conf.listWrap||'|';
		this.id = conf.id || 'notepad_inst';
		this.id_notepadlink = conf.id_notepadlink || 'sendnotepad';
		this.L  = conf.L||'0';
		this.isoL = conf.isoL||'';
	};
}

var notepad = new gbppdb_notepad();
