if (!Site) {
    var Site = {};
}
var colorStar = function(action, num, id)
{
    var images = [];
    images['on'] = 'http://static.jacoz.net/images/rate_staron.gif';
    images['off'] = 'http://static.jacoz.net/images/rate_staroff.gif';
    
    for (var i = 1; i <= num; i++) {
        $('star_' + id + '_' + i).src = images[action];
    }
};

Site.Directory = {
    init: function()
    {
        this.initRegisterNewClick();
        this.initRate();
    },
    
    initRegisterNewClick: function()
    {
        var me = this;
        
        $$('.dirlink').each(function(e)
        {
            var id = e.id.replace('dirlink', '');
            
            e.observe('mousedown', function(ev)
            {
                new Ajax.Request('/index.html', {postBody: 'action=click&id=' + id});
                new Ajax.Updater('dirlinkclicks' + id, '/index.html', {postBody: 'action=getclicks&id=' + id});
            });
        });
    },
    
    initRate: function()
    {
        $$('.dirlinkvote').each(function(e)
        {
            var id = e.id.replace('dirlinkvote', '');
            
            e.insert({after: ' <em style="cursor: hand; cursor: pointer; font-size: 10px; color: blue;" id="votelink_' + id + '">Vota</em><span id="votechoose_' + id + '"></span>'});
            $('votelink_' + id).observe('click', function(e)
            {
                Site.Directory.rateLink(id);
            });
        });
    },
    
    rateLink: function(id)
    {
        var div = $('votechoose_' + id);
        var link = $('votelink_' + id);
        var divContent = '<br />';
        
        (5).times(function(i)
        {
            i += 1;
            divContent += '<img style="cursor: hand; cursor: pointer;" ' +
                          'id="star_' + id + '_' + i + '" ' +
                          'src="http://static.jacoz.net/images/rate_staroff.gif" ' +
                          'onclick="Site.Directory.registerNewVote(' + i + ',' + id + ');" ' +
                          'onmouseover="colorStar(\'on\', ' + i + ',' + id + '); Site.Directory.printRateMessage(' + i + ',' + id + ');" ' +
                          'onmouseout="colorStar(\'off\', ' + i + ',' + id + '); Site.Directory.killRateMessage(' + id + ');" />';
        });
        divContent += '<span id="votemsg_' + id + '"></span>';
        
        if (link.innerHTML == 'Vota') {
            div.update(divContent).setStyle({display: 'inline'});
            link.update('Non votare');
        } else {
            div.update().hide();
            link.update('Vota');
        }
    },
    
    printRateMessage: function(vote, id)
    {
        var msg = ['Davvero brutto', 'Niente di speciale', 'Accettabile', 'Carino', 'Splendido'];
        
        $('votemsg_' + id).update(' <em style="font-size: 10px; color: #666666">' + msg[(vote - 1)] + '</em>');
    },
    
    killRateMessage: function(id)
    {
        $('votemsg_' + id).update();
    },
    
    registerNewVote: function(vote, id)
    {
        $('votechoose_' + id).update().hide();
        $('votelink_' + id).update('Vota');
        
        new Ajax.Updater('dirlinkvote' + id, '/index.html', {postBody: 'action=rate&ajax=true&id=' + id + '&vote=' + vote});
    }
};

Site.Directory.User = {
    currentSection: 'sites',
    scriptUrl: '/directory.html',
    menu: null,
    
    initConfig: function()
    {
        this.menu = new TabMenu(
           [['I tuoi siti', 'sites'], ['Aggiungi un sito', 'addnew']/*, ['Modifica / Elimina', 'edit']*/],
           {
              url: this.scriptUrl,
              mainPage: this.currentSection,
              onChange: this.onChangeTab
           }
        );
    },
    
    onChangeTab: function(w)
    {
        $$('#maincontent h1').first().update('Gestione directory - ' + w[0]);
    },
    
    goTo: function(sec)
    {
        this.menu.jumpTo(sec);
    },
    
    showOptions: function(id, w)
    {
        w = w || '';
        if (id == 'all') {
            (function()
            {
                if (w.innerHTML == 'Mostra le opzioni di tutti gli elementi') {
                    w.update('Nascondi le opzioni di tutti gli elementi');
                } else {
                    w.update('Mostra le opzioni di tutti gli elementi');
                }
            }).delay(0.5);
            
            $('directorysiteslist').select('[name="directorylinkoptions"]').each(function(element)
            {
                new Effect.toggle(element.id, 'blind', {duration: 0.5});
            });
        } else {
            new Effect.toggle('directorylinkoptions_' + id, 'blind', {duration: 0.5});
        }
    },
    
    execLinkOption: function(action, id)
    {
        if (action == 'modify') {
            this.menu.addTab('Modifica', 'edit', {
                jump: true,
                replace: true,
                parameters: 'sec=edit&action=modify&id=' + id,
                func: function(t, s) { alert('Selezionare un sito da ' + s[0].toLowerCase() + 're'); return false; }
            });
        } else if (action == 'delete') {
            this.menu.addTab('Elimina', 'edit', {position: 2, replace: true});
            this.menu.changeTab('edit');
            if (confirm('Sei sicuro di voler eliminare questo link dalla directory?')) {
                new Effect.Fade('directorylink_' + id);
                new Ajax.Updater('', this.scriptUrl, {postBody: 'sec=edit&action=delete&id=' + id});
            } else {
                new Effect.BlindUp('directorylinkoptions_' + id, {duration: 0.5});
            }
            this.menu.changeTab('sites');
            this.menu.delTab('edit');
        }
    },
    
    save: function(x)
    {
        $('accountconfig_form').select('[type="submit"]', '[type="button"]').invoke('disable');
        new Ajax.Updater('accountconfig_formresult', Site.Directory.User.scriptUrl, {postBody: x.formName.serialize()});
    },
    
    updateLink: function(id)
    {
        $('directorylinkoptions_' + id).hide();
        $('directorylinkmodify_' + id).hide();
        new Ajax.Updater('directorylinkinfo_' + id, this.scriptUrl, {postBody: 'sec=edit&action=info&id=' + id});
    }
};

