// * ********* ********* ********* ********* *********
// * Sticky Note Popup
// * iMarketingTool.com software
// * http://www.imarketingtool.com
// * (c)2000 Dan Shipe 
// *  danshipe@imarketingtool.com
// * rivisited by Sandro Del Bello
// * ********* ********* ********* ********* *********
// * useage...
// * 
// * <script type="text/javascript" src='jsStickyNote.js'></script>
// * <script type="text/javascript">
// * var objStickyNote1 = new fpuObject();
// * objStickyNote1.left = -100;
// * objStickyNote1.html = "<span style=\"font: 12pt verdana;\">My sticky note html message</span>";
// * objStickyNote1.create();     *** non sempre va messa: a volte crea un doppione***
// * </script>
// * ********* ********* ********* ********* *********
// * PROPERTY          TYPE     DEFAULT               DESCRIPTION
// * allowDrag         bool     true                  allow dragging of popup	
// * backgroundImage   string   images/postit75.gif   background-image style of popup div
// * closeButton       bool     true                  show or hide the close "x"
// * cssClass	       bool     true                  apply a css class to entire popup (good for font)
// * divID	       string   null                  id of existing DIV to use as the popup
// *                                                     use divID or html; if both will user divID
// * finalTop          int      window center         initial top of the popup div
// *                                                     when != top, popup will move to finalTop
// * finalLeft         int      window center         initial left of the popup div
// *                                                     when != top, popup will move to finalLeft
// * height            int      342                   pixel height of the popup div
// * html              string   ...                   HTML text to be displayed in popup div
// *                                                     use divID or html; if both will user divID
// * left              int      window center         initial left of the popup div
// * name              string   StickyNotePopup       unique name of the popup object						
// *                                                     useful for drag debug if m_blnFPUDebug=true 
// * padding           int      30                    padding of popup div
// * top               int      window center         initial top of the popup div
// * watermark         bool 	true                  re-center popup on scroll
// * width             int      305                   width of the popup div
// * ********* ********* ********* ********* *********

var m_blnFPUDebug = false;
var m_objFPUActivePopup;
var m_arrFPUObjects = new Array();
var m_arrFPUMovingObjects = new Array();
var m_intFPUMoveInterval = 1;

fpuAttachEventOnload(fpuInitialize);

// ********* ********* ********* ********* *********
// OnLoad FUNCTIONS
// ********* ********* ********* ********* *********

// * ********* ********* ********* ********* ********* 
function fpuAttachEventOnload(FNT) {

	if(typeof window.addEventListener != "undefined") {
		// moz, saf1.2, ow5b6.1
		window.addEventListener("load", FNT, false);
	} else if (typeof document.addEventListener != "undefined") {
		// MSN/OSX, op7.50, saf1.2, ow5b6.1
		document.addEventListener("load", FNT, false);
	} else if (typeof window.attachEvent != "undefined") {
		// ie5.0w, ie5.5w, ie6w
		window.attachEvent("onload", FNT);
	} else {
		// all other browsers
		if (typeof window.onload == "function") {
			var existingCode = onload;
			window.onload = function() {
				existingCode();
				FNT();
			};
		} else {
				window.onload = FNT;
		};
	};
};

// * ********* ********* ********* ********* ********* 
function fpuInitialize()
{
	// create all the necessary PostIt note DIVs
	// if we don't wait until the page is loaded,
	// MSIE will crash when trying to AppendNode
	for (var i=0; i<m_arrFPUObjects.length; i++)
	{	
		var obj = m_arrFPUObjects[i];
		obj.create();	
	}	


	// watermark
	window.onscroll = function() { fpuRecenter(); };
	window.onresize = function() { fpuRecenter(); };

	// begin moving timer
	this.moveTimer = setTimeout(fpuMove, m_intFPUMoveInterval);
};

// ********* ********* ********* ********* *********
// MAIN OBJECT
// ********* ********* ********* ********* *********

