nxc = window.nxc || {};
nxc.InfoWindow = function( map ) {
    this.setMap( map );

    this._div     = null;
    this._marker  = null;
    this._content = null;
};
nxc.InfoWindow.prototype = new google.maps.OverlayView();
nxc.InfoWindow.prototype.onAdd = function() {
    this._div = jQuery( '<div>' ).css( 'display', 'none' ).attr( 'class', 'nxc-infow-window-wrapper' );

    this._content = jQuery( '<div>' ).attr( 'class', 'nxc-infow-window-content' );
    this._div.append( this._content );
    var infowindow = this;
    var close = jQuery( '<a>' ).css( 'href', '#' ).attr( 'class', 'nxc-infow-window-close' ).click( function( e ) {
        infowindow.hide();
        e.preventDefault();
    } );
    this._div.append( close );

    var panes = this.getPanes();
    this._div.css( 'z-index', 200 );
    jQuery( panes.floatPane ).append( this._div );
};
nxc.InfoWindow.prototype.onRemove = function() {
    this._div.remove();
    this._div     = null;
    this._content = null;
    this._marker  = null;
};
nxc.InfoWindow.prototype.draw = function() {
    var cssOffset = this.getProjection().fromLatLngToDivPixel( this.getMap().getCenter() );
    var offest = {
        'x': cssOffset.x - parseInt( parseInt( this._div.css( 'width' ) ) / 2 ),
        'y': cssOffset.y - parseInt( parseInt( this._div.css( 'height' ) ) / 2 )
    };

    if( this._marker ) {
        var cssOffset = this.getProjection().fromLatLngToDivPixel( this._marker.getPosition() );
        var iconSizes = ( typeof( this._marker.getIcon() ) == 'object' )
            ? this._marker.getIcon().size
            : { 'width': 48, 'height': 48 };
        this._div.css( {
            /*'left': cssOffset.x - iconSizes.width / 2,
            'top': cssOffset.y - iconSizes.height - this._div.height()*/
            'left': offest.x,
            'top': offest.y
        } );
    }
};
nxc.InfoWindow.prototype.open = function( map, marker, pan ) {
    if( pan === true ) {
        map.panTo( marker.getPosition() );
        var that = this;
        google.maps.event.addListenerOnce( map, 'idle', function() {
            that.draw.apply( that );
        } );
    }
    this._marker = marker;

    this._div.css( 'display', 'block' );

    if( pan !== true ) {
        this.draw();
    }
};
nxc.InfoWindow.prototype.hide = function( map, marker ) {
    this._div.css( 'display', 'none' );
    this._content.html( '' );
};
