var mapDone = false;
var streetViewDone = false;

function mapInitialize(address,coor_uptodate,coor_lat,coor_long,container,fail_container,maps_holder,streetview_holder,id)
{
    if( id == 'show-maps' ){
	   var type = 'maps';
	   $('#show-maps').addClass('active');
	   $('#show-street').removeClass('active');
    } else if( id == 'show-street'){
	   var type = 'streetview';
	   $('#show-street').addClass('active');
	   $('#show-maps').removeClass('active');
    }

    $('.media-holder > div').hide();

    if(coor_uptodate == '0') {
	   var geocoder = new google.maps.Geocoder();
	   geocoder.geocode({'address': address
	   },  function(results, status)
		  {
			 if (status == google.maps.GeocoderStatus.OK) {
				showMap(results[0].geometry.location);
			 }
			 else {
				//console.log("Geocode was not successful for the following reason: " + status);
			 }
	   });
    }

    if(coor_lat != '') {
	   if(type=='maps'){
		  $('#' + maps_holder).show();
		  if( ! mapDone ) {
			 showMap(new google.maps.LatLng(coor_lat, coor_long),container,fail_container,address,maps_holder);
		  }
		  mapDone = true;
	   } else if(type=='streetview') {
		  $('#' + streetview_holder).show();
		  if( ! streetViewDone ){
			 showStreetView(new google.maps.LatLng(coor_lat, coor_long),container,fail_container,streetview_holder);
		  }
		  streetViewDone = true;
	   }
    }
}

function showMap(latLng,container,fail_container,address,maps_holder)
{
    $(fail_container).hide();
    var myOptions = {
	   zoom: 13,
	   center: latLng,
	   mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    $(container).show();
    var map = new google.maps.Map(document.getElementById(maps_holder), myOptions);
    var marker = new google.maps.Marker({
	   position: latLng,
	   map: map,
	   title: address
    });
}


function showStreetView(latLng,container,fail_container,streetview_holder)
{
    var panoramaOptions = {
	   position: latLng,
	   pov: {
		  heading: 0,
		  pitch: 0,
		  zoom: 0
	   }
    };

    $(fail_container).hide();
    $(container).show();

    var panorama = new google.maps.StreetViewPanorama(document.getElementById(streetview_holder), panoramaOptions);
}
