//
// $Id: ContentList.js,v 1.2 2010/03/25 18:08:18 steve Exp $
//
var ContentList = function() {

    this.changed  = 0
    
    this.cms      = new CMSActions()
    this.editor   = new CMSEditor()

    this.init = function() {
    
        var self = this
        
        $('img.contentListEditButton').click( function() { self.showEditor($(this)) })
        $('img.contentListDeleteButton').click( function() { self.cms.deleteSection($(this), 'List') })
        $('img.contentListMoveUpButton').click( function() { self.cms.move($(this), 'Up', 'List') })
        $('img.contentListMoveDownButton').click( function() { self.cms.move($(this), 'Down', 'List') })
        $('img.contentListAddBeforeButton').click( function() { self.cms.showAddSection($(this), 'Before', 'List') })
        $('img.contentListAddAfterButton').click( function() { self.cms.showAddSection($(this), 'After', 'List') })

        $('select.listType').change( function() { self.changeListType($(this)) })

        self.initButtons()
    }

    this.initButtons = function() {
    
        var self = this
        
        $('input.contentListNewItem').click( function() { self.newItem($(this)) })
        $('input.contentListDone').click( function() { self.hideEditor($(this)) })

        $('img.listItemSaveButton').click( function() { self.saveItem($(this)) })
        $('img.listItemDeleteButton').click( function() { self.deleteItem($(this)) })
        $('img.listItemMoveUpButton').click( function() { self.moveItem($(this), 'Up') })
        $('img.listItemMoveDownButton').click( function() { self.moveItem($(this), 'Down') })
    }
    
    this.showEditor = function(elem) {

        var self = this

        var id     = $(elem).attr('id').split('_')
        var editor = 'contentListEditorEdit_' + id[1]

        self.editor.position(elem, editor)
    }
    
    this.hideEditor = function(elem) {

        var self = this

        var id     = $(elem).attr('id').split('_')
        var editor = 'contentListEditorEdit_' + id[1]
        
        if (self.changed) {
            self.cms.reloadPage('#' + editor)
        } else {
            $('#' + editor).fadeOut()
        }
    }
    
    this.changeListType = function(elem) {
    
        var self = this
        
        var id         = $(elem).attr('id').split('_')
        id             = id[1]
        var listTypeId = $(elem).val()
        
        $.ajax({
                  type:     'POST',
                  url:      AJAXURL,
                  data:     'Action=changeListType&SectionId=' + id +
                                                 '&ListTypeId=' + listTypeId,
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    } else {
                        self.changed = 1
                    }
                }
        })
    }

    this.newItem = function(elem) {

        var self = this

        var id = $(elem).attr('id').split('_')
        id     = id[1]

        $.ajax({
                  type:     'POST',
                  url:      AJAXURL,
                  data:     'Action=newListItem&SectionId=' + id,
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    } else {
                        self.updateList(data.Content, id)
                    }
                }
        })
    }
    
    this.saveItem = function(elem) {

        var self = this

        var id        = $(elem).attr('id').split('_')
        var sectionId = id[1]
        id            = id[2]

        var link = escape($('#itemLink_' + id).val())
        var text = escape($('#itemText_' + id).val())

        $.ajax({
                  type:     'POST',
                  url:      AJAXURL,
                  data:     'Action=saveListItem&SectionId=' + sectionId +
                                               '&Id=' + id +
                                               '&Text=' + text +
                                               '&Link=' + link,
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    } else {
                        self.updateList(data.Content, sectionId)
                    }
                }
        })
    }
    
    this.moveItem = function(elem, direction) {

        var self = this

        var id        = $(elem).attr('id').split('_')
        var sectionId = id[1]
        id            = id[2]

        $.ajax({
                  type:     'POST',
                  url:      AJAXURL,
                  data:     'Action=moveListItem&SectionId=' + sectionId +
                                               '&Id=' + id +
                                               '&Direction=' + direction,
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    } else {
                        self.updateList(data.Content, sectionId)
                    }
                }
        })
    }
    
    this.deleteItem = function(elem, id) {

        var self = this

        var id        = $(elem).attr('id').split('_')
        var sectionId = id[1]
        id            = id[2]

        $.ajax({
                  type:     'POST',
                  url:      AJAXURL,
                  data:     'Action=deleteListItem&SectionId=' + sectionId +
                                                 '&Id=' + id,
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    } else {
                        self.updateList(data.Content, sectionId)
                    }
                }
        })
    }

    this.updateList = function(data, id) {
    
        var self = this
        
        self.changed = 1

        $('#contentListTable_' + id).replaceWith(data)
        self.initButtons()
    }
}
