var II = II || {};
II.InfoWindow = (function($, google) {
    'use strict';

    var InfoWindow = function (options) {
        options = options || {};
        this.marker = options.marker;
        this.content = options.content;
        this.opened = false;
    };

    InfoWindow.prototype = new google.maps.OverlayView();

    InfoWindow.prototype.onAdd = function() {
        var $div = $('<div class="ii_info_window"></div>');

        $div.append(this.content.html());

        var $close_div = $('<div class="ii_info_close"></div>');

        // Checking for IE6, if so, we use gifs because IE6 can't do simple things
        if ($.browser.msie && $.browser.version.substring(0,1) === '6'){
          var $close_button = $('<a href="javascript:void(0)">' + '<img src="/responsibility/global_health/img/info_close.gif" />' + '</a>');
        }
        else {
          var $close_button = $('<a href="javascript:void(0)">' + '<img src="/responsibility/global_health/img/info_close.png" />' + '</a>');
        }

        var thiscopy = this;
        $close_button.click((function () {
            var info = thiscopy;
            return function () { info.close();
			if (document.getElementById("map_canvas_menu_control").style.display = 'none') {
				document.getElementById("map_canvas_menu_control").style.display = 'block';
			}
			else
			{
				document.getElementById("map_canvas_menu_control").style.display = 'none';
			};
			};
        })());

        $close_div.append($close_button);
        $div.append($close_div);

        // Checking for IE6, if so, we use gifs because IE6 can't do simple things
        if ($.browser.msie && $.browser.version.substring(0,1) === '6') { 
          var arrow_image = this.marker.position.lng() < 0 ? 'arrow-left.gif' : 'arrow-right.gif';
        }
        else {
          var arrow_image = this.marker.position.lng() < 0 ? 'arrow-left.png' : 'arrow-right.png';
        }

        var $arrow_div = $('<div class="ii_info_arrow">' + '<img src="/responsibility/global_health/img/' + arrow_image + '" />' + '</div>');
        $div.append($arrow_div);

        $div.css('width', this.content.css('width'));

        $(this.getPanes().floatPane).append($div);
        this.div = $div;
    };

    InfoWindow.prototype.onRemove = function() {
        this.div.remove();
        this.div = null;
    };

    InfoWindow.prototype.draw = function() {
        var projection = this.getProjection();
        var marker_pos = projection.fromLatLngToDivPixel(this.marker.position);

        var x_offset;
        if (this.marker.position.lng() < 0) {
            x_offset = 69;
            this.div.find('.ii_info_arrow').css('left', -25);
        }
        else {
            x_offset = -(this.div.width() + 33)
            this.div.find('.ii_info_arrow').css('right', -25);
        }

        var head_height = this.div.find('h1').height();
        var y_offset = -(head_height + 72)

        this.div.css('left', marker_pos.x + x_offset);
        this.div.css('top',  marker_pos.y + y_offset);
        this.div.find('.ii_info_arrow').css('top', head_height + 22);
    };

    InfoWindow.prototype.open = function(map) {
        this.setMap(map);
        this.opened = true;
    };

    InfoWindow.prototype.close = function() {
        this.setMap(null);
        this.opened = false;
    };

    InfoWindow.prototype.toggle = function(map) {
        this.opened ? this.close() : this.open(map);
    };

    return InfoWindow;
})(jQuery, google);
