//
// $Id: Menu.js,v 1.1.1.1 2010/02/26 08:30:05 steve Exp $
//
var Menu = function() {

    this.id     = 0
    this.script = 0
    
    this.page   = new Page()
    
    this.init = function() {
    
        var self = this
        
        $('img.menuEditButton').click( function() { self.showEditor($(this)) })
        $('img.menuDeleteButton').click( function() { self.deleteItem($(this)) })
        $('img.menuMoveUpButton').click( function() { self.move($(this), 'Up') })
        $('img.menuMoveDownButton').click( function() { self.move($(this), 'Down') })
        $('img.menuAddBeforeButton').click( function() { self.addItem($(this), 'Before') })
        $('img.menuAddAfterButton').click( function() { self.addItem($(this), 'After') })
        $('img.menuAddChildButton').click( function() { self.addChildItem($(this)) })
        $('input.menuSave').click( function() { self.saveDetails($(this)) })
        $('input.menuInsert').click( function() { self.insertItem($(this)) })
        $('input.menuChildInsert').click( function() { self.insertChildItem($(this)) })
        
        $('#menuEditorEditText').keyup( function() { self.populateScript($(this)) })
        $('#menuEditorEditScript').keyup( function() { self.script = 1 })

        $('#menuEditorChildEditText').keyup( function() { self.populateChildScript($(this)) })
        $('#menuEditorChildEditScript').keyup( function() { self.script = 1 })
    }
    
    this.showEditor = function(elem) {

        var self = this
        
        var id     = $(elem).attr('id').split('_')
        var editor = 'menuEditorEdit_' + id[1]
        var left   = $(elem).offset().left
        var top    = $(elem).offset().top
        
        $('#' + editor).css('top', top + 'px')
        $('#' + editor).css('left', left + 'px')
        $('#' + editor).fadeIn()
    }
    
    this.populateScript = function(elem) {
    
        var self = this
        
        if (self.script) {
            return
        }
        
        var val = $(elem).val()
        val = val.replace(/\s/g, '')
        val = val.substr(0, 1).toLowerCase() + val.substr(1) + '.php'
        
        $('#menuEditorEditScript').val(val)
    }

    this.populateChildScript = function(elem) {
    
        var self = this
        
        if (self.script) {
            return
        }
        
        var val = $(elem).val()
        val = val.replace(/\s/g, '')
        val = val.substr(0, 1).toLowerCase() + val.substr(1) + '.php'
        
        $('#menuEditorChildEditScript').val(val)
    }
    
    this.saveDetails = function(elem) {
    
        var self = this

        var id = $(elem).attr('id').split('_')
        id = id[1]
        
        var text  = escape($('#menuEditorEditText_' + id).val())
        
        $.ajax({
                  type:     'POST',
                  url:      AJAXURL,
                  data:     'Action=saveMenuItem&Id=' + id +
                                               '&Label=' + text,
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    } else {
                        alert('Content Updated')
                    }
                    $('#menuEditorEdit_' + id).fadeOut()
                    location.replace(location.href)
                }
        })
    }
    
    this.move = function(elem, direction) {
    
    }
    
    this.deleteItem = function(elem, where) {

        var self = this

        var id = $(elem).attr('id').split('_')
        id = id[1]

        $.ajax({
                  type:     'POST',
                  url:      AJAXURL,
                  data:     'Action=deletePage&Id=' + id,
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    } else {
                        alert('Content Updated')
                    }
                    $('#menuEditorEdit_' + id).fadeOut()
                    location.replace(location.href)
                }
        })
    }
    
    this.addItem = function(elem, where) {

        var self = this

        var id     = $(elem).attr('id').split('_')
        self.id    = id[1]
        
        var editor = 'menuEditorEdit'
        var left   = $(elem).offset().left
        var top    = $(elem).offset().top
        
        $('#newItemTitle').html(where)

        $('#' + editor).css('top', top + 'px')
        $('#' + editor).css('left', left + 'px')
        $('#' + editor).fadeIn()
    }

    this.addChildItem = function(elem) {

        var self = this

        var id     = $(elem).attr('id').split('_')
        self.id    = id[1]
        
        var editor = 'menuEditorChildEdit'
        var left   = $(elem).offset().left
        var top    = $(elem).offset().top
        
        $('#parentId').html(self.id)

        $('#' + editor).css('top', top + 'px')
        $('#' + editor).css('left', left + 'px')
        $('#' + editor).fadeIn()
    }
    
    this.insertItem = function() {
    
        var self = this

        var text   = escape($('#menuEditorEditText').val())
        var script = escape($('#menuEditorEditScript').val())
        var where  = $('#newItemTitle').html()

        $.ajax({
                  type:     'POST',
                  url:      AJAXURL,
                  data:     'Action=newPage&Id=' + self.id +
                                          '&Where=' + where +
                                          '&Label=' + text +
                                          '&Script=' + script,
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    } else {
                        alert('Content Updated')
                    }
                    $('#menuEditorEdit').fadeOut()
                    self.page.replacePage(script)
                }
        })

    }    

    this.insertChildItem = function() {
    
        var self = this

        var text   = escape($('#menuEditorChildEditText').val())
        var script = escape($('#menuEditorChildEditScript').val())

        $.ajax({
                  type:     'POST',
                  url:      AJAXURL,
                  data:     'Action=newPage&ParentId=' + self.id +
                                          '&Label=' + text +
                                          '&Script=' + script,
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    } else {
                        alert('Content Updated')
                    }
                    $('#menuEditorEdit').fadeOut()
                    self.page.replacePage(script)
                }
        })

    }
}
