var CosmoxUserGMap =
{
	_enableMarkerCreation: false,
	_markerCreationCallback: null,
	
	_gMapClickAddUser_timeout:null,
	_gMapClickAddUser_timeoutData:null,
	
	_gMarker: null,
	_gIcon: null,
	
	init: function()
	{
		GEvent.addListener( CosmoxGMap.g_map, 'click', CosmoxUserGMap._gMapClickAddUserMarker );
	},
	
	enableMarkerCreation: function()
	{
		return CosmoxUserGMap._enableMarkerCreation;
	},
	
	// sa flag=true pocivnjem
	// sa flag=false zavrsavam
	setEnableMarkerCreation: function(flag, callback)
	{
		if(typeof(callback) == 'undefined' )
			callback = null;

		CosmoxUserGMap._enableMarkerCreation=Boolean(flag);
		
		if( CosmoxUserGMap._enableMarkerCreation )
		{
			if(CosmoxUserGMap._gMarker)
				CosmoxUserGMap._gMarker.enableDragging();
			
			CosmoxUserGMap._markerCreationCallback = callback;
		}
		else
		{
			if(CosmoxUserGMap._gMarker)
				CosmoxUserGMap._gMarker.disableDragging();
				
			CosmoxUserGMap._markerCreationCallback = null;
		}
	},
	
	resetMarkerCreation: function()
	{
		if(CosmoxUserGMap._gMapClickAddUser_timeout)
		{
			clearTimeout( CosmoxUserGMap._gMapClickAddUser_timeout );
		}
		
		if(CosmoxUserGMap._gMarker)
		{
			CosmoxGMap.g_map.removeOverlay( CosmoxUserGMap._gMarker );
			delete CosmoxUserGMap._gMarker;
			CosmoxUserGMap._gMarker=null;
		}
		
		CosmoxUserGMap._enableMarkerCreation=false;
		CosmoxUserGMap._markerCreationCallback=null;
		
		CosmoxUserGMap._gMapClickAddUser_timeout=null;
		CosmoxUserGMap._gMapClickAddUser_timeoutData=null;
		
		CosmoxUserGMap._gMarker=null;
	},
	
	//http://code.google.com/p/gmaps-api-issues/issues/detail?id=394
	_gMapClickAddUserMarker:function(overlay, latlng, overlaylatlng)
	{
		if( !CosmoxUserGMap.enableMarkerCreation())
			return;
		
		if(! (typeof(overlay) == 'undefined' || null === overlay) )
			return;
		
		if(CosmoxUserGMap._gMapClickAddUser_timeout == null)
		{
			CosmoxUserGMap._gMapClickAddUser_timeoutData=latlng;
			CosmoxUserGMap._gMapClickAddUser_timeout=setTimeout( 'CosmoxUserGMap._gMapClickAddUserMarker_timeoutCallback()', 300);
		}
		else
		{
			clearTimeout(CosmoxUserGMap._gMapClickAddUser_timeout);
			CosmoxUserGMap._gMapClickAddUser_timeout = null;
			CosmoxUserGMap._gMapClickAddUser_timeoutData = null;
		}
	},
	
	_gMapClickAddUserMarker_timeoutCallback:function()
	{
		if(null == CosmoxUserGMap._gMapClickAddUser_timeoutData )
			return;
		
		if( !CosmoxUserGMap.enableMarkerCreation() )
			return;
		
		CosmoxUserGMap._gMapClickAddUser_timeout = null;
				
		if( CosmoxUserGMap._gMarker != null )
		{
			// vec ga imam .... premjesti ga!
			
			CosmoxUserGMap._gMarker.setLatLng( CosmoxUserGMap._gMapClickAddUser_timeoutData );
			
			
			CosmoxGMap.gClientGeocoder.getLocations(CosmoxUserGMap._gMapClickAddUser_timeoutData, function(addresses)
																{
																	var html='';
																	var address='';

																	var result={
																					CountryName: '',
																					LocalityName: '',
																					ThoroughfareName: '',
																					PostalCodeNumber: '',
																					DependentLocalityName: ''
																				};
																	
																	if(addresses.Status.code != 200)
																	{
																		html = CosmoxUserGMap._gMapClickAddUser_timeoutData.toUrlValue();
																		//alert("reverse geocoder failed to find an address for " + latlng.toUrlValue());
																	}
																	else
																	{
																		var place = addresses.Placemark[0];
																		html = place.address;
																		
																		var addressDetails=place.AddressDetails;
																		//console.debug(addressDetails);
																		CosmoxUserGMap.parseAddressDetails(addressDetails, addressDetails.Accuracy, result);
																		//console.debug(result);
																	}
																	
																	CosmoxUserGMap._gMarker.myAddress=html;
																	CosmoxUserGMap._gMarker.myHtml=html;
																	CosmoxUserGMap._gMarker.myAddressDetails={
																											'state': result.CountryName,
																											'city': ( (result.DependentLocalityName !='') ? result.DependentLocalityName : result.LocalityName ),
																											'street': result.ThoroughfareName,
																											'postal_number': result.PostalCodeNumber
																											};
																	
																	CosmoxUserGMap._gMarker.openInfoWindowHtml( html );
																	
																	if(CosmoxUserGMap._markerCreationCallback)
																	{
																		//console.debug(result);
																		//console.debug(CosmoxUserGMap._gMarker.myAddressDetails);
																		
																		CosmoxUserGMap._markerCreationCallback(
																											   CosmoxUserGMap._gMapClickAddUser_timeoutData.lat(),
																											   CosmoxUserGMap._gMapClickAddUser_timeoutData.lng(),
																											   html,
																											   CosmoxUserGMap._gMarker.myAddressDetails
																											  );
																	}
																	
																	CosmoxUserGMap._gMapClickAddUser_timeoutData=null;
																} );
			
			return;
		}
		
		if(!CosmoxUserGMap._gIcon)
		{
			CosmoxUserGMap._gIcon=new GIcon();
			CosmoxUserGMap._gIcon.image = '/images/gmap/image.png';
			CosmoxUserGMap._gIcon.shadow = '/images/gmap/shadow.png';
			CosmoxUserGMap._gIcon.iconSize = new GSize(18,38);
			CosmoxUserGMap._gIcon.shadowSize = new GSize(37,38);
			CosmoxUserGMap._gIcon.iconAnchor = new GPoint(7, 37); // relativno top/left
			CosmoxUserGMap._gIcon.transparent = '/images/gmap/transparent.png';
			CosmoxUserGMap._gIcon.infoWindowAnchor = new GPoint(18, 4); // relativno top/left
			CosmoxUserGMap._gIcon.imageMap=[
												0,3,
												11,1,
												17,3,
												17,28,
												10,30,
												7,37,
												3,29,
												0,28
											];

			CosmoxUserGMap._gIcon.printImage = '/images/gmap/printImage.gif';
			CosmoxUserGMap._gIcon.mozPrintImage = '/images/gmap/mozPrintImage.gif';
			CosmoxUserGMap._gIcon.printShadow = '/images/gmap/printShadow.gif';
		}

		CosmoxUserGMap._gMarker=new GMarker( CosmoxUserGMap._gMapClickAddUser_timeoutData, {draggable: true, icon: CosmoxUserGMap._gIcon} );
		
		CosmoxGMap.gClientGeocoder.getLocations(CosmoxUserGMap._gMapClickAddUser_timeoutData, function(addresses)
										{
											var html='';
											var address='';
	
											var result={
															CountryName: '',
															LocalityName: '',
															ThoroughfareName: '',
															PostalCodeNumber: '',
															DependentLocalityName: ''
														};
											
											if(addresses.Status.code != 200)
											{
												html = CosmoxUserGMap._gMapClickAddUser_timeoutData.toUrlValue();
												//alert("reverse geocoder failed to find an address for " + latlng.toUrlValue());
											}
											else
											{
												var place = addresses.Placemark[0];
												html = place.address;
												
												var addressDetails=place.AddressDetails;
												//console.debug(addressDetails);
												CosmoxUserGMap.parseAddressDetails(addressDetails, addressDetails.Accuracy, result);
												//console.debug(result);
											}
											
											CosmoxUserGMap._gMarker.myAddress=html;
											CosmoxUserGMap._gMarker.myHtml=html;
											CosmoxUserGMap._gMarker.myAddressDetails={
																					'state': result.CountryName,
																					'city': ( (result.DependentLocalityName !='') ? result.DependentLocalityName : result.LocalityName ),
																					'street': result.ThoroughfareName,
																					'postal_number': result.PostalCodeNumber
																					};
											
											GEvent.addListener(CosmoxUserGMap._gMarker, "click", function()
																					{
																						this.openInfoWindowHtml( this.myHtml );
																					} );
											
											CosmoxGMap.g_map.addOverlay( CosmoxUserGMap._gMarker );
											
											CosmoxUserGMap._gMarker.openInfoWindowHtml( html );
											
											
											
											if(CosmoxUserGMap._markerCreationCallback)
											{
												//console.debug(result);
												//console.debug(CosmoxUserGMap._gMarker.myAddressDetails);
																		
												CosmoxUserGMap._markerCreationCallback(
																					   CosmoxUserGMap._gMapClickAddUser_timeoutData.lat(),
																					   CosmoxUserGMap._gMapClickAddUser_timeoutData.lng(),
																					   html,
																						CosmoxUserGMap._gMarker.myAddressDetails
																					  );
											}
											
											CosmoxUserGMap._gMapClickAddUser_timeoutData=null;
										} );
		
		GEvent.addListener(CosmoxUserGMap._gMarker, "dragstart", function()
		{ 
			this.closeInfoWindow();
		} );
		
		GEvent.addListener(CosmoxUserGMap._gMarker, "dragend", function(latlng)
				{ 
						CosmoxGMap.gClientGeocoder.getLocations(latlng, function(addresses)
																{
																	var html='';
																	var address='';

																	var result={
																					CountryName: '',
																					LocalityName: '',
																					ThoroughfareName: '',
																					PostalCodeNumber: '',
																					DependentLocalityName: ''
																				};
																	
																	if(addresses.Status.code != 200)
																	{
																		html = latlng.toUrlValue();
																		//alert("reverse geocoder failed to find an address for " + latlng.toUrlValue());
																	}
																	else
																	{
																		var place = addresses.Placemark[0];
																		html = place.address;
																		
																		var addressDetails=place.AddressDetails;
																		//console.debug(addressDetails);
																		CosmoxUserGMap.parseAddressDetails(addressDetails, addressDetails.Accuracy, result);
																		//console.debug(result);
																	}
																	
																	CosmoxUserGMap._gMarker.myAddress=html;
																	CosmoxUserGMap._gMarker.myHtml=html;
																	CosmoxUserGMap._gMarker.myAddressDetails={
																											'state': result.CountryName,
																											'city': ( (result.DependentLocalityName !='') ? result.DependentLocalityName : result.LocalityName ),
																											'street': result.ThoroughfareName,
																											'postal_number': result.PostalCodeNumber
																											};
																	
																	CosmoxUserGMap._gMarker.openInfoWindowHtml( html );
																	
																	if(CosmoxUserGMap._markerCreationCallback)
																	{
																		//console.debug(result);
																		//console.debug(CosmoxUserGMap._gMarker.myAddressDetails);
																		
																		CosmoxUserGMap._markerCreationCallback(
																											   latlng.lat(),
																											   latlng.lng(),
																											   html,
																											   CosmoxUserGMap._gMarker.myAddressDetails
																											  );
																	}
																} );
				} );
	}, // _gMapClickAddUserMarker_timeoutCallback
	
	parseAddressDetails: function(obj, accuracy, result)
	{
		if(typeof(obj) == 'object')
		{
			for(var ix in obj)
			{
				
				if( typeof(obj[ix]) == 'object' )
				{
					CosmoxUserGMap.parseAddressDetails(obj[ix], accuracy, result);
				}
				else
				{
					if( typeof( result[ix] ) != 'undefined' )
						result[ix] = obj[ix];
				}
			}
		}
	}
} // CosmoxUserGMap

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

