// JavaScript Document

/*** vars ***/
var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var latlng = new google.maps.LatLng(45.616898, 9.192333);
var mapOpen = false;
var directionsOpen = false;
var emailOpen = false;
var directionResult;
var lang;


/*** hide directions div ***/
$(document).ready(function() {
	$('#map_directions').hide();
	$('#map_email').hide();
	$('#invia_email').hide();
	$('#submit_directions').click(function(){
		calcRoute(); 
		return false;									   
	});
	$('#submit_email').click(function(){
		inviaEmail(); 
		return false;									   
	});
	$('#percorso').click(function(){
		showHideDirections(); 
		return false;									   
	});
	$('#chiudi_map_directions').click(function(){
		showHideDirections(); 
		return false;									   
	});	
	$('#invia_email').click(function(){
		showHideEmail(); 
		return false;									   
	});
	$('#chiudi_map_email').click(function(){
		showHideEmail(); 
		return false;									   
	});	
	$('#chiudi_maps').click(function(){
		hideGoogleMaps(); 
		return false;									   
	});
	
	
		  

		
		// default text in textfields
	$('input[type=text]').focus(function(){ 
	  if($(this).val() == $(this).data('default')){
		$(this).val('');
	  }
	});
	
	$('input[type=text]').blur(function(){
	  if($(this).val() == '')
	  {
		$(this).val($(this).data('default'));
	  } 
	});
	
});


			
$(window).load(function() {
	resizeGoogleMaps();
});

$(window).bind("resize",function() {
	resizeGoogleMaps();
});

$(window).bind("orientationchange",function(e) {
	setTimeout("resizeGoogleMaps()",100);
});
		
function initializeMap() {
			
	mapOpen = true;
	/*** initialize the directions object ***/
	directionsDisplay = new google.maps.DirectionsRenderer();

   /*** desaturate map ***/
	var stylez = [{
		featureType: "all",
		elementType: "all",
		stylers: [
		  { saturation:-100 }
		]
	 }];		  
  
	/* options */
	var myOptions = {
		zoom: 17,
		center: latlng,
		navigationControl: true,
		navigationControlOptions: {
		  	style: google.maps.NavigationControlStyle.DEFAULT,
		 	position: google.maps.ControlPosition.LEFT_TOP
		},
		mapTypeControl: false,
		scaleControl: true,
		streetViewControl:false,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		mapTypeControlOptions: {
		   mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'blackwhite']
		}
	};
  
	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  
	var styledMapOptions = {name: "Black-White"};

	var bwMapType = new google.maps.StyledMapType(stylez, styledMapOptions);

	map.mapTypes.set('blackwhite', bwMapType);
	map.setMapTypeId('blackwhite');
	
	
	/**** add marker ***/
	var marker_image = 'img/google_maps_marker.png';
	var marker = new google.maps.Marker({
		position: latlng,
		title:"Milano Bedding",
		icon: marker_image
	});
	
	
	// To add the marker to the map, call setMap();
	marker.setMap(map);
	directionsDisplay.setMap(map);
	resizeGoogleMaps();
}

function calcRoute() {
	var start = document.getElementById("start").value;
	var request = {
		origin:start, 
		destination:latlng,
		travelMode: google.maps.DirectionsTravelMode.DRIVING
	};
	directionsService.route(request, function(result, status) {
		if (status == google.maps.DirectionsStatus.OK) {
			directionsDisplay.setDirections(result);
			directionResult = result;
			showHideDirections();
			$('#invia_email').show();
		}
	 });
}

function showHideEmail(){	
	var area_height = (getWindowHeight() - 20);		// height of content area - -80 above and -20 below
	var destTop = ((area_height/2) );
	if(emailOpen){			// close
		$('#invia_email').html = langObject.invia_percorso;
		$('#map_email').animate({
			opacity: 0,
		 }, 500, function() {
			$('#map_directions').hide();
		});
	}else{
		
		$('#invia_email').html = langObject.chiudi_finestra_invia;
		$('#map_email').css('top', destTop);
		$('#map_email').show();
		$('#map_email').animate({
			opacity: 1,
		 }, 500, function() {
			
		});
	}
	emailOpen = !emailOpen;		//toggle
	
}


	
function showHideDirections(){
	var area_height = (getWindowHeight() - 20);		// height of content area - -80 above and -20 below
	var destTop = ((area_height/2) );
	if(directionsOpen){			// close
		$('#percorso').html = langObject.calcola_percorso;
		$('#map_directions').animate({
			opacity: 0,
		 }, 500, function() {
			$('#map_directions').hide();
		});
	}else{
		$('#percorso').html = langObject.chiudi_percorso;
		
		var buttonWidth = $('#submit_directions').css('width');
		var targ = getWindowWidth();
		
		$('#invia_email').hide();
		$('#map_directions').css('top', destTop);
		$('#map_directions').show();
		$('#map_directions').animate({
			opacity: 1,
		 }, 500, function() {
			
		});
	}
	directionsOpen = !directionsOpen;
}

