var map;

function initialize() {
	if (GBrowserIsCompatible())	{      
		map = new GMap2(document.getElementById("googleMaps"));		
		map.setCenter(new GLatLng(52.39161,4.84080), 10); 
		
		var point = new GLatLng(52.39161, 4.84080);
		var marker = new GMarker(point);
		map.addOverlay(marker);
		
		map.addControl(new GSmallMapControl());

	}
}
function gInitialize() {
	if (GBrowserIsCompatible()) {      
		map = new GMap2(document.getElementById("gLocatie"));
		map.setCenter(new GLatLng(52.39161,4.84080), 10); 
		
		var point = new GLatLng($('#gLat').html(),$('#gLong').html());
		var marker = new GMarker(point);
		marker.openInfoWindowHtml($('#gContent').html()); 

		map.addOverlay(marker);
		map.addControl(new GSmallMapControl());
	}
}

function setDirections(type) {
	
	var gdir = new GDirections(map, document.getElementById("routebeschrijving"));
	
	GEvent.addListener(gdir, "error", handleErrors);	
	
	map.clearOverlays();
		var addrString = "from:" + document.getElementById("addr2").value + " to: La Guardiaweg 4 1043DG Amsterdam" ;
		$("#routebeschrijving").html("");
		gdir.load(addrString);			
		function putStatus() { document.getElementById("status").innerHTML =	"getSummaryHtml() : " + gdir.getSummaryHtml(); }
}

function refreshMap() {
	map.clearOverlays();
	points = [];
	document.getElementById("directions").innerHTML = "";
	document.getElementById("status").innerHTML = "";
}

function handleErrors(){
	
	if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
		alert("Bestemming niet gevonden.");
		
	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. or 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.");
}



