notif = webkitNotifications.createNotification("images/48.png","Twitter Error","Twitter Notifier couldn't connect to Twitter - maybe Twitter's down, or maybe you aren't logged in properly. Go to the options page to logout and in again.");

notif.show();

setTimeout(function(){

b.stream=new twitter.stream(b.stream.wait);

b.stream.start(b)

},10000)

}

}else{

if(b.stream.wait<30000){

setTimeout(function(){

b.stream=new twitter.stream(b.stream.wait);

b.stream.start(b)

}, b.stream.wait);

b.stream.wait+=10000

}else{

connectionError()

}

}

b.waitReset= setTimeout(function(){

b.wait=0

},320000)

}

}

function connectionError(){

var a = webkitNotifications.createNotification("images/48.png","Connection Error","Twitter Notifier couldn't connect to Twitter - are you sure you're connected to the internet? Connection will be tried again in 1 minute.");

alert("An error occurred! Please check you have internet access and your clock is set correctly and try again.")

},5000)

}

},

error:function(d, c){

webkitNotifications.createNotification("images/error.png","Failed to authenticate with Twitter","Check you have internet access and your computer's clock is set correctly (common cause of error) and try again.").show()

}

})

};

twitter.accessToken=function(a, e){

accounts = chrome.extension.getBackgroundPage().accounts;

for(var b in accounts){

if(accounts[b].timestamp== e){

var d = accounts[b];

var c = b

}

}

twitter.oauthRequest({

url:"https://api.twitter.com/oauth/access_token",

type:"text",

method:"POST",

token: d.requestToken,

tokenSecret: d.requestTokenSecret,

parameters:[

["oauth_verifier", a]

],

success:function(j){

j = j.split("&");

var f =[];

for(var g in j){

var k = j[g].split("=");

f[k[0]]= k[1]

}

for(var g in accounts){

if(accounts[g].accessToken== f.oauth_token){

accounts.splice(c,1);

c =false

}

}

if(c){

delete d.timestamp;

delete d.requestToken;

delete d.requestTokenSecret;

d.accessToken= f.oauth_token;

d.accessTokenSecret= f.oauth_token_secret;

d.notifyDm=true;

d.notifyTweet=true;

d.notifyMention=true;

chrome.extension.getBackgroundPage().twitter.loadNewAccount(d);

webkitNotifications.createNotification("images/48.png","Authorised!","Twitter Notifier is now authorised and running.").show()

}else{

webkitNotifications.createNotification("images/48.png","Not Authorised","That account has already been connected to Twitter Notifier.").show()

}

updateAccounts();

chrome.tabs.getCurrent(function(i){

chrome.tabs.remove(i.id)

})

}

})

};

twitter.loadAccount=function(a){

console.log("Loading account "+ a.screenName);

twitter.oauthRequest({

url:"https://api.twitter.com/1/account/verify_credentials.json",

account: a,

success:function(c){

try{

a.id= c.id;

a.name= c.name;

a.screenName= c.screen_name;

a.image= c.profile_image_url;

a.stream=new twitter.stream();

console.log("Updating accounts store");

updateAccounts();

if(!a.disabled){

a.stream.start(a)

}

}catch(b){

connectionError()

}

},

error:function(c, b){

webkitNotifications.createNotification("images/48.png","Could not authenticate user @"+ a.screenName,"Twitter Notifier failed to authorise this account with Twitter. Try logging out and logging in of this account on the accounts page.").show()