/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatusGMap = 0;

//loading popup with jQuery magic!
function loadPopupGMap(){
	//loads popup only if it is disabled
	if(popupStatusGMap==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContactGMap").fadeIn("slow");
		var cell=document.getElementById("popupContactGMap");
		cell.style.display="block";
		popupStatusGMap = 1;
	
	
	 google.maps.event.trigger(map, "resize");
	 if (geocoder) {
	 var address = document.getElementById("address").value;
      geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          map.setCenter(results[0].geometry.location);
          var marker = new google.maps.Marker({
              map: map, 
              position: results[0].geometry.location
          });
        } else {
          alert("Geocode was not successful for the following reason: " + status);
        }
      });
    }
		
	}
}

//disabling popup with jQuery magic!
function disablePopupGMap(){
	//disables popup only if it is enabled
	if(popupStatusGMap==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContactGMap").fadeOut("slow");
		var cell=document.getElementById("popupContactGMap");
		cell.style.display="none";
		popupStatusGMap = 0;
	}
}

//centering popup
function centerPopupGMap(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContactGMap").height();
	var popupWidth = $("#popupContactGMap").width();
	
	//centering
	$("#popupContactGMap").css({
		"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	
	//LOADING POPUP
	//Click the button event!
	$("#button3").click(function(){
		//centering with css
		// var cell=document.getElementById("map_canvas");
		// cell.style.display="block";
		/*	if(cell!=null)
			{
				if ( cell.hasChildNodes() )
					{
						while ( cell.childNodes.length >= 1 )
							{
								cell.removeChild( cell.firstChild ); 
							} 
					}
			}*/
 			
		// initialize();
		centerPopupGMap();
		//load popup
		loadPopupGMap();
	});
				
	//CLOSING POPUP
	//Click the x event!
	$("#popupContactGMapClose").click(function(){
		disablePopupGMap();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopupGMap();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatusGMap==1){
			disablePopupGMap();
		}
	});

});
