/*
Copyright (c) 2006 Dusty Davidson - http://www.dustyd.net
(portions Copyright (c) 2005 Angus Turnbull http://www.twinhelix.come)

Permission is hereby granted, free of charge, to any person obtaining 
a copy of this software and associated documentation files (the "Software"), 
to deal in the Software without restriction, including without limitation 
the rights to use, copy, modify, merge, publish, distribute, sublicense, 
and/or sell copies of the Software, and to permit persons to whom the Software 
is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in 
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/



/*********************************************************/
/*** Photo Notes Container *******************************/
/*********************************************************/
function PhotoNoteContainer(element, config)
{
    var props = {
        element: element,
        dragresize: null,
        notes: new Array(),
        editing: false
    };

    for (var p in props)
    {
        this[p] = (!config || typeof config[p] == 'undefined') ? props[p] : config[p];
    }
        
};

PhotoNoteContainer.prototype.AddNote = function(note)
{
    if(!this.editing)
    {
        /* add the note to the array of notes, and set its container */
        this.notes[this.notes.length] = note;
        note.container = this;

        /* add the note to the DOM */
        this.element.appendChild(note.gui.ElementRect);
        this.element.appendChild(note.gui.ElementNote);
    }
};

/* hide all of the "text" parts of the notes. Primarily called when hovering a note, at which
    point we want to hide all of the other note texts */
PhotoNoteContainer.prototype.HideAllNoteTexts = function()
{
    for (var i = 0;i<this.notes.length;i++)
        this.notes[i].HideNoteText();
};

PhotoNoteContainer.prototype.HideAllNotes = function()
{
    for (var i = 0;i<this.notes.length;i++)
        this.notes[i].HideNote();
};

PhotoNoteContainer.prototype.ShowAllNotes = function()
{
    for (var i = 0;i<this.notes.length;i++)
        this.notes[i].ShowNote();
};

/*********************************************************/
/*** Photo Note ******************************************/
/*********************************************************/
function PhotoNote(text,id,rect)
{
    var props = {
        text: text,                   
        id: id,
        rect: rect,                    
        selected: false,
        container: null,
        dragresize: null,
        oldRect: null,
        YOffset: 10,
        XOffset: 0,
        onsave: null,
        ondelete: null,
        gui: null                  
        
    };

    for (var p in props)
    {
        this[p] = props[p];
    }
    
    this.CreateElements();
}

PhotoNote.prototype.ShowNoteText = function()
{
if(!this.container.editing)
{
	this.container.HideAllNoteTexts();

	this.gui.ElementNote.style.display='block';
	if(BrowserDetect.browser == 'Explorer')
	{
		this.gui.ElementRect.firstChild.filters.alpha.opacity = '85';
		this.gui.ElementRect.firstChild.firstChild.filters.alpha.opacity = '85';
	} else {
		this.gui.ElementRect.firstChild.firstChild.style.opacity = '.85';
		this.gui.ElementRect.firstChild.style.opacity = '.85';
	}

}
}

PhotoNote.prototype.HideNoteText = function ()
{
this.gui.ElementNote.style.display='none';
if(BrowserDetect.browser == 'Explorer')
{
	this.gui.ElementRect.firstChild.firstChild.filters.alpha.opacity = '40';
	this.gui.ElementRect.firstChild.filters.alpha.opacity = '40';
} else {
	this.gui.ElementRect.firstChild.style.opacity = '0.4';
	this.gui.ElementRect.firstChild.firstChild.style.opacity = '0.4';
}
}

PhotoNote.prototype.HideNote = function ()
{
this.gui.ElementRect.style.display='none';
this.gui.ElementNote.style.display='none';
}

PhotoNote.prototype.ShowNote = function ()
{
this.gui.ElementRect.style.display='block';
this.gui.ElementNote.style.display='none';
}

PhotoNote.prototype.SetEditable = function(editable)
{
this.container.editing = editable;

if(editable)
{
	//the first child of the note is the text
	this.gui.TextTitle.style.display = 'none';
	
	//the second child is the edit area...
	this.gui.EditArea.style.display = 'block';
	
	//if this is a "new" note, then hide the delete button
	if(this.id <= 0)
		this.gui.DeleteButton.style.display = 'none';
	else
		this.gui.DeleteButton.style.display = 'inline';
	
	// get the textarea and select the text...
	this.HighlightTextbox();
}
else
{
	//the first child of the note is the text
	this.gui.TextTitle.style.display = 'block';
	
	//the second child is the edit area...
	this.gui.EditArea.style.display = 'none';
}
}

PhotoNote.prototype.HighlightTextbox = function ()
{
// get the textarea and select the text...
if(this.gui.EditArea.style.display=='block')
{
	var textfield = this.gui.TextBox;
	setTimeout(function() {
			try
			{
				textfield.focus();
				textfield.select();
			}
			catch(e) {}
		}, 200);
}

}

