// Fotonote object
function Fotonote(id, x, y, width, height, username, author, content, contentHTML, write_perms, delete_perms, scale) {
    if(typeof(scale)=='undefined') scale = 1;
    this.scale = scale;

    this.id = id;
    this.x = x*this.scale;
    this.y = y*this.scale;
    this.width = width*this.scale;
    this.height = height*this.scale;
    this.username = username;
    this.author = author;
    this.content = content;
    this.contentHTML = contentHTML;
    this.write_perms = write_perms;
    this.delete_perms = delete_perms;

    $('fotonote'+id).setStyle({left:this.x+'px', top:this.y+'px', width:this.width+'px', height:this.height+'px'});
    return(this);
}



function saveNote(id) {
    var form = document.forms.fEdit;
    var obj = fotonotes[id];
    if (!obj.write_perms) return;

    if ( obj.content.length>0 ) {
	var postBody = 'fotonote_id='+encodeURI(id)+'&x='+encodeURI(Math.floor(obj.x/obj.scale))+'&y='+encodeURI(Math.floor(obj.y/obj.scale))+'&height='+encodeURI(Math.floor(obj.height/obj.scale))+'&width='+encodeURI(Math.floor(obj.width/obj.scale))+'&content='+encodeURI(obj.content);
	new Ajax.Request(fotonoteEditAction, {method:'post', postBody:postBody});
    } else {
        deleteNote(id);
    }
}

// Track status
var action = "nothing";
var selectedNote = '';
var highlightNotes = false;
var scalingRel = '';
var disableKeyDown = false;

var beginX = -1;
var beginY = -1;
var endX = -1;
var endY = -1;


// General functions for editing, moving, deleting and such stuff
function Box(x, y, w, h) {
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = h;
}
function getCurrentBox() {
    var w = endX-beginX;
    var h = endY-beginY;

    if (w>0) {
        x = beginX;
    } else {
        x = endX;
        w *= -1;
    }
    if (h>0) {
        y = beginY;
    } else {
        y = endY;
        h *= -1;
    }

    return(new Box(x, y, w, h));
}


// Display and layout stuff
function showFotonote(id) {
    if (!fotonotes[id]) return;

    if (selectedNote) return;

    var obj = _('fotonote'+id);

    _('fotonoteBubble').style.width = '';
    _('fotonoteBubbleName').innerHTML = '<a href="/' + fotonotes[id].username + '">' + fotonotes[id].author + '</a>:';
    _('fotonoteBubbleText').innerHTML = fotonotes[id].contentHTML;
    _('fotonoteBubble').style.display = '';

    if ( _('fotonoteBubble').offsetWidth>250 ) {
        _('fotonoteBubble').style.width = '250px';
    }

    var bubbleY = getAbsoluteY_obj(obj)+fotonotes[id].height+2;
    var bubbleX = getAbsoluteX_obj(obj) + ((obj.offsetWidth-_('fotonoteBubble').offsetWidth)/2);

    if (bubbleX<0) {
        bubbleX = 0;
    }

    _('fotonoteBubble').style.left = bubbleX+'px';
    _('fotonoteBubble').style.top = bubbleY+'px';
}

function hideFotonote() {
    _('fotonoteBubble').style.display = 'none';
}


function placeScales(obj) {
    // Place scales
    var absoluteX = getAbsoluteX_obj(obj);
    var absoluteY = getAbsoluteY_obj(obj);

    _('scale_tl').style.left = (absoluteX-2) + 'px';
    _('scale_tl').style.top = (absoluteY-2) + 'px';
    _('scale_tl').style.display = '';

    _('scale_tr').style.left = (absoluteX+obj.offsetWidth-6) + 'px';
    _('scale_tr').style.top = (absoluteY-2) + 'px';
    _('scale_tr').style.display = '';

    _('scale_bl').style.left = (absoluteX-2) + 'px';
    _('scale_bl').style.top = (absoluteY+obj.offsetHeight-6) + 'px';
    _('scale_bl').style.display = '';

    _('scale_br').style.left = (absoluteX+obj.offsetWidth-6) + 'px';
    _('scale_br').style.top = (absoluteY+obj.offsetHeight-6) + 'px';
    _('scale_br').style.display = '';
}

