

    function logOut( ){
       var data = { mode: "logout" };
       $.post( "/redesign/david/services.cfm", data, function( resp ){

        console.log( resp );

          /* Try to log them out of FB and Google */

          console.log( "Would've window.location.reload( )'d here" );

          // window.location.reload( );



          gapi.load('client:auth2', function() {
            gapi.auth2.init( ).then(function() {
                gapi.auth2.getAuthInstance().signOut();
            });
          });

          /*

          gapi.auth2.init( );

          var auth2 = gapi.auth2.getAuthInstance();

          console.log( auth2 );

          auth2.signOut();

          */
  
          setTimeout( function( ){
            window.location.reload( );
          }, 1000 );

          /*
          .then(function () {
            console.log('User signed out.');
          });
          */
  

        console.log( "Should be signed out now?" );

      } );

    }


$(document).ready(function(){

  if($(".srp-watercraft").length > 0){
    if( filterForm != undefined)
      filterForm();
    $(".bottomPagination > .pagination > ul.pagination").hide();
    $('#srpf_1 select').on('change', filterForm);

    $('#srpf_1 input').on('keyup', function(){
      window.clearTimeout(filterFormTimeout);
      filterFormTimeout = window.setTimeout(function(){
        filterForm();
      }, 300);
    });
  }

  if( typeof( window.loggedIn ) == "undefined" ){
    console.log( "Not logged in" );
    window.loggedIn = false;
  }

  $("#topNav-1 > a").mouseenter(function(){
    $("#topNav-1 > ul").css({top:70});
  })

  var hide = 0;
  var rhide =0;

  $(".header-alt a.btn.btn-lg.btn-primary").mouseenter(function(){
    var tp = $(".header-alt a.btn.btn-lg.btn-primary").offset().top - $("#topNav-1 > ul").height() - 50;

    $("#topNav-1 > ul").css({top:tp});
    $("#topNav-1 > ul").addClass("active");

    if($("#topNav-1 > ul").attr("class").indexOf("active") >= 0){
      hide = 0
    }
  })

  $("#topNav-1 > ul").mouseenter(function(){
    hide = 0;
    rhide = 0;
  }).mouseleave(function(){
    $("#topNav-1 > ul").removeClass("active");
    hide = 1;
  });
  $(".carousel-inner .header-alt > div > div").mouseleave(function(){
    if(hide == 1){
      $("#topNav-1 > ul").removeClass("active");
    }
    rhide = 1;
    setTimeout(function(){
      if(rhide == 1)
        $("#topNav-1 > ul").removeClass("active");
    },100);
  });
  setTimeout(function(){
    $('#title').keyup();
    $('.multiselect-container').mouseleave(function(){$(this).parent().removeClass('open')});
    if($("#id_marinas").children().length == 1){
      $("#id_marinas").parent().hide();
    }
  }, 500);
  if($("#id_marinas").children().length == 1){
    $("#id_marinas").parent().hide();
  }

  // script to make tel links works only on mobile devies
  $("a").click(function(){
    if(window.innerWidth > 768){
      if($(this).attr("href") != undefined && $(this).attr("href").indexOf("tel") >= 0){
        return false;
      }
      //return false;
    }
  });

  // Selected option population script
  setTimeout(function(){
      $(".is_video_1").multiselect('destroy');
      $(".is_video_1").removeAttr('multiple');
      $(".is_video_1").multiselect();
      $(".is_video_1").next().find('ul li label input').each(function(){
        $(this).attr('type','checkbox');
        if(window.location.href.indexOf('is_video:0') >= 0 && $(this).parent().html().indexOf('Photos') >= 0){
          $(this).click();
          $(document).click();
        }
        if(window.location.href.indexOf('is_video:1') >= 0 && $(this).parent().html().indexOf('Video') >= 0){
          $(this).click();
          $(document).click();
        }
        if(window.location.href.indexOf('is_video') !== 0 && $(this).parent().html().indexOf('All') >= 0){
          $(this).parent().parent().parent().addClass('active');
        }
      });
  },500);

  //// boat icon and wild icon change for activities
  setTimeout(function(){
    if($(".fontastic-squirrel").html() !== undefined){
      var sqParent = $(".fontastic-squirrel").parent();

      sqParent.html('<img src="/layout/img/wildlife-icon.png">');
    }
    if($(".fontastic-waterski").html() !== undefined){
      var swParent = $(".fontastic-boat").parent();

      swParent.html('<img src="/layout/img/boat-icon.png">');
    }
  },500);

  // Customizing compare view
  window.compare = 0;
  $('.results-control-view button[data-results-view="compare"]').unbind('click');
  $('.results-control-view button[data-results-view="compare"]').on('click', function(e){
    e.preventDefault();
    var b = '',
      l = $('body').data('lakeid');

    createOrVerifySession();
  	hbSession = JSON.parse($.cookie('hbSession2'));

    if (typeof hbSession !== 'undefined') {
      Object.keys(hbSession.saved).forEach(function(i){
        if (hbSession.saved[i].lake == l) {
          if (b != '')
            b += ',';
          b += hbSession.saved[i].id;
        }
      });
    }
    var url = '/redesign/compare.cfm?lakeID=' + $('body').data('lakeid') + '&b=' + b;

    $(".compare-container").load(url, function(){
      $(".compare-container").show();

      $(".compare-searchView").show();
      $(".results-control-bar .control-group").hide();
      $(".bottomPagination").hide();

      $(".resultsSet.houseboats").hide();
      $('.compare-link').unbind('click');
      $('.compare-link').on('click', function(e){
        window.compare = 1;
    		if (isBreakpoint('xs'))
    			return true;

    		var $ri = $(this).closest('th.results-item');
    		openCompareInModal($ri.data('idt'), $ri.data('id'));


    		e.preventDefault();
    	})
    });

    //openURLInModal(url);

    e.preventDefault();
  });
  $('body').on('click', '.compare-searchView', function(e){
    $(".compare-searchView").hide();
    $(".results-control-bar .control-group").show();
    $(".bottomPagination").show();

    $(".compare-container").hide();
    $(".resultsSet.houseboats").show();
  });
  $( "body" ).off( "click", ".modal-ear a" );
  $('body').on('click', '.modal-ear a', function(e){
		var dir = 1;
		if ($(e.currentTarget).closest('.modal-ear').hasClass('modal-left'))
			dir = -1;
    if(window.compare == 0)
		  showSubsequentListingInModal($(e.currentTarget).closest('.modal-ear').data('idt'), $(e.currentTarget).closest('.modal-ear').data('id'), dir);
    else
      showSubsequentCompareInModal($(e.currentTarget).closest('.modal-ear').data('idt'), $(e.currentTarget).closest('.modal-ear').data('id'), dir);
		e.preventDefault();
	});

  // Map Trip Nav event handling script

  $('#topNav-7 > a').off('click');

  $('#topNav-7 > a .mytrip-container').on('click', function(e){


    // openURLInModal("/redesign/modules/forms/trip_signup.cfm");

        console.log( loggedIn );


          if( ! loggedIn ){

            openURLInModal("/redesign/modules/forms/trip_signup.cfm");

          } else {

            openURLInModal("/redesign/modules/content/tripContent.cfm");
            setTimeout(function(){
              initCompare();
              $("script").each(function(){
                  if($(this).attr("src")=="[particular source]"){
                      $(this).remove();
                  }
              });

            }, 1000);
            $("#at4-share").hide();
            $(".trip-tool").removeClass("not-logged");
          }

  });


  $('#topNav-7 > a .trip-tool-item').on('click', function(e){
    openURLInModal("/redesign/modules/content/tripContent.cfm");
  });

  $('body').on('click', ".trip-home-content .trip-profile-editor.nonActive",function(){
    $(".trip-home-content .trip-profile-content input").removeAttr("disabled");
    $(".trip-home-content .trip-profile-content input").attr("style", "border:1px solid #ccc");
    $(this).removeClass("nonActive").addClass("active");
  });

  $('body').on('click', ".trip-home-content .trip-profile-editor.active",function(){
    $(".trip-home-content .trip-profile-content input").attr("disabled","disabled");
    $(".trip-home-content .trip-profile-content input").attr("style", "");
    $(this).removeClass("active").addClass("nonActive");
  });

  $('body').on('click', function(event){

    if($(".trip-home-content .trip-note-editor.active").length > 0){

      that = $(".trip-home-content .trip-note-editor.active")[0];

      if($(that).parent().has(event.target).length == 0){
        $(that).parent().find('p.trip-note-text').attr('contenteditable','false');
        $(that).parent().find('p.trip-note-text').attr("style", "");
        $(that).parent().find('p.trip-note-text').html($(that).parent().find('p.trip-note-text').text());
        $(that).removeClass("active").addClass("nonActive");

        saveNote( $(that).parent().find('p.trip-note-text') );
      }
    }
  })

  $('body').on('click', ".trip-home-content .trip-note-editor.nonActive",function(){

    makeNotesAndAlertsUnsortable( );

    $(this).parent().find('p.trip-note-text').attr('contenteditable','true');
    $(this).parent().find('p.trip-note-text').attr("style", "border:1px solid #ccc");
    $(this).parent().find('p.trip-note-text').text($(this).parent().find('p.trip-note-text').html().replace(/&nbsp;/gi,' ')).focus( ).select( );
    $(this).removeClass("nonActive").addClass("active");

  });

  $('body').on('click', ".trip-home-content .trip-note-editor.active",function(){

    $(this).parent().find('p.trip-note-text').attr('contenteditable','false');
    $(this).parent().find('p.trip-note-text').attr("style", "");
    $(this).parent().find('p.trip-note-text').html($(this).parent().find('p.trip-note-text').text());
    $(this).removeClass("active").addClass("nonActive");

    saveNote( $(this).parent().find('p.trip-note-text') );

  });

  $('.resultsSet.houseboats .resultsItem a.specialsAvailable, .resultsSet.houseboats .resultsItem .media-item > a, .resultsSet.houseboats .resultsItem .resultsItem-content a, .resultsSet.watercraft  .resultsItem a.specialsAvailable, .resultsSet.watercraft  .resultsItem .media-item > a, .resultsSet.watercraft  .resultsItem .resultsItem-content a').on('click', function(e){

    console.log( "Also R E N" );

    window.compare = 0;
		if (isBreakpoint('xs'))
			return true;

		var $ri = $(this).closest('.resultsItem');


    if( $ri.hasClass( "noDIP" ) ){
      console.log( "Non-DIPPABLE thou shalt not DIP" );
      e.preventDefault();
      return false;
    } else {


      openListingInModal($ri.data('idt'), $ri.data('id'));

      e.preventDefault();
      
    }

	})

});