// ********* ********* ********* ********* *********
function fpuObject()
{
	this.name = "PostIt";
	this.allowDrag = true;
	this.divID = null;
	this.closeButton = true;
	this.cssClass = null;
	this.watermark = false;  // watermark not working yet

	this.backgroundImage = "PostItYellow.gif";
	this.backgroundRepeat = "no-repeat";
	this.padding = 30;
	this.html = "PostIt Popup Note<br/>http://www.imarketingtool.com";

	// positioning properties

	this.height = 342;   //altezza dello scritto - dato originale=342
	this.width = 305;    //larghezza dello scritto - dato originale =305

	this.top = parseInt((document.body.clientHeight - this.height)/2, 10);
	this.left = parseInt((document.body.clientWidth - this.width)/2, 10);
	this.finalTop = parseInt((document.body.clientHeight - this.height)/2, 10);
	this.finalLeft = parseInt((document.body.clientWidth - this.width)/2, 10);

	// set DRAG properties
	this.inDrag = false;
	this.elStartLeft = 0;
	this.elStartTop = 0;
	this.cursorStartX = 0;
	this.cursorStartY = 0;

	// add object to global array
	m_arrFPUObjects[m_arrFPUObjects.length] = this;
	m_arrFPUMovingObjects[m_arrFPUMovingObjects.length] = this;
};

// ********* ********* ********* ********* *********
fpuObject.prototype.create = function()
{

	// create the actual DIV element
	// or grab a existing DIV 
	var objElement;
	if (this.divID)
		objElement = document.getElementById(this.divID);
	else
		objElement = fpuAppendNode(document.body, "div", "");	

	this.element = objElement;
	objElement.parentObject = this;

	if (this.cssClass)
		fpuAppendAttribute(objElement, "class", this.cssClass);

	objElement.style.backgroundImage = "url('" + this.backgroundImage + "')";
	objElement.style.backgroundRepeat = this.backgroundRepeat;
	objElement.style.padding = this.padding;

	objElement.style.position = "absolute";
	objElement.style.top = this.top;
	objElement.style.left = this.left;
	objElement.style.width = this.width;
	objElement.style.height = this.height;
	//objElement.style.border = "1px solid #000000";

	if (this.allowDrag)
		objElement.onmousedown = fpuDragStart;
	objElement.move = fpuMove;

	// create close button
	if (this.closeButton) {

		var objClose;
		if (this.divID)
			objClose = objElement.getElementsByTagName("div")[0];
		else
			objClose = fpuAppendNode(objElement, "div", "");
		objClose.style.width = 10+this.width;  //posizione del pulsante chiusura - originale ="320px";  
		objClose.style.textAlign = "right";
		objClose.parentObject = this;
		objClose.innerHTML = "<br/>";
		
		var objX = fpuAppendNode(objClose, "span", "");
		//objX.style.border = "1px solid #cccccc";
		objX.style.font = "12pt verdana";
		objX.innerHTML = "&nbsp;&#x00d7;&nbsp;";
		objX.onclick=fpuDestroy;
		objX.parentObject = this;
	}


	// append content
	if (!this.divID)
	{
		var objContent = fpuAppendNode(objElement, "div", "");
		objContent.innerHTML = this.html;	
	}	

	return objElement;	
};

// ********* ********* ********* ********* *********
function fpuDestroy() {this.parentObject.destroy();};
fpuObject.prototype.destroy = function()
{
	//fpuShowProperties(this);
	//fpuShowProperties(this.element);
	//document.body.removeChild(this.element);
	this.element.style.display = "none";	
};

// ********* ********* ********* ********* *********
// MOVE FUNCTIONS
// ********* ********* ********* ********* *********

function fpuMove() 
{
	for (var i=0; i<m_arrFPUMovingObjects.length; i++)
	{	
		var obj = m_arrFPUMovingObjects[i];
		if (obj)
		{
			var blnRemove = !(m_arrFPUMovingObjects[i].move());
			if (blnRemove)
				obj = null;
		};			
	};
	setTimeout(fpuMove, m_intFPUMoveInterval);
};

