/*
    +----------------------------------------------------------------------+
    | Ossigeno is a modularized CMS                                        |
    | Copyright (C) 2004-2007 Neovision                                    |
    | [http://www.neovision.it]                                            |
    | Giorgio Sironi, Roberto Reposo                                       |
    +----------------------------------------------------------------------+
    |  This program is free software; you can redistribute it and/or       |
    | modify it under the terms of the GNU General Public License as       |
    | published by the Free Software Foundation; either version 2 of the   |
    | License, or (at your option) any later version.                      |
    |  This program is distributed in the hope that it will be useful,     |
    | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
    | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
    | GNU General Public License for more details.                         |
    |  You should have received a copy of the GNU General Public License   |
    | along with this program; if not, write to the Free Software          |
    | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 |
    | USA                                                                  |
    +----------------------------------------------------------------------+
    Module: ossigeno-sections
    File
    /sections/buttons.js
    This file defines javascript routines for module
*/

//form utilities
function otk_html_sections_a(area)
{
    var id_a = prompt(otk_lang_buttons_sections_id_a, '');
    if (id_a == null) {
        exit;
    }
    var text = document.getElementById(area);
    insertText(text, '{{' + id_a + '}}');
}

function otk_html_sections_s(area)
{
    var id_s = prompt(otk_lang_buttons_sections_id_s, '');
    if (id_s == null) {
        exit;
    }
    var text = document.getElementById(area);
    insertText(text, '{{section=' + id_s + '}}');
}

function otk_html_sections_redirect(area)
{
    var url = prompt(otk_lang_buttons_sections_url, '');
    if (url == null) {
        exit;
    }
    var text = document.getElementById(area);
    text.value = '#REDIRECT ' + url + '\n' + text.value;
}

function otk_sections_update_comments(element, url)
{
    element = $(element);
    new Ajax.Request(url,  { 
        method: 'get',
        parameters: {ajax: 1},
        onSuccess: function(result) {
            element.update(result.responseText);
            element.getElementsBySelector('a').each(
                    function(link) {
                        if (!link.hasClassName('no_targetting')) {
                            link.observe('click', function(e) {
                                    otk_sections_update_comments(element, link.href);
                                    Event.stop(e);
                                });
                        }
                    }
                );
        },
        onComplete: otk_ajax
    });
    return false;
}

function otk_sections_open_comment(a, id_c, url)
{
    a = $(a);
    comment = $('sections_article_comment_' + id_c);
    if (!comment) {
        new Ajax.Request(url, { 
            method: 'get',
            parameters : {ajax: 1},
            insertion : Insertion.After,
            onSuccess: function(result) {
                new Insertion.After(a, result.responseText);
                a.up().getElementsBySelector('a').each(
                    function(link) {
                        if (!link.hasClassName('no_targetting')) {
                            link.observe('click', function(e) {
                                    otk_sections_update_comments(a.up(), link.href);
                                    Event.stop(e);
                                });
                        }
                    }
                );
            },
            onComplete: otk_ajax
        });
    } else if (comment.visible()) {
        comment.hide();
    } else {
        comment.show();
    }
    return false;
}
