// main JQuery function
var lsw, mw, fw, cw, brpOpts, tabsNode;

$().ready(function() {
	brpOpts = {
		buttonBasePath: brpBaseUrl + 'images/',
		maxZoomPois: 6,
		printPageUrl: $('body').data('sPrintPageUrl'),
		startLocation: new Andes.Coordinate($('body').data('iStartX'), $('body').data('iStartY') ),
		startZoom: $('body').data('iStartZoom')
	};	
	mw = new brpMapWidget(brpOpts, $('#map-widget'), $('.infoWindow'));
	mw.onIconClicked = function(loc){
		lsw.addLocation(loc);
	};
	
	mw.onTempIconClicked = function(loc){
		mw.hideTempMarker();
		mw.addUserMarker(loc);
		lsw.addLocation(loc);
	};
	
	mw.onUserIconClicked = function(loc){
		lsw.addLocation(loc);
    };

	lsw = new brpRouteWidget(brpOpts, $('#route-widget'));
	
	lsw.onRouteChanged = function(route){
		if (route){	  
			$('.routeinfo').show();
			$('.routelength').html(route.distance + ' km');
			if (route.distance) {
			    var time1 = Math.round(route.distance / 17 * 60);
			    $('.routedisttime').html('%0 km (ongeveer %1 minuten.)'.format(route.distance, time1));
			}
		} else {
			$('.routeinfo').hide();
			$('.routelength').empty();
		}
		if ($('#tab2 .itinerarylist').hasClass('striped')){
			$('#tab2 .itinerarylist li:odd').addClass('odd');
			$('#tab2 .itinerarylist li:even').addClass('even');
		}
		mw.drawRoute(route);
	};

	lsw.onLocationsChanged = function(locs){
		if (locs.length > 0)
    {
		  if ( locs
		       && locs[0]
		       && locs[0].name ) {
		    $('#startpoint').text(locs[0].name);
		  }
			$('#step3_content .options').show();
			$('#step3_content .instructions').hide();
			$('#step3_content #step3_lenght').show();
			if (locs.length > 1)
			{
			  lsw.SetBar(3);
      }
		} 
    else 
    {
			lsw.SetBar(1);
			$('#step3_content .options').hide();
			$('#step3_content .instructions').show();
			$('#step3_content #step3_lenght').hide();
  		$('#startpoint').text('Klik in de kaart om een startpunt aan te geven');
		}
	};
	lsw.onPlanRoute = function(){
		$('.routelength').html('wordt berekend...');
		$('.routeinfo').show();
	};
	lsw.onLocationClicked = function(loc){
		mw.zoomTo(loc, 4);
	};
	
	cw = new brpCategoryWidget(brpOpts, $('#step2_content'));
	cw.onCategoryChanged = function(cat){
		mw.loadIcons();
	};
	
	fw = new brpFindWidget(brpOpts, $('#find-widget'));
	fw.onLocationFound = function(loc, bIsPostalCode){
		mw.showTempMarker(loc);
		if (bIsPostalCode)
		{
			var iZoomLevel = 4;
		}
		else
		{
			var iZoomLevel = 6;
		}
		mw.zoomTo(loc, iZoomLevel);
	};

	// set gui
	$('#step3_content .options').hide();
	$('#step3_content .instructions').show();
	$('#step3_content #step3_lenght').hide();

	// set splashscreen options
	custom_splashInit();

	$('.splashclose').click( function(oEvent){
      oEvent.preventDefault()
      $('#splash').hide();
	  $.setCookie( 'skip_splash', 'true', {
	    duration : 365 // In days
	  });	    
	});	
  $('.manual').click( function(oEvent){
    oEvent.preventDefault()
    $('#splash').show();
  });
  $('#splash-images-area1').click( function(oEvent){
    oEvent.preventDefault()
    $('#splash').hide();
    lsw.SetBar(1);
  });
  $('#splash-images-area2').click( function(oEvent){
    oEvent.preventDefault()
    $('#splash').hide();
    lsw.SetBar(2);
  });
  $('#splash-images-area3').click( function(oEvent){
    oEvent.preventDefault()
    $('#splash').hide();
    lsw.SetBar(3);
  });
  $('#splash-images-area4').click( function(oEvent){
    oEvent.preventDefault()
    $('#splash').hide();
    lsw.SetBar(4);
  });
    
	 // links
    
	 $('#stap1_h,#stap1_hh,#stap2_h,#stap2_hh,#stap3_h,#stap3_hh,#stap4_h,#stap4_hh,#faq-content-repeat,#step1a,#step2a,#step3a,#step4a,#v1,#v2,#v3,#v4').click(function(event){
	    var anchor = this;
	    switch(anchor.id)
	    {
	    case 'stap1_h':
	    case 'stap1_hh':
	    case 'step1a':
	        window.open("http://www.falk.nl/nl/fietsrouteplanner/toelichtingen/toelichting_stap_1.html");
	        break;
        case 'stap2_h':
	    case 'stap2_hh':
	    case 'step2a':
	        window.open("http://www.falk.nl/nl/fietsrouteplanner/toelichtingen/toelichting_stap_2.html");
	        break;
        case 'stap3_h':
	    case 'stap3_hh':
	    case 'step3a':
	        window.open("http://www.falk.nl/nl/fietsrouteplanner/toelichtingen/toelichting_stap_3.html");
	        break;	        	        
        case 'stap4_h':
	    case 'stap4_hh':
	    case 'step4a':
	        window.open("http://www.falk.nl/nl/fietsrouteplanner/toelichtingen/toelichting_stap_4.html");
	        break;
	    case 'faq-content-repeat':
	    	window.open("http://www.falk.nl/nl/fietsrouteplanner/veel_gestelde_vragen.html");
	        break;
	    }
	  });   

  $('input.inputexampletext')
    .each(function() {
      $(this).data('defaultVal',$(this).val());
      $(this).addClass('showsexampletext');
    })
    .focus(function() {
      if ($(this).val() == $(this).data('defaultVal'))
      {
        $(this).val('');
        $(this).removeClass('showsexampletext');
      }
    })
    .blur(function() {
      if ($(this).val() == '')
      {
        $(this).val($(this).data('defaultVal'));
        $(this).addClass('showsexampletext');
      }
    });
});


