/****************************
Copyright (c) 2013 Canadensys
Explorer Portal
****************************/
/*global EXPLORER, $, window, document, console, google, _*/

EXPLORER.preview = (function() {

  'use strict';

  var _private = {

    sideBar: "",
    occPreview: "",
    closeElement: "",
    sidebarTop: 0,

    init: function() {
      this.sideBar = $('#control_wrapper');
      this.occPreview = $('#occ_preview');
      this.closeElement = $('#preview_close');
      this.sidebarTop = 0;

      if(this.occPreview.length && this.closeElement.length){
        this.sidebarTop = this.sideBar.offset().top - 36;
        this.loadEvents();
      }
    },

    loadEvents: function() {
      this.initClosePreview();
      $(window).scroll(function() {
        if ($(this).scrollTop() >= this.sidebarTop) {
          this.occPreview.addClass('fixed');
        } else {
          this.occPreview.removeClass('fixed');
        }
      });
    },

    initClosePreview: function() {
      var self = this;
      $('#preview_close').on('click', function(){
        self.occPreview.hide('slide',500);
      });
    },

    togglePreview: function(oldSelectionId, newSelectionId) {
      //check if the click is on the same element
      if(oldSelectionId && (oldSelectionId === newSelectionId)){
        if(this.occPreview.is(":visible")){
          this.occPreview.hide("slide",500);
        } else {
          this.occPreview.show("slide",500);
        }
      } else {
        //check if the component is not already shown
        if(!this.occPreview.is(":visible")){
          this.occPreview.show("slide",500);
        }
      }
    },

    replacePreviewContent: function(htmlFragment) {
      this.occPreview.html(htmlFragment);
      this.initClosePreview();
    }

  };

  return {
    init: function() { _private.init(); },
    togglePreview : function(oldSelectionId, newSelectionId) {
      _private.togglePreview(oldSelectionId, newSelectionId);
    },
    replacePreviewContent : function(htmlFragment) {
      _private.replacePreviewContent(htmlFragment);
    }
  };

}());


EXPLORER.control = (function() {

  'use strict';

  var _private = {

    COOKIE_NAME: 'canadensys-responsive-table-settings',

    init: function() {
      this.loadEvents();
    },

    loadEvents: function() {
      var id = "";

      $('#control_buttons').on('click', 'a', function() {
        id = $(this).attr("href");
        $(this).addClass("selected").parent().siblings().children().removeClass("selected");
        $(id).removeClass("hidden").siblings().addClass("hidden");
        return false;
      });
    },

    restoreDisplay: function() {
      var self = this, display = $('#display_columns');

      if(display.length){
        display.on('click', 'input:checkbox', function() {
          var selectedColumn = [];
          $.each(display.find('input:checked'), function() {
            selectedColumn.push($(this).val());
          });
          EXPLORER.utils.createCookie(self.COOKIE_NAME,selectedColumn.join('|'),7);
        });

        var settingsCookie = EXPLORER.utils.readCookie(self.COOKIE_NAME);
        if(settingsCookie){
          //we cannot store a ; in the cookie so use a pipe (|)
          var arrayOfSettings = settingsCookie.split('|');
          $.each(display.find('input:checkbox'), function() {
            var isSelected = _.include(arrayOfSettings, $(this).val());
            if(isSelected !== this.checked){
              //need to click to update the table display
              this.trigger('click');
            }
          });
        }
      }
    }

  };

  return {
    init: function() { _private.init(); },
    restoreDisplay: function() { _private.restoreDisplay(); }
  };

}());


EXPLORER.table = (function() {

  'use strict';

  var _private = {

    results: "",
    occPreview: "",

    init: function() {
      this.results = $('#results');
      this.occPreview = $('#occ_preview');
      if(this.occPreview.length && this.results.length){
        this.loadEvents();
      }
    },

    loadEvents: function() {
      var oldSelection;

      this.results.on('click', 'tbody tr', function() {

        //do not send query to the server for the same element
        if(!oldSelection || oldSelection.attr('id') !== $(this).attr('id')){

          $.get("occurrence-preview/"+$(this).attr("id"), function(htmlFragment) {
            EXPLORER.preview.replacePreviewContent(htmlFragment);
          })
          .fail(function(jqXHR, textStatus, errorThrown) {
              console.log( textStatus );
            });
        }

        if(oldSelection){
          EXPLORER.preview.togglePreview(oldSelection.attr('id'),$(this).attr('id'));
        } else {
          EXPLORER.preview.togglePreview(undefined,$(this).attr('id'));
        }

        if(oldSelection){
          oldSelection.toggleClass('selected');
        }
        $(this).toggleClass('selected');
        oldSelection = $(this); 
      });

      //responsive design
      this.results.table({
        idprefix: "co-",
        persist: "persist"
      });
    }

  };

  return {
    init: function() { _private.init(); }
  };

}());


EXPLORER.details = (function() {

  'use strict';

  var _private = {

    init: function() {
      if($('#dwc_table').length) {
        this.loadEvents();
      }
    },

    loadEvents: function() {
      $('#dwc_table_toggle').on('click', function() {
        $('tr.unused').toggle();
      });
    },

    setupSingleOccurrenceMap: function(mapCanvasId, lat, lng, uncertainty) {
      var latlng, map, marker, coordinateUncertainty;

      if(!lat || !lng){
        $('#'+mapCanvasId).addClass('no_geo');
        return;
      }

      latlng = new google.maps.LatLng(lat,lng);

      map = new google.maps.Map($('#'+mapCanvasId)[0], {
        center: latlng,
        zoom: 12,
        scaleControl: true,
        mapTypeId: google.maps.MapTypeId.TERRAIN,
        mapTypeControl: true
      });

      marker = new google.maps.Marker({
        position:latlng,
        map: map
      });

      coordinateUncertainty = new google.maps.Circle({
        map: map,
        center: latlng,
        radius: uncertainty,
        strokeWeight: 1
        //strokeColor: 'white',
        //strokeOpacity: 0.5,
        //fillColor: '#2C48A6',
        //fillOpacity: 0.2
      });
    }

  };

  return {
    init: function() { _private.init(); },
    setupSingleOccurrenceMap : function(mapCanvasId, lat, lng, uncertainty) {
      _private.setupSingleOccurrenceMap(mapCanvasId, lat, lng, uncertainty);
    }
  };

}());
@@ -495,100 +495,8 @@
 op =
- 0;%0A%0A      if(this.occPreview.length && this.closeElement.length)%7B%0A        this.sidebarTop =
  thi
@@ -526,26 +526,24 @@
 - 36;%0A      
-  
 this.loadEve
@@ -545,32 +545,24 @@
 adEvents();%0A
-      %7D%0A
     %7D,%0A%0A    
@@ -578,32 +578,56 @@
 s: function() %7B%0A
+      var self = this;%0A%0A
       this.initC
@@ -712,20 +712,20 @@
 op() %3E= 
-this
+self
 .sidebar
@@ -733,36 +733,36 @@
 op) %7B%0A          
-this
+self
 .occPreview.addC
@@ -795,36 +795,36 @@
 lse %7B%0A          
-this
+self
 .occPreview.remo

Fix flyout positioning when scrolling page