/*
	Matías Martínez, (cc) 2010.
*/



  function initialize() {
  	// Hostal -33.441564 -70.63407 
  	 var myLatlng = new google.maps.LatLng(-33.441564,-70.63407);
    var myOptions = {
      zoom: 15,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    
    // Info
    var contentString = '<div id="content">'+
        '<div id="siteNotice">'+
        '</div>'+
        '<h3 id="firstHeading" class="firstHeading">Hostal Providencia</h3>'+
        '<div id="bodyContent">'+
        '<p>Av. Vicuña Mackenna 92-A|B Providencia, Santiago, Chile.</p>'+
        '</div>'+
        '</div>';
        
    var infowindow = new google.maps.InfoWindow({
        content: contentString
    });
    
    
    var marker = new google.maps.Marker({
        position: myLatlng, 
        map: map,
        title:"Hostal"
    });
    
    google.maps.event.addListener(marker, 'click', function() {
      infowindow.open(map,marker);
    });
    
  }
  
// Popup + init
var popupStatus = 0;

function loadPopup(){
	if(popupStatus==0){
		jQuery("#pop-prel").css({
			"opacity": "0.7"
		});
		jQuery("#pop-prel").fadeIn("slow");
		jQuery("#busca-l").fadeIn("slow");
		popupStatus = 1;
		
		initialize(); // Maps
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		jQuery("#pop-prel").fadeOut("slow");
		jQuery("#busca-l").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = jQuery("#busca-l").height();
	var popupWidth = jQuery("#busca-l").width();
	//centering
	jQuery("#busca-l").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	jQuery("#pop-prel").css({
		"height": windowHeight
	});
	
}


//CONTROLLING EVENTS IN jQuery
jQuery(document).ready(function(){
	
	//LOADING POPUP
	//Click the button event!
	jQuery("#showMap").click(function(){
		// scroll to top
		jQuery('html, body').animate({scrollTop:0}, 'slow');
		
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});
	
	jQuery("#map-preview").click(function(){
		// same
		jQuery('html, body').animate({scrollTop:0}, 'slow');
		
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
	});
				
	//CLOSING POPUP
	//Click the x event!
	jQuery("#close-btn").click(function(){
		disablePopup();
	});
	//Click out event!
	jQuery("#pop-prel").click(function(){
		disablePopup();
	});
	//Press Escape event!
	jQuery(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
	
	//misc
	jQuery("#bookpagebutton").click(function(){
		window.location = "http://63.247.74.234/~hostalpr/reservations";
	});

});

