var _noteEditors = new Array(); 
var colorChangeDisp = false; 
var deleteSwitch = false;
var scale = false;
var changeColorPossible = true;

registerPlugin('ServiceNotes');

/**
 * Wechselt Sichtbarkeit der ColorChange div-box
 * @author Nils Wrobel
 * @date 22.04.2010
 */  
function changeColorSwitch(id) {
    if (colorChangeDisp == false) {
        colorChangeDisp = true;
        $('changeColor'+ id).show();
    } else {
        colorChangeDisp = false;
        $('changeColor'+ id).hide();
    }
}

 /**
 * Wechselt Sichtbarkeit der ColorChange div-box
 * @author Nils Wrobel
 * @date 23.04.2010
 */  
function changeDeleteSwitch(id, divopen) {
    if (deleteSwitch == false) {
        deleteSwitch = true;
        $('deleteNote'+ id).show();
    } else {
        deleteSwitch = false;
        $('deleteNote'+ id).hide();
    }
}                            
/**
 * Fügt eine Notiz hinzu
 * Aktualisiert den Notizencontainer
 * Löscht den Inhalt des Textfeldes
 * @author Nils Wrobel
 */
function ServiceNotes_add(color, left, right) {
    if (_mode != 'use') {
        return false;
    }
    var text = $('note').value;
    $('note').value = '';
    text = text.replace('/', '--SLASH--');
    text = text.replace(':', '--DOPPELP--');
    text = text.replace('?', '--QUESTIONM--');
    text = text.replace('&', '--ANDSIGN--');
    text = text.replace('%', '--PROCENT--');
    text = text.replace(';', '--SEMICOLON--');
    new Ajax.Request(siteWebroot + 'Notes/add/' + encodeURIComponent(text) + '/' + color, 
                {onSuccess: function()
                    {
                        if (left == 1 ) {
                            new Ajax.Updater('serviceleft',siteWebroot + 'Notes/load', {asynchronous:true, evalScripts:true, requestHeaders:['X-Update', 'serviceleft']})
                        }
                        if (right == 1 ) {
                            new Ajax.Updater('serviceright',siteWebroot + 'Notes/load', {asynchronous:true, evalScripts:true, requestHeaders:['X-Update', 'serviceright']})
                        }
                        $('NotesSpinner').hide();
                    },
                 onLoading: function()
                     {
                         $("NotesSpinner").show();
                     }
                });  
}

/**
 * Löscht eine Notiz
 * Aktualisiert den Notizencontainer
 * @author Nils Wrobel
 * @date 20.04.2010
 */
function ServiceNotes_del(id, left, right) {
    if (_mode != 'use') {
        return false;
    }
    new Ajax.Request(siteWebroot + 'Notes/del/' + id, 
                {onSuccess: function()
                    {
                        if (left == 1 ) {
                            new Ajax.Updater('serviceleft',siteWebroot + 'Notes/load', {asynchronous:true, evalScripts:true, requestHeaders:['X-Update', 'serviceleft']})
                        }
                        if (right == 1 ) {
                            new Ajax.Updater('serviceright',siteWebroot + 'Notes/load', {asynchronous:true, evalScripts:true, requestHeaders:['X-Update', 'serviceright']})
                        }
                        $('NotesSpinner').hide();
                    },
                 onLoading: function()
                     {
                         $("NotesSpinner").show();
                     }
                });    
}

/**
 * Vergrößert und verschiebt die aktuelle Notiz
 * @author Nils Wrobel
 * @date 30.04.2010
 */
function moveScaleNote(note_id) {
    if (scale == false) {
        new Effect.Scale('note_' + note_id, 200, {scaleContent: false} );
        scale = true;
    }         
}

/**
 * Verkleinert und verschiebt die aktuelle Notiz
 * @author Nils Wrobel
 * @date 30.04.2010
 */
function moveScaleNoteRevert(note_id) {
    if (scale == true) {
        new Effect.Scale('note_' + note_id, 100, {scaleMode: { originalHeight: 70, originalWidth: 95 }} );
        scale = false;
    }         
}
              
/**
 * Initialisiert NoteEditor
 * @author Nils Wrobel
 */
function ServiceNotes_load(note_id) {
    _noteEditors[note_id] =
        new Ajax.InPlaceEditor($('NoteText_' + note_id),
                               siteWebroot + 'Notes/edit/' + note_id,
                               {
                                   submitOnBlur: true,
                                   okButton: false,
                                   cancelLink: false,
                                   clickToEditText: 'Zum editieren anklicken',
                                   savingText: '...',
                                   highlightendcolor: '#FFFFFF',
                                   ajaxOptions: {method:'post'}
                               });
        $('note_' + note_id).title = '';
}

/**
 * Deaktiviert alle InPlace-Editoren fuer Links
 * @author Nils Wrobel
 */
function disableNoteEditors() {
    for (var note_id in _noteEditors) if (isInt(note_id)) { 
        _noteEditors[note_id].editing = true;
        _noteEditors[note_id].options.highlightcolor = 'transparent';
        $('note_' + note_id).title = '';
    }
} 

function changeColor(note_id, color, left, right) {
    //If Abfrage mit Globaler Variablen. Wenn gerade die Position geändert wird, soll die Farbe nicht zu verändern sein.
    if (changeColorPossible == true) {
        new Ajax.Request(siteWebroot + 'Notes/changeColor/' + note_id + '/' + color, 
                    {onSuccess: function()
                        {
                            if (left == 1 ) {
                                new Ajax.Updater('serviceleft',siteWebroot + 'Notes/load', {asynchronous:true, evalScripts:true, requestHeaders:['X-Update', 'serviceleft']})
                            }
                            if (right == 1 ) {
                                new Ajax.Updater('serviceright',siteWebroot + 'Notes/load', {asynchronous:true, evalScripts:true, requestHeaders:['X-Update', 'serviceright']})
                            }
                            $('NotesSpinner').hide();
                        },
                     onLoading: function()
                         {
                             $("NotesSpinner").show();
                         }
                    });
    }
    changeColorPossible = true;
}

/**
 * Deaktiviert alle InPlace-Editoren fuer Links
 * @author Nils Wrobel
 * @date: 15.04.2010
 */
function savePosition(note_id) {
    changeColorPossible = false;
    lft = $('note_' + note_id).style.left;
    top = $('note_' + note_id).style.top;
    
    lftpx = lft.indexOf('px');
    toppx = top.indexOf('px');
    
    lft = lft.substring(0, lftpx);
    top = top.substring(0, toppx);
    
    new Ajax.Request(siteWebroot + 'Notes/saveposition/' + note_id + '/' + lft + '/' + top,
        {
            method:'post',
            onSuccess: function()
                {
                    $('spin_' + note_id).hide();
                },
            onLoading: function()
            {
                $('spin_' + note_id).show();
            }
        }
    );
}

/*
function ServiceNotes_showText(note_id, shortText, text) {
	if (_mode != 'use') {
        return false;
    }
    if (shortText == text) {
        return false;
    }

    $('fullNote').innerHTML = text;
    $('fullNote').style.visibility = 'visible';
    //new Effect.ScrollTo('fullNote', {offset: -24});
}

function ServiceNotes_hideText() {
	$('fullNote').style.visibility = 'hidden';
}
*/

function ServiceNotes_switchMode(mode) {

}

