var background=chrome.extension.getBackgroundPage(),newSession;function getMessage(a,b){try{var c=chrome.i18n.getMessage(a,b);if(c)return c}catch(d){console.error(d)}}function renderList(a){var b=loadPreviousSession(a);showCrashWindow(b);$(".sessionLabel").css("color","grey");$("#"+a).css("color","blue")}
function init(){document.title=getMessage("crashRestoreTitle");$(textBox).html(getMessage("restorePreviousSession"));var a=loadPreviousSession("prevSession");showCrashWindow(a);if(a){(a=getMessage("snapshotHint"))&&$(snapshotHint).html(a);for(var a=$(sessionList),b=0;b<=5;b++)localStorage["prevSession-"+b]&&$("<span/>",{"class":"sessionLabel",text:" | Snapshot #"+(b+1),id:"prevSession-"+b,click:function(){renderList(this.id)}}).css("color","grey").appendTo(a);$(".sessionLabel").css("color","grey");
$("#prevSession").click(function(){renderList(this.id)}).css("color","blue")}else $(sessionList).hide(),$(snapshotHint).hide()}
function loadPreviousSession(a){var b,c=localStorage[a];if(!a||!c)return null;var a=JSON.parse(c),d=new Date,c=d.getMonth()+1,e=d.getDate(),d=d.getFullYear(),c=e+"/"+c+"/"+d;a.tabs?(b=new background.SessionData(a.title,c),a.tabs.forEach(function(a){b.addTab(a)})):(b=new background.SessionData(getMessage("restoreSessionOn"),c),$.each(a,function(a,c){c.urls.forEach(function(c){b.addTab({url:c.url,title:c.title,win:a})})}));b.created=a.created;return b}var selectedSession;
function showCrashWindow(a){try{if($(crashWindowBox).empty(),!a||a.getWinCount()<=0)$("<div/>",{html:getMessage("noSessionToRestore")}).appendTo($("#crashWindowBox"));else{selectedSession=a;var b=$("#crashWindowBox"),c=1,d;for(d in a.windows){var e=$("<div/>",{"class":"crashSessionDiv"}).appendTo(b),g=$("<div/>",{"class":"titleDiv"}).appendTo(e);$("<div/>",{id:"window_"+c,"class":"crashWindow",html:getMessage("savedWindow",[""+c])}).appendTo(g);var m=$("<div/>",{"class":"buttonContainer"}).appendTo(g);
$("<div/>",{id:"restoreSession_"+c,html:getMessage("restoreSessionButton"),"class":"sessionbutton",winId:d,title:getMessage("restoreSessionButtonDescription"),click:function(a){restoreCrashSession($(this).attr("winId"),a.button)}}).appendTo(m);for(var k=a.windows[d],i=0;i<k.length;i++)try{var f=k[i],j=$("<a/>",{"class":"tabbox",href:f.url,title:f.url,target:"_blank"}).appendTo(e);$("<img/>",{src:"http://www.google.com/s2/favicons?domain_url="+f.url}).appendTo(j);$("<span/>",{text:f.title?f.title:
f.url}).appendTo(j);f.pinned&&$("<img/>",{src:"/img/pin.png"}).appendTo(j)}catch(n){console.error("restore page render error",n)}$("<br/>").appendTo(b);c++}var l=a.date;if(a.created){var h=new Date(parseInt(a.created));$("<div>",{"class":"note",text:"Snapshot taken at "+h.toLocaleString()}).appendTo(b);l=h.getDate()+"/"+(h.getMonth()+1)+"/"+(h.getYear()+1900)}$("<div/>",{html:getMessage("saveSessionButton"),title:getMessage("saveSessionButtonDescription"),"class":"sessionbutton",click:function(){var b=
prompt(getMessage("enterSessionName"),getMessage("restoreSessionOn")+changeDateFormat(l));if(b!=null)a.title=b,a.save(function(){var a=getMessage("sessionCreated",b)||'Session "'+b+'" is created';$(message).text(a)})}}).appendTo(b);$("<span>",{id:"message"}).appendTo(b)}}catch(o){console.error("showCrashWindow",o),$("<div/>",{html:getMessage("noSessionToRestore")}).appendTo($("#crashWindowBox"))}}
function changeDateFormat(a){var b=a.split("/");switch(localStorage.dateFormat){case "1":a=b[0]+"/"+b[1]+"/"+b[2];break;case "2":a=b[1]+"/"+b[0]+"/"+b[2];break;case "3":a=b[2]+"-"+b[1]+"-"+b[0];break}return a}
function restoreCrashSession(a,b){console.log(a,b);if(selectedSession)try{var c=selectedSession.windows[a];if(c){var d=[],e=[];c.forEach(function(a){a.pinned?e.push(a):d.push(a)});e.length>0&&(d=d.concat(e));if(b==0)chrome.windows.getLastFocused(function(a){chrome.windows.create({url:d[0].url,width:a.width,height:a.height},function(a){for(var b=1;b<d.length;b++)chrome.tabs.create({windowId:a.id,url:d[b].url,pinned:d[b].pinned?!0:!1})})});else if(b==1)for(c=0;c<d.length;c++)chrome.tabs.create({url:d[c].url,
pinned:d[c].pinned?!0:!1})}}catch(g){console.error(g)}}$(document).ready(function(){init()});
