var mapOldOnload = window.onload;

window.onload = function()
{
    if (typeof mapOldOnload == 'function')
    {
         mapOldOnload();
    }

    window.mapInstance = new storeMap();
    mapInstance.init();

}

storeMap = function ()
{
    this.defaultTextBox = null;
    this.locationTextBoxes = {};
}

storeMap.prototype.init = function()
{
    this.container = document.getElementById('mapTextBoxes');
    if (!this.container)
    {
        return null;
    }

    this.collectTextBoxes();
    this.attachMouseHandlers();

    this.showDefaultText();
    this.display(); // show main container

}

storeMap.prototype.display = function()
{
    this.show(this.container);
}

storeMap.prototype.collectTextBoxes = function()
{
    this.defaultTextBox = document.getElementById('defaultTextBox');
    if (!this.defaultTextBox)
    {
        return null;
    }

    var boxes = this.container.getElementsByTagName('li');
    for (var i=0; i<boxes.length; i++)
    {
        var box = boxes[i];
        var idParts = box.id.split('_');
        if (idParts[0] != 'locationTextBox')
        {
            continue;
        }

        var storeId = idParts[1];
        this.locationTextBoxes[storeId] = box;
    }

}

storeMap.prototype.getMap = function()
{
    var map = document.getElementById('countryMap');
    if (!map)
    {
        map = document.getElementById('cityMap');
    }
    return map;

}
storeMap.prototype.attachMouseHandlers = function()
{
    var map = this.getMap();
    if (!map)
    {
        return null;
    }

    var areas = map.getElementsByTagName('area');

    var script = this;

    for (var i=0; i<areas.length; i++)
    {
        var area = areas[i];
        area.onmouseover = function()
        {
            script.showTextBox( this );
        }

        area.onmouseout = function()
        {
            script.showDefaultText();
        }
    }
}

storeMap.prototype.hideTextBoxes = function()
{
    this.hide( this.defaultTextBox );
    for (x in this.locationTextBoxes)
    {
        this.hide(this.locationTextBoxes[x]);
    }

}

storeMap.prototype.showTextBox = function( area )
{
    var storeId = area.id.split('_')[1];
    if (!storeId)
    {
        return null;
    }
    if (this.locationTextBoxes[storeId])
    {
        this.hideTextBoxes();
        this.show( this.locationTextBoxes[storeId] );
    }
}

storeMap.prototype.showDefaultText = function()
{
    this.hideTextBoxes();
    this.show( this.defaultTextBox );
}

storeMap.prototype.hide = function( el )
{
    el.style.display = 'none';
}

storeMap.prototype.show = function( el )
{
    el.style.display = 'block';
}