PhotoNote.prototype.CreateElements = function()
{
this.gui = new PhotoNoteGUI();

var newArea = document.createElement('div');
newArea.className = 'fn-area';
newArea.id = 'fn-area-new';

var newAreaBlack = document.createElement('div');
newAreaBlack.className = 'fn-area-blackborder';
var newAreaWhite = document.createElement('div');
newAreaWhite.className = 'fn-area-whiteborder';

var currentNote = this;

var newAreaInner = document.createElement('div');
newAreaInner.className = 'fn-area-inner';
newAreaWhite.appendChild(newAreaInner);

//attach mouse events to this element...
addEvent(newAreaInner, 'mouseover', function() {
		currentNote.ShowNoteText();
	});
addEvent(newAreaInner, 'mouseout', function() {

		if(!currentNote.selected)
		{
			setTimeout(function () {
				currentNote.HideNoteText();
				}, 250);
				
		}
	});
	
newAreaBlack.appendChild(newAreaWhite);
newArea.appendChild(newAreaBlack);

// add the notes area
var noteArea = document.createElement('div');
noteArea.className = 'fn-note';

var titleArea = document.createElement('div');
titleArea.className = 'fn-note-text';
var t = document.createTextNode(this.text);
titleArea.appendChild(t);
noteArea.appendChild(titleArea);
    
    /* setup the GUI object */
    this.gui.ElementRect = newArea;
    this.gui.ElementNote = noteArea;
    this.gui.TextTitle = titleArea;
    
    /* position the note text below the note area */
    this.PositionNote();
}

PhotoNote.prototype.PositionNote = function()
{
    /* outer most box */
    this.gui.ElementRect.style.left  = this.rect.left + 'px';
    this.gui.ElementRect.style.top  = this.rect.top + 'px';
    this.gui.ElementRect.style.width  = this.rect.width + 'px';
    this.gui.ElementRect.style.height  = this.rect.height + 'px';
    
    // black border
    this.gui.ElementRect.firstChild.style.width  = parseInt(this.gui.ElementRect.style.width) - 2 + 'px';
    this.gui.ElementRect.firstChild.style.height  = parseInt(this.gui.ElementRect.style.height) - 2 + 'px';        
    
    // white border
    this.gui.ElementRect.firstChild.firstChild.style.width  = parseInt(this.gui.ElementRect.style.width) - 4 + 'px';
    this.gui.ElementRect.firstChild.firstChild.style.height  = parseInt(this.gui.ElementRect.style.height) - 4 + 'px';        

    // inner box
    this.gui.ElementRect.firstChild.firstChild.firstChild.style.width  = parseInt(this.gui.ElementRect.style.width) - 6 + 'px';
    this.gui.ElementRect.firstChild.firstChild.firstChild.style.height  = parseInt(this.gui.ElementRect.style.height) - 6 + 'px';        
 
    this.gui.ElementNote.style.left  = this.rect.left + this.XOffset + 'px';
    this.gui.ElementNote.style.top  = this.rect.top + this.YOffset + this.rect.height + 'px';
   
}

/*********************************************************/
/*** Photo Note GUI Object *******************************/
/*********************************************************/
function PhotoNoteGUI()
{
    this.ElementRect = null;
    
    // the note text area...
    this.ElementNote = null;
    this.TextTitle = null;
    this.EditArea = null;
    this.TextBox = null;
    
    // buttons
    this.DeleteButton = null;
}



/*********************************************************/
/*** Rectangle *******************************************/
/*********************************************************/
function PhotoNoteRect(left,top,width,height)
{
    this.left = left;
    this.top = top;
    this.width = width;
    this.height = height;
}

/* for debugging purposes */
PhotoNoteRect.prototype.toString = function()
{
    return 'left: ' + this.left + ', top: ' + this.top + ', width: ' + this.width + ', height: ' + this.height;
}


// *** Common API Code ***
// (c) 2005 Angus Turnbull http://www.twinhelix.com

var aeOL = [];
function addEvent(o, n, f, l)
{
 var a = 'addEventListener', h = 'on'+n, b = '', s = '';
 if (o[a] && !l) return o[a](n, f, false);
 o._c |= 0;
 if (o[h])
 {
  b = '_f' + o._c++;
  o[b] = o[h];
 }
 s = '_f' + o._c++;
 o[s] = f;
 o[h] = function(e)
 {
  e = e || window.event;
  var r = true;
  if (b) r = o[b](e) != false && r;
  r = o[s](e) != false && r;
  return r;
 };
 aeOL[aeOL.length] = { o: o, h: h };
};
addEvent(window, 'unload', function() {
 for (var i = 0; i < aeOL.length; i++) with (aeOL[i])
 {
  o[h] = null;
  for (var c = 0; o['_f' + c]; c++) o['_f' + c] = null;
 }
});

function cancelEvent(e, c)
{
 e.returnValue = false;
 if (e.preventDefault) e.preventDefault();
 if (c)
 {
  e.cancelBubble = true;
  if (e.stopPropagation) e.stopPropagation();
 }
};

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}


/* Extend the Array object with some useful features 
   http://www.ditchnet.org/wp/?p=8
*/

Array.prototype.clear = function () {
    this.length = 0;
};

Array.prototype.remove = function (element) {
	var result = false;
	var array = [];
	for (var i = 0; i < this.length; i++) {
		if (this[i] == element) {
			result = true;
		} else {
			array.push(this[i]);
		}
	}
	this.clear();
	for (var i = 0; i < array.length; i++) {
		this.push(array[i]);
	}
	array = null;
	return result;
};


