// charset=utf-8
// $Id: PinboardDocumentAdder.js 378 2010-04-22 10:00:39Z hebing $
// $HeadURL: svn://svnserver/thielemeyer/2010/plugins/Pinboard_ProductsDb/scripts/PinboardDocumentAdder.js $
// +----------------------------------------------------------------------+
// | mcm board-Plugin                                                     |
// | version 1.0                                                          |
// | for mcm v5.5                                                         |
// | (c) 2002-2007 monsun media (http://www.monsun-media.com)             |
// +----------------------------------------------------------------------+


/**
* mcmPinboardDocumentAdder 
*
* @author	dierker <dierker@monsun-media.com>
* @author	jansen <jansen@monsun-media.com>
* @author	hebing <hebing@monsun-media.com>
*/
var mcmPinboardDocumentAdder = {
	/**
	* add the document to the pinboard
	*
	* @param	Event	evt
	* @param	int		document_id
	* @param	int		product_id
	* @return	void
	*/
	addDocument : function(evt,document_id){
		var x = mcm.getPageX(evt);
		var y = mcm.getPageY(evt);
		var xmlReq = mcm.createXmlHttpRequest();
		xmlReq.onreadystatechange = function(){
			if( xmlReq.readyState==4 ){
				if( xmlReq.status==200 ){
					var message = xmlReq.responseText;
					mcmPinboardDocumentAdder.showConfirmationMessage(x,y,message);
				}
			}
		}
		var servletUrl = 'plugins/Pinboard_ProductsDb/servlet.php?action=addDocument&'+'document_id='+document_id;
		xmlReq.open('GET',servletUrl,true);
		xmlReq.send(null);
		mcm.cancelEvent(evt);
	}
	,
	/**
	* add the product document to the pinboard
	*
	* @param	Event	evt
	* @param	int		document_id
	* @param	int		product_id
	* @return	void
	*/
	addProductDocument : function(evt,document_id,product_id){
		var x = mcm.getPageX(evt);
		var y = mcm.getPageY(evt);
		var xmlReq = mcm.createXmlHttpRequest();
		xmlReq.onreadystatechange = function(){
			if( xmlReq.readyState==4 ){
				if( xmlReq.status==200 ){
					var message = xmlReq.responseText;
					mcmPinboardDocumentAdder.showConfirmationMessage(x,y,message);
				}
			}
		}
		var servletUrl = 'plugins/Pinboard_ProductsDb/servlet.php?action=addDocument&'+'document_id='+document_id+'&'+'product_id='+product_id;
		xmlReq.open('GET',servletUrl,true);
		xmlReq.send(null);
		mcm.cancelEvent(evt);
	}
	,
	/**
	* show a confirmation message
	* @param	int		x
	* @param	int		y
	* @return	void
	*/
	showConfirmationMessage : function(x,y,message){
		var d;
		d = document.getElementById('PinboardConfirmationBox');
		if( d==null ){
			var d = document.createElement('div');
			d.setAttribute('id','PinboardConfirmationBox');
			d.style.visibility = 'hidden';
			document.body.appendChild(d);
		};
		d.innerHTML = message;
		d.style.left = parseInt(x)-parseInt(d.offsetWidth)+'px';
		d.style.top  = parseInt(y)+'px';
		d.style.visibility = 'visible';
		setTimeout(mcmPinboardDocumentAdder.hideConfirmationMessage,2000);
	}
	,
	hideConfirmationMessage : function(){
		var d = document.getElementById('PinboardConfirmationBox');
		d.style.visibility = 'hidden';
	}
}
