if(typeof(nk)=="undefined"){nk={}}nk.options={};nk.options.domain="nk.pl";nk.OAuth={};(function(){try{var a=null;if(window.opener&&window.opener.is_dest_oauth_window){a=window.opener}else{if(window.parent&&window.parent.is_dest_oauth_window){a=window.parent}}if((/access_token=/.test(window.location.hash)||(/error=/.test(window.location.hash)))&&a){a.location.assign(window.location.href+"&dest_frame=true");window.close()}}catch(b){}})();
/*!
 * Sends request for token.
 * After request is sent, the authorization server validates the request to ensure all required
 * parameters are present and valid and directs the user-agent to the provided client redirection URI using an HTTP
 * redirection response.
 * Based on OAuth-v2-20 (http://tools.ietf.org/html/draft-ietf-oauth-v2-20).
 * @param client_id. A unique string representing the registration
 *   information provided by authorization server. 
 * @param redirect_uri OPTIONAL. Absolute URI to redirect after processing completion.
 *   URI must be on exactly the same domain as specified in Administration Panel, with
 *   http:// or https:// schema.
 * @param scope OPTIONAL. The scope of the access request expressed as a list
 *   of comma-delimited, case sensitive strings.  The value is
 *   defined by the authorization server.  If the value contains
 *   multiple comma-delimited strings, their order does not matter,
 *   and each string adds an additional access range to the
 *   requested scope. Use NULL for default value for specified client_id.
 * @param state OPTIONAL.  An opaque value used by the client to maintain state
 *   between the request and callback.  The authorization server
 *   includes this value when redirecting the user-agent back to the client.
 * @param response_type. A type of authorization flow to use. Defaults to "token". Available values: "token", "code"
 * @return bool Returns true if request is sent, false in case of error such as invalid arguments
 */
nk.OAuth.get_token=function(e,b,j,d,g){if(!e){return false}if(!g){g="token"}var a="https://"+nk.options.domain+"/oauth2/auth?client_id="+encodeURIComponent(e);a+="&response_type="+g;if(j!=null){a+="&scope="+encodeURIComponent(j)}if(b!=null){a+="&redirect_uri="+encodeURIComponent(b)}if(d!=null){a+="&state="+encodeURIComponent(d)}if(g=="token"){window.is_dest_oauth_window=true;var i=500;var f=260;var c=window.open(a,"NK","left="+(screen.availWidth/2-i/2)+",top="+(screen.availHeight/2-f/2)+",width="+i+",height="+f+",resizable=1,status=0,scrollbars=1");c.focus()}else{if(g=="code"){window.location.assign(a)}}return true}
/*!
 * Sends request for token, but succeeds only when user is logged into NK.pl and already accepted required scope.
 * After request is sent, the authorization server validates the request to ensure all required
 * parameters are present and valid and directs the user-agent to the provided client redirection URI using an HTTP
 * redirection response.
 * Based on OAuth-v2-20 (http://tools.ietf.org/html/draft-ietf-oauth-v2-20).
 * @param client_id. A unique string representing the registration
 *   information provided by authorization server. 
 * @param redirect_uri OPTIONAL. Absolute URI to redirect after processing completion.
 *   URI must be on exactly the same domain as specified in Administration Panel, with
 *   http:// or https:// schema.
 * @param scope OPTIONAL. The scope of the access request expressed as a list
 *   of comma-delimited, case sensitive strings.  The value is
 *   defined by the authorization server.  If the value contains
 *   multiple comma-delimited strings, their order does not matter,
 *   and each string adds an additional access range to the
 *   requested scope. Use NULL for default value for specified client_id.
 * @param state OPTIONAL.  An opaque value used by the client to maintain state
 *   between the request and callback.  The authorization server
 *   includes this value when redirecting the user-agent back to the client.
 * @return bool Returns true if request is sent, false in case of error such as invalid arguments
 */
;nk.OAuth.try_to_get_token=function(f,a,b,d){if(!f){return false}var c="https://"+nk.options.domain+"/oauth2/auth?client_id="+encodeURIComponent(f);c+="&response_type=token";if(b!=null){c+="&scope="+encodeURIComponent(b)}if(a!=null){c+="&redirect_uri="+encodeURIComponent(a)}if(d!=null){c+="&state="+encodeURIComponent(d)}window.is_dest_oauth_window=true;var e=document.createElement("iframe");document.body.appendChild(e);e.src=c;e.style.position="absolute";e.style.left="-10000px";return true}
/*!
 * Checks if there is any token available for current page.
 * @param callback A callback function(token, state) which is called in case token is available
 * @return bool Returns true if token is available, false if it's not
 */
;nk.OAuth.is_token_available=function(b){var a=function(){if(window.location.hash.match(/access_token=([^&]*)/)){var c=RegExp.$1;if(window.location.hash.match(/dest_frame=true/)){var d=null;if(window.location.hash.match(/state=([^&]*)/)){d=decodeURIComponent(RegExp.$1.replace(/\+/g," "))}window.location.hash="";b(c,d)}return true}return false};setInterval(a,100);return a()}
/*!
 * Creates button for client authorization with NK.pl
 * Based on OAuth-v2-20 (http://tools.ietf.org/html/draft-ietf-oauth-v2-20).
 * @param client_id. A unique string representing the registration
 *   information provided by authorization server. 
 * @param redirect_uri OPTIONAL. Absolute URI to redirect after processing completion.
 *   URI must be on exactly the same domain as specified in Administration Panel, with
 *   http:// or https:// schema.
 * @param scope OPTIONAL. The scope of the access request expressed as a list
 *   of comma-delimited, case sensitive strings.  The value is
 *   defined by the authorization server.  If the value contains
 *   multiple comma-delimited strings, their order does not matter,
 *   and each string adds an additional access range to the
 *   requested scope. Use NULL for default value for specified client_id.
 * @param state OPTIONAL.  An opaque value used by the client to maintain state
 *   between the request and callback.  The authorization server
 *   includes this value when redirecting the user-agent back to the client.
 * @param response_type. A type of authorization flow to use. Defaults to "token". Available values: "token", "code"
 * @return void
 */
;nk.OAuth.create_button=function(f,a,b,c,d){var e="'"+f+"'";e+=", "+(a!=null?("'"+a+"'"):"null");e+=", "+(b!=null?("'"+b+"'"):"null");e+=", "+(c!=null?("'"+c+"'"):"null");e+=", "+(d!=null?("'"+d+"'"):"null");document.write('<img onclick="nk.OAuth.get_token('+e+')" src="https://'+nk.options.domain+'/img/oauth2/connect">')};
