/**
* @contructor MMDisambiguation Provides a disambiguation handler for geocodes
* @param node {Element} Element for the panel to be overlaid on, defaults to the mapviewer container
* @param classes {Object} Javascript object literal container css classname for elements
* @param addResize {Boolean} True adds a resize call to reset the disambiguation panels dimensions on window resize
**/
MMDisambiguation = function( node, classes, addResize ) {
    this.node = node ? node : Page.oMap.getContainer().parentNode;
    new MMElement( this.node );
    this.cssClasses = {};
    this.cssClasses.container = classes && classes.container ? classes.container : 'MMDisambiguation';
    this.cssClasses.panel = classes && classes.panel ? classes.panel : 'MMPanel';
    this.init( addResize );
}

MMDisambiguation.prototype.container = null;
MMDisambiguation.prototype.panel = null;

MMDisambiguation.prototype.init = function( addResize ) {
    var me = this;
    this.createContainer();
    // add an event to resize the container on window resize
    if ( addResize ) {
        GEvent.addListener( window, 'resize', function() {
            me.setContainerDimensions();
        } );
    }
}

MMDisambiguation.prototype.createContainer = function() {
    // create the container and the panel
    this.container = new MMElement( 'div' ).addCssClass( this.cssClasses.container );
    this.panel = new MMElement( 'div' ).addCssClass( this.cssClasses.panel );
    this.container.style.position = this.panel.style.position = 'absolute';

    document.body.appendChild( this.container );
    document.body.appendChild( this.panel );
    // clear the elements and set the dimensions
    this.clearDisam();
}

MMDisambiguation.prototype.clearDisam = function() {
    this.panel.removeAllChildren();
    this.toggleDisam( false );
}

MMDisambiguation.prototype.populateDisam = function( content ) {
    // make sure its empty before we add anything
    this.setContainerDimensions();
    this.panel.removeAllChildren();
    // if content is a string we'll set the inner html otherwise append a child element
    if ( typeof content == 'string' ) {
        this.panel.innerHTML = content;
    } else {
        this.panel.appendChild( content );
    }
    // ...and display the disam ...
    this.toggleDisam( true );
}

MMDisambiguation.prototype.toggleDisam = function( show ) {
    return this.container.style.display = this.panel.style.display = ( show ) ? 'block' : 'none';
}

MMDisambiguation.prototype.setContainerDimensions = function() {
    // get the dimension for the map container, the disam panel will be going on top of this
    var dim = this.node.getDimensions();
    for ( var style in dim ) {
        this.container.style[style] = dim[style] + 'px';
    }
    // make sure the inner panel is smaller than the outer panel so we get the transparent effect
    this.panel.style.width = ( dim.width - 120 ) + 'px';
    this.panel.style.height = ( dim.height - 120 ) + 'px';
    this.panel.style.top = ( dim.top + 50 ) + 'px';
    this.panel.style.left = ( dim.left + 50 ) + 'px';
    return true;
}

MMDisambiguation.prototype.createOptions = function( root, results, callback ) {
    var anchor, me = this;
    // cycle through the results and add anchors to the root
    for ( var i = 0, j = results.length; i < j; i++ ) {
        anchor = new MMElement( 'a' );
        anchor.href = '#';
        anchor.addCssClass( 'MMGeocodeResult' );
        anchor.innerHTML = results[i].address.display_name;
        anchor.result = results[i];
        anchor.onclick = function() {
            me.clearDisam();
            callback( this.result );
            return false;
        }
        root.appendChild( anchor );
    }
    return root;
}

MMDisambiguation.prototype.handleGeocode = function( results, error, type, callback ) {
    var title = new MMElement( 'h4' ), root = new MMElement( 'div' );
    title.addCssClass( 'MMGeocodeTitle' );
    if ( error == 'MM_GEOCODE_MULTIPLE_MATCHES' ) {
        title.innerHTML = 'Multiple locations were found for your search';
        title.addCssClass( 'MMGeocodeMultipleResults' );
        root.appendChild( title );
        root = this.createOptions( root, results, callback );
    } else {
        title.innerHTML = "Sorry, we couldn't find that address.<br />Please try a search using another part of the address.";
        title.addCssClass( 'MMGeocodeNoResults' );
        root.appendChild( title );
    }
    this.populateDisam( root );
}
