/**
taroNotes by John Bakker

**/

		
function taroNotes(cn)
{
	this.cn = cn;
	this.notes = new Array();
	this.config = new Array();
	this.config.noteimage='/tarobly/layouts/note.gif';
	this.config.writebackpath='/tarobly/addnote/datagoeshere';
	this.makeXMLHttpRequest();
	this.canaddnote=false;
}

taroNotes.prototype.init = function(config)
{
	if(typeof config=='object')
	{

		this.config = config;
	}
	else
	{
		//go with default
		this.useingdefaults =true;
	}
	
	
}
taroNotes.prototype.setTarget = function(element,parentelement,data)
{
	
	this.targetelement = document.getElementById(element);
	
	this.oldonclick = this.targetelement.onclick;
	
	eval('this.targetelement.onclick =  function (e){if (!e) var e = window.event;'+this.cn+'.addNote(e)}');
	this.parentelement = document.getElementById(parentelement);
	this.adddata = data;
	this.targetdimensions = this.dimensions(this.targetelement);
	this.canaddnote=true;
}
taroNotes.prototype.recoverTarget = function()
{
	try{
		this.targetelement.onclick =  this.oldonclick;
	}
	catch(e)
	{
		
	}
}
taroNotes.prototype.addNote = function(e)
{
	if(this.canaddnote)
	{
		    mousepos = this.captureMousePosition(e);
		    var title ='';
		    var text ='';
		    title = prompt('Please add note title','');
		    
		    noteposx = mousepos.x- this.targetdimensions.x;
		    noteposy = mousepos.y- this.targetdimensions.y;
		    
		    if((title!=null)&&(title!=''))
		    {
		    	
			    text = prompt('Please add note text','');	
			    if((text!=null)&&(text!=''))
			    {
				   	newnote = this.putnote(noteposx,noteposy,title,text);
				   	
				    if(this.config.writebackpath!='')
				    {
				    	
				    	this.dowriteback(newnote);
				    }
				    this.recoverTarget();
			    }
			    else
			    {
			    	alert('you have to add text');
			    }
			    
		    }
		    else
		    {
		    	alert('you have to add a title');
		    }
	 		
    }
    else
    {
    	
    }
    
    
}
taroNotes.prototype.cleanup = function()
{
	
	if(this.notes.length>0)
	{
		if(this.parentElement)
		{
			for(uu=0;uu<this.notes.length;uu++)
			{
				tmpelem = document.getElementById(this.cn+'note'+uu)
				this.parentElement.removeElement(tmpelement);
			}
		}
		this.notes = new Array();
	}
	else
	{
		this.notes = new Array();
	}
}
taroNotes.prototype.dowriteback = function(id)
{
	writebackpath = this.config.writebackpath.replace(/datagoeshere/,this.adddata);
	this.xmlhttp.open("POST", writebackpath,true);
 	
    eval('this.xmlhttp.onreadystatechange= function() {\n'+this.cn+'.recievenotewriteback();\n }');
	doc = 'title='+encodeURIComponent(this.notes[id].title)+'&text='+encodeURIComponent(this.notes[id].text)+'&xpos='+encodeURIComponent(this.notes[id].x)+'&ypos='+encodeURIComponent(this.notes[id].y)+'';
	this.xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    this.xmlhttp.send(doc);
}

