// vars
var photonote_var_action = "nothing";
var photonote_var_selected_note = '';
var photonote_var_highlight_notes = false;
var photonote_var_scaling_rel = '';

var photonote_var_begin_x = -1;
var photonote_var_begin_y = -1;


// misc photonote-js

var OnKeyRequestBuffer = 
{
	status: false,
    bufferText: false,
    bufferTime: 500,
    
    modified : function(strId)
    {
		setTimeout('OnKeyRequestBuffer.compareBuffer("'+strId+'","'+xajax.$(strId).value+'");', this.bufferTime);
		
		return this.status;
    },
    
    compareBuffer : function(strId, strText)
    {
        if (strText == xajax.$(strId).value && strText != this.bufferText)
        {
            this.bufferText = strText;
            this.status = true;
            return;
        }
        
        else {
        	 this.status = false;
        	 return;
        }      	
    }
};

function photonote_display_note_extern(note_id)
{
	photonote_var_highlight_notes= 1;
	photonote_draw_notes();
	photonote_show_note(note_id);
	return false;	
}

function photonote_hide_note_extern()
{
	photonote_var_highlight_notes= 0;
	photonote_draw_notes();
	photonote_hide_note();
	return false;	
}

// send update-form
function photonote_change_note()
{
	xajax_gallery_ajax_edit_photonote(xajax.getFormValues('photonote_content_form'));
}


//photonote car-functions
function photonote_car_noenter(key) {
	if (key == 13) {
		return false;
	} else {
		return true;
	}
}

function photonote_car_suggest(key,element_id) {
	element = document.getElementById(element_id);
	
	if (key != 13)
	{
		if (element.value.length > 2)
		{
			if( OnKeyRequestBuffer.modified(element_id) === true)
			{
				xajax_gallery_ajax_edit_photonote_car_suggest(element.value);
			}
		}
	}
}


// photonote_member-functions
function photonote_member_suggest(key,element_id) {
	element = document.getElementById(element_id);
	
	if (key != 13)
	{
		if (element.value.length > 2)
		{
			if( OnKeyRequestBuffer.modified(element_id) === true)
			{
				xajax_gallery_ajax_edit_photonote_member_suggest(element.value);
			}
		}
		
		else
		{
			suggest_element = document.getElementById('photonote_suggest_member_container');
			suggest_element.innerHTML = '';
		}
	}
	
	element = null;
}

function photonote_member_select_suggestion(username) {
	suggcont = document.getElementById("photonote_suggest_member_container");
	member_input = document.getElementById("photonote_member_name");
	
	member_input.value = ""+ username +"";
	suggcont.innerHTML = '';
	
	member_input = null;
	suggcont = null;
}


// create photonote-object
function photonote(x, y, width, height, contentHTML, write_perms, delete_perms) {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    this.contentHTML = contentHTML;
    this.write_perms = write_perms;
    this.delete_perms = delete_perms;
    return(this);
}


function photonote_save_note_position(id) {
    var obj = photonotes[id];
    if (!obj.write_perms)
    {
    	obj = null;
    	return;
    }
    

    xajax_gallery_ajax_move_photonote(id, obj.x, obj.y, obj.width, obj.height);    
    obj = null;
}


// display etc. functions
function photonote_show_note(id) {
    if (!photonotes[id])
    {
    	return;
    }

    if (photonote_var_selected_note)
    {
    	return;
    }

    var obj = document.getElementById('photonote'+id);
    
    document.getElementById('photonote_textbox').style.width = '';
    document.getElementById('photonote_textbox').innerHTML = photonotes[id].contentHTML;
    document.getElementById('photonote_textbox').style.display = '';

    if ( document.getElementById('photonote_textbox').offsetWidth>250 ) {
    	document.getElementById('photonote_textbox').style.width = '250px';
    }

    var bubbleY = photonote_get_y_obj(obj)+photonotes[id].height+2;
    var bubbleX = photonote_get_x_obj(obj) + ((obj.offsetWidth-document.getElementById('photonote_textbox').offsetWidth)/2);

    if (bubbleX<0) {
        bubbleX = 0;
    }

    document.getElementById('photonote_textbox').style.left = bubbleX+'px';
    document.getElementById('photonote_textbox').style.top = bubbleY+'px';
    
    obj = null;
}

function photonote_hide_note() {
	document.getElementById('photonote_textbox').style.display = 'none';
}


function photonote_place_scales(obj) {
    var absoluteX = photonote_get_x_obj(obj);
    var absoluteY = photonote_get_y_obj(obj);

    document.getElementById('photonote_scale_tl').style.left = (absoluteX-2) + 'px';
    document.getElementById('photonote_scale_tl').style.top = (absoluteY-2) + 'px';
    document.getElementById('photonote_scale_tl').style.display = '';

    document.getElementById('photonote_scale_tr').style.left = (absoluteX+obj.offsetWidth-6) + 'px';
    document.getElementById('photonote_scale_tr').style.top = (absoluteY-2) + 'px';
    document.getElementById('photonote_scale_tr').style.display = '';

    document.getElementById('photonote_scale_bl').style.left = (absoluteX-2) + 'px';
    document.getElementById('photonote_scale_bl').style.top = (absoluteY+obj.offsetHeight-6) + 'px';
    document.getElementById('photonote_scale_bl').style.display = '';

    document.getElementById('photonote_scale_br').style.left = (absoluteX+obj.offsetWidth-6) + 'px';
    document.getElementById('photonote_scale_br').style.top = (absoluteY+obj.offsetHeight-6) + 'px';
    document.getElementById('photonote_scale_br').style.display = '';
}

