var map = null;
var gMap;
window.onload = function()
{
    if (typeof GUnload == 'undefined' || typeof mapConfig == 'undefined')
    {
        return;
    }

    window.onunload = GUnload;
    map = new mapScript( mapConfig );
    if (!map.ok)
    {
        return;
    }
    map.initGMap();


	$('.youtube-section-toggle').click(function() {
		$(this).next().slideToggle('fast');
	});
}

function mapScript( config )
{


    this.ok = false;
    this.config = config;

    if (!this.config)
    {
        return;
    }

    this.box = document.getElementById( this.config.boxId );
    if (
        (!this.box)
        ||
        (!GBrowserIsCompatible())
    )
    {
        return;
    }

    this.map = null;
    this.librariesDataUrlBase = null;
    this.thumbsLayer = null;
    this.ok = true;
}

mapScript.prototype.initGMap = function()
{
    this.map = new GMap2( this.box );
	gMap = this.map;
    var center = new GLatLng(56.968221051706436 , 24.61142310333253);
    with (this.map)
    {
        addControl(new GLargeMapControl());

        enableDragging();
        enableScrollWheelZoom();
        enableDoubleClickZoom();
        enableContinuousZoom();
        setCenter( center , 7 );
    }

    // this.map.openInfoWindowHtml( center );
    // this.map.closeInfoWindow(  );
    var self = this;
    this.requestLibraries();

}

mapScript.prototype.getLibrariesDataUrlBase = function()
{
    if (!this.librariesDataUrlBase)
    {
        this.librariesDataUrlBase = this.config.librariesDataUrl.concat('?task=getLibrariesJson');
    }
    return this.librariesDataUrlBase;
}



mapScript.prototype.requestLibraries = function()
{
    var bounds = this.map.getBounds();
    var sw = bounds.getSouthWest();
    var ne = bounds.getNorthEast();
    var librariesDataUrl = this.getLibrariesDataUrlBase();

    librariesDataUrl = librariesDataUrl.concat(
    '&minLat=', sw.lat(),
    '&maxLat=', ne.lat(),
    '&minLng=', sw.lng(),
    '&maxLng=', ne.lng(),
    '&rand=', Math.random()
    );

    var self = this;
    loadXmlHttp ( librariesDataUrl,
    function(xmlhttp, params)
    {
        var librariesData = eval(xmlhttp.responseText);
        self.loadLibraries(librariesData);
    },
    null, 'GET' , null, true);
}

mapScript.prototype.loadLibraries = function(librariesData)
{
    var sameLibraries = false;
    if (
    (this.thumbsLayer)
    &&
    (this.thumbsLayer.librariesData)
    &&
    (librariesData.length == this.thumbsLayer.librariesData.length)
    )
    {
        sameLibraries = true;
        for (var i = 0; i < librariesData.length; i++)
        {
            if (!sameLibraries)
            {
                continue;
            }
            if (librariesData[i].id != this.thumbsLayer.librariesData[i].id)
            {
                sameLibraries = false;
            }
        }
    }

    if (!sameLibraries)
    {
        if (this.thumbsLayer)
        {
            this.map.removeOverlay( this.thumbsLayer );
        }

        this.thumbsLayer = new thumbsLayer( librariesData, this );
        this.map.addOverlay(this.thumbsLayer);

    }
}


function thumbsLayer( librariesData, mapScript )
{
    this.librariesData = librariesData;
    this.mapScript = mapScript;
}

if (typeof GOverlay != 'undefined')
{

    thumbsLayer.prototype = new GOverlay();

    thumbsLayer.prototype.initialize = function( map )
    {
        this.map    = map;
		this.markers = [];

        for (var i = 0, library; library = this.librariesData[i]; i++)
        {
            library.point = new GLatLng( parseFloat ( library.lat ) , parseFloat( library.lng ) );

			var libraryIcon = new GIcon();
			libraryIcon.image = library.iconUrl;
			libraryIcon.iconSize = new GSize(library.iconWidth, library.iconHeight);
			libraryIcon.iconAnchor = new GPoint(library.iconWidth / 2, library.iconHeight / 2);
			libraryIcon.infoWindowAnchor = new GPoint(0, -2);

			var options = {
				icon: libraryIcon
			};

			var marker = new GMarker(library.point, options);
			this.map.addOverlay(marker);
			this.markers.push(marker);

            library.marker = marker;

			marker.library = library;

		     GEvent.addListener(marker, 'mouseover', function()
			 {
		        this.openExtInfoWindow(
		          map,
		          "libraryWindow",
		          this.library.infoHtml,
		          {beakOffset: 7}
		        );
		      });
		     
		     GEvent.addListener(marker, "click", function(  )
	 	        {
	 				location.href = this.library.itemUrl;
	 	        });
        }
    }

    thumbsLayer.prototype.redraw = function(force) {
		return;
   }
    thumbsLayer.prototype.copy = function() {
		return this;
   }
    thumbsLayer.prototype.remove = function() {
		for(var i = 0, marker; marker = this.markers[i]; ++i)
		{
			this.map.removeOverlay(marker);
		}
		return;
   }

}

function showMap()
{
    var flash = document.getElementById('introFlashBox');
    if (!flash)
    {
        return;
    }
    flash.parentNode.removeChild(flash);
}