var CosmoxGMap =
{
	gClientGeocoder: null,
	items: {},
	typesIx: {},

	itemsArea: {}, // ako za postavljanje podrucja ne nadem u items onda gledam u itemsArea
	typesAreaIx: {},

	g_map: null,

	// def podaci za http://maps.google.com/maps?q=croatia&z=7
	def_lat:52.132633,
	def_lng:5.291266,
	def_zoom:8,
	g_def_latlng: null,

	item_area_zoom: 11,
	item_zoom: 13,

	open_info_window_item_id: 0, // sadrzi id item-a koji ima trenutno otvoreni prozor infowindow

	g_icons: {}, // assoc niz koji sadrzi ikone za razlicite tipove

	_showMarkerVisibility: {},
	
	_defaultLocationId: false,
	
	_embedFlag: false,
	
	setEmbedFlag: function(embedFlag)
	{
		CosmoxGMap._embedFlag= embedFlag ? true : false;
	},
	
	// poziva se NAKON addItem!
	createMap: function(elementId, address, getLat, getLng, getZoom)
	{
		if(typeof(address)=='undefined')
			address=false;
			
		if(typeof(getLat)=='undefined')
			getLat=false;
			
		if(typeof(getLng)=='undefined')
			getLng=false;
			
		if(typeof(getZoom)=='undefined')
			getZoom=false;
			
		if (typeof('GBrowserIsCompatible') != 'undefined' && GBrowserIsCompatible())
		{
			if(getZoom != false)
			{
				zoom=getZoom;
			}
			else
			{
				zoom = CosmoxGMap.def_zoom;
			}
			
			CosmoxGMap.gClientGeocoder = new GClientGeocoder();
					
			CosmoxGMap.g_def_latlng=new GLatLng(CosmoxGMap.def_lat, CosmoxGMap.def_lng);
			CosmoxGMap.g_map = new GMap2( document.getElementById(elementId) );
			
			if(address!=false)
			{
				CosmoxGMap.g_map.setCenter(CosmoxGMap.g_def_latlng, zoom );
				CosmoxGMap.gotoAddress(address, zoom);
			}
			else if(getLat != false && getLng != false)
			{
				var g_latlng=new GLatLng(getLat, getLng);	
				CosmoxGMap.g_map.setCenter(g_latlng, zoom );
			}
			else
			{
				CosmoxGMap.g_map.setCenter(CosmoxGMap.g_def_latlng, zoom );
			}
				
			
			CosmoxGMap.g_map.addControl( new GLargeMapControl() );
			CosmoxGMap.g_map.addControl( new GMenuMapTypeControl() );
			CosmoxGMap.g_map.enableContinuousZoom();
			CosmoxGMap.g_map.enableInfoWindow();
			
			CosmoxGMap.g_map.addMapType(G_PHYSICAL_MAP);
			CosmoxGMap.g_map.setMapType(G_PHYSICAL_MAP);

			CosmoxGMap._createIcons();

			CosmoxGMap._createMarkers();
			
			

			
			// defaultLocationId via url
			var arr=String(window.location).split('#');

			
			if(typeof(arr[1]) != 'undefined' )
			{
				var str=String(arr[1]);
				
				if( str.substr(0,1) == 'l' || str.substr(0,1) == 'L' )
				{
					var num=parseInt(str.substr(1))
		
					if(!isNaN(num) && num>0)
					{
						CosmoxGMap._defaultLocationId=num;
					}
				}
			}
			
			if(CosmoxGMap._defaultLocationId)
			{
				CosmoxGMap.setMapItem(CosmoxGMap._defaultLocationId, false, true, false); //nezum, otvori info box
			}
			
		}
	},

	_createIcons: function()
	{
		for(type in CosmoxGMap.typesIx)
		{
			if(typeof( CosmoxGMap.g_icons[type] ) != 'undefined' )
				continue;

			switch(type)
			{
				default:
				case 'location':
				{
					CosmoxGMap.g_icons[type]=new GIcon();
					CosmoxGMap.g_icons[type].image = '/images/gmap/image.png';
					CosmoxGMap.g_icons[type].shadow = '/images/gmap/shadow.png';
					CosmoxGMap.g_icons[type].iconSize = new GSize(18,38);
					CosmoxGMap.g_icons[type].shadowSize = new GSize(37,38);
					CosmoxGMap.g_icons[type].iconAnchor = new GPoint(7, 37); // relativno top/left
					CosmoxGMap.g_icons[type].transparent = '/images/gmap/transparent.png';
					CosmoxGMap.g_icons[type].infoWindowAnchor = new GPoint(18, 4); // relativno top/left
					CosmoxGMap.g_icons[type].imageMap=[
														0,3,
														11,1,
														17,3,
														17,28,
														10,30,
														7,37,
														3,29,
														0,28
													];

					CosmoxGMap.g_icons[type].printImage = '/images/gmap/printImage.gif';
					CosmoxGMap.g_icons[type].mozPrintImage = '/images/gmap/mozPrintImage.gif';
					CosmoxGMap.g_icons[type].printShadow = '/images/gmap/printShadow.gif';
				}
				break;
			} // switch

		} // for
	},

	_getIcon: function(type)
	{
		if(typeof( CosmoxGMap.g_icons[type] ) == 'undefined')
			return G_DEFAULT_ICON;

		return CosmoxGMap.g_icons[type];
	},

	clearItems: function()
	{
		CosmoxGMap.items={};
		CosmoxGMap.typesIx={};

		CosmoxGMap.itemsArea={};
		CosmoxGMap.typesAreaIx={};
	},
	
	// dodavanje nakon createMap
	checkAndInsertItems: function(arr)
	{
		for(var key in arr)
		{
			var newItem=CosmoxGMap.addItem(arr[key].id, arr[key].type, arr[key].cosmox_book_id, arr[key].address, arr[key].description,
										   arr[key].map_lat, arr[key].map_lng, arr[key].smart_position, arr[key].book_name, arr[key].book_isbn,
										   arr[key].book_author,
										   arr[key].book_thumb_url
										   );
			
			if(newItem)
			{
				CosmoxGMap._createOneMarker( newItem );
			}
		}
	},
	
	getCurrentItem: function()
	{
		var id=CosmoxGMap.open_info_window_item_id;
		
		if(id<=0)
			return false;
		
		if( typeof( CosmoxGMap.items[ id] ) == 'undefined' || CosmoxGMap.items[ id] == null )
			return false;
		
		return CosmoxGMap.items[id];
		
	},

	// poziva se PRIJE createMap
	// ako se poziva poslije onda treba provjeriti sto vraca te pozvati _createOneMarker
	// vidi checkAndInsertItems
	addItem: function(id, type, cosmox_book_id, address, description,
										   map_lat, map_lng, smart_position, book_name, book_isbn,
										   book_author, book_thumb_url
										   )
	{
		if(! ( typeof( CosmoxGMap.items[id] ) == 'undefined' || CosmoxGMap.items[id] == null ) )
			return false;
		
		var obj={};

		obj.id=id;
		obj.type=type;
		obj.cosmox_book_id=cosmox_book_id;
		obj.address=address;
		obj.description=description;
		obj.map_lat=parseFloat(map_lat);
		obj.map_lng=parseFloat(map_lng);

		obj.g_latlng=new GLatLng(obj.map_lat, obj.map_lng);

		obj.g_marker=null; // u addMarkers
		
		obj.smart_position = smart_position;
		obj.book_name=book_name;
		obj.book_isbn=book_isbn;
		
		obj.book_author=book_author;
		obj.book_thumb_url=book_thumb_url;
		
		obj.html = CosmoxGMap._createHtml(obj);

		//alert('('+book_name+')('+obj.book_name+')');

		CosmoxGMap.items[id]=obj;

		if(typeof( CosmoxGMap.typesIx[obj.type] ) == 'undefined')
		{
			CosmoxGMap.typesIx[obj.type]=new Array();
		}

		CosmoxGMap.typesIx[ obj.type ][ CosmoxGMap.typesIx[ obj.type ].length ] = obj.id;
		
		return CosmoxGMap.items[id];
	},



	// poziva se PRIJE createMap
	// stavke za zumiranje na area ako nemam odredenu stavku u items ... za adresar mi ne treba ali mi treba za stete!
	/*
	addItemArea: function(id, type, name, address, city_name, tel, tel_2, fax, email, map_lat, map_lng)
	{
		var obj={};

		obj.id=id;
		obj.type=type;
		obj.name=name;
		obj.address=address;
		obj.city_name=city_name;
		obj.tel=tel;
		obj.tel_2=tel_2;
		obj.fax=fax;
		obj.email=email;
		obj.map_lat=parseFloat(map_lat);
		obj.map_lng=parseFloat(map_lng);

		obj.g_latlng=new GLatLng(obj.map_lat, obj.map_lng);

		obj.g_marker=null; // u addMarkers



		obj.html = CosmoxGMap._createHtml(obj);

		CosmoxGMap.itemsArea[id]=obj;

		if(typeof( CosmoxGMap.typesAreaIx[obj.type] ) == 'undefined')
		{
			CosmoxGMap.typesAreaIx[obj.type]=new Array();
		}

		CosmoxGMap.typesAreaIx[ obj.type ][ CosmoxGMap.typesAreaIx[ obj.type ].length ] = obj.id;
	},
	*/

	_createMarkers: function()
	{
		if(!CosmoxGMap.g_map)
			return;

		for(var i in CosmoxGMap.items)
		{
			//var item = CosmoxGMap.items[i];
			CosmoxGMap._createOneMarker( CosmoxGMap.items[i] );
		} // for
	},
	
	_createOneMarker: function( item )
	{
		item.g_marker=new GMarker(item.g_latlng, CosmoxGMap._getIcon( item.type )  );
		item.g_marker.cosmox_cosmox_location_id = item.id;

		GEvent.addListener(item.g_marker, "click", function(point)
										{
											if( this.cosmox_cosmox_location_id != CosmoxGMap.open_info_window_item_id )
											{

												CosmoxGMap.g_map.closeInfoWindow();
												var obj=CosmoxGMap.items[ this.cosmox_cosmox_location_id ];
												this.openInfoWindowHtml( obj.html );
												
												/*****************

												// zatvorim stari
												$('#box_3').dialog('close');
												
												// dohvatim podatke za trenutni marker
												var obj=CosmoxGMap.items[ this.cosmox_cosmox_location_id ];

												// postavim podatke
												$('#box_3 input[name=text1]').val( obj.name );
												$('#box_3 input[name=text2]').val( '(' + obj.map_lat + ', ' + obj.map_lng + ')');

												// pozicioniram
												
												var dialogDiv=$('#box_3');
												var dialog_w=dialogDiv.parent().width();
												var dialog_h=dialogDiv.parent().height();
												
												var new_pos_x = 0;
												var new_pos_y = 0;
												
												if(obj.smart_position)
												{
													var googleMapDiv=$('#google_map:first');
													var max_w=googleMapDiv.width();
													var max_h=googleMapDiv.height();
													var offset_x= googleMapDiv.offset().left;
													var offset_y= googleMapDiv.offset().top;
													
													var markerPos=CosmoxGMap.g_map.fromLatLngToContainerPixel( obj.g_latlng );
													markerPos.x+=offset_x;
													markerPos.y+=offset_y;
													
													new_pos_x = markerPos.x - dialog_w - 15;
													new_pos_y = markerPos.y - (dialog_h / 2);
													
													if(new_pos_x < 0 )
													{
														new_pos_x = markerPos.x + 10;
														
														if(new_pos_x + dialog_w > $(document).width() )
														{
															new_pos_x = $(document).width() / 2 - (dialog_w / 2 );
														}
													}
													
													if(new_pos_y < 0 )
													{
														new_pos_y = 0;
													}
													
													if(new_pos_y + dialog_h > $(document).height())
													{
														new_pos_y = $(document).height()  - dialog_h;
													}
												}
												else
												{
													new_pos_x = $(document).width() / 2 - (dialog_w / 2 );
													new_pos_y = $(document).height() / 2 - (dialog_h / 2 );
												}
												
												dialogDiv.dialog('option', 'position', [new_pos_x,new_pos_y] );
												dialogDiv.dialog('open');
												
												**************************/
											}
										});

		GEvent.addListener(item.g_marker, "infowindowclose", function()
				{

					CosmoxGMap.open_info_window_item_id = 0;
				});

		GEvent.addListener(item.g_marker, "infowindowopen", function()
				{

					CosmoxGMap.open_info_window_item_id = this.cosmox_cosmox_location_id;
				});

		

		if( CosmoxGMap._showMarkerVisibility[item.type] )
		{
			item.addOverlay=true;
			CosmoxGMap.g_map.addOverlay( item.g_marker );
		}
		else
		{
			item.addOverlay=false;
		}
	},

	showMarkers: function(type, status, setCookieType)
	{
		if(typeof( CosmoxGMap.typesIx[type] ) == 'undefined' )
		{
			return;
		}

		if(typeof(setCookieType) == 'undefined')
		{
			setCookieType=false;
		}

		for(var i=0; i<CosmoxGMap.typesIx[type].length; i++)
		{
			var itemIx=CosmoxGMap.typesIx[type][i];
			var item=CosmoxGMap.items[ itemIx ];

//							alert('('+item.id+')('+item.name+')');

			if(status && (item.g_marker.isHidden() || !item.addOverlay))
			{
				if(!item.addOverlay)
				{
					item.addOverlay=true;
					CosmoxGMap.g_map.addOverlay( item.g_marker );
				}
				else
				{
					item.g_marker.show();
				}
			}
			else if( !status && !item.g_marker.isHidden())
			{
				item.g_marker.hide();
			}
		} // for


		if(setCookieType)
		{
			if(cookieDomainName)
				$.cookie('adr_' + type, status ? 1 : 0, {expires:365, path:'/', domain:cookieDomainName} );
			else
				$.cookie('adr_' + type, status ? 1 : 0, {expires:365, path:'/'} );
		}

	},

	setMapDefault:function()
	{
		if(!CosmoxGMap.g_map)
			return;

		CosmoxGMap.g_map.closeInfoWindow();
		CosmoxGMap.g_map.setCenter(CosmoxGMap.g_def_latlng, CosmoxGMap.def_zoom );
	},

	setMapItem: function(id, zoomInFlag, infoOpenFlag, dontCenter)
	{
		if(!CosmoxGMap.g_map)
			return;

		if(typeof ( CosmoxGMap.items[id] ) == 'undefined' )
			return;

		var currentItem=CosmoxGMap.items[id];

		if(typeof(zoomInFlag)=='undefined')
			zoomInFlag=true;
			
		if(typeof(dontCenter)=='undefined')
			dontCenter=false;
			
		if(typeof(infoOpenFlag)=='undefined')
			infoOpenFlag=false;

		if(!dontCenter)
		{
			if(zoomInFlag)
			{
				CosmoxGMap.g_map.setCenter(
													 currentItem.g_latlng,
													  CosmoxGMap.item_zoom
													);
			}
			else
			{
				CosmoxGMap.g_map.setCenter(
										  currentItem.g_latlng
										);
			}
		}
		
		if(infoOpenFlag)
		{
			currentItem.g_marker.openInfoWindowHtml(currentItem.html );
		}
	},

	setMapArea: function(id)
	{
		if(!CosmoxGMap.g_map)
			return;

		if(! (typeof ( CosmoxGMap.items[id] ) == 'undefined' ) )
		{
			CosmoxGMap.g_map.setCenter(
												  CosmoxGMap.items[id].g_latlng,
												  CosmoxGMap.item_area_zoom
												);
		}
		else if(! (typeof ( CosmoxGMap.itemsArea[id] ) == 'undefined' ) )
		{
			CosmoxGMap.g_map.setCenter(
												  CosmoxGMap.itemsArea[id].g_latlng,
												  CosmoxGMap.item_area_zoom
												);
		}
	},



	_createHtml: function(obj)
	{
		var html='';
		html+='<div class="google_office">';

		var anchor1='';
		var anchor2='';
		if(CosmoxGMap._embedFlag)
		{
			anchor='<a href="#" onclick="return EmbedControl.isbnClick(this, \''+obj.book_isbn+'\', '+obj.id+');">';
			anchor2='<a class="book_map" href="#" onclick="return EmbedControl.isbnClick(this, \''+obj.book_isbn+'\', '+obj.id+');">';
		}
		else
		{
			anchor='<a href="#" onclick="BookDisplay.open(\''+obj.book_isbn+'\', \'\', \'\', \'\', \'\', 0, '+obj.id+'); return false;">';
			anchor2='<a class="book_map" href="#" onclick="BookDisplay.open(\''+obj.book_isbn+'\', \'\', \'\', \'\', \'\', 0, '+obj.id+'); return false;">';
		}
		
		if( obj.book_thumb_url)
		{
			html += anchor;
			html += '<img width="51" class="google_thumb" src="'+ obj.book_thumb_url+'" onerror="fixThumbUrl(this);"  alt="'+obj.book_name+'" /'+'>'; 
			html += '<' + '/a>';
		}
		
		html+='<h6 class="office_name">'+anchor+obj.book_name+'<'+'/a><'+'/h6>';

		if(obj.book_author)
		{
			html+='<dl class="author">' +
					'<dd>'+obj.book_author+'<'+'/dd>'+
					'<'+'/dl>';
		}

		if(obj.address)
		{
		//'<dt>Address:<'+'/dt>'+
			html+='<dl>' +
					'<dd>'+obj.address+'<'+'/dd>'+
					'<'+'/dl>';
		}
		
		if (obj.description)
		{
		//	'<dt>Description:<'+'/dt>'+
			html+='<dl>' +
				'<dd>'+obj.description+'<'+'/dd>'+
				'<'+'/dl>';
		}
		
		
		//html+='<dl>' +
		//	  '<dd><a onclick="ShareControl.infoTweeter(this, \''+obj.id+'\', \''+obj.book_name+'\', \''+obj.address+'\'); return false;" class="info_tweeter_share"  href="http://twitter.com/home?status=http://www.boekopdekaart.nl/%23L'+obj.id+' Boek op de kaart '+obj.book_name+' '+obj.address+'" >tweeter</a><'+'/dd>'; +
		//	  '<'+'/dl>';
		
		/*marktodo*/
		
		// twitter
		html+='<dl>' +
			  '<dd><a onclick="return ShareControl.twitter(this);" class="info_tweeter_share"  href="http://twitter.com/home?status=http://www.boekopdekaart.nl/ Boek op de kaart" >tweeter</a><'+'/dd>'; +
			  '<'+'/dl>';
		
		// facebook
		html+='<dl>' +
			  '<dd><a onclick="return ShareControl.fb(this);" class="info_facebook_share"  href="http://www.facebook.com/share.php?u=http://www.boekopdekaart.nl&t=Boek op de kaart">tweeter</a><'+'/dd>'; +
			  '<'+'/dl>';
		
		// email
		html+='<dl>' +
			  '<dd><a onclick="return ShareControl.email(this);" class="info_mail_share"  href="mailto:?Subject=Boek%20op%20de%20kaart&amp;Body=Boek%20op%20de%20kaart ' + escape('http://www.boekopdekaart.nl') + '" >tweeter</a><'+'/dd>'; +
			  '<'+'/dl>';
			  
		
		

		
		
		html+='<dl>' +
				'<dd>'+anchor2+'Bekijk boek<'+'/a><'+'/dd>'+
				'<'+'/dl>';

		
		
		


		html+='<div class="map_control_alt">';
		html+='<ul class="gmap_controls">';
		html+='<li><a href="#" onclick="CosmoxGMap.setMapItem('+obj.id+'); return false;" class="zoom">Zoom<'+'/a></li>';
		html+='<li><a href="#" onclick="CosmoxGMap.zoom('+obj.id+'); return false;" class="area">Gebied<'+'/a></li>';
		html+='<li><a href="#" onclick="CosmoxGMap.zoom(0); return false;" class="def">Default<'+'/a></li>';
		html+='<'+'/ul>';
		html+='<p class="report"><a href="#" onclick="return submit_abuseReport('+obj.id+');">Deze locatie klopt niet!<'+'/a></p>';
		//html+='<'+'p>';
		html+='<'+'/div>';
		
		html+='<'+'/div>';
		return html;
	},


	// zumira na podrucje ili na kartu te postavlja sel box na odgovarajucu vrijednost
	zoom: function(id)
	{

		if(id>0)
		{
			CosmoxGMap.setMapArea(  id  );
		}
		else
		{
			CosmoxGMap.setMapDefault();
		}


		return;
		//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		/*
		var selBox=document.getElementById('podruznica_grad');

		if(typeof(id)=='undefined')
		{

			if(selBox.selectedIndex == 0)
				id = 0;
			else if(selBox.selectedIndex > 0 && selBox.selectedIndex < selBox.options.length)
				id = selBox.options[selBox.selectedIndex ].value;
			else
				return;
		}

		if(id==0)
		{
			CosmoxGMap.setMapDefault();
			selBox.selectedIndex=0;
		}
		else
		{

			CosmoxGMap.setMapArea(  id  );

			if( $('#podruznica_grad option[value='+id+']').length )
			{
				$('#podruznica_grad option[value='+id+']').attr('selected','selected');
			}
			else
			{
				selBox.selectedIndex=0;
			}
		}
		*/

	},
	
	


	setDefaultMarkerVisibility: function(opt)
	{
		CosmoxGMap._showMarkerVisibility=opt;
	},
	
	
	
	gotoAddress:function(address, zoom)
	{
		if(typeof(zoom)=='undefined')
		{
			zoom=false;
		}
		
		CosmoxGMap.gClientGeocoder.getLatLng(address,
												function(latlng)
												{
												  if(!latlng)
												  {
													  latlng=CosmoxGMap.g_def_latlng;
												  }
												  
												  if(zoom)
													  CosmoxGMap.g_map.setCenter(latlng, zoom);
												  else
													  CosmoxGMap.g_map.setCenter(latlng);
												}
											);
	}
} // CosmoxGMap


////////////////////////////////////////
