/*
 * Applicaiton initialization
 */

/* Main Site */
function init () {
    addPopupToLinks();
}

/* CMS */
function cms_init () {
    
}

/*
 * Displaying/hiding the WYM editor
 */
function start_wym (element_id) {
    RedBox.showInline('wym_container');
    
    var container = $(element_id + '_block');
    var content = container.innerHTML;

    wym_block = $('wym_container');
    wym_block.insert(new Element('textarea', {
        id : element_id + "_wym",
        'class' : "wymeditor",
        name : "content[content]"
    }).update(content));

    wym_block.insert(
    {
        bottom:
        new Element('input', {
            type : 'hidden',
            name : "content[block_identifier]",
            value: element_id
        })
    });


    $(element_id + '_wym').insert({
        after : '<a link="#" onClick="cancel_wym(\'' + element_id + '\');">Cancel</a>'
    })
    $(element_id + '_wym').insert({
        after : '<input type="submit" class="wymupdate" value="Save Content"/>'
    });


    jQuery('.wymeditor').wymeditor({
        postInit: insert_image_button,
        classesItems: [
        {
            'name': 'popup',
            'title': 'Open link in new window',
            'expr': 'a'
        }
        ]
    });


    $$('.edit_btn').each(function(button) {
        button.hide();
    })

    RedBox.setWindowPosition('wym_container');
}

function cancel_wym (element_id) {

    var element = $(element_id + '_block');
    var content = $(element_id + '_wym').innerHTML;

    wym_block = $('wym_container');
    wym_block.update('');

    element.update(content.gsub('&lt;', '<').gsub('&gt;', '>'));
    $('edit_' + element_id + '_wym').show();

    $$('.edit_btn').each(function(button) {
        button.show();
    });

    RedBox.close('wym_container');
}

function insert_image_button (wym) {
    //construct the button's html
    var html = "<li class='wym_tools_newbutton'>"
    + "<a name='NewButton' href='#'"
    + " style='background-image:"
    + " url(/images/icons/add_image.gif)'>"
    + "Do something"
    + "</a></li>";

    //add the button to the tools box
    jQuery(wym._box)
    .find(wym._options.toolsSelector + wym._options.toolsListSelector)
    .append(html);

    //handle click event
    jQuery(wym._box)
    .find('li.wym_tools_newbutton a').click(function() {
        //do something
        new Ajax.Request('/page_assets/select', {
            asynchronous: true,
            parameters: 'wym=true',
            method: 'get'
        });

        return(false);
    });
}

function set_page_asset (image, id, identifier) {
    var element = $('image');

    if (! element) {
        element = $$('.image_drop').first();
        image_drop = true;

        element.removeClassName('image_drop');
    }

    var form = $$('form.edit_page').first();
    if (form) {
        form.insert({
            bottom: new Element('input', {
                type: 'hidden',
                name: 'page_assets[' + identifier + ']',
                value: id
            })
        });
    }


    var hidden = $('page_asset_id');
    if (hidden) {
        hidden.value = id;
    }
    
    element.down('img').remove();
    element.insert({
        bottom: new Element('img', {
            src: image
        })
    });
        
    RedBox.close();
}


/*
 * Drap/drop sortable page order
 */
function make_sortable() {
    var page_uls = $$("#pages ul");

    page_uls.each(function(item) {
        Sortable.create(
            item.identify(),
            {
                onUpdate:function(){
                    save = $('save_page_positions');
                    save.appear();
                    save.shake({
                        distance:2
                    });
                },
                dropOnEmpty:true,
                containment:$$("#pages ul")
            });
    });

    Sortable.create(
        "pages",
        {
            onUpdate:function(){
                save = $('save_page_positions');
                save.appear();
                save.shake({
                    distance:2
                });
            }
        });

    $('save_page_positions').hide();
}

/*
 * Selecting images
 */
function hideImageChooser () {
    $('image_chooser').hide();
    $$('.image_drop').first().removeClassName('image_drop');
}

/*
 * Publishing and navigating pages
 */
function hidePagePublishing () {
    $$('.publish').each(function(e) {
        e.hide();
    });
}

function hidePageNavigation () {
    $$('.navigation_selection').each(function(e) {
        e.hide();
    });
}

/*
 * Selecting uploaded files
 */
function changeFileUpload () {
    $('change_file_upload').hide();
    $('file_upload').show();
}

function doNotChangeFileUpload () {
    $('change_file_upload').show();
    $('file_upload').hide();
}

/*
 * Making links open in new windows
 */
function addPopupToLinks () {
    $$("a.popup").each(function(e) {
        e.observe('click', function(event) {
            event.stop();
            window.open(e.href);
        })
    });
}

document.observe("dom:loaded", init);
