/**
 *
 * @author    adam.pinter@dream.hu
 * @since     2011-05-17
 */
 

var markers = new Array();
var locations;
var curr_infw;
var content;
var bounds = new google.maps.LatLngBounds();
var map;

function googleMapsInitialize( element_id , url_or_json , language, callback, mouseEvent )
{
  if(!mouseEvent){
  	var mouseEvent = 'mouseover';
  }

  if(!element_id){
  	alert('googleMapsInitialize missing parameter: element_id');
  	return false;	
  }
  
  if(!language){
  	alert('googleMapsInitialize missing parameter: language');
  	return false;	
  }

  if(!url_or_json){
  	alert('googleMapsInitialize missing parameter: url_or_json');
  	return false;	
  }

  var latlng = new google.maps.LatLng(47.475, 19.062);
  
  var myOptions = 
      {
		zoom: 14,
		center: latlng,
		panControl: false,
		streetViewControl: false,
		mapTypeControl: false,
		zoomControl: true,
		scaleControl: false,
		mapTypeId: google.maps.MapTypeId.ROADMAP
      };
  map = new google.maps.Map(document.getElementById( element_id ), myOptions );
  
  if(callback){
  	google.maps.event.addListenerOnce(map, 'tilesloaded', callback);
  }
  
  if(typeof url_or_json != 'object'){
  	
  	// Ajax
  	jQuery.ajax({
  	  url: url_or_json,
  	  success: function(data) {
  	  
  	    data = jQuery.parseJSON(data);

  	
  		marker = new Array();
  		infowindow = new Array();
  	
  	    for( i in  data){
  	   
  			
  			locations = new google.maps.LatLng(data[i].latitude,data[i].longitude);
  			
  			var html = '<a style="text-decoration:none;" class="gmapsMarkerInfo" href="'+URI_FRONTEND+'ingatlan/'+data[i]['title_url_'+language]+'"><b style="background:#e1e2e2;display:block;padding:1px 2px;">'+data[i]['title_'+language]+'</b><img src="'+data[i]["image_url"]+'" title="'+data[i]['title_'+language]+'" /><img src="'+URI_STATIC+'images/logo.gif" alt="" style="height: 26px;margin: 0 0 8px 14px;" /><div>'+data[i]["current_build_area"]+'</div><div class="noBgRow">'+data[i]["planned_construction"]+'</div><div>'+data[i]["space_aviable"]+'</div><div class="noBgRow">'+data[i]["min_unit_size"]+'</div><div>'+data[i]["property_area"]+'</div></a>';
  	    
  	    	markers[i] = createMarker(locations, data[i]['title_'+language], html, map, mouseEvent);
  		
  			bounds.extend(locations);
  			//map.fitBounds(bounds);
  	
  			if(i==data.length-1){
  			
  				if(markers.length>1){
  	    			map.fitBounds(bounds);
  	    		}
  			
  				map.setCenter(bounds.getCenter());
  			}
  	    	
  	    }
  	  	
  	  }
  	});
  	// end of ajax
  	
  } else {
  	// JSON
  	
  	data = url_or_json;
  	marker = new Array();
  	infowindow = new Array();
  	
  	
  	for( i in  data){
  	    
  	    locations = new google.maps.LatLng(data[i].latitude,data[i].longitude);

        title = '';
        html = '';

        if( data[i]['title'] ){

          title = data[i]['title'];
          var html = '<a style="text-decoration:none;" class="gmapsMarkerInfo" href="'+URI_FRONTEND+'ingatlan/'+data[i]['title_url']+'"><b style="background:#e1e2e2;display:block;padding:1px 2px;">'+data[i]['title']+'</b><img src="'+data[i]["image_url"]+'" title="'+data[i]['title']+'" /><img src="'+URI_STATIC+'images/logo.gif" alt="" style="height: 26px;margin: 0 0 8px 14px;" /><div>'+data[i]["current_build_area"]+'</div><div class="noBgRow">'+data[i]["planned_construction"]+'</div><div>'+data[i]["space_aviable"]+'</div><div class="noBgRow">'+data[i]["min_unit_size"]+'</div><div>'+data[i]["property_area"]+'</div></a>';
        }
  	    
  	    markers[i] = createMarker(locations, title , html, map, mouseEvent);
  	
  	    bounds.extend(locations);
  	
  	    if(i==data.length-1){
  	    	//map.setZoom(map.getBoundsZoomLevel(bounds));

  	    	if(markers.length>1){
  	    		map.fitBounds(bounds);
  	    		
  	    	}
  	    	
  	    	map.setCenter(bounds.getCenter());
  	    	//alert(bounds.getCenter());
  	    }
  	    
  	}
  	
  	
  }
}

function createMarker(point, title, content, map, mouseEvent) {

  var icon = new google.maps.MarkerImage(URI_STATIC+'images/marker_logo.png',
      new google.maps.Size(28,28),
      new google.maps.Point(0,0),
      new google.maps.Point(0,28));

  var marker = new google.maps.Marker({
    position: point,
    map: map,
    title: title,
    icon: icon
  });

  var infowindow = new google.maps.InfoWindow({
    content: content
  });


  google.maps.event.addListener(marker, mouseEvent, function() {
    if(curr_infw) { curr_infw.close();}
    curr_infw = infowindow;
    infowindow.open(map, marker);

  });

  return marker;

};
