var geocoder;

var map;
var count;

var bounds;

var directionDisplay;

var directionsService;

var shops = [];

var ff = 'false';

$(document).ready(function(){

	if($('body.ie6').length == 1) {
		if(document.getElementById("map")) {
			$('#map').css('background','url(../img/map.gif) no-repeat');
		}
		return false;
	}
	
	if(document.getElementById("map")) {
	
	//directions handler
	
	directionsDisplay = new google.maps.DirectionsRenderer();
	
	geocoder = new google.maps.Geocoder();
	
	var latlng = new google.maps.LatLng(51.456745,-2.597201);
	
	var myOptions = { 
		zoom: 15, 
		scrollwheel: false,
		disableDefaultUI: false, 
		center: latlng,
		mapTypeId: google.maps.MapTypeId.ROADMAP 
	};
	
	map = new google.maps.Map(document.getElementById("map"), myOptions);
	
	//direction setup!
	
	directionsDisplay.setMap(map);
	
	//Create a new viewpoint bound
	
	bounds = new google.maps.LatLngBounds();

	
	//get Markers
	addMakers();
	

	
	
	
	$('.listStudios a').click(function(event) {
		event.preventDefault();
		var value = $(this).attr('href');
		var withoutHash = value.substr(1);
		window.location.hash = value;
		 
		 
		$('li.studios').css('display','none');
	    $('#'+withoutHash).fadeIn('slow');
	    
	   
	    
	    return false;
	});
	
	}
});


function addMakers() {

	var places = new Array();
	
	/*places[0] = new Array("Cotswold Outdoor Shop", "51.457218", "-2.591824", "BS1 2DL");
	places[1] = new Array("Pellegrino's Fish and Chip Shop", "51.456179", "-2.596373", "BS1 5BT");
	places[2] = new Array("St Nicholas Market", "51.454692", "-2.593403", "BS1 1JG");
	places[3] = new Array("Christmas Steps", "51.456033", "-2.596846", "BS1 5BS");
	places[4] = new Array("Colston Yard", "51.457289", "-2.597044", "BS1 5BD");
	places[5] = new Array("Bristol Bus Station", "51.459458", "-2.593335", "BS1 3NU");
	places[6] = new Array("Colston Hall", "51.454785", "-2.598254", "BS1 5AR");
	places[7] = new Array("Kathmandu (restaurant)", "51.454311", "-2.597629", "BS1 4XE");
	places[8] = new Array("Trenchard Street Car Park", "51.455351", "-2.600251", "BS1 5AN");
	places[9] = new Array("Hotel du VIn", "51.456699", "-2.596596", "BS1 2NU");
	places[10] = new Array("Coffee (Gusto)", "51.457327", "-2.596848", "BS2 8DJ");*/
	places[0] = new Array("Snowline", "51.456741", "-2.5972", "BS1 5BB");
	
	length = places.length;

	var img = '/img/map-pins.png';
	var office = '/img/snowline-office-pin.png';
		
	for (var i = 0; i < length; i++) {
		
		myLatLng = new google.maps.LatLng(places[i][1], places[i][2]);
		var postcode = places[i][3];
		var title = places[i][0];
		
		//if(i ==11) {
			addMaker(myLatLng, postcode, i, office, length, title);
		//} else {
		//	addMaker(myLatLng, postcode, i, img, length, title);
		//}
	}
		
	
}

function addMaker(myLatLng, postcode, index, img, length, title) {
	count++;
	shops.push({
					'id': index,
					'lat':myLatLng.lat(),
					'lng':myLatLng.lng(),
					'postcode': postcode
				});
	
	var contentString;
	contentString = "Test";

	var infowindow = new google.maps.InfoWindow({
		content: contentString
	});
							
	var marker = new google.maps.Marker({
		position: myLatLng,
		map: map,
		animation: google.maps.Animation.DROP,
		title: title,
		icon: img
		
	});

	if (count == length ) {
	
	}
	bounds.extend(myLatLng);
	//map.fitBounds(bounds);
}

function toggleBounce(marker) {
    if (marker.getAnimation() != null) {
        marker.setAnimation(null);
    } else {
        marker.setAnimation(google.maps.Animation.BOUNCE);
    }
}






/** Converts numeric degrees to radians */
if (typeof(Number.prototype.toRad) === "undefined") {
  Number.prototype.toRad = function() {
    return this * Math.PI / 180;
  }
}

/** 
 * Formats the significant digits of a number, using only fixed-point notation (no exponential)
 * 
 * @param   {Number} precision: Number of significant digits to appear in the returned string
 * @returns {String} A string representation of number which contains precision significant digits
 */
if (typeof(Number.prototype.toPrecisionFixed) === "undefined") {
  Number.prototype.toPrecisionFixed = function(precision) {
    if (isNaN(this)) return 'NaN';
    var numb = this < 0 ? -this : this;  // can't take log of -ve number...
    var sign = this < 0 ? '-' : '';
    
    if (numb == 0) { n = '0.'; while (precision--) n += '0'; return n };  // can't take log of zero
  
    var scale = Math.ceil(Math.log(numb)*Math.LOG10E);  // no of digits before decimal
    var n = String(Math.round(numb * Math.pow(10, precision-scale)));
    if (scale > 0) {  // add trailing zeros & insert decimal as required
      l = scale - n.length;
      while (l-- > 0) n = n + '0';
      if (scale < n.length) n = n.slice(0,scale) + '.' + n.slice(scale);
    } else {          // prefix decimal and leading zeros if required
      while (scale++ < 0) n = '0' + n;
      n = '0.' + n;
    }
    return sign + n;
  }
}

function success(position) {
	//console.log(position.coords.latitude + "," + position.coords.longitude);
	//var latlng = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
	var img = '/img/you-pin.png';
	var hcard = $('#26B-Shaftesbury-Road');
	addMaker(position,100,hcard,img,30, false);
}

function error(msg) {
	console.log(msg);
  console.log(arguments);
}



