$.googleMap = {
	maps: {},
	marker: function(m) {
		if (!m) {
			return null;
		} else if (m.lat == null && m.lng == null) {
			return $.googleMap.marker($.googleMap.readFromGeo(m));
		} else {
			var marker = new GMarker(new GLatLng(m.lat, m.lng));
			if (m.txt) {
				GEvent.addListener(marker, "click", function() {
    				marker.openInfoWindowHtml(m.txt);
  				});
			}
			return marker;
		}
	},
	readFromGeo: function(elem) {
		var latElem = $(".latitude", elem)[0];
		var lngElem = $(".longitude", elem)[0];
		if (latElem && lngElem) {
			return { lat:parseFloat($(latElem).attr("title")), lng:parseFloat($(lngElem).attr("title")), txt:$(elem).attr("title") }
		} else {
			return null;
		}
	},
	mapNum: 1
};

$.fn.googleMap = function(lat, lng, zoom, options) {

	// If we aren't supported, we're done
	if (!window.GBrowserIsCompatible || !GBrowserIsCompatible()) return this;

	// Default values make for easy debugging
	if (lat == null) lat = 52.557588;
	if (lng == null) lng = 19.713555;
	if (!zoom) zoom = 14;

	// Sanitize options
	if (!options || typeof options != 'object')	options = {};
	options.mapOptions = options.mapOptions || {};
	options.markers = options.markers || [];
	options.controls = options.controls || {};

	// Map all our elements
	return this.each(function() {
		// Make sure we have a valid id
		if (!this.id) this.id = "gMap" + $.googleMap.mapNum++;
		// Create a map and a shortcut to it at the same time
		var map = $.googleMap.maps[this.id] = new GMap2(this, options.mapOptions);
		// Center and zoom the map
       	map.setCenter(new GLatLng(lat, lng), zoom);
       	// Add controls to our map
       	for (var i = 0; i < options.controls.length; i++) {
	       	var c = options.controls[i];
	       	eval("map.addControl(new " + c + "());");
       	}
       	// If we have markers, put them on the map
       	var marker = null;
       	for (var i = 0; i < options.markers.length; i++) {
	       	if (marker = $.googleMap.marker(options.markers[i])) map.addOverlay(marker);
       	}
    });

};
//$(function(){
var Contact = {

	init : function(){
	    /*
		$('#formName_surname').click(function(){
			if ($("#formName_surname").val() == "Prosze wypelnić to pole")
            {
                 $("#formName_surname").val("");
                 $("#formName_surname").css("color","#000000");
                 $("#formName_surname").css("font-weight","normal");
            }
			return false;
		});
	    */
	   		$("#map").googleMap('50.098659', '18.573101', 14, {
			 controls: ["GSmallMapControl", "GMapTypeControl"]
			,markers: [
			   { lat:parseFloat(new String('50.098659'))
				,lng:parseFloat(new String('18.573101'))
				,txt:''
				}
			]
		});


		$('#formText').bind('click',function(e){
		    $("#formText").css("border-color","#A2B7C3");
		});

		$('#formMail').bind('click',function(e){
		    $("#formMail").css("border-color","#A2B7C3");
			if ($("#formMail").val() == "Prosze wypelnić to pole") 
            {
                 $("#formMail").val("");
                 $("#formMail").css("color","#000000");
                 $("#formMail").css("font-weight","normal");
            }
			if ($("#formMail").val() == "Proszę wpisać poprawny email")
            {
                 $("#formMail").val("");
                // $("#formMail").css("color","#000000");
                // $("#formMail").css("font-weight","normal");
            }            
			return false;
		});

		$("#buttonSend").click(function(){
		    if(trysubmit())
			document.getElementById('contactForm').submit();
		    return false;
		});
//});
    }
}	

function trysubmit() {
	var allowToSend = true;  
	/*
	document.getElementById('formMail').style.borderColor="#c0c5d1";
	document.getElementById('formName_surname').style.borderColor="#c0c5d1";
	document.getElementById('formText').style.borderColor="#c0c5d1";
	*/
       /*
    if( document.getElementById('formName_surname').value.length ==0 ) {
		allowToSend = false; 
		document.getElementById('formName_surname').style.borderColor="#DE3D09";
		//document.getElementById('formName_surname').style.color="#FF0000";
		//document.getElementById('formName_surname').style.fontWeight="bold";
		//document.getElementById('formName_surname').value="Prosze wypelnić to pole";
	}
		*/
	if( document.getElementById('formText').value.length ==0 ) {
		allowToSend = false; 
		document.getElementById('formText').style.borderColor="#DE3D09";
		//document.getElementById('formText').style.color="#FF0000";
		//document.getElementById('formText').style.fontWeight="bold";
		//document.getElementById('formText').value="Prosze wypelnić to pole";
	}
	
    if( document.getElementById('formMail').value.length == 0 ) {
		allowToSend = false; 
		document.getElementById('formMail').style.borderColor="#DE3D09";
		//document.getElementById('formMail').style.color="#FF0000";
		//document.getElementById('formMail').style.fontWeight="bold";
		//document.getElementById('formMail').value="Prosze wypelnić to pole";
	
	}else if( !email_validate(document.getElementById('formMail').value) ) {
		allowToSend = false; 
		document.getElementById('formMail').style.borderColor="#DE3D09";
		//document.getElementById('formMail').style.color="#FF0000";
		//document.getElementById('formMail').style.fontWeight="bold";
		document.getElementById('formMail').value="Proszę wpisać poprawny email";
	}
	/*
    if( allowToSend == false ) {
		document.getElementById('formerror').innerHTML = 'Błąd - nie wszystkie wymagane pola są poprawne lub zostały wypełnione!';
	} else {
		document.getElementById('polec_form').submit();
	}
	*/
	if( allowToSend == true ) {
		//document.getElementById('contactForm').submit();
		//alert('jestem tu');
		return true;
	} else {
	    document.getElementById('formerror').innerHTML = 'aby wysłać wiadomość wypełnij wszystkie wymagane pola!';
	}
	
	return false;
}; 	


function email_validate(src)  {
	var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
	return regex.test(src);
};


/*
    $(document).ready(function(){
    	$('#formName_surname').click( function(){clearField('formName_surname');  } );
    	document.getElementById("formMail").onclick( function(){
        document.getElementById("formMail").value = '';
        alert('jestem tu');
        } );
        //document.getElementById("description").style.display="block";    
        }
    );
    
    function clearField(field_id)
    {
        alert('jestem tu');
        //document.getElementById(field_id).innerHTML = '';
    }
*/
