//
// $Id: Filters.js,v 1.1 2010/03/25 18:08:18 steve Exp $
//
var Filters = function() {

    this.html     = new Html()
    this.thickbox = new ThickBoxGeneric()
    this.url      = new Url()
    
    // Listing functions
    //
    this.initCMS = function() {

        var self = this

        self.type = $('#filterType').val()

        $('#cmsFiltersTable .enableFilter').click( function() { self.enable($(this)) })
        $('#cmsFiltersTable .deleteFilter').click( function() { self.deleteFilter($(this)) })

    }

    this.enable = function(elem) {
    
        var self = this
        
        var elemId = $(elem).attr('id')
        var enable = self.html.getCheckBoxValue('#' + elemId)
        id         = elemId.split('_')
        id         = id[1]
                
        $('#filterTr_' + id).addClass('selectedRow')
        if (enable == 0) {
            action = 'disableFilter'
            if (! confirm('Disable this ' + self.type + ' filter?')) {
                self.html.setCheckBoxValue('#' + elemId, 1)
                $('#filterTr_' + id).removeClass('selectedRow')
                return
            }
        } else {
            action = 'enableFilter'
            if (! confirm('Enable this ' + self.type + ' filter?')) {
                self.html.setCheckBoxValue('#' + elemId, 0)
                $('#filterTr_' + id).removeClass('selectedRow')
                return
            }
        }
        
        $('#filterTr_' + id).removeClass('selectedRow')
        
        $.ajax({
                  type:     'POST',
                  url:      CMSURL,
                  data:     'Action=' + action + '&Id=' + id,
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    } else {
                        if (enable == 0) {
                            $('#filterTr_' + id).addClass('disabledTr')
                        } else {
                            $('#filterTr_' + id).removeClass('disabledTr')
                        }
                    }
                }
        })
    }
    
    this.deleteFilter = function(elem) {
    
        var self = this
        var id     = $(elem).attr('id')
        id         = id.split('_')
        id         = id[1]

        $('#filterTr_' + id).addClass('selectedRow')
        if (! confirm('Delete this ' + self.type + ' filter?')) {
            self.html.setCheckBoxValue(id, 1)
            $('#filterTr_' + id).removeClass('selectedRow')
            return
        }

        $('#filterTr_' + id).remove()
        
        $.ajax({
                  type:     'POST',
                  url:      CMSURL,
                  data:     'Action=deleteFilter&Id=' + id,
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    }
                }
        })
    }

    // Filter editing
    //
    this.init = function() {
    
        var self = this
        
        self.thickbox.init()

        self.id   = self.url.clickedUrlArg('Id')
        self.type = self.url.clickedUrlArg('Type')
        self.stem = self.url.clickedUrlArg('Stem')
        
        if (self.id) {
            $('#filterTitle').html('Edit Filter')
            self.loadFilter()
        } else {
            $('#filterTitle').html('Create Filter')
            self.html.setCheckBoxValue('#filterEnabled', 1)
        }
        $('#filterSaveButton').click( function() { self.saveFilter() })
    }
    
    this.loadFilter = function() {
    
        var self = this

        $.ajax({
                  type:     'POST',
                  url:      CMSURL,
                  data:     'Action=loadFilter&Id=' + self.id,
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    } else {
                        $('#filterStem').val(data.Filter.stem)
                        self.html.setCheckBoxValue('#filterEnabled', data.Filter.enabled)
                    }
                }
        })
    }

    this.saveFilter = function(type, ucType) {
    
        var self = this
        
        var stem    = $('#filterStem').val()
        var enabled = self.html.getCheckBoxValue('#filterEnabled')

        if (stem == '') {
            alert('Please enter a filter')
            return
        }

        $.ajax({
                  type:     'POST',
                  url:      CMSURL,
                  data:     'Action=saveFilter&Id=' + self.id +
                                             '&Stem=' + stem +
                                             '&Type=' + self.type + 
                                             '&Enabled=' + enabled,
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    } else {
                        $('#content').replaceWith(data.Filters)
                        self.thickbox.applyThickBox('#content .thickbox')
                        if (self.id) {
                            alert('Filter updated')
                        } else {
                            alert('Filter created')
                        }
                        self.initCMS()
                    }
                    tb_remove()
                }
        })
    }
}
