//
// $Id: ContentText.js,v 1.3 2010/07/10 16:48:24 steve Exp $
//
var ContentText = function() {

    this.cms      = new CMSActions()
    this.editor   = new CMSEditor()
    this.textarea = new Textarea()    
    this.thickbox = new ThickBoxGeneric()
    this.url      = new Url()

    // Popup editor
    //
    this.init = function() {
    
        var self = this
        
        $('img.contentTextDeleteButton').click( function() { self.cms.deleteSection($(this), 'Paragraph') })
        $('img.contentTextMoveUpButton').click( function() { self.cms.move($(this), 'Up', 'Paragraph') })
        $('img.contentTextMoveDownButton').click( function() { self.cms.move($(this), 'Down', 'Paragraph') })
        $('img.contentTextAddBeforeButton').click( function() { self.cms.showAddSection($(this), 'Before', 'Paragraph') })
        $('img.contentTextAddAfterButton').click( function() { self.cms.showAddSection($(this), 'After', 'Paragraph') })
    }

    // Edit item
    //
    this.initText = function() {

        var self = this

        self.thickbox.init()

        self.id = self.url.clickedUrlArg('Id')

        self.loadTextParameters()

        $('#textSaveButton').click( function() { self.saveText() })

        $('#boldButton').click( function() { self.textarea.bold($('#textText')) })
        $('#italicButton').click( function() { self.textarea.italic($('#textText')) })
        $('#underlineButton').click( function() { self.textarea.underline($('#textText')) })
    }

    this.loadTextParameters = function() {

        var self = this

        $.ajax({
                  type:     'POST',
                  url:      CMSURL,
                  data:     'Action=loadTextParameters&InsertsId=insertType',
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    } else {

                        self.editor.inserters(data, 'textText', 'textTable')

                        if (self.id) {
                            self.loadText()
                        }
                    }
                }
        })    
    }

    this.loadText = function() {
    
        var self = this

        $.ajax({
                  type:     'POST',
                  url:      CMSURL,
                  data:     'Action=loadContentText&Id=' + self.id,
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    } else {
                        $('#textText').val(data.Text)
                    }
                }
        })
    }

    this.saveText = function(elem) {
    
        var self = this
        
        var text  = escape($('#textText').val())
        
        $.ajax({
                  type:     'POST',
                  url:      CMSURL,
                  data:     'Action=saveContentText&SectionId=' + self.id +
                                                  '&Text=' + text,
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    } else {
                        $('#contentText_' + self.id).html(data.Text)
                        tb_remove()
                    }
                }
        })
    }
}
