//
// $Id: ContentHeading.js,v 1.2 2010/03/25 18:08:18 steve Exp $
//
var ContentHeading = function() {

    this.cms    = new CMSActions()
    this.editor = new CMSEditor()


    this.init = function() {
    
        var self = this
        
        $('img.contentHeadingEditButton').click( function() { self.showEditor($(this)) })
        $('img.contentHeadingDeleteButton').click( function() { self.cms.deleteSection($(this), 'Heading') })
        $('img.contentHeadingMoveUpButton').click( function() { self.cms.move($(this), 'Up', 'Heading') })
        $('img.contentHeadingMoveDownButton').click( function() { self.cms.move($(this), 'Down', 'Heading') })
        $('img.contentHeadingAddBeforeButton').click( function() { self.cms.showAddSection($(this), 'Before', 'Heading') })
        $('img.contentHeadingAddAfterButton').click( function() { self.cms.showAddSection($(this), 'After', 'Heading') })
        $('input.contentHeadingSave').click( function() { self.saveDetails($(this)) })
    }
    
    this.showEditor = function(elem) {

        var self = this
        
        var id     = $(elem).attr('id').split('_')
        var editor = 'contentHeadingEditorEdit_' + id[1]

        self.editor.position(elem, editor)
    }
    
    this.saveDetails = function(elem) {
    
        var self = this
        
        var id = $(elem).attr('id').split('_')
        id = id[1]
        
        var text  = $('#contentHeadingEditorEditText_' + id).val()
        
        $.ajax({
                  type:     'POST',
                  url:      AJAXURL,
                  data:     'Action=saveContentHeading&SectionId=' + id +
                                                     '&Text=' + text,
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    } else {
                        self.cms.reloadPage('#contentHeadingEditorEdit_' + id)
                    }
                }
        })
    }
}