taroNotes.prototype.recievenotewriteback = function()
{
	if (this.xmlhttp.readyState==4) {
		responsetxt = this.xmlhttp.responseText;
		
		alert(responsetxt);
	}
}
taroNotes.prototype.makeXMLHttpRequest = function()
{
	//create a XMLHTTPREQUEST
	if (window.XMLHttpRequest) { // Non-IE browsers
      this.xmlhttp = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE
      this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
}

taroNotes.prototype.showNotes = function()
{
	for(yo=0;yo<this.notes.length;yo++)
	{
		try{
			document.getElementById(this.cn+'note'+yo).style.display='';
		}
		catch(e)
		{
			
		}
	}

}

taroNotes.prototype.hideNotes = function()
{
	
	for(yo=0;yo<this.notes.length;yo++)
	{
		try{
			document.getElementById(this.cn+'note'+yo).style.display='none';
		}
		catch(e)
		{
			
		}
	}

}
taroNotes.prototype.putnote = function(x,y,title,text)
{
	
	
	nl = this.notes.length;
	this.notes[nl]= new Array();
	this.notes[nl].title=title;
	this.notes[nl].text=text;
	this.notes[nl].x=x;
	this.notes[nl].y=y;
	
	this.drawNote(nl);
	return nl;
}
taroNotes.prototype.drawNote = function(id)
{
	newdiv = document.createElement('div');
	newdiv2 = document.createElement('div');
	newdiv.setAttribute('id',this.cn+'note'+id);
	newdiv2.setAttribute('id',this.cn+'notebox'+id);
	
	newdiv.style.position='absolute';
	newdiv.style.zIndex='255';
	newdiv.style.top = (this.notes[id].y)+'px';
	newdiv.style.left = (this.notes[id].x)+'px';
	    			var browserName = navigator.appName;
					if (browserName == "Microsoft Internet Explorer") {	  
	  			    
       			    newdiv.style.filter ='alpha(opacity='+40+')';
       			    
					}
					else
					{
						newdiv.style.opacity =0.4;
					}
	newdiv2.style.position='absolute';
	newdiv2.style.zIndex='255';
	newdiv2.style.top = (this.notes[id].y+20)+'px';
	newdiv2.style.left = (this.notes[id].x+20)+'px';
	newdiv2.style.backgroundColor='#ffffff';
	newdiv2.style.border='1px dashed black';
	newdiv2.style.display='none';
	
	
	newdiv.innerHTML ='<img src="'+this.config.noteimage+'" onMouseOver="'+this.cn+'.showNote(\''+id+'\');" onMouseOut="'+this.cn+'.hideNote(\''+id+'\');">';
	newdiv2.innerHTML ='<span style="font-size:11px;font-weight:bold;color:black;">'+this.notes[id].title+'</span><br/>';
	newdiv2.innerHTML +='<span style="font-size:10px;color:black;">'+this.notes[id].text+'</span>';
	
	this.parentelement.appendChild(newdiv);
	this.parentelement.appendChild(newdiv2);
	
	
}
taroNotes.prototype.showNote = function(id)
{
	document.getElementById(this.cn+'notebox'+id).style.display='block';
}
taroNotes.prototype.hideNote = function(id)
{
	document.getElementById(this.cn+'notebox'+id).style.display='none';
}




taroNotes.prototype.captureMousePosition = function(e) {
    if (document.layers) {
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
    } else if (document.all) {

        xMousePos = window.event.x+document.body.scrollLeft;
        yMousePos = window.event.y+document.body.scrollTop;
        xMousePosMax = document.body.clientWidth+document.body.scrollLeft;
        yMousePosMax = document.body.clientHeight+document.body.scrollTop;
    } else if (document.getElementById) {
        
        xMousePos = e.pageX;
        yMousePos = e.pageY;
        xMousePosMax = window.innerWidth+window.pageXOffset;
        yMousePosMax = window.innerHeight+window.pageYOffset;
    }
    
    mousepos = new Array();
    mousepos.x = xMousePos;
    mousepos.y = yMousePos;
    mousepos.xmax = xMousePosMax;
    mousepos.ymax = yMousePosMax;
    return mousepos
}







/**
to get the dimensions
**/

taroNotes.prototype.getPageOffsetLeft = function(el){
var x;
x=el.offsetLeft;
if(el.offsetParent!=null)x+=this.getPageOffsetLeft(el.offsetParent);
return x;
}
taroNotes.prototype.getPageOffsetTop = function(el){
	var y;y=el.offsetTop;
if(el.offsetParent!=null)y+=this.getPageOffsetTop(el.offsetParent);
return y;
}

taroNotes.prototype._getElementHeight = function(elem)
{
	var h;
	
	if(elem.tagName=='img')
	{
		h = elem.width;
	}
	else
	{
		h = elem.scrollHeight;
		return h;
		if (this.isOpera) { 
			h = elem.style.pixelHeight;
		} else {
			h = elem.offsetHeight;
		}
	}
	return h;


}
taroNotes.prototype._getElementWidth = function(elem)
{
	var w;
	if(elem.tagName=='img')
	{
		w = elem.width;
	}
	else
	{	
		w = elem.scrollWidth;
		return w;
		if (this.isOpera) {
			w = elem.style.pixelWidth;
		} else {
			w = elem.offsetWidth;
		}
	}
	return w;


}
taroNotes.prototype.dimensions = function(el)
{

var dim = new Array();

dim.x = this.getPageOffsetLeft(el);

dim.y = this.getPageOffsetTop(el);

dim.w = this._getElementWidth(el);
dim.h = this._getElementHeight(el);
return dim;


}


_tnotes = new taroNotes('_tnotes');

