//
//  This script was created
//  by Mircho Mirev
//  mo /mo@momche.net/
//
//	:: feel free to use it BUT
//	:: if you want to use this code PLEASE send me a note
//	:: and please keep this disclaimer intact
//

/*
Object.prototype.createContext = function( sMethod )
{
	var hNewContext = this
	return function() { hNewContext[ sMethod ].apply( hNewContext, arguments ) }
}
*/

function createContext( hObject, sMethod )
{
	var hNewContext = eval( hObject )
	return function() { hNewContext[ sMethod ].apply( hNewContext, arguments ) }
}

cPageNote.createNote = function( sNoteId, nLeft, nTop, nWidth, nHeight, nZIndex, sText )
{
	var hNewNote = document.createElement( 'div' )
	hNewNote.className = 'note'
	var sLinkVisible = 'visible'

	if( sNoteId == null )
	{
		sNoteId = cPageNote.CS_NOTE_ID_PREFIX + ( cPageNote.nNotesCount++ )
		while( document.getElementById( sNoteId ) )
		{
			sNoteId = cPageNote.CS_NOTE_ID_PREFIX + ( cPageNote.nNotesCount++ )
		}
		sLinkVisible = 'hidden'
	}

	hNewNote.id = sNoteId

	hNewNote.setAttribute( 'dragable', 'true' )
	hNewNote.setAttribute( 'resizable', 'true' )
	hNewNote.setAttribute( 'onmove', 'cPageNote.onNoteResizerMove(this)' )
	hNewNote.setAttribute( 'onresize', 'cPageNote.onNoteResizerResize(this)' )

	cDomEvent.addEvent( hNewNote, 'mouseover', cPageNote.onNoteOver )
	cDomEvent.addEvent( hNewNote, 'mouseout', cPageNote.onNoteOut )

	var hTextArea = document.createElement( 'textarea' )
	hTextArea.className = 'draghandle'

	hNewNote.appendChild( hTextArea )

	document.body.appendChild( hNewNote )

	hNewNote.style.left = nLeft + 'px'
	hNewNote.style.top = nTop + 'px'
	if( nWidth != null )
	{
		hNewNote.style.width = nWidth + 'px'
	}
	if( nHeight != null )
	{
		hNewNote.style.height = nHeight + 'px'
	}
	hNewNote.style.zIndex = nZIndex != null ? nZIndex : cPageNote.nNotesCount

	hTextArea.style.left = '3px'
	hTextArea.style.width = cDomObject.getWidth( hNewNote ) - 8 + 'px'
	hTextArea.style.height = cDomObject.getHeight( hNewNote ) - 20 + 'px'
	hTextArea.value = sText

	cDomEvent.addEvent( hTextArea, 'change', cPageNote.onNoteTextChange )
	cDomEvent.addEvent( hTextArea, 'blur', cPageNote.onNoteTextChange )
	cDomEvent.addEvent( hTextArea, 'mouseover', cPageNote.onNoteOver )

	var hCloseLink = document.createElement( 'a' )
	hCloseLink.href = '#'
	hCloseLink.appendChild( document.createTextNode( 'delete' ) )
	hCloseLink.className = 'close'
	hNewNote.appendChild( hCloseLink )
	if( sNoteId != null )
	{
		hCloseLink.style.visibility = sLinkVisible
	}
	cDomEvent.addEvent( hCloseLink, 'click', cPageNote.onDeleteNote )

	var hResizer = new cElementResizer( sNoteId )

	return new cPageNote( hNewNote )
}


function cPageNote( hNoteElement )
{
	this.sNoteId = hNoteElement.id
	hNoteElement.pageNote = this
}

cPageNote.prototype.createContext = function( sMethod )
{
	var hNewContext = this
	return function() { hNewContext[ sMethod ].apply( hNewContext, arguments ) }
}

cPageNote.CN_SERIALIZE_TIMEOUT = 2000
cPageNote.CS_NOTE_ID_PREFIX = 'pageNote'
cPageNote.nNotesCount = 0
cPageNote.CS_WAIT_MESSAGE = 'Please wait...'
cPageNote.CS_NEW_NOTE = ''
cPageNote.CS_NEW_NOTE_TEXT = 'Drag me! Give me a reason to exist!'
cPageNote.nNotesToSave = 0
cPageNote.hNotesToSavePool = new Object()

cPageNote.prototype.onSerialize = function()
{
	if( this.hServerRequest.readyState == 4 )
	{
		this.onNoteSaved()
	}
}

cPageNote.prototype.serializeNote = function()
{
	if( !this.hServerRequest )
	{
		this.hServerRequest = XmlHttp.create()
	}
	this.hServerRequest.open( 'POST', 'save.php', true )
	this.hServerRequest.setRequestHeader( 'Content-type', 'application/x-www-form-urlencoded' )

	this.hServerRequest.onreadystatechange = this.createContext( 'onSerialize' )

	var hNoteElement = document.getElementById( this.sNoteId )
	if( hNoteElement )
	{
		var hTextArea = hNoteElement.childNodes[ 0 ]

		var sRequest = ''
		sRequest += 'noteid=' + this.sNoteId
		sRequest += '&left=' + parseInt( hNoteElement.style.left )
		sRequest += '&top=' + parseInt( hNoteElement.style.top )
		sRequest += '&width=' + cDomObject.getWidth( hNoteElement )
		sRequest += '&height=' + cDomObject.getHeight( hNoteElement )
		sRequest += '&zindex=' + parseInt( hNoteElement.style.zIndex )
		sRequest += '&notetext='+hTextArea.value
		this.hServerRequest.send( sRequest )
	}
}