function inviaEmail(){
	var emailAddress = document.getElementById("email").value;
	var myRoute = directionResult.routes[0].legs[0];
	var directionText = "";
	if(lang=='it'){
		directionText = "Come da Lei richiesto<br>Le inviamo le indicazioni stradali per poterci raggiungere:<ul>";
		for (var i = 0; i < myRoute.steps.length; i++) {
			directionText+="<li>"+myRoute.steps[i].instructions+"</li>";
		}
		directionText+="</ul>";
		directionText+="Grazie torni presto a trovarci<br>";
		directionText+="Buona giornata<br><br>";
		directionText+="Milano Bedding.";			
	}else{
		directionText = "As requested<br>here are the directions to our headquarters:<ul>";
		for (var i = 0; i < myRoute.steps.length; i++) {
			directionText+="<li>"+myRoute.steps[i].instructions+"</li>";
		}
		directionText+="</ul>";
		directionText+="Have a nice day,<br><br>";
		directionText+="Milano Bedding.";			
	}
	showHideEmail();
	
	//alert(directionText);
	$.post("http://www.milanobedding.it/directions/directions.php", { directions: directionText, email:emailAddress, submit:"submit"} ,function(data){
     //alert("Data Loaded: " + data);
   });
}

function getWindowHeight() {
        var windowHeight = 0;
        if (typeof(window.innerHeight) == 'number') {
            windowHeight = window.innerHeight;
        }
        else {
            if (document.documentElement && document.documentElement.clientHeight) {
                windowHeight = document.documentElement.clientHeight;
            }
            else {
                if (document.body && document.body.clientHeight) {
                    windowHeight = document.body.clientHeight;
                }
            }
        }
		/*if(windowHeight < $(document).height()){
			windowHeight = $(document).height();
		}*/
        return windowHeight;
    };

    // Dependable function to get Window Width
    function getWindowWidth() {
        var windowWidth = 0;
        if (typeof(window.innerWidth) == 'number') {
            windowWidth = window.innerWidth;
        }
        else {
            if (document.documentElement && document.documentElement.clientWidth) {
                windowWidth = document.documentElement.clientWidth;
            }
            else {
                if (document.body && document.body.clientWidth) {
                    windowWidth = document.body.clientWidth;
                }
            }
        }
		if(windowWidth < $(document).width()){
			windowWidth = $(document).width();
		}
        return windowWidth;
    };

function resizeGoogleMaps(){
	
	var targetHeight = (getWindowHeight() ) + "px";
	$('#map_container').css('height', targetHeight);
	
}

function openGoogleMaps(){
	$('#chiudi_maps').html(langObject.chiudi_finestra);
	$('#percorso').html(langObject.calcola_percorso);
	$('#invia_email').html(langObject.invia_percorso);
	$('#submit_email').val(langObject.send);
	$('#submit_directions').val(langObject.cerca);
	$('#start').val(langObject.indirizzo_partenza);
	$('#start').data('default', langObject.indirizzo_partenza);	
	$('#email').val(langObject.inserisci_email);
	$('#email').data('default', langObject.inserisci_email);
	initializeMap();
	//$('#map_container').slideUp(1000, "easeOutExpo");	
	$('#map_container').show();
	$('#map_container').css('top', getWindowHeight());
	google.maps.event.trigger(map, 'resize');
	map.setCenter(latlng);
	$('html').css({'overflow':'hidden'});
	$('#map_container').animate({
			top: 0,
		 }, 1500, 'easeInOutExpo', function() {
			google.maps.event.trigger(map, 'resize');
			$('html').css({'overflow':'auto'});
		});
	mapOpen = true;
}

function hideGoogleMaps(){
	mapOpen = false;
	$('html').css({'overflow':'hidden'});
	$('#map_container').animate({
			top: getWindowHeight(),
		 }, 1500, 'easeInOutExpo', function() {
			$('#map_container').hide();	
			$('html').css({'overflow':'auto'});
		});
	
}

