var address = 'Zuidergrachtswal 2 Leeuwarden';
var staticUrlBase = 'http://maps.google.com/maps/api/staticmap?size=400x400&sensor=false&path=' + address + '|';
var ShopLocation;
var directionDisplay;
var directionsService;
var map;
var gdir;

function init() {
	directionsService = new google.maps.DirectionsService();

	directionsDisplay = new google.maps.DirectionsRenderer();
	var geocoder = new google.maps.Geocoder();
	
	var myOptions = {
		zoom: 16,
		disableDefaultUI: true,
		navigationControl: true,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	map = new google.maps.Map(document.getElementById("map"), myOptions);
	directionsDisplay.setMap(map);
	directionsDisplay.setPanel(document.getElementById("map-directions"));
	
	//GEvent.addListener(gdir, "addoverlay", onGDirectionsLoad);	// add for static maps directions
	
	if(geocoder) {
		geocoder.geocode( { 'address': address}, function(results, status) {
			if(status == google.maps.GeocoderStatus.OK) {
/* 				var path = results.routes[0].overview_path;  */
				// alert(results.getPolyline());
			
				ShopLocation = results[0].geometry.location;
				map.setCenter(ShopLocation);
				var image = 'assets/images/design/map_icon.png';
				
				var mapMarker = new google.maps.Marker({
					position: ShopLocation,
					map: map,
					icon: image
				});
			} else {
 				// alert("Geocode was not successful for the following reason: " + status);
			}
		});
	}
}

function PlanRoute() {
	var start = document.getElementById("vertrekpunt").value;
	var end = address;
	var request = {
		origin:start, 
		destination:end,
		travelMode: google.maps.DirectionsTravelMode.DRIVING
	};
	directionsService.route(request, function(response, status) {
		// alert(response.getPolyline());
			if (status == google.maps.DirectionsStatus.OK) {
				directionsDisplay.setDirections(response);
			}
	
		if(jQuery('#routemap-directions').html().length > 0) {
			jQuery('#print').fadeIn(500);
		} else {
			jQuery('#print').fadeOut(500);
		}
		
		
	});
	
/* 	jQuery('#print-map').attr('src', staticUrlBase + start); */
}

/***** START POLYLINE TEST *****/
function onGDirectionsLoad() {
	var poly = gdir.getPolyline();
   if (poly.getVertexCount() > 100) {
     alert("This route has too many vertices");
     return;
   }
   var baseUrl = "http://maps.google.com/staticmap?";

   var params = [];
   var markersArray = [];
   markersArray.push(poly.getVertex(0).toUrlValue(5) + ",greena");
   markersArray.push(poly.getVertex(poly.getVertexCount()-1).toUrlValue(5) + ",greenb");
   params.push("markers=" + markersArray.join("|"));

   var polyParams = "rgba:0x0000FF80,weight:5|";
   var polyLatLngs = [];
   for (var j = 0; j < poly.getVertexCount(); j++) {
     polyLatLngs.push(poly.getVertex(j).lat().toFixed(5) + "," + poly.getVertex(j).lng().toFixed(5));
   }
   params.push("path=" + polyParams + polyLatLngs.join("|"));
   params.push("size=300x300");
   params.push("key=ABQIAAAAjU0EJWnWPMv7oQ-jjS7dYxSPW5CJgpdgO_s4yyMovOaVh_KvvhSfpvagV18eOyDWu7VytS6Bi1CWxw");

   baseUrl += params.join("&");

   var extraParams = [];
   extraParams.push("center=" + map.getCenter().lat().toFixed(6) + "," + map.getCenter().lng().toFixed(6));
   extraParams.push("zoom=" + map.getZoom());
   addImg(baseUrl + "&" + extraParams.join("&"), "staticMapOverviewIMG");

   var extraParams = [];
   extraParams.push("center=" + poly.getVertex(0).toUrlValue(5));
   extraParams.push("zoom=" + 15);
   addImg(baseUrl + "&" + extraParams.join("&"), "staticMapStartIMG");

   var extraParams = [];
   extraParams.push("center=" + poly.getVertex(poly.getVertexCount()-1).toUrlValue(5));
   extraParams.push("zoom=" + 15);
   alert(baseUrl + "&" + extraParams.join("&"), "staticMapEndIMG");
}
/**** END POLYLINE TEST ***/

jQuery(function() {
	jQuery('#planroute').click(function() {
		PlanRoute();
	});
	jQuery('#vertrekpunt').keyup(function(e) {
		if(e.which == 13) {
			PlanRoute();
		}
	});
	
	jQuery('#search').click(function() {
		if(jQuery(this).val() == 'Vul hier uw zoekwoord in...') {
			jQuery(this).val('');
		}
	});
	
	jQuery('#search').blur(function() {
		if(jQuery(this).val() == '') {
			jQuery(this).val('Vul hier uw zoekwoord in...');
		}
	});
	
	if(jQuery('#highlight').length > 0) {
		jQuery('#highlight').cycle({ 
		    fx:     'scrollLeft', 
		    speed:   500, 
		    timeout: 5000, 
		    next:   '#highlight-next',
		    prev: '#highlight-prev',
		    pause:   1 
		});
	}
	
	if(jQuery('.thumblist').length > 0) {
		jQuery('.thumblist img').mouseover(function() {
			var row1 = jQuery(this).parent().find('.case-row1').html();
			var row2 = jQuery(this).parent().find('.case-row2').html();
			var offsetY = jQuery(this).offset().top;
			var newOffsetY = offsetY + 25;
			jQuery('#case-target h2').html(row1);
			jQuery('#case-target p').html(row2);
			jQuery('#case-target').css('top', newOffsetY + 'px')
		});
	}
	
	if(jQuery('#wwd').length > 0) {
		jQuery('.wwd-expand').click(function() {
			jQuery(this).addClass('wwd-current');
			jQuery('#wwd .expanded').slideUp(500, function() {
				jQuery('#wwd .expanded').addClass('minimized');
				jQuery('#wwd .expanded').removeClass('expanded');
				
				jQuery('.wwd-current').parent().find('ul').slideDown(500, function() {
					jQuery('.wwd-current').parent().find('ul').addClass('expanded');
					jQuery('.wwd-current').parent().find('ul').removeClass('minimized');
					jQuery('.wwd-current').removeClass('wwd-current');
					
					jQuery('.minimized').parent().find('a.wwd-expand').slideDown(200);
					jQuery('.expanded').parent().find('a.wwd-expand').slideUp(200);
				});
			});
		});
	}
	
	if(jQuery('#right .thumblist').length > 0) {
		var mainPic = jQuery('.thumblist a:first').attr('href');
		jQuery('#main-pic').fadeOut(0, function() {
			jQuery('#main-pic').attr('src', mainPic);
			jQuery('#main-pic').fadeIn(0);
		});

	
		jQuery('#right .thumblist a').click(function() {
			var mainPic = jQuery(this).attr('href');
			jQuery('#main-pic').fadeOut(500, function() {
				jQuery('#main-pic').attr('src', mainPic);
				jQuery('#main-pic').fadeIn(1000);
			});
		});
	}
	
	if(jQuery('.fancybox').length > 0) {
		jQuery('.fancybox').fancybox({
			'type': 'iframe',
			'width': 900,
			'height': 650
		});
	}
	
	jQuery('.trackcontent').each(function(i, val) {
		var liCount = jQuery(this).find('li').size();
		if(liCount == 0) {
			jQuery(this).hide();
		}
	});
});

function CheckMultiple2(frm, name) {
	for (var i=0; i < frm.length; i++) {
		fldObj = frm.elements[i];
		fldId = fldObj.id;
		if (fldId) {
			var fieldnamecheck=fldObj.id.indexOf(name);
			if (fieldnamecheck != -1) {
				if (fldObj.checked) {
					return true;
				}
			}
		}
	}
	return false;
}
function CheckForm2(f) {
	var email_re = /[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i;
	if (!email_re.test(f.email.value)) {
		alert("Vul a.u.b. het e-mail adres in.");
		f.email.focus();
		return false;
	}
	return true;
}