function photonote_hide_scales() {
	document.getElementById('photonote_scale_tl').style.top = '-10px';
	document.getElementById('photonote_scale_tl').style.left = '-10px';
	document.getElementById('photonote_scale_tr').style.top = '-10px';
	document.getElementById('photonote_scale_tr').style.left = '-10px';
	document.getElementById('photonote_scale_bl').style.top = '-10px';
	document.getElementById('photonote_scale_bl').style.left = '-10px';
	document.getElementById('photonote_scale_br').style.top = '-10px';
	document.getElementById('photonote_scale_br').style.left = '-10px';
}

function photonote_draw_notes() {
    if (!photonote_var_highlight_notes && !photonote_var_selected_note) {
        // Nothing but outlines
        for (i in photonotes) {
            var obj = document.getElementById('photonote'+i);
        	
        	if (obj) {
                obj.style.borderWidth = "0px";
                obj.style.zIndex = "1";
            }
            
            obj = null;
        }

        // Don't show scales
        photonote_hide_scales();
    } 
    
    else {
        // Show text and opaque background        
        for (i in photonotes) {
           var obj = document.getElementById('photonote'+i);
        	
           if (obj) {
               obj.style.zIndex = '';
               obj.style.borderWidth = "1px";
                
               obj = null;
            }
        }

        // ... and possible mark an object
        if (photonote_var_selected_note) {
            var obj = document.getElementById('photonote'+photonote_var_selected_note);
            
            // not edit and delete
            if (!photonotes[photonote_var_selected_note].write_perms && !photonotes[photonote_var_selected_note].delete_perms) 
            {
            	photonote_show_note(photonote_var_selected_note);
            	
            	obj = null;
            	return;
            }	
            
            if (obj) {
                obj.style.borderWidth = "2px";
                obj.style.zIndex = "100";

                photonote_place_scales(obj);
                
                obj = null;
            }
            
            // no edit
            if (!photonotes[photonote_var_selected_note].write_perms) 
            {
            	photonote_hide_scales();
            } 
        } else {
            photonote_hide_scales();
        }
    }

    photonote_place_note_edit();
}


// move-functions
function photonote_begin_moving_note(e) {
    photonote_var_action = "moving note";
    photonote_var_begin_x=photonote_get_x(e);
    photonote_var_begin_y=photonote_get_y(e);
}

function photonote_move_note(e) {
    if ( photonote_var_action=="moving note" ) {
        var distanceX = photonote_var_begin_x-photonote_get_x(e);
        var distanceY = photonote_var_begin_y-photonote_get_y(e);

        obj = photonotes[photonote_var_selected_note];
        if (!obj.write_perms)
        {
        	distanceX = null;
        	distanceY = null;
        	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;
		}
	
		document.getElementById('photonote'+photonote_var_selected_note).style.left = obj.x + 'px';
		document.getElementById('photonote'+photonote_var_selected_note).style.top = obj.y + 'px';
		photonote_place_scales(document.getElementById('photonote'+photonote_var_selected_note));

        photonote_var_begin_x=photonote_get_x(e);
        photonote_var_begin_y=photonote_get_y(e);

        photonote_place_note_edit();
    }
    
    obj = null;
}

function photonote_end_moving_note(e) {
    photonote_var_action = "";

    photonote_save_note_position(photonote_var_selected_note);
}


// scale notes
function photonote_begin_scaling_note(e, rel) {
    photonote_var_action = "scaling note";
    photonote_var_scaling_rel = rel;
    photonote_var_begin_x=photonote_get_x(e);
    photonote_var_begin_y=photonote_get_y(e);
}

function photonote_scale_note(e, rel) {
    if ( photonote_var_action=="scaling note" ) {
        if (rel===undefined)
        {
        	rel=photonote_var_scaling_rel;
        }

        var distanceX = photonote_var_begin_x-photonote_get_x(e);
        var distanceY = photonote_var_begin_y-photonote_get_y(e);

        obj = photonotes[photonote_var_selected_note];
		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;
		}


		document.getElementById('photonote'+photonote_var_selected_note).style.left = obj.x + 'px';
		document.getElementById('photonote'+photonote_var_selected_note).style.top = obj.y + 'px';
		document.getElementById('photonote'+photonote_var_selected_note).style.width = obj.width + 'px';
		document.getElementById('photonote'+photonote_var_selected_note).style.height = obj.height + 'px';
        photonote_place_scales(document.getElementById('photonote'+photonote_var_selected_note));

        photonote_var_begin_x=photonote_get_x(e);
        photonote_var_begin_y=photonote_get_y(e);

        photonote_place_note_edit();
        
        obj = null;
    }
}

