DisamHandler = {
    container: null,
    disam: {
        container: null,
        disam: null
    },
    init: function( container ) {
        this.container = container;
        this.createDisam();
        this.setUpCloseButton();
        this.switchDisamDisplayState( 'none' );
    },
    setStyle: function( o, css ) {
        if ( navigator.userAgent.indexOf( 'MSIE' ) > -1 ) {
            o.style.setAttribute( 'cssText', this.IECss( css ) );
        } else {
            o.setAttribute( 'style', css );
        }
    },
    IECss: function( css ) {
        return css.replace( /opacity\:\s?.{3}/g, 'filter: alpha( opacity=75 )' );
    },
    createDisam: function() {
        this.dimensions = this.calculateDisamDimensions();
        var cssText = 'position: absolute; background: #FFF; z-index: 999999; ', containerCss, disamCss;
        var container = document.createElement( 'div' ), disam = document.createElement( 'div' ), close = document.createElement( 'a' );
        var parent = this.container.parentNode;
        var ie = navigator.userAgent.indexOf( 'MSIE' ) > -1;

        container.id = 'disam';
        containerCss = cssText + 'top: ' + this.dimensions['top'] +  'px; left: ' + this.dimensions['left'] + 'px; width: ' + this.dimensions['width'] + 'px; height: ' + this.dimensions['height'] + 'px; opacity: 0.5;';
        this.setStyle( container, containerCss );
        parent.appendChild( container );
        
        disamCss = cssText + 'top: ' + ( this.dimensions['top'] + 150 ) + 'px; left: ' + ( this.dimensions['left'] + 20 ) + 'px; ';
        disamCss += 'width: ' + ( this.dimensions['width'] - 72 ) + 'px; height: ' + ( this.dimensions['height'] - 332 ) + 'px; ';
        disamCss += 'border: 1px solid #000066; padding: 15px; overflow-x: hidden; overflow-y: auto;';
        this.setStyle( disam, disamCss );
        parent.appendChild( disam );

        close.className = 'MMclose';
        close.href = '#';
        closeCss = cssText + 'top: ' + ( this.dimensions['top'] + 160 ) + 'px; left: ' + ( this.dimensions['left'] + this.dimensions['width'] - 46 ) + 'px;';
        closeCss += 'width: 16px; height: 16px; background: url( css/'+campaign_var+'/images/multimap/close.png ) no-repeat;';
        this.setStyle( close, closeCss );
        parent.appendChild( close );

        return this.disam = { container: container, disam: disam, close: close };
    },
    calculateDisamDimensions: function() {
        var offset, dimensions;
        offset = this.getOffset( this.container, this.container.parentNode );
        dimensions = {
            width: Number( this.container.offsetWidth ),
            height: Number( this.container.offsetHeight ),
            left: offset.x,
            top: offset.y
        }
        return dimensions;
    },
    clearDisam: function() {
        this.disam.disam.innerHTML = '';
        this.switchDisamDisplayState( 'none' );
    },
    createResults: function( results, searchterm, callback ) {
        var anchor, title, div;
        div = document.createElement( 'div' );
        title = document.createElement( 'h3' );
        title.innerHTML = 'Multiple locations were found for: ' + searchterm;
        div.appendChild( title );
        for ( var i = 0, j = results.length; i < j; i++ ) {
            anchor = document.createElement( 'a' );
            anchor.href = '#';
            anchor.className = 'geoResult';
            anchor.innerHTML = results[i].address.display_name;
            anchor.result = results[i];
            anchor.onclick = callback;
            div.appendChild( anchor );
        }
        return div;
    },
    setUpCloseButton: function() {
        if ( this.disam && this.disam.close ) {
            this.disam.close.onclick = function() {
                DisamHandler.clearDisam();
                return false;
            }
        }
    },
    getOffset: function( o, parent ) {
        var ret = { x: o.offsetLeft, y: o.offsetTop };
        parent = parent ? parent : document.body;
        while( o.offsetParent && o.offsetParent != parent ) {
            o = o.offsetParent;
            ret.x += o.offsetLeft;
            ret.y += o.offsetTop;
        }
        return ret;
    },
    handleGeoError: function( results, error, type, o ) {
        var me = DisamHandler, callback;
        if ( error == 'MM_GEOCODE_NO_MATCHES' ) {
            var o = document.createElement( 'div' );
            o.innerHTML =  '<h3>Sorry, we could not find any locations that matched your search.</h3>';
			o.innerHTML += '<p>If you need assistance, please follow the link below, fill in your contact details and we\'ll call you back.</p>';
			o.innerHTML += '<a href=\'how-to-buy\' style=\'font-weight: bold; color: #000066; text-decoration: underline;\'>Help with my enquiry</a>';

            me.populateDisam( o );
        } else {
            if ( typeof type != 'undefined' ) {
                if ( type == 'location' ) {
                    var searchterm = locator.search_field.value;
                    callback = function() {
                        locator.handleResult( this.result );
                        DisamHandler.clearDisam();
                        return false;
                    }
                    me.populateDisam( me.createResults( results, searchterm, callback ) );
                } else if ( type == 'route' ) {
                    callback = function() {
                        o.init( this.result, o.end );
                        DisamHandler.clearDisam();
                        return false;
                    }
                    me.populateDisam( me.createResults( results.results, o.start.qs, callback ) );
                }
            }
        }
    },
    populateDisam: function( child ) {
        this.clearDisam();
        this.disam.disam.appendChild( child );
        this.switchDisamDisplayState( 'block' );
    },
    switchDisamDisplayState: function( style ) {
        for ( var element in this.disam ) {
            this.disam[element].style.display = style;
        }
    }
}
