(function($)
{
  
  
  /***
   * All jQuery stuff (document.ready)
  */
	$(document).ready(function()
  {
    /*
     * Execute the following as soon as the document is ready
    */
    var urlParams = getUrlParams();//Get all parameters of the current window
    console.log(urlParams);
    //@TODO: if you need to open a modal when the document is ready: if(linkParams['x-toggleable-target']){var targetModal = $( '.'+linkParams['x-toggleable-target'] ).attr('data-x-toggleable');window.xToggleUpdate( targetModal, true );}
    
    /*
     * UI Interactions
    */
    $('a[href*="x-toggleable"]').on('click', function(e)
    {
      e.preventDefault();//Prevent default behaviour; following the href destination
      
      var linkParams = getStringParams( $(this).attr('href') );//Get all parameters of the clicked link
      console.log(linkParams);
      
      //If needed, close previously openend modals
      if(linkParams['x-toggleable-closeall'])
      {
      	$('button.x-modal-close').trigger('click');
      }
      
      //If x-toggleable-target is present, find out which modal to open and do so
      if(linkParams['x-toggleable-target'])
      {
      	var targetModal = $( '.'+linkParams['x-toggleable-target'] ).attr('data-x-toggleable');
      	window.xToggleUpdate( targetModal, true );
      }
    });//End a.href=x-toggleable
    
	});//End document ready
  
  /***
   * Get paremeters from the current window's url
   * 
   * @param String prop (optional) - if prop is provided this specific's parameter's value will be returned instead of all properties
   * 
   * @return String|Object - returns a string with the value of 'prop' if 'prop' was set, otherwise returns an Object with all parameters and their values
   */
  function getUrlParams( prop )
  {
      return prop ? getStringParams( window.location.href, prop ) : getStringParams( window.location.href );
  }//End function getUrlParams()
  
  /***
   * Get parameters from a string. Inspired by https://www.kevinleary.net/javascript-get-url-parameters/ .
   * 
   * @param String string (required) - the string to get the parameters from. Parameters should be formatted name=value and seperated by ?.
   * @param String|object prop (optional) - if provided, the given prop's value will be returned instead of an object of all properties 
   *
   * @return String|Object - returns a string with the value of 'prop' if 'prop' was set, otherwise returns an Object with all parameters and their values
   */
  function getStringParams( string, prop )
  {
    var params = {};
    var search = decodeURIComponent( string.slice( string.indexOf( '?' ) + 1 ) );
    var definitions = search.split( '&' );

    definitions.forEach( function( val, key ) 
    {
      var parts = val.split( '=', 2 );
      params[ parts[ 0 ] ] = parts[ 1 ];
    } );
    return ( prop && prop in params ) ? params[ prop ] : params;
  }//End function getStringParams()
  
  /*
   * 
  */
  
})(jQuery);//End jQuery