/**
 * Contains definition for class Setra.Marker
 */

/**
 * Represents an event
 *
 * @param {int} pageId
 * @param {String} categoryName
 * @param {String} title
 * @param {int} latitude
 * @param {int} longitude
 */
Setra.Marker = function( pageId, categoryName, title, latitude, longitude )
{
    this.PageId = pageId;
    this.Title = title.replace( "&#39;", "'" ).replace( "&#34;", "\"" );
    this.Point = new GLatLng( latitude, longitude, 13 );
    this.CategoryName = categoryName;
    this.CurrentMarker = null;
}

/**
  * Stores the currently selected marker
  * Is null if no marker is selected
  */
Setra.Marker.SelectedObj = null;

/**
 * Shows info window in given zoomLevel
 * 
 * @param {int} zoomLevel
 */
Setra.Marker.prototype.ShowInfoWin = function()
{
    var thisObj = this;
    var url = Setra.AjaxPageURL + "?pageId=" + this.PageId + "&mainPageId=" + Setra.CurrentPageId + "&" + Setra.LanguageCodeName + "=" + Setra.LanguageCode;

    jQuery.ajax(
        {   url: url,
            cache: false,
            success:function( data, textStatus )
            {
                $( "#facility-content-wrapper div.ajax-content" ).html( data );
                $( "#facility-info-win-wrapper" ).slideDown( "fast" );
                thisObj.Center();
                $( "#facility-content-wrapper a.close-button" ).click(
                    function()
                    {
                        Setra.Marker.DeSelect();
                    }
                );
            },
            error: function()
            {
                alert( "Error when retrieving data..." );
                
                if( Setra.IsTestEnviroment )
                {
                    prompt( "Debug", url );
                }
            }
        }
    );
}

/**
 * Selects event with given zoomLevel
 *
 * @param zoomLevel
 */
Setra.Marker.prototype.Select = function()
{
    Setra.UpdateMarkers();
    this.ShowInfoWin();
    Setra.Map.removeOverlay( this.CurrentMarker );
    var marker = new GMarker( this.Point, {icon:Setra.GetLargeIcon( this.CategoryName ), title:this.Title } );
    GEvent.bind( marker, "click", this, Setra.Marker.DeSelect );
    Setra.Map.addOverlay( marker );
    Setra.Marker.SelectedObj = this;
}

/**
 * Centers the marker
 *
 */ 
Setra.Marker.prototype.Center = function()
{
    var centerPoint = Setra.Map.fromLatLngToContainerPixel( this.Point );
    
    centerPoint.x += $( "#facility-info-win-wrapper" ).width() - Setra.InfoWinWidth / 2;
    Setra.Map.setCenter( Setra.Map.fromContainerPixelToLatLng( centerPoint ), Setra.Map.getZoom() );  
}

/**
 * Deselects current active marker
 *
 */
Setra.Marker.DeSelect = function()
{
    $( "#facility-info-win-wrapper" ).slideUp( "fast" );
    Setra.UpdateMarkers();
    Setra.Marker.SelectedObj = null;
}