$(document).ready(function(){

    ////////////////////////////////
    // add print page click event
    ////////////////////////////////
    $('.print').click(function(){		 
		var url = location.href.replace('#',''); 
		window.open(url + "?printable");

        //if (window.print) {
        //    window.print();
        //} else {
        //    alert('Sorry! We kunnen de pagina niet uitprinten.')
        //}
    })

    /////////////////////////////
    // Add google maps
    /////////////////////////////
    
    if ($('#googlemaps_map').length > 0) {
        var latlong = new Array(new GLatLng(52.22864, 5.17067),
								 new GLatLng(52.22974, 5.16980),
								 new GLatLng(52.22935, 5.17160),
								 new GLatLng(52.22559, 5.18141),
								 new GLatLng(52.22894, 5.18516),
								 new GLatLng(52.22895, 5.17122),
								 new GLatLng(52.22392, 5.17138),
								 new GLatLng(52.2201, 5.1960)
								 );
		
        var map = new GMap2(document.getElementById("googlemaps_map"));

        map.setCenter(latlong[0], 13);
        map.setMapType(G_HYBRID_MAP);
        
        for ( var i in latlong )
			map.addOverlay(new GMarker(latlong[i]));
		
		var mapControl = new GMapTypeControl();

map.addControl(new GLargeMapControl());
        var directionsPanel = document.getElementById("googlemaps_route");
        directions = new GDirections(map, directionsPanel);
        GEvent.addListener(directions, "error", handleErrors);
        
        //on submit show directions
        $('form.form-route input[name=submit]').click(function(ev){
            // Van
            var from = $('form.form-route input[name=zip]').val();
            if (!from || from == ''){
                from = $('form.form-route input[name=place]').val()
            }
            
            // Naar
            var to = $('.destination').val();
            var adress;
            if ( to == 1 )
				adress = "Dudokpark 10, 1217 JE";
			else if ( to == 2 )
				adress = "Dudokpark 1, 1217 JE";
			else if ( to == 3 )
				adress = "Oude Enghweg 23, 1217 JB";
			else if ( to == 4 )
				adress = "Wilhelminastraat 1-19, 1211 RH";
			else if ( to == 5 )
				adress = "Larenseweg 30, 1221 CN";
			else if ( to == 6 )
				adress = "Oude Enghweg 21, 1217 JB";
			else if ( to == 8 )
				adress = "Mussenstraat 21, 1223 RB";	
			else
				adress = "Kerkbrink 6, 1211 BX";
            
            directions.load("from: " + from + ", Nederland to:  "+adress+", Hilversum, Nederland");
            return false;
        })

        //on enter clear form
        $('form.form-route input[type=text]').focus(function(){
            $('form.form-route input[type=text]').not(this).val('');
        })
    }
    /////////////////////////////
    // handle sollicitatie form
    /////////////////////////////
    var form_rules = {             
        email: {
            required:true,
            email:true
        },
        email_friend:{
            required:true,
            email:true
        }     
    }
    var form_messages = {
        name:{
            required:"Uw naam ontbreekt"
        },
        adress: {
          required:"Uw adres ontbreekt"
        },
        zipcode: {
            required:"Uw postcode ontbreekt",
            zipcode:"Voer een geldige postcode in. (1201GM)",
            maxlength:"Uw postcode moet precies 6 tekens zijn",
            minlength:"Uw postcode moet precies 6 tekens zijn"
        },
        place: {
            required:"Uw plaats ontbreekt"
        },
        telephone: {
            required:"Uw telefoonnummer ontbreekt",
            maxlength:"Uw telefoonnummer moet precies 10 tekens zijn",
            minlength:"Uw telefoonnummer moet precies 10 tekens zijn"
        },
        description: {
            required:"Uw motivatie ontbreekt"
        },
        file: {
            required:"Uw cv ontbreekt"
        },
        name_friend:{
            required:"We hebben de naam van uw vriend/vriendin nodig"           
        },
        email: {
            required:"Uw email adres ontbreekt",
            email:"Controleer uw email adres"
        },
        email_friend: {
            required:"We hebben het email-adres van uw vriend/vriendin nodig",
            email:"Voer een geldig email-adres in"
        }

    }
    $('input[maxlength]').keyup(function(){
		if(this.value.length >= this.maxLength){
                        var name = $(this).attr('name');
			if (name == "zipcode") {
                           $('input[name=place]').focus();
                        }else if (name == "telephone") {
                            $('textarea[name=description]').focus();
                        }
		}
	});
    $('#sollicitatie-form').validate({
        errorPlacement: function(error, element) {

           error.appendTo("#errorMessageBox");
           $('#errorMessageBox').append('<br/>');
        },
        errorClass: "selected",
        rules:form_rules,
        messages:form_messages,
        submitHandler: function(form) {
   	$(form).ajaxSubmit({
            success:function(ev){
                $('#messageBox').text(ev);
            }
        });
        return false;
        }

    });

    ////////////////////////////////
    // mailto friend functionality
    ////////////////////////////////
//    $('a[href^=mailto]').click(function(){
//        var href = $(this).attr('href');
//        var friendfrom = 'marc';
//        var friendto = 'marc@wend.nl';
//        var friendsname = 'Henk';
//        href = href.replace('<friendto>',friendto).replace('<friendfrom>',friendfrom).replace('<friendsname>',friendsname);
//        $(this).attr('href',href);
//        return true;
//    });
    
});

 function handleErrors(){
    if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
        alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
    else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
        alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
    else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
        alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
    else if (gdir.getStatus().code == G_GEO_BAD_KEY)
        alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
    else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
        alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
    else alert("An unknown error occurred.");
 }