function photonote_end_scaling_note(e, rel) {
    photonote_var_action = "";

    photonote_save_note_position(photonote_var_selected_note);
}


// edit functions
function photonote_begin_note_edit() {
    if (!photonote_var_selected_note)
    {
    	return;
    }
    
    photonote_place_note_edit();
}

function photonote_place_note_edit() {
    if (!photonote_var_selected_note) {
    	document.getElementById('buttons').style.display = 'none';
        return;
    }

    var write_perms = photonotes[photonote_var_selected_note].write_perms;
    var delete_perms = photonotes[photonote_var_selected_note].delete_perms;

    if (!write_perms && !delete_perms)
    {
    	return;
    }
    
    var obj = document.getElementById('photonote'+photonote_var_selected_note);

    var y = photonote_get_y_obj(obj)+obj.offsetHeight+3;
    var x = photonote_get_x_obj(obj)+((obj.offsetWidth-160)/2);
    if (x<0) {x = 0;}

    if (write_perms) {
    	document.getElementById('save').style.display = '';
    }
    
    else
    {
    	document.getElementById('save').style.display = 'none';
    }

    
    if (delete_perms) {
    	document.getElementById('delete').style.display = '';
    }
    
    else
    {
    	document.getElementById('delete').style.display = 'none';
    }
    
    
    var buttons = document.getElementById('buttons');
    buttons.style.left = x + 'px';
    buttons.style.top = y + 'px';
    buttons.style.width = '160px';
    buttons.style.textAlign = 'center';
    buttons.style.display = '';

    obj = null;
}


// handler functions
function photonote_handle_mouse_move(e) {
    switch (photonote_var_action) {
        case 'moving note':
            photonote_move_note(e);
            break;
        case 'scaling note':
            photonote_scale_note(e);
            break;
    }
    if (e) { e.cancelBubble = true;}
}

function photonote_handle_mouse_up(e) {
    switch (photonote_var_action) {
        case 'moving note':
            photonote_end_moving_note(e);
            break;
        case 'scaling note':
            photonote_end_scaling_note(e);
            break;
    }
    if (e) {e.cancelBubble = true;}
}


// img mouse actions
function photonote_img_mouse_in(e) {
	photonote_hide_note();
	photonote_var_highlight_notes = true;
	photonote_draw_notes();
}

function photonote_img_mouse_out(e) {
	photonote_var_highlight_notes = false;
	photonote_draw_notes();
}

function photonote_img_mouse_down(e) {
	if (!photonote_unselect_note() && typeof(largeURL)!='undefined') {
		location.href=largeURL;
	}
}

function photonote_img_mouse_move(e) {
	photonote_handle_mouse_move(e);
}

function photonote_img_mouse_up(id, e) {
	photonote_handle_mouse_up(e);
}

// note mouse actions
function photonote_note_mouse_in(id, e) {
	photonote_show_note(id);
	photonote_var_highlight_notes = true;
	photonote_draw_notes();
	e.cancelBubble = true;
}

function photonote_note_mouse_out(id, e) {
}

function photonote_note_mouse_down(id, e) {
	photonote_select_note(id);
	photonote_begin_moving_note(e);
	
	if (e) {
		e.cancelBubble = true;
	}
}

function photonote_note_mouse_move(id, e) {
	photonote_handle_mouse_move(e);
}

function photonote_note_mouse_up(id, e) {
	photonote_handle_mouse_up(e);
}


// note functions
function photonote_select_note(id) {
    if (!photonotes[id]) {
    	return;
    }
    
    photonote_var_selected_note = id;
    photonote_hide_note();
    photonote_begin_note_edit(photonote_var_selected_note);
    photonote_draw_notes();
}
function photonote_unselect_note() {
    if (!photonote_var_selected_note) {
    	return(false);
    }

    photonote_hide_note();
    photonote_var_selected_note = false;
    photonote_draw_notes();

    return(true);
}


// scale mouse functions
function photonote_scale_mouse_down(e, rel) {
	photonote_begin_scaling_note(e, rel);
	if (e) {
		e.cancelBubble = true;
	}
}

function photonote_scale_mouse_move(e) {
	photonote_handle_mouse_move(e);
}

function photonote_scale_mouse_up(e) {
	photonote_handle_mouse_up(e);
}


// calculate position of mouse and object
function photonote_get_x(e) {
	if (e.pageX) {
		return(e.pageX);
	}
	else {
		return(e.clientX + document.body.scrollLeft);
	}
}

function photonote_get_y(e) {
	if (e.pageY) {
		return(e.pageY);
	}
	else {
		return(e.clientY + document.body.scrollLeft);
	}
}

function photonote_get_x_obj(obj) {
	var x = obj.offsetLeft;
	while (obj.offsetParent) {
		obj = obj.offsetParent;
		x += obj.offsetLeft;
	}
	
	return(x);
}

function photonote_get_y_obj(obj) {
	var y = obj.offsetTop;
	while (obj.offsetParent) {
		obj = obj.offsetParent;
		y += obj.offsetTop;
	}
	
	return(y);
}