cPageNote.prototype.prepareToSaveNote = function()
{
	var hNoteElement = document.getElementById( this.sElementId )
	if( this.hSerializeTimeout != null )
	{
		clearTimeout( this.hSerializeTimeout )
	}
	cPageNote.onBeforeSaveNote( this.sNoteId )
	this.hSerializeTimeout = setTimeout( this.createContext( 'serializeNote' ), cPageNote.CN_SERIALIZE_TIMEOUT )
}

cPageNote.prototype.onNoteSaved = function()
{
	cPageNote.onAfterSaveNote( this.sNoteId )
}

cPageNote.prototype.deleteNote = function()
{
	if( confirm( 'Are you sure you want to delete this note?' ) )
	{
		var hNoteElement = document.getElementById( this.sNoteId )
	    hNoteElement.resizer = null
		hNoteElement.pageNote = null
		hNoteElement.parentNode.removeChild( hNoteElement )
		var hServerRequest = XmlHttp.create()
		hServerRequest.open( 'GET', 'delete.php?noteid='+this.sNoteId, false )
		hServerRequest.send( null )
	}
}

cPageNote.onDeleteNote = function( hEvent )
{
	cDomEvent.init( hEvent )
	var hNoteElement = getParentByProperty( cDomEvent.target, 'className', 'note' )
	if( !hNoteElement )
	{
	    return
	}
	hNoteElement.pageNote.deleteNote()
	if( hEvent.preventDefault )
	{
	    hEvent.preventDefault()
	}
	hEvent.cancelBubble = true
	hEvent.returnValue = false
	return false
}


cPageNote.onBeforeSaveNote = function( sNoteId )
{
	if( ( typeof cPageNote.hNotesToSavePool[ sNoteId ] == 'undefined' )  || ( cPageNote.hNotesToSavePool[ sNoteId ] == false ) )
	{
		cPageNote.hNotesToSavePool[ sNoteId ] = true
		setUnsavedMode()
	}
}

cPageNote.onAfterSaveNote = function( sNoteId )
{
	if( cPageNote.hNotesToSavePool[ sNoteId ] == true )
	{
		cPageNote.hNotesToSavePool[ sNoteId ] = false
		setUnsavedMode()
	}
}

cPageNote.onNoteResizerMove = function( hNote )
{
	var hNoteElement = document.getElementById( hNote.sElementId )
	if( !hNote.bMoved )
	{
		hNoteElement.getElementsByTagName( 'a' )[ 0 ].style.visibility = 'visible'
		hNote.bMoved = true
	}
	hNoteElement.pageNote.prepareToSaveNote()
}

cPageNote.onNoteResizerResize = function( hNote )
{
	var hNoteElement = document.getElementById( hNote.sElementId )
	var hTextArea = hNoteElement.childNodes[ 0 ]
	hTextArea.style.width = cDomObject.getWidth( hNoteElement ) - 8 + 'px'
	hTextArea.style.height = cDomObject.getHeight( hNoteElement ) - 20 + 'px'
	hNoteElement.pageNote.prepareToSaveNote()
}

cPageNote.onNoteTextChange = function( hEvent )
{
	cDomEvent.init( hEvent )
	var hNoteElement = getParentByProperty( cDomEvent.target, 'className', 'note' )
	if( !hNoteElement )
	{
	    return
	}
	hNoteElement.pageNote.prepareToSaveNote()
}

cPageNote.onNoteOver = function( hEvent )
{
	cDomEvent.init( hEvent )
	var hNoteElement = getParentByProperty( cDomEvent.target, 'className', 'note' )
	if( !hNoteElement )
	{
	    return
	}
	hNoteElement.style.opacity = '0.9'
	if( hNoteElement.style.MozOpacity )
	{
		hNoteElement.style.MozOpacity = '0.9'
	}
	else if( hNoteElement.filters )
	{
		hNoteElement.filters.alpha.opacity = '90'
	}
}

cPageNote.onNoteOut = function( hEvent )
{
	cDomEvent.init( hEvent )
	var hNoteElement = getParentByProperty( cDomEvent.target, 'className', 'note' )
	if( !hNoteElement || ( hNoteElement != null && hNoteElement != cDomEvent.target ) )
	{
	    return
	}
	hNoteElement.style.opacity = '0.6'
	if( hNoteElement.style.MozOpacity )
	{
		hNoteElement.style.MozOpacity = '0.6'
	}
	else if( hNoteElement.filters )
	{
		hNoteElement.filters.alpha.opacity = '60'
	}
	if( hNoteElement.pageNote && !hNoteElement.resizer.bMoved && !hNoteElement.pageNote.bRestored )
	{
	    hNoteElement.resizer = null
		hNoteElement.pageNote = null
		hNoteElement.parentNode.removeChild( hNoteElement )
		if( hEvent.preventDefault )
		{
		    hEvent.preventDefault()
		}
		hEvent.cancelBubble = true
		hEvent.returnValue = false
		return false
	}
}

