//
// $Id: CMSEditor.js,v 1.2 2010/07/10 16:48:24 steve Exp $
//
var CMSEditor = function() {

    this.html     = new Html()
    this.textarea = new Textarea()

    this.init = function() {

    }

    this.position = function(elem, editor) {

        var self = this
        
        var left   = $(elem).offset().left
        var top    = $(elem).offset().top

        if (typeof(EDITOROFFSETDIV) != 'undefined') {
            left = left - $('#' + EDITOROFFSETDIV).offset().left
        }        

        $('#' + editor).css('top', top + 'px')
        $('#' + editor).css('left', left + 'px')
        $('#' + editor).fadeIn()
    }

    // Inserters 
    //
    this.inserters = function(data, target, table) {

        var self = this

        self.inserterTarget = target
        self.inserterTable  = table

        $('#insertTd').html(data.Inserts)
        $('#inserters').html(data.Inserters)

        $('#insertInsert').click( function() { self.insertLink() })

        $('#insertLinkSubmit').click( function() { self.insertTheLink() })
        $('#insertLinkCancel').click( function() { self.cancelInsertLink() })

        $('#insertFormLinkSubmit').click( function() { self.insertTheFormLink() })
        $('#insertFormLinkCancel').click( function() { self.cancelInsertFormLink() })

        $('#insertDocumentSubmit').click( function() { self.insertTheDocumentLink() })
        $('#insertDocumentCancel').click( function() { self.cancelInsertDocumentLink() })

        $('#insertCommitteeRoleSubmit').click( function() { self.insertTheCommitteeRole() })
        $('#insertCommitteeRoleCancel').click( function() { self.cancelInsertCommitteeRole() })

        $('#insertContactSubmit').click( function() { self.insertTheContact() })
        $('#insertContactCancel').click( function() { self.cancelInsertContact() })

        $('#insertContactUsSubmit').click( function() { self.insertTheContactUs() })
        $('#insertContactUsCancel').click( function() { self.cancelInsertContactUs() })

        $('#insertCSVTableSubmit').click( function() { self.insertTheCSVTable() })
        $('#insertCSVTableCancel').click( function() { self.cancelInsertCSVTable() })

        self.priceListsHaveSections = $('#priceListsHaveSections').val()
        $('#insertPriceListSubmit').click( function() { self.insertThePriceList() })
        $('#insertPriceList').change( function() { self.getPriceListSections() })
        $('#insertPriceListCancel').click( function() { self.cancelInsertPriceList() })

        $('#insertOpeningTimesSubmit').click( function() { self.insertTheOpeningTimes() })
        $('#insertOpeningTimesCancel').click( function() { self.cancelInsertOpeningTimes() })
    }

    this.hideBackgroundEditor = function() {

        $('#' + self.inserterTable).addClass('editorHidden')
        $('#' + self.inserterTable + ' *').addClass('editorHidden')
    }

    this.showBackgroundEditor = function() {

        $('#' + self.inserterTable).removeClass('editorHidden')
        $('#' + self.inserterTable + ' *').removeClass('editorHidden')
    }

    this.insertLink = function() {
    
        self = this
        
        val = $('#insertType').val()
        if (val == 0) {
            alert('Please select an element to insert')
            return
        }
        
        switch(val) {
            case 'Link':
               self.hideBackgroundEditor()
               $('#insertLinkDiv').fadeIn()
                break
            case 'Form Link':
                self.hideBackgroundEditor()
                $('#insertFormLinkDiv').fadeIn()
                break
            case 'Document':
                self.hideBackgroundEditor()
                $('#insertDocumentDiv').fadeIn()
                break
            case 'Contact':
                self.hideBackgroundEditor()
                $('#insertContactDiv').fadeIn()
                break
            case 'Contact Us':
                self.hideBackgroundEditor()
                $('#insertContactUsDiv').fadeIn()
                break
            case 'Committee Role':
                self.hideBackgroundEditor()
                $('#insertCommitteeRoleDiv').fadeIn()
                break
            case 'Table From CSV File':
                self.hideBackgroundEditor()
                $('#insertCSVTableDiv').fadeIn()
                break
            case 'Price List':
                self.hideBackgroundEditor()
                $('#insertPriceListDiv').fadeIn()
                break
            case 'Opening Times':
                self.hideBackgroundEditor()
                $('#insertOpeningTimesDiv').fadeIn()
                break
            case 'Plain List':
                self.textarea.insertList('#' + self.inserterTarget, 'Plain')
                break
            case 'Numbered List':
                self.textarea.insertList('#' + self.inserterTarget, 'Numbered')
                break
        }
    }

    // Contacts
    //
    this.cancelInsertContact = function() {
    
        $('#contactSelect').val('')
        $('#insertContactSubject').val('')
        $('#insertContactDiv').fadeOut()
        self.showBackgroundEditor()
    }

    this.insertTheContact = function() {
    
        var self = this

        var contact = $('#contactSelect').val()
        var name    = $('#contactSelect option:selected').html()
        var text    = $('#contactText').val()        
        var subject = $('#contactSubject').val()        

        if (! text) { text = name }
 
        self.textarea.insertAtCaret('#' + self.inserterTarget, " <a href='html/contact.html?height=480&width=480&Contact=" + contact + "&Subject=" + subject + "' class='thickbox'>" + text + "</a> ")
        self.cancelInsertContact()
    }

    // Contacts
    //
    this.cancelInsertContactUs = function() {
    
        $('#contactUsSelect').val('')
        $('#insertContactUsSubject').val('')
        $('#insertContactUsDiv').fadeOut()
        self.showBackgroundEditor()
    }

    this.insertTheContactUs = function() {
    
        var self = this

        var contact = $('#contactUsSelect').val()
        var name    = $('#contactUsSelect option:selected').html()
        var text    = $('#contactUsText').val()        
        var subject = $('#contactUsSubject').val()        

        if (! text) { text = name }
 
        self.textarea.insertAtCaret('#' + self.inserterTarget, " <a href='html/contact.html?height=480&width=480&Contact=" + contact + "&Subject=" + subject + "' class='thickbox'>" + text + "</a> ")
        self.cancelInsertContactUs()
    }

    // Links
    //
    this.cancelInsertLink = function() {
    
        $('#insertLinkLink').val('')
        $('#insertLinkText').val('')
        $('#insertLinkDiv').fadeOut()
        self.showBackgroundEditor()
    }

    this.insertTheLink = function() {
    
        var linkLink = $('#insertLinkLink').val()
        var linkText = $('#insertLinkText').val()        
        var siteLink = $('#pageLinkSelect').val()

        if (siteLink != 0) {
            linkLink = siteLink
            if (! linkText) {
                linkText = $('#pageLinkSelect option:selected').html()
            }
        } else {
            if (! linkLink.match(/^http:\/\//)) {
                linkLink = 'http://' + linkLink
            }
        }
        if (! linkText) { linkText = linkLink }

        self.textarea.insertAtCaret('#' + self.inserterTarget, " <a href='" + linkLink + "'>" + linkText + "</a> ")

        self.cancelInsertLink()
    }

    // Form Links
    //
    this.cancelInsertFormLink = function() {
    
        $('#insertFormLink').val('')
        $('#insertFormText').val('')
        $('#insertFormLinkDiv').fadeOut()
        self.showBackgroundEditor()
    }

    this.insertTheFormLink = function() {

        var self = this

        var linkId   = $('#insertFormLink').val()
        var linkText = $('#insertFormLinkText').val()
    
        var linkLink = $('#insertFormUrl_' + linkId).val()

        if (! linkText) { linkText = $('#insertFormText_' + linkId).val() }
        self.textarea.insertAtCaret('#' + self.inserterTarget, " <a class='thickbox formLink' href='" + linkLink + "&Id={Id}'>" + linkText + "</a> ")

        self.cancelInsertFormLink()
    }    

    // Document
    //
    this.cancelInsertDocumentLink = function() {
    
        $('#insertDocumentLink').val('')
        $('#insertDocumentText').val('')
        $('#insertDocumentDiv').fadeOut()
        self.showBackgroundEditor()
    }

    this.insertTheDocumentLink = function() {

        var self = this

        var documentId   = $('#insertDocument').val()
        var documentText = $('#insertDocumentText').val()

        if (! documentText) { documentText = $('#insertDocument option:selected').html() }
        //self.textarea.insertAtCaret('#' + self.inserterTarget, " <a class='thickbox' href='" + documentId + "&Id={Id}'>" + documentText + "</a> ")
        self.textarea.insertAtCaret('#' + self.inserterTarget, " <a href='text/docs/" + documentId + "'>" + documentText + "</a> ")
         
        self.cancelInsertDocumentLink()
    }   

    // Table from CSV File
    //
    this.cancelInsertCSVTable = function() {
    
        var self = this

        self.html.setCheckBoxValue($('#insertCSVTableHeader'), 1) 
        $('#insertCSVTableDiv').fadeOut()
        self.showBackgroundEditor()
    }

    this.insertTheCSVTable = function() {

        var self = this

        var file     = $('#insertCSVTableFile').val()

        if (file == 0) {
            alert('Please select a file')
            return
        }
        var fileName  = $('#insertCSVTableFile option:selected').html()
        var header    =  self.html.getCheckBoxValue($('#insertCSVTableHeader')) 
        var delimiter =  $('#insertCSVTableDelimiter').val()
        var align     =  $('#insertCSVTableAlignment').val()

        self.textarea.insertAtCaret('#' + self.inserterTarget, "[CSVTable file='" + fileName + 
                                                                       "' header='" + header +
                                                                       "' delimiter='" + delimiter + 
                                                                       "' align='" + align + 
                                                                       "']\n")

        self.cancelInsertCSVTable()

        $('#uploadCSVFileForm').submit()
    }

    // Price List
    //
    this.getPriceListSections = function() {

        var self = this

        if (! self.priceListsHaveSections) {
            return
        }
        var list = $('#insertPriceList').val()

        if (! list) {
            return
        }

        $.ajax({
                  type:     'POST',
                  url:      CMSURL,
                  data:     'Action=getPriceListSectionsDropDown&Id=' + list,
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    } else {
                        $('#insertPriceListSectionTd').html(data.Sections)
                    }
                }
        })
    }

    this.cancelInsertPriceList = function() {

        var self = this
   
        $('#insertPriceList').val('')
        $('#insertPriceListSectionTd').html('')

        $('#insertPriceListDiv').fadeOut()
        self.showBackgroundEditor()
    }

    this.insertThePriceList = function() {

        var self = this
 
        var list = $('#insertPriceList').val()

        if (list == 0) {
            alert('Please select a price list')
            return
        }
 
        var section = 0
        if (self.priceListsHaveSections) {
            section = $('#insertPriceListSections').val()
        }             

        if (section != 0) {
            self.textarea.insertAtCaret('#' + self.inserterTarget, "[PriceList list='" + list + "' section='" + section + "']") 
        } else {
            self.textarea.insertAtCaret('#' + self.inserterTarget, "[PriceList list='" + list + "']") 
        }
        
        self.cancelInsertPriceList() 
    }

    // Opening Times
    //
    this.cancelInsertOpeningTimes = function() {

        var self = this
    
        $('#insertOpeningTimesDiv').fadeOut()
        self.showBackgroundEditor()
    }

    this.insertTheOpeningTimes = function() {

        var self = this

        self.textarea.insertAtCaret('#' + self.inserterTarget, "[OpeningTimes]") 

        self.cancelInsertPriceList() 
    }
}