function hideScales() {
    _('scale_tl').style.top = '-10px';
    _('scale_tl').style.left = '-10px';
    _('scale_tr').style.top = '-10px';
    _('scale_tr').style.left = '-10px';
    _('scale_bl').style.top = '-10px';
    _('scale_bl').style.left = '-10px';
    _('scale_br').style.top = '-10px';
    _('scale_br').style.left = '-10px';
}

function drawNotes() {
    if (!highlightNotes && !selectedNote) {
        // Nothing but outlines
        for (i in fotonotes) {
            var obj = document.getElementById('fotonote'+i);
            if (obj) {
                obj.style.borderWidth = "0px";
                obj.style.zIndex = "1";
            }
        }

	// Don't show scales
        hideScales();
    } else {
        // Show text and opaque background
        for (i in fotonotes) {
            var obj = document.getElementById('fotonote'+i);
            if (obj) {
                obj.style.zIndex = '';
                obj.style.borderWidth = "1px";
            }
        }

        // ... and possible mark an object
        if (selectedNote) {
            var obj = document.getElementById('fotonote'+selectedNote);
            if (!fotonotes[selectedNote].write_perms) return;
            if (obj) {
                obj.style.borderWidth = "2px";
                obj.style.zIndex = "100";

		placeScales(obj);
            }
        } else {
            hideScales();
        }
    }

    placeNoteEdit();
}



// Moving
function beginMovingNote(e) {
    action = "moving note";
    beginX=getAbsoluteX(e);
    beginY=getAbsoluteY(e);
}
function moveNote(e) {
    if ( action=="moving note" ) {
        var distanceX = beginX-getAbsoluteX(e);
        var distanceY = beginY-getAbsoluteY(e);

        obj = fotonotes[selectedNote];
        if (!obj.write_perms) return;
        obj.x -= distanceX;
        obj.y -= distanceY;

	if (obj.x<0) obj.x = 0;
	if (obj.width<5) obj.width = 5;
	if (obj.y<0) obj.y = 0;
	if (obj.height<5) obj.height = 5;

        _('fotonote'+selectedNote).style.left = obj.x + 'px';
        _('fotonote'+selectedNote).style.top = obj.y + 'px';
        placeScales(_('fotonote'+selectedNote))

        beginX=getAbsoluteX(e);
        beginY=getAbsoluteY(e);

        placeNoteEdit();
    }
}

function endMovingNote(e) {
    action = "";

    saveNote(selectedNote);
}

// Scaling
function beginScalingNote(e, rel) {
    action = "scaling note";
    scalingRel = rel;
    beginX=getAbsoluteX(e);
    beginY=getAbsoluteY(e);
}
function scaleNote(e, rel) {
    if ( action=="scaling note" ) {
        if (rel==undefined) rel=scalingRel;

        var distanceX = beginX-getAbsoluteX(e);
        var distanceY = beginY-getAbsoluteY(e);

        obj = fotonotes[selectedNote];
	switch (rel) {
	    case 'tl':
                obj.width += distanceX;
		if (obj.width>=5) {
                    obj.x -= distanceX;
	        } else {
		    obj.width = 5;
		}

                obj.height += distanceY;
		if (obj.height>=5) {
                    obj.y -= distanceY;
	        } else {
		    obj.height = 5;
		}
                break;
	    case 'tr':
                obj.width -= distanceX;

                obj.height += distanceY;
		if (obj.height>=5) {
                    obj.y -= distanceY;
	        } else {
		    obj.height = 5;
		}
                break;
	    case 'bl':
                obj.width += distanceX;
		if (obj.width>=5) {
                    obj.x -= distanceX;
	        } else {
		    obj.width = 5;
		}

                obj.height -= distanceY;
                break;
	    case 'br':
                obj.width -= distanceX;
                obj.height -= distanceY;
                break;
	}


        _('fotonote'+selectedNote).style.left = obj.x + 'px';
        _('fotonote'+selectedNote).style.top = obj.y + 'px';
        _('fotonote'+selectedNote).style.width = obj.width + 'px';
        _('fotonote'+selectedNote).style.height = obj.height + 'px';
        placeScales(_('fotonote'+selectedNote))

        beginX=getAbsoluteX(e);
        beginY=getAbsoluteY(e);

        placeNoteEdit();
    }
}

function endScalingNote(e, rel) {
    action = "";

    saveNote(selectedNote);
}