cPageNote.prototype.focus = function()
{
}

cPageNote.prototype.blur = function()
{
	hNoteElement.style.opacity = '0.6'
	if( hNoteElement.style.MozOpacity )
	{
		hNoteElement.style.MozOpacity = '0.6'
	}
	else if( hNoteElement.filters )
	{
		hNoteElement.filters.alpha.opacity = '60'
	}
}

function onNotePrototypeMove( hEvent )
{
	cDomEvent.init( hEvent )
	var hNoteElement = getParentByProperty( cDomEvent.target, 'className', 'note' )
	if( !hNoteElement )
	{
	    return
	}
	hNoteElement.style.cursor = 'move'
	hNoteElement.style.opacity = '0.9'
	if( hNoteElement.style.MozOpacity )
	{
		hNoteElement.style.MozOpacity = '0.9'
	}
	else if( hNoteElement.filters )
	{
		hNoteElement.filters.alpha.opacity = '90'
	}

	var nLeft = cDomObject.getOffsetParam( hNoteElement, 'offsetLeft' )
	var nTop = cDomObject.getOffsetParam( hNoteElement, 'offsetTop' )
	cPageNote.createNote( null, nLeft, nTop, null, null, 10000, cPageNote.CS_NEW_NOTE_TEXT )
}

function onNotePrototypeOut( hEvent )
{
	cDomEvent.init( hEvent )
	cDomEvent.target.style.opacity = '0.6'
	if( cDomEvent.target.style.MozOpacity )
	{
		cDomEvent.target.style.MozOpacity = '0.6'
	}
	else if( cDomEvent.target.filters )
	{
		cDomEvent.target.filters.alpha.opacity = '60'
	}
}

function initNotePrototype( hNotePrototype )
{
	cDomEvent.addEvent( hNotePrototype, 'mousemove', onNotePrototypeMove )
	cDomEvent.addEvent( hNotePrototype, 'mouseout', onNotePrototypeOut )
}

cDomExtensionManager.register( new cDomExtension( document, [ "div[id=notePrototype]" ], initNotePrototype ) )

function setUnsavedMode()
{
	var hNotePrototypeHolder = document.getElementById( 'notePrototypeHolder' )
	var hSavedLink = hNotePrototypeHolder.getElementsByTagName( 'a' )[ 0 ]
	var nCount = 0
	for( var hK in cPageNote.hNotesToSavePool )
	{
		if( cPageNote.hNotesToSavePool [ hK ] )
		{
			nCount++
		}
	}
	if( nCount > 0 )
	{
		hSavedLink.style.visibility = 'visible'
	}
	else
	{
		hSavedLink.style.visibility = 'hidden'
	}
}

function initPageNotes()
{
	var hNotePrototype = document.getElementById( 'notePrototype' )
	hNotePrototype.removeChild( hNotePrototype.childNodes[ 0 ] )
	hNotePrototype.appendChild( document.createTextNode( cPageNote.CS_WAIT_MESSAGE ) )

	var hServerRequest = XmlHttp.create()
	hServerRequest.open( 'GET', 'loadlist.php', false )
	hServerRequest.send( null )

	var hNotes = hServerRequest.responseXML.documentElement.getElementsByTagName( 'noteid' )

	var sNoteId = ''
	var hPageNote = null
	for( var nI = 0; nI < hNotes.length; nI ++ )
	{
		sNoteId = getNodeText( hNotes[ nI ] )
		hServerRequest.open( 'GET', 'load.php?noteid=' + sNoteId, false )
		hServerRequest.send( null )

		var nLeft = new Number( getNodeText( hServerRequest.responseXML.documentElement.getElementsByTagName( 'left' )[0] ) )
		var nTop = new Number( getNodeText( hServerRequest.responseXML.documentElement.getElementsByTagName( 'top' )[0] ) )
		var nWidth = new Number( getNodeText( hServerRequest.responseXML.documentElement.getElementsByTagName( 'width' )[0] ) )
		var nHeight = new Number( getNodeText( hServerRequest.responseXML.documentElement.getElementsByTagName( 'height' )[0] ) )
		var nZIndex = new Number( getNodeText( hServerRequest.responseXML.documentElement.getElementsByTagName( 'zindex' )[0] ) )
		var sText = getNodeText( hServerRequest.responseXML.documentElement.getElementsByTagName( 'text' )[0] )
		hPageNote = cPageNote.createNote( sNoteId, nLeft, nTop, nWidth, nHeight, nZIndex, sText )
		hPageNote.bRestored = true
	}

	hNotePrototype.removeChild( hNotePrototype.childNodes[ 0 ] )
	hNotePrototype.appendChild( document.createTextNode( cPageNote.CS_NEW_NOTE ) )
}

cDomEvent.addEvent( window, 'load', initPageNotes )