//
// $Id: BoatModel.js,v 1.1.1.1 2010/02/26 08:30:04 steve Exp $
//
var BoatModel = function() {

    this.editor   = new CMSEditor()
    this.html     = new Html()
    this.textarea = new Textarea()
    this.thickbox = new ThickBoxGeneric()
    this.url      = new Url()
    
    this.init = function() {
    
        var self = this

        self.id             = self.url.clickedUrlArg('Id')
        self.manufacturerId = self.url.clickedUrlArg('ManufacturerId')
        self.name           = self.url.clickedUrlArg('Name')

        self.thickbox.init()

        if (self.id) {
            $('#modelTitle').html(self.name)
            $('#modelName').html(self.name)
            $('#modelSaveButton').val('Save')
        }
        self.loadBoatParameters()

        $('img.boldButton').click( function() { self.textarea.bold($('#modelText')) })
        $('img.italicButton').click( function() { self.textarea.italic($('#modelText')) })
        $('img.underlineButton').click( function() { self.textarea.underline($('#modelText')) })

        $('#insertIntoModelInsert').click( function() { self.insertLink() })
        $('#insertIntoModelLinkSubmit').click( function() { self.insertTheLink() })
        $('#insertIntoModelLinkCancel').click( function() { self.cancelInsertLink() })

        $('#modelSaveButton').click( function() { self.saveDetails() })
    }
    
    this.loadBoatParameters = function() {

        var self = this

        $.ajax({
                  type:     'POST',
                  url:      CMSURL,
                  data:     'Action=loadModelParameters&InsertsId=insertType',
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    } else {

                        self.editor.inserters(data, 'modelText', 'modelTable')

                        if (self.id) {
                            self.loadModel()
                        }
                    }
                }
        })    
    }

    this.loadModel = function() {
    
        var self = this
        
        $.ajax({
                  type:     'POST',
                  url:      CMSURL,
                  data:     'Action=getModel&Id=' + self.id,
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    } else {
                    
                        self.name     = data.Model.modelname
                        self.text     = data.Model.text
                        self.moreInfo = data.Model.moreinfolink
                        self.imagePos = data.Model.imageposition
                        
                        $('#modelName').val(self.name)
                        $('#modelText').val(self.text)
                        $('#modelMoreInfoLink').val(self.moreInfo)
                        $('#modelImagePosition').val(self.imagePos)
                    }
                }
        })
    }

    this.hideBackgroundEditor = function() {

        $('#modelTable').addClass('editorHidden')
        $('#modelTable *').addClass('editorHidden')
    }

    this.showBackgroundEditor = function() {

        $('#modelTable').removeClass('editorHidden')
        $('#modelTable *').removeClass('editorHidden')
    }

    this.saveDetails = function() {
    
        var self = this

        var name          = escape($('#modelName').val())
        var text          = escape($('#modelText').val())
        var moreInfoLink  = escape($('#modelMoreInfoLink').val())
        var imagePosition = $('#modelImagePosition').val()

        $.ajax({
                  type:     'POST',
                  url:      CMSURL,
                  data:     'Action=saveModel&Id=' + self.id +
                                            '&ManufacturerId=' + self.manufacturerId +
                                            '&Name=' + name +
                                            '&MoreInfoLink=' + moreInfoLink +
                                            '&ImagePosition=' + imagePosition +
                                            '&Text=' + text,
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    } else {
                        alert('Details saved')
                        $('#modelName_' + self.id).html(unescape(name))
                    }
                    tb_remove()
                }
        })
    }
}