// Deleting
function deleteNote(id) {
    hideFotonote();
    _('fotonote'+id).style.display = 'none';

    var newFotonotesObj = new Array();
    for (i in fotonotes) {
        if ( i!=id ) newFotonotesObj[i] = fotonotes[i];
    }
    fotonotes = newFotonotesObj;


    // Perform delete
    document.forms.fDelete.fotonote_id.value = id;
    document.forms.fDelete.submit();

    if (id==selectedNote) {
        selectedNote = false;
        drawNotes();
    }
}


// Edit
function beginNoteEdit() {
    if (!selectedNote) return;

    _('fotonoteBubble').style.display = 'none';

    var ta = _('textarea');
    ta.style.display = 'none';
    ta.value = fotonotes[selectedNote].content;

    placeNoteEdit();

    ta.focus();
    ta.select();
}
function placeNoteEdit() {
    if (!selectedNote) {
        _('textarea').style.display = 'none';
        _('buttons').style.display = 'none';
        return;
    }

    var write_perms = fotonotes[selectedNote].write_perms;
    var delete_perms = fotonotes[selectedNote].delete_perms;
    if (!write_perms && !delete_perms) return;

    var ta = _('textarea');
    var obj = _('fotonote'+selectedNote);

    var y = getAbsoluteY_obj(obj)+obj.offsetHeight+3;
    var x = getAbsoluteX_obj(obj)+((obj.offsetWidth-160)/2);
    if (x<0) {x = 0;}

    if (write_perms) {
        ta.style.left = x + 'px';
        ta.style.top = y + 'px';
        ta.style.width = '160px';
        ta.style.height = '80px';
        ta.style.display = '';

        _('save').style.display = '';
        var buttons = _('buttons');
        buttons.style.left = x + 'px';
        buttons.style.top = (y+85) + 'px';
        buttons.style.width = '160px';
        buttons.style.textAlign = 'center';
        buttons.style.display = '';
    } else if (delete_perms) {
        _('save').style.display = 'none';
        var buttons = _('buttons');
        buttons.style.left = x + 'px';
        buttons.style.top = y + 'px';
        buttons.style.width = '160px';
        buttons.style.textAlign = 'center';
        buttons.style.display = '';
    }

}
function saveNoteEdit(id) {
    if (!fotonotes[id]) return;

    fotonotes[id].content = _('textarea').value;
    fotonotes[id].contentHTML = fotonotes[id].content;
    saveNote(id);
}

function handleMouseMove(e) {
    switch (action) {
        case 'moving note':
            moveNote(e);
            break;
        case 'scaling note':
            scaleNote(e);
            break;
    }
    if (e) e.cancelBubble = true;
}

function handleMouseUp(e) {
    switch (action) {
        case 'moving note':
            endMovingNote(e);
            break;
        case 'scaling note':
            endScalingNote(e);
            break;
    }
    if (e) e.cancelBubble = true;
}

function imgMouseIn(e) {hideFotonote(); highlightNotes = true; drawNotes();}
function imgMouseOut(e) {highlightNotes = false; drawNotes();}
function imgMouseDown(e) {if (!unselectNote() && typeof(originalURL)!='undefined') location.href=originalURL;}
function imgMouseMove(e) {handleMouseMove(e);}
function imgMouseUp(id, e) {handleMouseUp(e);}

function noteMouseIn(id, e) {showFotonote(id); highlightNotes = true; drawNotes(); e.cancelBubble = true;}
function noteMouseOut(id, e) {}
function noteMouseDown(id, e) {selectNote(id); beginMovingNote(e); if (e) e.cancelBubble = true;}
function noteMouseMove(id, e) {handleMouseMove(e);}
function noteMouseUp(id, e) {handleMouseUp(e)};

function selectNote(id) {
    if (!fotonotes[id]) return;
    //if (selectedNote) saveNoteEdit(selectedNote);

    selectedNote = id;
    hideFotonote();
    beginNoteEdit(selectedNote);
    drawNotes();
}
function unselectNote() {
    if (!selectedNote) return(false);;
    //saveNoteEdit(selectedNote);
    hideFotonote();
    selectedNote = false;
    drawNotes();

    return(true);
}

function scaleMouseDown(e, rel) {beginScalingNote(e, rel); if (e) e.cancelBubble = true;}
function scaleMouseMove(e) {handleMouseMove(e);}
function scaleMouseUp(e) {handleMouseUp(e);}