
Integrify.returnData=null;Integrify.Auth=function(){};Integrify.Auth.prototype={pendingTokenExchange:false,tokenExchanged:false,settings:{reqToken:null,reqTokenSecret:null,reqInProgress:false,consumerKey:null,consumerSecret:null,token:null,tokenSecret:null,method:'GET',reqTokenEP:null,exchTokenEP:null,authEP:null,authCallbackUrl:null,version:'1.0',signatureMethod:'HMAC-SHA1',callbackFunc:function(){},contentType:'script',authRequiredDisplay:null,proxy:null,authDisplay:function(url){},authComplete:function(){}},signRequest:function(actionUrl,method,proxy){var signedUrl=null;tokenstr=jQuery.cookie('iapi_token');if(tokenstr&&tokenstr.indexOf('&')!=-1){tokenstr=decodeURIComponent(tokenstr);if(actionUrl.substr(0,1)=="/"){actionUrl=Integrify.service.root+actionUrl;}
this.settings.token=tokenstr.split('&')[0].split('=')[1];this.settings.tokenSecret=tokenstr.split('&')[1].split('=')[1];if(!this.settings.signThroughProxy){var oauthParameters={oauth_version:this.settings.version,oauth_consumer_key:this.settings.consumerKey,oauth_token:this.settings.token,oauth_timestamp:OAuth.timestamp(),oauth_nonce:OAuth.nonce(6),oauth_signature_method:this.settings.signatureMethod};if(method&&method.indexOf("SIG")!=-1){method=method.split("_")[1];}
var message={method:(!method?'GET':method),action:actionUrl,parameters:oauthParameters};signedUrl=this.getSignedUrl(message,this.settings.consumerSecret,this.settings.tokenSecret);}
else{signedUrl=actionUrl;}
if(proxy){if(this.settings.signThroughProxy){var signedUrl=jQuery.ajax({url:Integrify.service.proxy+encodeURIComponent(signedUrl)+"&sig="+method,async:false,cache:false}).responseText;}
signedUrl=proxy+encodeURIComponent(signedUrl);}
else if(this.settings.signThroughProxy){if(Integrify.service.proxy.indexOf('.php')!=-1&&method&&method.indexOf("SIG")!=-1){var signedUrl=jQuery.ajax({url:Integrify.service.proxy+encodeURIComponent(signedUrl)+"&sig="+method.split("_")[1],async:false,cache:false}).responseText;}
else{signedUrl=Integrify.service.proxy+encodeURIComponent(signedUrl);}}
return signedUrl;}
else{if(this.pendingTokenExchange||document.location.hash.indexOf('temptoken')!=-1){if(document.location.hash.indexOf('temptoken')!=-1){var tempStr=document.location.hash.split("=")[1].split("|");jQuery.cookie('iapi_reqtoken',"oauth_token="+tempStr[0]+"&oauth_token_secret="+tempStr[1].split("&")[0]);}
this.exchangeToken();}
else
if(!this.tokenExchanged){this.requestToken();}
return null;}},requestToken:function(){this.settings.reqInProgress=true;Integrify.returnData=null;if(!this.settings.signThroughProxy){oauthParameters={oauth_version:this.settings.version,oauth_consumer_key:this.settings.consumerKey,oauth_timestamp:OAuth.timestamp(),oauth_nonce:OAuth.nonce(6),oauth_signature_method:this.settings.signatureMethod};}
var tokenStr;var endpointUrl=this.settings.reqTokenEP;if(endpointUrl.substr(0,1)=="/"){endpointUrl=Integrify.service.root+endpointUrl;}
if(this.settings.contentType=='script'){endpointUrl+=endpointUrl.indexOf('?')==-1?'?':'&';endpointUrl+='iapi_var=Integrify.returnData';}
var signedUrl=endpointUrl;if(!this.settings.signThroughProxy){var message={method:"GET",action:endpointUrl,parameters:oauthParameters};signedUrl=this.getSignedUrl(message,this.settings.consumerSecret,'');}
if(this.settings.proxy!=null)
signedUrl=this.settings.proxy+encodeURIComponent(signedUrl);var authObj=this;jQuery.ajax({type:this.settings.method,url:signedUrl,dataType:this.settings.contentType,timeout:30000,success:function success(){if(Integrify.returnData){if(!Integrify.returnData.errorMessage&&Integrify.returnData.indexOf("&oauth_")>-1){tparts=Integrify.returnData.split('&');jQuery.cookie('iapi_reqtoken',decodeURIComponent(Integrify.returnData));authObj.settings.reqToken=tparts[0].replace('oauth_token=','');authObj.settings.reqTokenSecret=tparts[1].replace('oauth_token_secret=','');if(tparts[2]){authObj.settings.authEP=tparts[2].replace('authorization_url=','');}
if(authObj.settings.authEP!=null){uri=authObj.settings.authEP+authObj.settings.reqToken+'&r='+encodeURIComponent(document.location.href)+'&c='+authObj.settings.consumerKey;authObj.settings.authDisplay(uri);}
else{authObj.exchangeToken();}}
else{if(Integrify.returnData.errorMessage){var c=confirm(Integrify.returnData.errorMessage+"\n\nShow Detail?");if(c){alert(Integrify.returnData.errorStack);}}
else{alert(Integrify.returnData);}}}
else{alert('Authorization unsuccessful');}},error:function(data,status,error){authObj.tokenError("request",data,status,error);}});},getSignedUrl:function(message,cs,ts){baseString=OAuth.SignatureMethod.getBaseString(message);signer=OAuth.SignatureMethod.newMethod(this.settings.signatureMethod,{consumerSecret:cs,tokenSecret:ts});signature=signer.getSignature(baseString);return OAuth.SignatureMethod.getSignedUrl(message,signature);},exchangeToken:function(){this.settings.authComplete();Integrify.returnData=null;var req_token=jQuery.cookie('iapi_reqtoken');if(!this.tokenExchanged&&req_token&&req_token.indexOf('&')!=-1){var req_token_data=jQuery.cookie('iapi_reqtoken').split('&');this.settings.reqToken=req_token_data[0].replace('oauth_token=','');this.settings.reqTokenSecret=req_token_data[1].replace('oauth_token_secret=','');jQuery.cookie('iapi_exchtoken',jQuery.cookie('iapi_reqtoken'));jQuery.cookie('iapi_reqtoken',null);this.pendingTokenExchange=false;this.tokenExchanged=true;var tokenStr;var endpointUrl=this.settings.exchTokenEP;if(endpointUrl.substr(0,1)=="/"){endpointUrl=Integrify.service.root+endpointUrl;}
if(this.settings.contentType=='script'){endpointUrl+=endpointUrl.indexOf('?')==-1?'?':'&';endpointUrl+='iapi_var=Integrify.returnData';}
var signedUrl=endpointUrl;if(!this.settings.signThroughProxy){var oauthParameters={oauth_version:this.settings.version,oauth_consumer_key:this.settings.consumerKey,oauth_token:this.settings.reqToken,oauth_timestamp:OAuth.timestamp(),oauth_nonce:OAuth.nonce(6),oauth_signature_method:this.settings.signatureMethod};var message={method:this.settings.method,action:endpointUrl,parameters:oauthParameters};signedUrl=this.getSignedUrl(message,this.settings.consumerSecret,this.settings.reqTokenSecret);}
var authObj=this;if(this.settings.proxy!=null)
signedUrl=this.settings.proxy+encodeURIComponent(signedUrl);jQuery.ajax({type:'GET',url:signedUrl,dataType:"script",timeout:10000,success:function success(){if(Integrify.returnData.indexOf('&')!=-1){jQuery.cookie('iapi_exchtoken',null);jQuery.cookie('iapi_token',decodeURIComponent(Integrify.returnData));authObj.settings.reqInProgress=false;authObj.settings.callbackFunc.call();}
else{var res=confirm("An error occured during the authorization process?  Would you like to retry?");if(res){jQuery.cookie('iapi_token',null);jQuery.cookie('iapi_exchtoken',null);jQuery.cookie('iapi_reqtoken',null);document.location.href=document.location.href;}}},error:function(data,status,error){authObj.tokenError("exchange",data,status,error);}});}
else{alert("Authorization failed, please reopen your browser and try again.");}},tokenError:function(requestType,data,status,error){var errorObj={errorMessage:"Error occured in token"+requestType,errorStack:"(No detail)"};if(status=="timeout"){errorObj.errorMessage="Token "+requestType+" has timed-out";errorObj.errorStack=null;}
else if(data.responseText){if(data.responseText.substr(0,1)==="{"){errorObj=eval(data.responseText);}
else{errorObj.errorStack=data.reponseText;}}
if(errorObj.errorStack){var c=confirm(errorObj.errorMessage+"\n\nShow Detail?");if(c){alert(errorObj.errorStack);}}
else{alert(errorObj.errorMessage);}}};Integrify.apply(Integrify,{oauth:new Integrify.Auth()});
