var oStorelocator = null ;
var sUserZipcode = null ;

google.load( "maps", "2.x" );
google.setOnLoadCallback( Startstorelocator );
window.onUnload = google.maps.Unload   ;


function Startstorelocator()
{
	oStorelocator = new Storelocator( "feedback" )  ;
	oStorelocator.init() ;
}


function Storelocator( sFeedbackElement )
{
	this._startpoint      = new google.maps.LatLng(52.077396, 5.121689)  ;
	this._zoom            = 11 ;
	this._showZoomControl = true ;
	this._scaleControl    = false ;
	this._feedbackElement = sFeedbackElement;
	this.geocoder = new google.maps.ClientGeocoder() ;
	this.geocoder.setBaseCountryCode( "nl" ) ;
	window[ "$_Storelocator" ] = this;
}


Storelocator.prototype.init = function()
{
	this.map = new google.maps.Map2( document.getElementById( "map_canvas" ) );

	this.gotoClientLocation() ;

	var customUI = this.map.getDefaultUI();
	customUI.controls.scalecontrol = false;
	customUI.controls.menumaptypecontrol = false ;
	customUI.controls.maptypecontrol  = false ;
	customUI.controls.largemapcontrol3d = true ;
	this.map.setUI(customUI);



	this.geocoder = new google.maps.ClientGeocoder() ;
	this.geocoder.setBaseCountryCode( "nl" ) ;


	/* Uitleef arena */
	this.getStores() ;


}

Storelocator.prototype.getStores = function()
{
// 	var oVariable = new Object();
// 	oFetch = new RPCFetch( '/stores.xml' ) ;
// 
// 	oFetch.onload = function()
// 	{
// 		var oXML = this.getData()  ;
// 		oXML = oXML.firstChild ;
// 		if ( oXML.childNodes && oXML.childNodes.length > 0 )
// 		{
// 			for ( var i = 0; i < oXML.childNodes.length; ++i )
// 			{
// 				if ( oXML.childNodes[ i ].nodeName == "content" )
// 				{
// 					var n = 0;
// 					for ( var j = 0; j < oXML.childNodes[ i ].childNodes.length; ++j )
// 					{
// 						var oNode = oXML.childNodes[ i ].childNodes[ j ];
// 						switch( oNode.nodeName )
// 						{
// 							case "item" :
// 								var sDesc = oNode.childNodes[0].firstChild.nodeValue ;
// 								var nLatt = oNode.childNodes[1].firstChild.nodeValue ;
// 								var	nLong = oNode.childNodes[2].firstChild.nodeValue ;
// 
// 								oStorelocator.addMarker( nLatt, nLong, oStorelocator.createIconBpLogo(), sDesc ) ;
// 							default : break;
// 						}
// 					}
// 				}
// 			}
// 		}
// 
// 	}
// 	oFetch.get( '', '', oVariable  ) ;
      $(document).ready(function()
      {
        $.ajax({
          type: "GET",
          url: "/stores.xml",
          dataType: "xml",
          success: parseXml
        });
      });
}

function  parseXml(xml)
{
  
  $(xml).find("content").each(function()
  {
    $(xml).find("item").each(function()
      {
        var sDesc = $(this).find("description").text();
        var nLatt = $(this).find("lattitude").text();
        var nLong = $(this).find("longitude").text();
        oStorelocator.addMarker( nLatt, nLong, oStorelocator.createIconBpLogo(), sDesc ) ;
      });
  });
}



Storelocator.prototype.addMarker = function ( nLat, nLang, oIcon, sWindowcontents )
{
	if( !oIcon )
		oIcon = null ;
	var oPoint   = new google.maps.LatLng( nLat, nLang ) ;
	var oMarker  = new google.maps.Marker( oPoint, oIcon ) ;
	var map      = this.map ;
	var fEventer = function()
	{

		map.openInfoWindowHtml( oPoint, sWindowcontents );

	} ;

	google.maps.Event.addListener( oMarker, "click", fEventer ) ;
	this.map.addOverlay( oMarker ) ;
}


Storelocator.prototype.createIconBpLogo = function()
{
	var Icon 				= new google.maps.Icon() ;
	Icon.image 				= "http://www.benfit.nl/images/benfit_small_logo.png" ;
	//Icon.shadow 			= "/images/logo_shadow.png" ;
	Icon.iconSize 			= new google.maps.Size(24, 24);
	Icon.shadowSize			= new google.maps.Size(24, 24);
	Icon.iconAnchor			= new google.maps.Point(12, 12);
	Icon.infoWindowAnchor	= new google.maps.Point(-10, -10);
	markerOptions           = { icon:Icon };
	return markerOptions ;
}


Storelocator.prototype.showInfoWindow = function( sContents, oPoint )
{
	this.map.openInfoWindowHtml( oPoint, sContents ) ;
}


Storelocator.prototype.gotoClientLocation = function()
{

	if( sUserZipcode )
	{
		this.moveToZipcode( sUserZipcode ) ;
		return ;
	}

	if ( 	google.loader.ClientLocation &&
			google.loader.ClientLocation.longitude &&
			google.loader.ClientLocation.latitude )
		var oPoint =  new google.maps.LatLng( google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude )  ;
	else
		var oPoint = this._startpoint ;

	this.moveToPoint( oPoint ) ;
}


Storelocator.prototype.moveToZipcode = function( sZipcode , sZipcode2)
{
	sZipcode = sZipcode.replace( /(\s|-)/, "" );
	sZipcode+= sZipcode2.replace( /(\s|-)/, "" );

	if( sZipcode.length <= 3 )
	{
		this.setMessage( "Vul je volledige postcode in" );
		return false ;
	}   
	var fCallback = function( oPoint )
	{
		if( !oPoint )
		{
			oStorelocator.setMessage( "Postcode niet gevonden" ) ;
			return false;
		}
		oStorelocator.moveToPoint( oPoint ) ;
	}
	if (sZipcode2)
	{
	this.geocoder.getLatLng( sZipcode + ' , netherlands', fCallback ) ;
	}
	else
	{
	this.geocoder.getLatLng( sZipcode + ' , belguim', fCallback ) ;
	}

	return false ;
}


Storelocator.prototype.moveToPoint = function( oPoint )
{
	this.map.setCenter( oPoint, this._zoom   ) ;
}

Storelocator.prototype.setMessage = function( sMessage )
{
	oElement = document.getElementById( this._feedbackElement );
	if( oElement == null )
		return false;

	oElement.className += "feedback error";
	oElement.innerHTML = sMessage;
	setTimeout( "window.$_Storelocator.delMessage();", 5000 );
}
Storelocator.prototype.delMessage = function()
{
	oElement = document.getElementById( this._feedbackElement );
	if( oElement == null )
		return false;
	oElement.className.replace( /\sinfo/, "" );
	oElement.innerHTML = "&#160;";
}


