// main JQuery function
var lsw, mw, fw,fn, cw, brpOpts, tabsNode, rlw,ww,pop;
var showmaximised = false;
var fwbar, fstartMap;
var startw, endw;
var abOn = false;

$().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'),
		baseDocument: $('body').data('sBaseDocument'),
		template: $('body').data('sTemplate'),
		defPoi : $('body').data('sDefPoi').split(','),
		defRouteTypes : $('body').data('sDefRouteTypes').split(',')
	};	
	mw = new brpMapWidget(brpOpts, $('#map-widget'), $('#infoWindow'));
	mw.onIconClicked = function(loc){
		lsw.addLocation(loc);
	     mw.routeLocations = lsw.locations;
	     mw.roundTrip = lsw.roundTrip;
		 mw.drawIcons();

	};
	
	mw.onTempIconClicked = function(loc){		
		mw.addUserMarker(loc);
		mw.hideTempMarker();
		lsw.addLocation(loc);
	};
	
	mw.onUserIconClicked = function(loc){
		lsw.addLocation(loc);
    };
    mw.onShowInfo = function(x,y,loc){
      pop.show(x,y,loc);  
    }

    pop = new brpPopupWidget($('#infoWindow'),mw);
    
	lsw = new brpRouteWidget(brpOpts, $('#route-widget'));
	
	rlw = new brpRouteListWidget(brpOpts,$('#route_list'),lsw);

	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');
		}
        
        if ($('#ab-planner').is(":visible")) {
            $('#ab-planner').slideUp('slow');

            DoBlur($('#find-widget-start .find-text'));
            DoBlur($('#find-widget-end .find-text'));

            lsw.showAllLoc();   
        }
   	    mw.drawRoute(route);
	};

	lsw.onLocationsChanged = function(locs){
		if (locs.length > 0)
        {
            $('#find-widget h3').html('Mijn volgende routepunt is');
		      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 > 0)	{
			      lsw.SetBar(3);
              }
		    } 
        else 
        {
                $('#find-widget h3').html('Ik wil fietsen vanaf');
			    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');
		}
		mw.routeLocations = locs;
		mw.roundTrip = lsw.roundTrip;
		mw.drawIcons();
	};
	lsw.onPlanRoute = function(){
		$('.routelength').html('wordt berekend...');
		$('.routeinfo').show();
	};
	lsw.onLocationClicked = function(loc){

        $('div#route_list').hide();
        $('div#route_detail').hide();
        $('div#map').show();

		mw.zoomTo(loc, 4);
	};
	
	lsw.onClearRoute = function() {
	    fw.setState(0);
	    fn.setState(0);
	    fwbar.setState(0);
	    fstartMap.setState(0);
	    mw.clearMarkers();
	    mw.drawBaseCoords();
	    rlw.reset();
	    lsw.SetBar(1);
	    pop.hide();
	    cw.setDefault();
	};
	lsw.onShowCategorie = function(p) {
	    cw.setRoutePois(p);
	}
	
	lsw.onDrawBase = function(p) {
        mw.routeLocations = lsw.locations;
	    mw.roundTrip = lsw.roundTrip;
	    mw.drawBaseCoords(p);
	}
	
	cw = new brpCategoryWidget(brpOpts, $('#step2_content'));
	mw.loadIcons();
	
	//categorylister = new brpCatListing("#catlist");
	
	cw.onCategoryChanged = function(){
		mw.loadIcons();
	};
	
	fw = new brpFindWidget(brpOpts, $('#find-widget'),false);
	fw.onLocationFound = function(loc, bIsPostalCode){
		mw.showTempMarker(loc);
		if (bIsPostalCode)
		{
			var iZoomLevel = 4;
		}
		else
		{
			var iZoomLevel = 6;
		}
		mw.zoomTo(loc, iZoomLevel);
		lsw.showMap();
	};
	fwbar = new brpFindWidget(brpOpts, $('#find-widget-bar'),true);
	fwbar.onLocationFound = function(loc, bIsPostalCode){
		mw.showTempMarker(loc);
		if (bIsPostalCode)
		{
			var iZoomLevel = 4;
		}
		else
		{
			var iZoomLevel = 6;
		}
		mw.zoomTo(loc, iZoomLevel);
		lsw.showMap();
	};
	//find-next
	fn = new brpFindWidget(brpOpts, $('#find-next'),false);
	fn.onLocationFound = function(loc, bIsPostalCode){
		mw.showTempMarker(loc);
		if (bIsPostalCode)
		{
			var iZoomLevel = 4;
		}
		else
		{
			var iZoomLevel = 6;
		}
		mw.zoomTo(loc, iZoomLevel);
		lsw.showMap();
	};
	//find-startMap position
	fstartMap = new brpFindWidget(brpOpts, $('#find-map'),true);
	fstartMap.onLocationFound = function(loc, bIsPostalCode){
		//mw.showTempMarker(loc);
		if (bIsPostalCode)
		{
			var iZoomLevel = 4;
		}
		else
		{
			var iZoomLevel = 6;
		}
		mw.zoomTo(loc, iZoomLevel);
		lsw.showMap();
	};

	// set the plan A.B function
	/*startw = new brpFindWidget(brpOpts, $('#find-widget-start'),true);
	startw.onLocationFound = function(loc, bIsPostalCode){
		mw.showTempMarker(loc);
		if (bIsPostalCode)
		{
			var iZoomLevel = 4;
		}
		else
		{
			var iZoomLevel = 6;
		}
		mw.zoomTo(loc, iZoomLevel);
		lsw.showMap();
	};
	endw = new brpFindWidget(brpOpts, $('#find-widget-end'),true);
	endw.onLocationFound = function(loc, bIsPostalCode){
		mw.showTempMarker(loc);
		if (bIsPostalCode)
		{
			var iZoomLevel = 4;
		}
		else
		{
			var iZoomLevel = 6;
		}
		mw.zoomTo(loc, iZoomLevel);
		lsw.showMap();
		$('#find-widget-end').focus();
	};
	*/
	$('#ab-planner').hide();
	$('#bar_ab').click(function(){
	    var me = this;
	    if ($('#ab-planner').is(":visible")) {
	        $('#ab-planner').slideUp('slow');
            DoBlur($('#find-widget-start .find-text'));
            DoBlur($('#find-widget-end .find-text'));
	    }
	    else {	    
	        $('#ab-planner').slideDown('slow');
	    }
	});
	$("#routelist-options-ab").click(function(){
		$("#ab-planner").slideToggle('slow');
	});
	$('#BtnPlanAB').click(function(){

      //implement google tracking
      if (_gaq){
          _gaq.push(['_trackEvent', 'Toon Optie', 'Plan AB']);
      }	        
	  
	  var locA=null
	  var locB=null;
	  // find startpoint;
	  var txt = $('#find-widget-start .find-text').val();
      if (txt.length > 0){
		$.getJSON(brpFindLocationUrl + '&format=json&callback=?', {'text': txt}, function(data, textStatus, bIsPostalCode){
			if((data) && (data.location))
			    locA = data.location;

                txt = $('#find-widget-end .find-text').val();
                if (txt.length > 0){
                  $.getJSON(brpFindLocationUrl + '&format=json&callback=?', {'text': txt}, function(data, textStatus, bIsPostalCode){
                    if((data) && (data.location))
                        locB = data.location;

	                    if ((locA) &&(locB)) {
	                      lsw.planAB(locA, locB );
	                      lsw.showMap();
	                    }  
	                    else
	                      alert('Bepaal eerst een startpunt en bestemming.');      


                });
                }

		});
	  }
	    
	});
	
	
	ww = new brpWeerWidget($('#current-weather'));
	$('#weer_btn').click(function(){
	    ww.toggleWeer();
		$('#weer_btn').toggle
	});

	$('#weer_btn').toggle(
		function(){ 
		    $(this).attr("src", "images/mapBar/mapbar_weather.png"); 
   	        //implement google tracking
	        if (_gaq){
	            _gaq.push(['_trackEvent', 'Toon Optie', 'weerinfo']);
	        }	        		
		},
		function(){ $(this).attr("src", "images/mapBar/mapbar_weather_on.png"); }
	);
    //weericonen initieel aan 
    ww.drawStations();
	mw.onMapMove = function(){
	    pop.hide();
		ww.setClosestStation();
	};
	
	// fvp icoon aan/uit
	$('#fvp_btn').toggle(
		function(){ 
		    $(this).attr("src", "images/mapBar/mapbar_fvp_on.png");
		    cw.setFVP(true); 
   	        //implement google tracking
	        if (_gaq){
	            _gaq.push(['_trackEvent', 'Toon Optie', 'fietsvoordeelpas']);
	        }	        
		    
		    },
		function(){ 
                $(this).attr("src", "images/mapBar/mapbar_fvp.png"); 
		        cw.setFVP(false); 

		    }
	);
	
    // set resize events
    
    function content_resize() 
	{
	    if (showmaximised)
	    {
		    var w = $( window );
		    var H = w.height();
		    var W = w.width();
		    $("#fullscreen").css( {width: W-20, height: H-20} );
		    $("#header").hide();
			$("#site").css({marginLeft: "0px"});
		    $('.pageholder-top-m').css( {width: W-20});
		    $('.pageholder-mid-l').css( {height: H-20} );
		    $('.pageholder-mid-m').css( {width: W-20, height: H-20} );
		    $('.pageholder-mid-r').css( {height: H-20} );
		    $('.pageholder-bottom-m').css( {width: W-20} );
		    $('#routeplannermenu').css( {height: H-40} );
		    $('div#routeplannermenu div.expand').css( {height: H-186} );
		    $('div#routeplannermenu div.expandfirst').css( {height: H-180} );
			
		    
			$('div#step1_spacer').css( {height: H-738} );
		    $('div#step4_spacer').css( {height: H-736} );
		    //$('#step2_content .poilist').css( {height: H-540} );
		    $('div#routewrapper').css( {height: H-378} );
			
		    $('#content_holder-routeplanner').css( {width: W-480} );
			
	        $('#content-routeplanner_m').css( {width: W-420, height: H-42} );
	        $('#content-routeplanner_ml').css( {height: H-40} );
	        $('#content-routeplanner_mr').css( {height: H-40} );
	        $('#map-bar').css( {width: W-420} );
	        $('#map-widget').css( {width: W-420, height: H-75} );
	        $('#ab-planner').css( {width: W-420} );
			$("#routeplanner_overlay").css({height: H-75});
			$(".routeplanner-offer").css({height: H-75});
			$("#buien_img").css({width: W-120, height: H-44});
	        $('#content-routeplanner_b').css( {width: W-440} );
	        $('#content-routeplanner_t').css( {width: W-440} );
			$('.popdest_clone').css({paddingBottom: 10});
			//$('div#route-list-container').css( {height: H-388} );
			
		    mw.resize();
            var w = $('#map-widget');
            var l = $('#routeplanner_overlay');
            var nL = w.width() - l.width();
            $('#routeplanner_overlay').css( {left: nL} );

            rlw.movetoPage(0);
            
            $('#adContainer1').show();
		}
	}
    
    function content_reset()
    {
        $('#fullscreen' ).css( {width: '', height: ''} );
        $("#header").show();
		$("#site").css({marginLeft: ""});
	    $('.pageholder-top-m').css( {width: 980});
	    $('.pageholder-mid-l').css( {height: 634} );
	    $('.pageholder-mid-m').css( {width: 980, height: 634} );
	    $('.pageholder-mid-r').css( {height: 634} );
	    $('.pageholder-bottom-m').css( {width: 980} );
		$('#routeplannermenu').css( {height: 612} );
		$('div#routeplannermenu div.expand').css( {height: 464} );
		$('div#routeplannermenu div.expandfirst').css( {height: 471} );
		
		$("div.middle").css({width: 564});
		
		$('div#step1_spacer').css( {height: 0} );
		$('div#step4_spacer').css( {height: 0} );
		    
		$('div#route1_content').css( {height: ''} );
		$('div#route2_content').css( {height: ''} );
		//$('#step2_content .poilist').css( {height: 145} );
		$('div#routewrapper').css( {height: 300} );
		

		$('#content_holder-routeplanner').css( {width: ''} );
		
		$('#content-routeplanner_m').css( {width: 584, height: 612} );
		$('#content-routeplanner_ml').css( {height: 612} );
		$('#content-routeplanner_mr').css( {height: 612} );
		$('#map-bar').css( {width: 584} );
		$('#map-widget').css( {width: 584, height: 577} );
		$('#ab-planner').css( {width: 584} );
		$("#routeplanner_overlay").css({height: 578});
		$(".routeplanner-offer").css({height: 578});
		$("#buien_img").css({width: "100%", height: "100%"});
		$('#content-routeplanner_b').css( {width: 564} );
		$('#content-routeplanner_t').css( {width: 564} );    
		$('.popdest_clone').css({paddingBottom: 462});
 
        mw.resize();

        var w = $( '#map-widget');
        var l = $('#routeplanner_overlay');
        var nL = w.width() - l.width();
        $('#routeplanner_overlay').css( {left: nL} );
        rlw.movetoPage(0);
        
        $('#adContainer1').hide();
    }
    
    //hide all overlaywindows
    $('div.overlay').hide();
        
    
    
	$( window ).wresize( content_resize );

	content_resize();
    $ ('#maximize_btn,#google_btn,#buien_btn,#weer_btn,#image_btn').mouseenter(function(oEvent)
    {
      oEvent.preventDefault();
      if (oEvent.srcElement)
        $(oEvent.srcElement).css( {color: 'blue'});  
    }).mouseleave(function(oEvent)
    {
      oEvent.preventDefault();
      if (oEvent.srcElement)
        $(oEvent.srcElement).css( {color: ''});  
    });
    $ ('.minmax').click( function(oEvent){
      oEvent.preventDefault();
      showmaximised = !showmaximised;
      if (showmaximised){
        content_resize();
      }
      else {
        content_reset();
      }
	});	
	$('.minmax').toggle(
		function(){ 
		    $(this).attr("src", "images/mapBar/mapbar_minimize.png"); 
   	        //implement google tracking
	        if (_gaq){
	            _gaq.push(['_trackEvent', 'Toon Optie', 'maximize']);
	        }	        		
		},
		function(){ $(this).attr("src", "images/mapBar/mapbar_maximize.png"); }
	);
    $ ('#buien_btn').click( function(oEvent){
      oEvent.preventDefault();
      ww.showBuien = (!ww.showBuien);
      ww.initBuien(true);
	});


	$('#buien_btn').toggle(
		function(){ 
	        $(this).attr("src", "images/mapBar/mapbar_buienradar_on.png"); 
   	        //implement google tracking
	        if (_gaq){
	            _gaq.push(['_trackEvent', 'Toon Optie', 'buienradar']);
	        }	        
	    },
		function(){ $(this).attr("src", "images/mapBar/mapbar_buienradar.png"); }
	);
    
        
	// 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).removeClass('showedittext');
      $(this).addClass('showsexampletext');
    })
    .focus(function() {
      if ($(this).val() == $(this).data('defaultVal'))
      {
        $(this).val('');
        $(this).removeClass('showsexampletext');
        $(this).addClass('showedittext');
      }
    })
    .blur(function() {
      if ($(this).val() == '')
      {
        $(this).val($(this).data('defaultVal'));
        $(this).removeClass('showedittext');
        $(this).addClass('showsexampletext');
      }
    });
    
    function DoBlur(Obj) {
        $(Obj).val($(Obj).data('defaultVal'));
        $(Obj).removeClass('showedittext');
        $(Obj).addClass('showsexampletext');
    }
	
	// Create stars
	$("#rat").stars({
		inputType: "select",
		oneVoteOnly: false
	});
	$("#rat").stars("selectID", -1);
	
    $("#score_general").stars({
		inputType: "select",
		disabled: true
	});
	

    

	$(".radio").dgStyle();
	$(".checkbox").dgStyle();

    $('#adContainer1').hide();
});