fpuObject.prototype.move = function()
{
	// if inDrag then stop moving
	if (this == m_objFPUActivePopup)
		return false;	

	var blnContinue = false;

	// move vertically
	if (this.top != this.finalTop)
		blnContinue = true;
	if (this.top > this.finalTop)
		this.top -= 1;
	if (this.top < this.finalTop)
		this.top += 1;

	// move horizontally
	if (this.left != this.finalLeft)
		blnContinue = true;
	if (this.left > this.finalLeft)
		this.left -= 1;
	if (this.left < this.finalLeft)
		this.left += 1;

	// redraw & continue timer looping
	if (blnContinue)
	{
		var objPopup = this;
		//objPopup.element.style.display="none";
		objPopup.element.style.top = objPopup.top + "px";
		objPopup.element.style.left = objPopup.left + "px";
		//objPopup.element.style.display="block";
	};
	
	return blnContinue;
};

// ********* ********* ********* ********* *********
// DRAG FUNCTIONS
// ********* ********* ********* ********* *********

// ********* ********* ********* ********* *********
function fpuDragStart(event) {this.parentObject.dragStart(event);};
fpuObject.prototype.dragStart = function(event)
{
	var objElement = this.element;
	var objPopup = this;
	var x, y;
	var i;

	if (m_blnFPUDebug) window.status = objPopup.name;


	// activate window
	if (m_objFPUActivePopup) m_objFPUActivePopup.element.style.zIndex=9;
	m_objFPUActivePopup = objPopup;
	m_objFPUActivePopup.element.style.zIndex=10;

	// Get cursor position with respect to the page.
	if (window.event) {
		x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
		y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
	} else {
		x = event.clientX + window.scrollX;
		y = event.clientY + window.scrollY;
	};
	
	objPopup.cursorStartX = x;
	objPopup.cursorStartY = y;
	objPopup.elementStartX = parseInt(objPopup.element.style.left, 10);
	objPopup.elementStartY = parseInt(objPopup.element.style.top, 10);
	if (isNaN(objPopup.elementStartX)) objPopup.elementStartX = 0;
	if (isNaN(objPopup.elementStartY)) objPopup.elementStartY = 0;

	// Capture mousemove and mouseup events on the page.
	if (window.event) {
		document.attachEvent("onmousemove", fpuDragGo);
		document.attachEvent("onmouseup", fpuDragStop);
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	} else {
		document.addEventListener("mousemove", fpuDragGo, true);
		document.addEventListener("mouseup", fpuDragStop, true);
		event.preventDefault();
	};
};

// ********* ********* ********* ********* *********
function fpuDragGo(event) { m_objFPUActivePopup.dragGo(event); };
fpuObject.prototype.dragGo = function(event)
{
	var objElement = this.element;
	var objPopup = this;
	var x, y;

	// Get cursor position with respect to the page.
	if (window.event) {
		x = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
		y = window.event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
	} else {
		x = event.clientX + window.scrollX;
		y = event.clientY + window.scrollY;
	};

	// Move drag element by the same amount the cursor has moved.
	objElement.style.left = (this.elementStartX + x - this.cursorStartX) + "px";
	objElement.style.top = (this.elementStartY + y - this.cursorStartY) + "px";

	if (window.event) {
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	} else {
		event.preventDefault();
	};

	objPopup.inDrag = true;
};

// ********* ********* ********* ********* *********
function fpuDragStop(event) { m_objFPUActivePopup.dragStop(event); };
fpuObject.prototype.dragStop = function(event)
{
	var objElement = this.element;
	var objPopup = this;

	if (m_blnFPUDebug) window.status = "";

	// Stop capturing mousemove and mouseup events.
	if (window.event) {
		document.detachEvent("onmousemove", fpuDragGo);
		document.detachEvent("onmouseup", fpuDragStop);
	} else {
		document.removeEventListener("mousemove", fpuDragGo, true);
		document.removeEventListener("mouseup", fpuDragStop, true);
	};

	objPopup.inDrag = false;
};


// ********* ********* ********* ********* *********
// WATERMARK
// ********* ********* ********* ********* *********

function fpuRecenter() {
	for (var i=0; i<m_arrFPUObjects.length; i++)
	{	
		var obj = m_arrFPUObjects[i];
		if (obj.watermark)
			//if (obj.top == obj.finalTop && obj.left == obj.finalLeft)
				fpuWatermark(obj);
	
	}
} 

