﻿/*
    This is the client script library to interface the Memento web service.
    It uses the behaviour.js, prototype.js and scriptaculous.js libraries and
    the web service proxy created by ASP.NET AJAX.
*/

function GetMemos() { 
    $('ajax-loading').show();          
    Mondorux.Memento.GetHtmlMemos(
        function(result){
            $('memos').innerHTML = result;
            Behaviour.apply();
            new Effect.Fade('ajax-loading');
        }, 
        function(result){
            ShowError("Errore: " + result.get_message());
            new Effect.Fade('ajax-loading');
        }
    );            
}

function RemoveMemo(memo) {
    $('ajax-loading').show();          
    Mondorux.Memento.RemoveMemo(
        memo.attributes['id'].value,
        function(result){
            if (result) {
                var memoRemove = function(){ memo.remove(); }
                new Effect.Fade(memo, {afterFinish: memoRemove});                
            } else {
                ShowError("Promemoria non cancellato");
            }
            new Effect.Fade('ajax-loading');    
        }, 
        function(result){
            ShowError("Errore: " + result.get_message());
            new Effect.Fade('ajax-loading');    
        }
    );   
}

function AddMemo(memoText, memoPriority, sharedWith) {
    Mondorux.Memento.AddMemo(
        memoText,
        memoPriority,
        sharedWith,
        function(result){
            if (result) {
                GetMemos();
            } else {
                ShowError("Promemoria non aggiunto");
            }
        }, 
        function(result){
            ShowError("Errore: " + result.get_message());
        }
    );
}

function GetTip() {           
    Mondorux.Memento.GetTip(
        function(result){
            $('tipcontent').innerHTML = result;
        }, 
        function(result){
            $('tipcontent').innerHTML = "Errore: " + result.get_message();
            ShowError("Errore: " + result.get_message());
        }
    );            
}

function UpdateTag(memo, tag){
    Element.extend(memo);
    var priority = tag.innerHTML;
    var form = '<li id="'+memo.readAttribute('id')+'">'+
                    '<input id="newpriority" type="text" value="'+priority.substring(1, priority.length - 1).replace(/(<([^>]+)>)/ig,"")+'"/>'+
                    '<input id="prioritychange" type="button" value="Modifica" onclick="'+
                        'AddMemo(\''+memo.descendants()[2].innerHTML.replace(/(<([^>]+)>)/ig,"")+'\', this.parentNode.childNodes[0].value, \''+memo.descendants()[2].title.substring(14)+'\');'+
                        'RemoveMemo(this.parentNode)'+
                    '"/>'+
                    '<input id="prioritycancel" type="button" value="Annulla" onclick="GetMemos()"/>&nbsp;'+
                    '<span>'+memo.descendants()[2].innerHTML+'</span>'+
                '</li>';
    memo.update(form);
    $('newpriority').focus();
}

function ShowError(message){
    $('errore').hide();
    $('errore').innerHTML = "<div style=\"padding: 1ex 1em 1ex 1em; cursor: pointer\" title=\"Selezionami per chiudermi\">" + message + "</div>";
    $('errore').setOpacity(1.00);
    Effect.SlideDown('errore');
    Effect.Fade('errore', { duration: 5.0 });
}

function FilterMemos(filter){
    var memos = $A($('memos').getElementsByTagName('li'));
    var shared;
    var sharedwith;
    if(filter.length > 0){
        memos.each(function(li){
                Element.extend(li);
                shared = li.select('span.shared');
                sharedwith = "";
                if(shared.length > 0) {
                    sharedwith = shared[0].getAttribute("title");
                    sharedwith = sharedwith.replace('Condiviso con ','');
                }
                if(StripTags(li.innerHTML).toLowerCase().include(filter.toLowerCase()) || sharedwith.toLowerCase().include(filter.toLowerCase())){
                    li.show();
                } else {
                    li.hide();
                }                                
            }
        );
    } else {
        memos.each(function(li){li.show();});
    }
}

function StripTags(text){
    return text.replace(/<\/?[^>]+>/gi,"");
}

// Behaviours
var memento_rules = {
    'input.removememo': function(element){
        element.onclick = function() {
            RemoveMemo(element.parentNode);
        }
    },
    '#newmemo': function(element){
        element.onclick = function() {
            $('newmemo').hide();
            new Effect.Appear('newmemodetails', {duration: 0.5});            
        }
    },
    '#savememo': function(element){
        element.onclick = function() {
            AddMemo($('memotext').value, $('memopriority').value, $('sharedwith').value);
            $('newmemodetails').hide();
            new Effect.Appear('newmemo');
        }
    },
    '#cancel': function(element){
        element.onclick = function() {            
            $('newmemodetails').hide();
            new Effect.Appear('newmemo');
        }
    },
    '#gettip': function(element){
        element.onclick = function() {
            new Effect.toggle($('tip'), 'blind');            
        }
    },
    '#tip': function(element){
        element.onclick = function() {
            GetTip();            
        }
    },
    '#memos ul li span.order': function(element){
        element.onclick = function() {
            UpdateTag(element.parentNode, element);
        }
    },
    '#memofilter': function(element){
        element.onkeyup = function() {
            FilterMemos(element.value);
        }
    }
};
Behaviour.register(memento_rules);
Behaviour.addLoadEvent(
    function(){
        GetMemos();
        GetTip();
    }
);