$(document).ready(function () {
	
	if($("#googleMaps").length > 0) {
		$('#googlesearch .toSubmit').click(function(e){
			e.preventDefault();			
			setDirections();			
		});
		
		$('.toLocation').keyup(function(e) {
			if(e.keyCode == 13) {
			  e.preventDefault();
			
			  $('#googlesearch .toSubmit').trigger('click');
			}
		});	
		
		 if ($("table.contentTable").length > 0)
            {
                $("table.contentTable tr td a").attr("target", "_blank");
            }
		
		
		initialize();
		window.onunload=GUnload;
		
			
	
	}
	
	if($("#gLocatie").length > 0) {
		gInitialize();
		window.onunload=GUnload;
	}
	

	if($(".trefwoord").length > 0) {
		$(".trefwoord").Watermark("Vul een zoekterm in", "#666").keyup(function(e){
			var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
            if (key == 13) {
                e.preventDefault();
                var button = $(this).next(":submit");
                if (button.length > 0) {
                    if (typeof (button.get(0).onclick) == 'function') {
                        button.trigger('click');
                    }
                    else if (button.attr('href')) {
                        window.location = button.attr('href'); } else {
                        button.trigger('click');
                    }
                }
            }
		});		
	}
	if($(".toLocation").length > 0) {
		$(".toLocation").Watermark("Vertrekpunt..", "#666");
	}

	if($("#zoekresultaten").length > 0) {
		$("#zoekresultaten h3").addClass("noreplace");
	}
	Cufon.replace('#mainMenu a:not(.active), h1', { fontFamily: 'Vista Sans OT Light', hover: true});
	Cufon.replace('#mainMenu a.active', { fontFamily: 'Vista Sans OT Bold'});
	if($("#subnavigation").length > 0) {
		Cufon.replace('#subnavigation a:not(.current), .goback', { fontFamily: 'Vista Sans OT Light', hover: true});
		Cufon.replace('#subnavigation a.current, #mainMenu a.current ', { fontFamily: 'Vista Sans OT Bold'});
	}
	Cufon.replace('h2, h3:not(.noreplace),.header-description', { fontFamily: 'Vista Sans OT Reg' , hover: true});
	
	
	/*
	if($(".replace").length > 0) {
		Cufon.replace('.replae', { fontFamily: 'Vista Sans OT Light', hover: true});
	}
	*/
	

	/*
	$("#carousel-portfolio li").each(function(){
		var currentTallest = 0;
		$(this).each(function(i){
			if ($(this).height() > currentTallest) { 
				currentTallest = $(this).height();				
			}
		});
		//currentTallest = currentTallest + 20;

		$("#carousel-portfolio li").css({'min-height': currentTallest}); 
	});
*/
	

	if($("#carousel-portfolio").length > 0) {
		$('#carousel-portfolio ul:first').children().append("<p><a class=\"controls-prev\" href=\"#\"><span>Vorige</span></a> <a class=\"controls-next\" href=\"#\"><span>Volgende</span></a></p>");
		$('#carousel-portfolio ul:first').cycle({ 
			fx:     'fade', 
			speed:  'fast', 
			timeout: 0, 
			next:   '.controls-next', 
			prev:   '.controls-prev' 
		});
	}

	

	if($("#carousel-allround").length > 0) {
		$('#carousel-allround ul:first').children().append("<p><a class=\"controls-prev\" href=\"#\"><span>Vorige</span></a> <a class=\"controls-next\" href=\"#\"><span>Volgende</span></a></p>");
		$('#carousel-allround ul:first').cycle({ 
			fx:     'fade', 
			speed:  'fast', 
			timeout: 0, 
			next:   '.controls-next', 
			prev:   '.controls-prev' 
		});
	}

	if($("#headings-carousel-items").length > 0) {
		
		$.fn.cycle.updateActivePagerLink = function(pager, currSlideIndex) { 
			$(pager).find('li').removeClass('activeLI') 
				.filter('li:eq('+currSlideIndex+')').addClass('activeLI');
				Cufon.replace('#navItems li a', { fontFamily: 'Vista Sans OT Light'});
				Cufon.replace('.activeLI a', { fontFamily: 'Vista Sans OT Bold'});
		}; 
		$('#headings-carousel-items').before('<ul id="navItems">').cycle({  
			speed:  0,
			fx: 'none',
			timeout: 8000, 
			pager:  '#navItems',
			pagerEvent: 'mouseover',
			cleartype:  1,
			pagerAnchorBuilder: function(idx, slide){

				var src = $('h3 a', slide).html();
				return '<li><a href="#">' + src + '</a></li>';
			}
		});
		/*$('#headings-carousel-items h3').each(function(){
			var thisH3 = $("a", this).html();
			alert(thisH3);
		});*/
			

		$(".navItemsBg").show();
		$("#headings-carousel .fullBg").hide();
		$("#headings-carousel .item-full").hide();
		$(".item-full .noreplace .item-showless").css({"visibility":"hidden"});
		$("#headings-carousel .item-full .noreplace .item-showless").hide(); 
		
		if($("#headings-carousel-items").length > 0) {
			$("#headings").addClass("carousel-activated");
			//$("#overviewpage #contentContainer").css({"padding-top":"450px"});
		}
		$("#navItems li ").mouseover(function(){
			$('#headings-carousel-items').cycle('pause');
		});
		$(".item-showmore").click(function(){
			var itemType = $(this).attr("href");
			if(itemType == "#"){
				$("#headings-carousel .fullBg").show();
				$("#headings-carousel .item-full").show();
				$(".fullBg").css({"visibility":"visible"});
				$(".item-full").css({"visibility":"visible"});
				//$(".item-summery").append("ik moet weg!");
				$(".item-summery").hide();
					 
				$("#headings-carousel .item-full .noreplace .item-showless").show();
				$(".item-full .noreplace .item-showless").css({"visibility":"visible"});
				
				$('#headings-carousel-items').cycle('pause');
				return false;
			} else {
			}
			
		});
		$(".item-showless").click(function(){
			$(".fullBg").hide();
			$(".item-full").hide();
			$(".fullBg").css({"visibility":"hidden"});
			$(".item-full").css({"visibility":"hidden"});
			$(".item-full .noreplace .item-showless").css({"visibility":"hidden"});
			$(".item-summery").show();
			return false;
		});
		Cufon.replace('#navItems li a', { fontFamily: 'Vista Sans OT Light', hover: true});
		Cufon.replace('#navItems li a:first', { fontFamily: 'Vista Sans OT Bold'});
		Cufon.replace('.item-summery p:not(.noreplace), .item-full p:not(.noreplace), .details dd', { fontFamily: 'Vista Sans OT Reg'});
		//Cufon.replace('.item-full .intro, .details dt', { fontFamily: 'Vista Sans OT Bold'});
		Cufon.replace('#planuwroute h2, #headings-carousel h2, blockquote p', { fontFamily: 'Vista Sans OT LightItalic' , hover: true});
		

		$(".item-summery li a").hover(function(){
			Cufon.replace( $(this) , { fontFamily: 'Vista Sans OT Bold'});
		}, function() {
			Cufon.replace($(this), { fontFamily: 'Vista Sans OT Reg'});
		});
	}


		/*if($(".contacts .maskedImage p").length > 0) {
			
			
			if($(".contacts .maskedImage p img").attr("src" === "")){
				$(".contacts .maskedImage p img").attr("alt", "")
				$(".contacts .maskedImage p img").attr("src", "/_layouts/BPFCorporateWebsite/images/img_masked_Image_bg.png")					
			}
			
			
			//$(".contacts .maskedImage p img").attr("src", "../images/img_masked_Image_bg.png"); 
		}*/

	if($("div.navigation").length > 0) {
		// We only want these styles applied when javascript is enabled
		$('div.navigation').css({'width' : '200px', 'float' : 'left'});
		$('div.content').css('display', 'block');

		// Initially set opacity on thumbs and add
		// additional styling for hover effect on thumbs
		var onMouseOutOpacity = 0.67;
		$('#thumbs ul.thumbs li').opacityrollover({
			mouseOutOpacity:   onMouseOutOpacity,
			mouseOverOpacity:  1.0,
			fadeSpeed:         'fast',
			exemptionSelector: '.selected'
		});
		
		// Initialize Advanced Galleriffic Gallery
		var testWaarde = "2";
		var gallery = $('#thumbs').galleriffic({
			delay:                     5500,
			numThumbs:                 3,
			preloadAhead:              10,
			enableTopPager:            true,
			enableBottomPager:         true,
			maxPagesToShow:            7,
			imageContainerSel:         '#slideshow',
			controlsContainerSel:      '#controls',
			captionContainerSel:       '#caption',
			loadingContainerSel:       '#loading',
			renderSSControls:          true,
			renderNavControls:         true,
			playLinkText:              false,
			pauseLinkText:             false,
			prevLinkText:              '&lsaquo; Previous Photo',
			nextLinkText:              'Next Photo &rsaquo;',
			nextPageLinkText:          'Next &rsaquo;',
			prevPageLinkText:          '&lsaquo; Prev',
			enableHistory:             false,
			enableKeyboardNavigation:  false,
			autoStart:                 true,
			syncTransitions:           true,
			defaultTransitionDuration: 900,
			onSlideChange:             function(prevIndex, nextIndex) {
				// 'this' refers to the gallery, which is an extension of $('#thumbs')
				this.find('ul.thumbs').children()
					.eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
					.eq(nextIndex).fadeTo('fast', 1.0);
			},
			onPageTransitionOut:       function(callback) {
				this.fadeTo('fast', 0.0, callback);
			},
			onPageTransitionIn:        function() {
				this.fadeTo('fast', 1.0);
			}
		});
	}

});