function initCompare(){

}

function openCompareInModal(idt, id){
	window.currentCompareSet = {};

	currentCompareSet.listings = [];
	currentCompareSet.activeIndex = 0;

	$('thead th.results-item').each(function(i){
		var o = {};
		o.id = $(this).data('id');
		o.idt = $(this).data('idt');
		o.hasdip = typeof $(this).data('hasdip') === 'undefined' ? 1 : $(this).data('hasdip');
		o.img = $(this).find('.broadhead img').attr('src');
		o.title = $(this).find('.compare-link').html();
		o.url = $(this).find('.compare-link').attr('href');

    currentCompareSet.listings.push(o);

		if (o.id == id)
			currentCompareSet.activeIndex = currentCompareSet.listings.length - 1;
	});

	if ($('.listing-modal-wrapper').length <= 0) {
		$('<div class="listing-modal-wrapper"><i class="fa fa-refresh fa-spin"></i></div>').appendTo(document.body);
		$('<div class="listing-modal-wrapper-preloader left"><i class="fa fa-refresh fa-spin"></i></div>').appendTo(document.body);
		$('<div class="listing-modal-wrapper-preloader right"><i class="fa fa-refresh fa-spin"></i></div>').appendTo(document.body);

		$('.listing-modal-wrapper').on('click', ".modal-header > .close", function(){
			$('body').removeClass('media-modal-open');
			$('.listing-modal-wrapper').removeClass('active').empty().append('<i class="fa fa-refresh fa-spin"></i>');
			$('.listing-modal-wrapper-preloader').empty().append('<i class="fa fa-refresh fa-spin"></i>');

		}).on('click', function(e){
			if ( $(e.target).hasClass('listing-modal') ) {
				$('.listing-modal-wrapper .modal-header > .close').trigger('click');
			}
		});
	}

	$('.listing-modal-wrapper').modal();

	var url = "/modules/content/houseboat-feeder.cfm?idLake=" + $('body').data('lakeid') + "&idt=" + idt + "&id=" + id;
	$('.listing-modal-wrapper:not(.active)').addClass('active').load(url, function(){
		updateCompareModalEars();
		initializeListing();
    //$(".modal-ear").hide();
		preloadCompare(true, true);
	});
}
function showSubsequentCompareInModal(idt, id, dir) {

	$('.listing-modal-wrapper').empty().append('<i class="fa fa-refresh fa-spin"></i>');

	var newIndex = currentCompareSet.activeIndex + dir;
	if (newIndex < 0) {
		newIndex = currentCompareSet.listings.length - 1;
	} else if (newIndex >= currentCompareSet.listings.length) {
		newIndex = 0;
	}

	currentCompareSet.activeIndex = newIndex;

	var url = "/modules/content/houseboat-feeder.cfm?idLake=" + $('body').data('lakeid') + "&idt=" + idt + "&id=" + id;
	$('.listing-modal-wrapper').load(url, function(){
		updateCompareModalEars();
		initializeListing();
	});
}
function updateCompareModalEars() {

	var prevIndex = currentCompareSet.activeIndex - 1;
	if (prevIndex < 0) {
		prevIndex = currentCompareSet.listings.length - 1;
	}

	var nextIndex = currentCompareSet.activeIndex + 1;
	if (nextIndex >= currentCompareSet.listings.length) {
		nextIndex = 0;
	}

	$('.modal-ear.modal-left')
		.removeClass('hidden')
		.attr('data-id', currentCompareSet.listings[prevIndex].id)
		.attr('data-idt', currentCompareSet.listings[prevIndex].idt)
		.find('a').attr('href', currentCompareSet.listings[prevIndex].url).end()
		.find('img').attr('src', currentCompareSet.listings[prevIndex].img).end()
		.find('.title').text(currentCompareSet.listings[prevIndex].title);

	$('.modal-ear.modal-right')
		.removeClass('hidden')
		.attr('data-id', currentCompareSet.listings[nextIndex].id)
		.attr('data-idt', currentCompareSet.listings[nextIndex].idt)
		.find('a').attr('href', currentCompareSet.listings[nextIndex].url).end()
		.find('img').attr('src', currentCompareSet.listings[nextIndex].img).end()
		.find('.title').text(currentCompareSet.listings[nextIndex].title);
  $('.modal-ear').addClass("compare");
	if (currentCompareSet.listings.length == 1) {
		$('.modal-ear').addClass('hidden');
	} else if (prevIndex == nextIndex) {
		$('.modal-ear.modal-left').addClass('hidden');
	}
}

function preloadCompare(left, right){

	if (left) {
		var prevIndex = currentCompareSet.activeIndex - 1;
		if (prevIndex < 0) {
			prevIndex = currentCompareSet.listings.length - 1;
		}

		var url = "/modules/content/houseboat-feeder.cfm?idLake=" + $('body').data('lakeid') + "&idt=" + currentCompareSet.listings[prevIndex].idt + "&id=" + currentCompareSet.listings[prevIndex].id;
		$('.listing-modal-wrapper-preloader.left').load(url, function(){
			//updateListingModalEars();
			initializeListing();
		});
	}

	if (right) {
		var nextIndex = currentCompareSet.activeIndex + 1;
		if (nextIndex >= currentCompareSet.listings.length) {
			nextIndex = 0;
		}

		var url = "/modules/content/houseboat-feeder.cfm?idLake=" + $('body').data('lakeid') + "&idt=" + currentCompareSet.listings[nextIndex].idt + "&id=" + currentCompareSet.listings[nextIndex].id;
		$('.listing-modal-wrapper-preloader.right').load(url, function(){
			//updateListingModalEars();
			initializeListing();
		});
	}
}
