//
// $Id: CMSGeneric.js,v 1.1 2010/07/10 16:48:25 steve Exp $
//
var CMSGeneric = function() {

    this.currentDiv = ''

    this.position = function(elem, leftIndent, topIndent) {

        var self = this
        
        var id = $(elem).attr('id')
        if (id) {

            var left = parseInt($(elem).offset().left) + leftIndent
            var top  = parseInt($(elem).offset().top) + topIndent
            
            id = id.split('_')
            var editor = id[0] + 'Editor_' + id[1]

            if (typeof(EDITOROFFSETDIV) != 'undefined') {
                left = left - $('#' + EDITOROFFSETDIV).offset().left
            }

            $('#' + editor).css('top', top + 'px')
            $('#' + editor).css('left', left + 'px')
        }
    }

    this.showEditor = function(elem, classStr) {

        var self = this

        if (self.currentDiv) { 
            $('#' + self.currentDiv).removeClass('cmsSelected')
        }

        var id = $(elem).attr('id')
        if (id && ! id.match(/Editor/) && id != self.currentDiv) {
            self.currentDiv = id
            $(elem).addClass('cmsSelected')

            ids = id.split('_')
            var editor = ids[0] + 'Editor_' + ids[1]
            
            $('.editor').each(
                function() {
                    if ($(this).attr('id') != editor) {
                        $(this).fadeOut()
                    }
                })

            $('#' + editor).fadeIn()
        }
    }

    this.hideEditor = function(elem) {

        var self = this

        var id = $(elem).attr('id').replace(/Close/, 'Editor')
        $('#' + id).fadeOut()
    }

    this.hideSubEditor = function(elem) {

        var self = this

        var id = $(elem).attr('id').replace(/Close/, 'Edit')
        $('#' + id).fadeOut()
    }

    this.hideNewSectionEditor = function(elem) {

        var self = this

        var id = $(elem).attr('id').replace(/Close/, '')
        $('#' + id).fadeOut()
    }

    this.addNewSection = function(elem) {
    
        var self = this

        var id         = $(elem).attr('id').split('_')
        var type       = id[1]
        id             = id[2]

        // Small hack(s) for Contact Us Link and Small Heading!
        //
        if (type == 'Contact') {
            type = 'Contact_Us_Link'
            id   = 0
        }
        //if (type == 'Small Heading') {
        //    type = 'Heading Small'
        //}

        var where      = $('#newSectionTitle_' + type + '_' + id).html()
        var insertType = $('#newSectionSelect_' + type + '_' + id).val()
        var pageId     = $('#currentPageId').val()

        $.ajax({
                  type:     'POST',
                  url:      AJAXURL,
                  data:     'Action=insertNewSection&PageId=' + pageId +
                                                   '&SectionId=' + id +
                                                   '&Type=' + type +
                                                   '&SectionTypeId=' + insertType +
                                                   '&Where=' + where,
                  dataType: 'json',
                  timeout:  40000,
            error:
                function() {
                    return false;
                },
            success:
                function(data) {
                    if (data.Error) {
                        alert(data.Message)
                    }
                    location.reload()
                }
        })
    }
}