function fpuWatermark(objPopup) {

    	var winWidth
	var winHeight
	var scrLeft
	var scrTop;
  
  	if (self.innerHeight) 
    	{
		// all except Explorer
        	winWidth = self.innerWidth;
        	winHeight = self.innerHeight;
        	scrLeft = self.pageXOffset;
        	scrTop = self.pageYOffset;	        
	}
    	else if (document.documentElement && document.documentElement.clientHeight)
    	{
	        // Explorer 6 Strict Mode
        	winWidth = document.documentElement.clientWidth;
        	winHeight = document.documentElement.clientHeight;
        	scrLeft = document.documentElement.scrollLeft;
        	scrTop = document.documentElement.scrollTop;	        
   	}
    	else if (document.body) 
    	{
		// other Explorers
        	winWidth = document.body.clientWidth;
        	winHeight = document.body.clientHeight;
        	scrLeft = document.body.scrollLeft;
       		scrTop = document.body.scrollTop;
    	}	

        objPopup.left += scrLeft;
        objPopup.top += scrTop;


        objPopup.finalLeft += scrLeft;
        objPopup.finalTop += scrTop;

    
	objPopup.element.style.top = objPopup.top + "px";
	objPopup.element.style.left = objPopup.left + "px";
}


// ********* ********* ********* ********* *********
// NODE FUNCTIONS
// ********* ********* ********* ********* *********

// ********* ********* ********* ********* *********
function fpuAppendNode(objParent, strName, strValue) 
{
	var objDoc = objParent.ownerDocument;
	if (!objDoc) objDoc = objParent;
	var objNode = objDoc.createElement(strName);
	if (strValue) {
		var objNodeText = objDoc.createTextNode(strValue);
		objNode.appendChild(objNodeText);
	};
	objParent.appendChild(objNode);
	return objNode;
};

// ********* ********* ********* ********* *********
function fpuAppendAttribute(objParent, strName, strValue) 
{
	var objDoc = objParent.ownerDocument;
	if (!objDoc) objDoc = objParent;
	var objNode = objDoc.createAttribute(strName);
	if (strValue) objNode.nodeValue = strValue;
	objParent.attributes.setNamedItem(objNode);
};

// ********* ********* ********* ********* *********
// DEBUG
// ********* ********* ********* ********* *********
 
// ********* ********* ********* ********* *********
function fpuOutput(text) {
	var obj; 
	if (document.createElement && (obj = document.createElement('textarea'))) {
		fpuAppendAttribute(obj, "rows", "5");
		fpuAppendAttribute(obj, "cols", "80");
		obj.appendChild(document.createTextNode(text));
		document.body.appendChild(obj);
	};
};

// ********* ********* ********* ********* *********
function fpuShowProperties(obj) {
	var i;
	var strOutput;
	var strTerm;
	var strData;
	var strSpace;
	var lngCols;
	var lngMaxLen = 100;
	
	// initialize number of columns
	lngCols=3;
	if (document.layers) lngCols=3;

	// initialize an empty string
	strSpace = "                                        ";
	
	strOutput = obj.name + "\n\n";
	i=1;
	for (var prop in obj) {
		strTerm = "\n";
		if (i==lngCols) strTerm = "\n";

		i=i+1;
		if (i>lngCols) i=1;	

		strData = "." + prop + " = " + obj[prop];
		if (strData.length<lngMaxLen) strData += strSpace.substring(0, lngMaxLen-strData.length);
		if (strData.length>lngMaxLen) strData = strData.substring(0, lngMaxLen);

		strOutput += strData + strTerm;
	};	
	fpuOutput(strOutput);
	return true;
};

// ********* ********* ********* ********* *********
// TIPSHOW FUNCTION by Sandro Del Bello
// ********* ********* ********* ********* *********

function tipshow(back,msgTitle,msgTxt,largo,alto)
{
var objStickyNote1 = new fpuObject();
objStickyNote1.backgroundImage=back
objStickyNote1.width = largo; 
objStickyNote1.height = alto;
objStickyNote1.html = "<span style='color: #ff0000; font: bold 16pt comic sans ms;'>"+msgTitle+"</span>"+msgTxt;
objStickyNote1.create();
};
