var Offline=(function(){var k;
var U;
var W=0;
var P=[{storeName:"webreader.dynamic",type:"dynamic",total:0,captured:0,workerId:-1,status:"online"},{storeName:"webreader.static",type:"static",total:0,captured:0,workerId:-1,status:"online"},{storeName:"webreader.static",type:"static",total:0,captured:0,workerId:-1,status:"online"}];
var b;
var H={};
var E=false;
function R(){return window.google&&window.google.gears
}function l(){var AA=true;
if(!R()){$("#button_link_download").trigger("click");
AA=false
}else{try{o()
}catch(AB){AA=false
}}return AA
}function h(){if(!k){k=google.gears.factory.create("beta.workerpool");
k.onmessage=function(AC,AA,AF){var AB=AF.body.status;
var AD=AF.sender;
if(AB=="urls"){if(AF.body.detail.indexOf("capture")==0){V(AD);
O()
}else{if(AF.body.detail.indexOf("total:")==0){var AE=parseInt(AF.body.detail.substring(6,AF.body.detail.indexOf(" ",6)));
J(AD,AE)
}}}else{if(AB=="completed"){Q(AD,"ready");
O()
}}N(AD,AF.body)
}
}}function J(AC,AB){for(var AA=0;
AA<P.length;
AA++){if(P[AA].workerId==AC){P[AA].total+=AB;
break
}}}function V(AB){for(var AA=0;
AA<P.length;
AA++){if(P[AA].workerId==AB){P[AA].captured++;
break
}}}function Q(AC,AA){for(var AB=0;
AB<P.length;
AB++){if(P[AB].workerId==AC){P[AB].status=AA;
if(AA=="ready"){P[AB].captured=P[AB].total
}break
}}}function A(AB,AD,AA){var AC={command:AD};
if(typeof AA!="undefined"){AC.content=AA
}k.sendMessage(AC,AB)
}function e(){var AB=0;
for(var AA=0;
AA<P.length;
AA++){AB+=P[AA].total
}return AB
}function Z(){var AB=0;
for(var AA=0;
AA<P.length;
AA++){AB+=P[AA].captured
}return AB
}function t(AB,AA){A(AB,"getUrls",{urls:AA,update:H.version_updated})
}function o(){if(!U){U=google.gears.factory.create("beta.database")
}}function s(){o();
U.open();
U.execute("create table if not exists collection (url text primary key, desktop_shortcut text)");
U.execute("create table if not exists document (url text primary key, collection_id integer, title text, publish_date text, static_domain text, dynamic_domain text, zoom_level integer, zoom_in_level integer, page_mode integer, u1 text, version text )");
var AA=U.execute('select name from sqlite_master where type = "table" and name="search_text"');
if(!AA.isValidRow()){U.execute("create virtual table search_text using fts2 (text)")
}U.execute("create table if not exists search_page (document_id integer, page_number integer, title text, thumbnail text)");
U.execute("create table if not exists search_word (page_id integer, word text, x integer, y integer, w integer, h integer)");
AA.close();
U.close()
}function w(AC){var AB=U.execute("select rowid from search_page where document_id = ? and page_number = ?",[H.document_id,AC.pageNumber]);
if(!AB.isValidRow()){U.execute("begin transaction");
U.execute("insert into search_text(text) values (?)",[AC.text]);
var AA=U.lastInsertRowId;
U.execute("insert into search_page (rowid, document_id, page_number, title, thumbnail) values( ?, ?, ?, ?, ? )",[U.lastInsertRowId,H.document_id,AC.pageNumber,AC.title,AC.thumbnailUrl]);
if(AC.searchWord!=undefined){X(AA,AC.searchWord)
}U.execute("commit transaction")
}AB.close()
}function X(AA,AC){for(var AB=0;
AB<AC.length;
AB++){U.execute("insert into search_word values (?, ?, ?, ?, ?, ?)",[AA,AC[AB].word,AC[AB].x,AC[AB].y,AC[AB].w,AC[AB].h])
}}function m(AD){o();
U.open();
var AC={};
try{var AA=U.execute("select d.rowid, d.static_domain, d.dynamic_domain, d.zoom_level, d.zoom_in_level, d.page_mode, d.u1, d.version, c.desktop_shortcut from document d, collection c where d.collection_id = c.rowid and d.url = ?",[AD]);
if(AA.isValidRow()){AC.document_id=AA.field(0);
AC.url=DocumentProperties.getDocumentUrl();
AC.static_domain=AA.field(1);
AC.dynamic_domain=AA.field(2);
AC.z=AA.field(3);
AC.zin=AA.field(4);
AC.pm=AA.field(5);
AC.u1=AA.field(6);
AC.version=AA.field(7);
AC.desktop_shortcut=(AA.field(8)=="yes")?true:false;
AA.close()
}}catch(AB){}U.close();
return AC
}function z(){H.document_id=null;
H.url=DocumentProperties.getDocumentUrl();
H.static_domain=DocumentProperties.getStaticDomain();
H.dynamic_domain=q();
H.z=PageModel.zoomLevel;
H.zin=PageModel.getZoomInLevel();
H.pm=PageModel.pageMode;
H.u1=PageModel.u1;
H.collection_url=DocumentProperties.getCollectionUrl();
o();
U.open();
var AA=U.execute("select rowid, desktop_shortcut from collection where url = ? ",[H.collection_url]);
if(AA.isValidRow()){H.collection_id=AA.field(0);
H.desktop_shortcut=AA.field(1)=="yes"?true:false
}else{U.execute("insert into collection values (?, ?)",[H.collection_url,"no"]);
H.collection_id=U.lastInsertRowId
}AA.close();
AA=U.execute("select rowid, version from document where url=?",[H.url]);
if(AA.isValidRow()){H.document_id=AA.field(0);
H.version=AA.field(1);
U.execute("update document set static_domain = ?, zoom_level = ?, zoom_in_level = ?, page_mode = ?, u1 = ? where rowid = ?",[H.static_domain,H.z,H.zin,H.pm,H.u1,H.document_id]);
H.new_doc=false;
AA.close()
}else{U.execute("insert into document(url, title, publish_date, static_domain, dynamic_domain, zoom_level, zoom_in_level, page_mode, u1, collection_id) values(?,?,?,?,?,?,?, ?,?,?)",[H.url,DocumentProperties.getTitle(),DocumentProperties.getPublishDate(),H.static_domain,H.dynamic_domain,H.z,H.zin,H.pm,H.u1,H.collection_id]);
H.document_id=U.lastInsertRowId;
H.new_doc=true;
AA.close()
}U.close()
}function L(AA){if(!H.update_version){if(AA!=H.version){H.version=AA;
H.version_updated=true;
o();
U.open();
U.execute("update document set version = ? where rowid = ?",[AA,H.document_id]);
U.close()
}}}function p(){s();
z();
if(H.new_doc){for(var AB=1;
AB<=PageModel.lastPage;
AB+=50){if(E===true){return 
}var AA="search_"+x(""+AB,4,"0")+".json";
n(H.url+"/data/offline/"+AA,i)
}}}function C(){if(E===true){return 
}h();
W=0;
for(var AA=0;
AA<P.length;
AA++){P[AA].total=0;
P[AA].captured=0;
if(P[AA].workerId<0){if(P[AA].type=="dynamic"){P[AA].domain=H.dynamic_domain
}else{P[AA].domain=H.static_domain
}P[AA].workerId=k.createWorkerFromUrl(P[AA].domain+"/javascript/offline/cache-worker.js");
A(P[AA].workerId,"openStore",P[AA].storeName)
}}var AB=new Date();
b=AB.getTime();
n(H.url+"/data/offline/dynamic_manifest.json",j);
n(H.url+"/data/offline/static_manifest.json",M);
n(H.url+"/data/offline/static_manifest_"+H.z+".json",M);
n(H.url+"/data/offline/static_manifest_"+H.zin+".json",K);
Offline.timerId=window.setInterval(function(){var AD=true;
for(var AE=0;
AE<P.length;
AE++){if(P[AE].status!="ready"){AD=false;
break
}}if(AD&&E===false){window.clearInterval(Offline.timerId);
F();
I(e());
G();
Navbar.View.goOffline();
Console.log("The document is now available offline!!!\n");
var AF=new Date();
var AC=AF.getTime();
AC=AC-b;
AC=AC/1000;
Console.log("total urls: "+e()+" completed in "+AC+" seconds");
if(CookieManager.get("shouldCreateShortcut")){g()
}}},1000)
}function y(AB,AE,AD,AC){var AA="pm="+AB+"&z="+AE;
if(AC){AA+="&numPagesViewed=-1"
}if(AD){AA+="&u1="+AD
}return AA
}function S(AA){return c(H.url,AA,H.pm,H.z,H.u1,true)
}function c(AE,AA,AC,AG,AF,AB){var AD="#pg"+AA;
if(AB){AD=escape(AD)
}return AE+"?"+y(AC,AG,AF)+AD
}function j(AD){if(E===true){return 
}var AE=new Array();
var AC=0;
L(AD.version);
AE.push(H.url+"?"+y(H.pm,H.z,H.u1));
for(AC=1;
AC<=PageModel.lastPage;
AC++){AE.push(S(AC))
}for(AC=0;
AC<AD.entries.length;
AC++){var AB=AD.entries[AC].url;
if(AB.indexOf("Page.action?")>0){AE.push(AB+"&"+y(H.pm,H.z,H.u1));
AE.push(AB+"&"+y(H.pm,H.zin,H.u1))
}else{if(AB.indexOf("Popup.action?")>0){AE.push(AB+"&"+y(H.pm,H.z,H.u1,true));
AE.push(AB+"&"+y(H.pm,H.zin,H.u1,true))
}else{if(AB.indexOf("?pm=")>0){var AA=AB.substring(0,AB.indexOf("?pm="));
AE.push(AA+"?pm="+H.pm)
}else{AE.push(AB)
}}}}AE.push(H.dynamic_domain+H.collection_url+"/Offline.action");
AE.push(H.dynamic_domain+H.url+"/FreeMarker.action?template=offline/offline_indicator");
AE.push(H.dynamic_domain+H.url+"/FreeMarker.action?template=offline/offline_welcome");
AE.push(H.dynamic_domain+H.url+"/FreeMarker.action?template=offline/offline_reactivated");
AE.push(H.dynamic_domain+H.url+"/FreeMarker.action?template=offline/offline_back_online");
t(P[0].workerId,AE)
}function M(AB){if(E===true){return 
}L(AB.version);
var AC=new Array();
for(var AA=0;
AA<AB.entries.length;
AA++){AC.push(AB.entries[AA].url)
}t(P[1].workerId,AC)
}function K(AB){if(E===true){return 
}var AC=new Array();
for(var AA=0;
AA<AB.entries.length;
AA++){AC.push(AB.entries[AA].url)
}t(P[2].workerId,AC)
}function i(AC){if(E===true){return 
}U.open();
var AA=AC.searchPageList.searchPage;
for(var AB=0;
AB<AA.length;
AB++){w(AA[AB])
}delete AC;
U.close()
}function u(AG,AC){o();
U.open();
var AB;
if(AC){AB=AG.split(" ").join(" OR ")
}else{AB=AG
}var AF="select d.url, d.zoom_level, d.page_mode, p.page_number, p.title, p.thumbnail, snippet(search_text, '<span class=\"highlight\">', '</span>'),";
AF+=" offsets(search_text), ";
AF+="d.u1";
AF+=" from search_text t, search_page p, document d ";
AF+=" where t.rowid = p.rowid and d.rowid = p.document_id and search_text match ? ";
Console.log("query: "+AF+"\n match="+AB);
var AD=U.execute(AF,[AB]);
var AE=[];
while(AD.isValidRow()){var AA=new SearchResult(AD);
AE.push(AA);
AD.next()
}AD.close();
U.close();
return AE.sort(T)
}function T(AB,AA){if(AB.termMatch>AA.termMatch){return -1
}else{if(AB.termMatch<AA.termMatch){return 1
}else{if(AB.matchCount>AA.matchCount){return -1
}else{if(AB.matchCount<AA.matchCount){return 1
}else{return AB.pageNumber-AA.pageNumber
}}}}}function r(AG,AH,AC,AF){U.open();
var AD="select w.x, w.y, w.w, w.h from search_word w, search_page p where w.page_id = p.rowid and p.document_id = ? and p.page_number = ? and (";
$.each(AG.split(" "),function(AI,AJ){if(AI>0){AD+=" OR "
}AD+="lower(w.word) like '"+AJ.toLowerCase()+"%'"
});
AD+=")";
Console.log("search hight query: "+AD);
var AB=U.execute(AD,[H.document_id,AH]);
var AE=[];
while(AB.isValidRow()){var AA={};
AA.x=Math.round(AB.field(0)*AF/100);
AA.y=Math.round(AB.field(1)*AF/100);
AA.w=Math.round(AB.field(2)*AF/100);
AA.h=Math.round(AB.field(3)*AF/100);
AE.push(AA);
AB.next()
}AB.close();
U.close();
return AE
}function g(){var AC=google.gears.factory.create("beta.desktop");
var AB="This shortcut launches the "+DocumentProperties.getCollectionTitle()+" offline";
var AA={"32x32":DocumentProperties.getDesktopShortcutIcon()};
AC.createShortcut(DocumentProperties.getCollectionTitle(),H.dynamic_domain+H.collection_url+"/Offline.action",AA,AB);
H.desktop_shortcut=true;
U.open();
U.execute("update collection set desktop_shortcut = ? where rowid = ? ",["yes",H.collection_id]);
U.close();
CookieManager.remove("shouldCreateShortcut")
}function n(AA,AC,AB){opts={};
opts.url=AA;
opts.type="GET";
opts.dataType="json";
if(AC&&typeof AC=="function"){opts.success=AC
}if(AB&&typeof AB=="function"){opts.error=AB
}else{opts.error=v
}$.ajax(opts)
}function v(){E=true;
$("#offline_progress_dialog").dialog("close");
window.clearInterval(Offline.timerId);
Offline.goOnline();
ViewHelper.dataSwitchError()
}function N(AA,AB){}function q(){var AA=location.href;
var AB=AA.indexOf("/",8);
AA=AA.substring(0,AB);
return AA
}function f(){if(!H.collection_url){var AA=location.href;
var AB=AA.indexOf("/",8);
H.collection_url=AA.substring(AB,AA.indexOf("/",AB+1))
}return H.collection_url
}function x(AC,AB,AD){var AA=AC;
while(AA.length<AB){AA=AD+AA
}return AA
}function A(AB,AD,AA){var AC={command:AD};
if(typeof AA!="undefined"){AC.content=AA
}if(k){k.sendMessage(AC,AB)
}}function e(){var AB=0;
for(var AA=0;
AA<P.length;
AA++){AB+=P[AA].total
}return AB
}function Z(){var AB=0;
for(var AA=0;
AA<P.length;
AA++){AB+=P[AA].captured
}return AB
}function O(){var AD=e();
var AC=Z();
var AA=false;
if(E===true){$("#offline_progress_dialog").dialog("close")
}else{if(AD>0){var AB=Math.floor((AC*100)/AD);
if(AA===false&&$("#downloading").is(".hidden")){$("#checking").addClass("hidden");
$("#downloading").removeClass("hidden");
AA=true
}if(AB>W){$("#offline_progress").progressbar("value",AB);
$("#percent_complete").text(AB);
W=AB
}}}}function I(AI){var AA=$("#offline_progress_dialog").parents(".ui-dialog");
var AE=$("#offline_progress_content");
var AC=$(window).height();
var AG=$(window).width();
var AK=AA.height();
var AF=AA.width();
var AD=(AC/2)-(AK/2);
var AH=(AG/2)-(AF/2);
var AJ=H.url;
var AB=AI>0?"download_complete":"offline_reactivated";
Offline.downloadInProgress=false;
AE.load(AJ+"/FreeMarker.action?template=offline/"+AB,function(){$(this).animate({height:130});
$("#close_dialog",$(this)).bind("click",function(){var AM=$("#button_link_offline_options").offset();
var AL=AA.offset();
AA.animate({left:(AL.left*2)},{queue:true,duration:150},"linear").animate({left:(AL.left*2+(AM.left-AL.left*2))},{queue:true,duration:250},"linear").animate({left:AM.left,opacity:0.5},{queue:true,duration:300},"linear").animate({opacity:0},{queue:true,duration:200});
AA.animate({top:0,height:31,width:97},{queue:false,duration:1000,easing:"easeOutQuint",complete:function(){$("#offline_progress_dialog").dialog("close")
}});
AE.animate({opacity:0},750)
})
});
AA.animate({top:AD,left:AH,height:300},"slow",function(){setTimeout(function(){$("#close_dialog",AA).trigger("click")
},12000)
})
}function G(){var AA=H.url;
DataSwitch.get({url:AA+"/FreeMarker.action?template=offline/offline_indicator",success:function(AB){$(AB).insertBefore("#button_link_help")
}})
}function B(){var AA=DocumentProperties.getDocumentUrl();
DataSwitch.get({url:AA+"/FreeMarker.action?template=offline/offline_back_online",success:function(AB){$(ViewHelper.shadowWrap(AB,"black","offline_back_online_dialog")).appendTo("body").dialog({width:300,modal:false,resizable:false,draggable:false,close:function(AD,AC){delete PageElements.dialogs.offline_back_online;
$(this).dialog("destroy").remove()
},open:function(AE,AD){var AC=$(this);
$("#enable_online",AC).bind("click",function(){$("#offline_back_online_dialog").dialog("close")
});
PageElements.dialogs.offline_back_online={id:"offline_back_online_dialog",link_active:true,dialog_active:true};
setTimeout(function(){$("#offline_back_online_dialog").parents(".ui-dialog").fadeOut("slow",function(){$("#offline_back_online_dialog").dialog("close")
})
},5000)
}})
}})
}function a(){var AA=DocumentProperties.getDocumentUrl();
DataSwitch.get({url:AA+"/FreeMarker.action?template=offline/offline_welcome",success:function(AB){$(ViewHelper.shadowWrap(AB,"black","offline_welcome_dialog")).appendTo("body").dialog({width:450,modal:false,resizable:false,draggable:false,close:function(AD,AC){delete PageElements.dialogs.offline_welcome;
$(this).dialog("destroy").remove()
},open:function(AE,AD){var AC=$(this);
$("#continue_offline",AC).bind("click",function(){$("#offline_welcome_dialog").dialog("close")
});
$("#enable_online",AC).bind("click",function(){$("#offline_welcome_dialog").dialog("close");
Offline.goOnline();
B()
});
PageElements.dialogs.offline_welcome={id:"offline_welcome_dialog",link_active:true,dialog_active:true}
}})
}})
}function d(){var AA=DocumentProperties.getDocumentUrl();
if($("input.install_shortcut:checked",$("#button_link_download_dialog")).length>0){CookieManager.set("shouldCreateShortcut","true")
}ViewHelper.closeAllDialogs();
$("#zoom_wrapper",Navbar.Model.navbar).hide();
DataSwitch.get({url:AA+"/FreeMarker.action?template=offline/offline_progress",success:function(AB){$(ViewHelper.shadowWrap(AB,"black","offline_progress_dialog")).appendTo("body").dialog({position:[$(window).width()-350,40],closeOnEscape:false,modal:false,resizable:false,draggable:false,close:function(AE,AD){var AC=$(this);
ViewHelper.setDialogModalOverride(false);
AC.fadeOut("slow",function(){delete PageElements.dialogs.offline_progress;
AC.dialog("destroy").remove()
})
},open:function(AE,AD){var AC=$(this);
if(E===true){AC.dialog("close")
}else{if(!/MSIE 6.0/.test(navigator.userAgent)){AC.parent().css("position","fixed")
}ViewHelper.setDialogModalOverride(true);
ViewHelper.removeDialogCloseButton(AC);
$("#offline_progress",AC).progressbar();
PageElements.dialogs.offline_progress={id:"offline_progress_dialog",link_active:true,dialog_active:true}
}}})
}})
}function Y(){o();
U.open();
var AD=[];
var AA=U.execute("select d.dynamic_domain, d.static_domain, d.url, d.title, d.publish_date, d.page_mode, d.zoom_level, d.u1 from document d, collection c where d.collection_id = c.rowid and c.url = ? order by d.publish_date desc",[f()]);
while(AA.isValidRow()){var AB=AA.field(0)+AA.field(2)+"?"+y(AA.field(5),AA.field(6),AA.field(7));
AD.push({url:AB,img:AA.field(1)+AA.field(2)+"/cover.gif",title:AA.field(3)});
AA.next()
}AA.close();
U.close();
var AC="";
$.each(AD,function(AE,AF){AC+='<div class="thumb"><a href="'+AF.url+'">';
AC+='<img border="0" alt="'+AF.title+'" src="'+AF.img+'"/>';
AC+="<span>"+AF.title+"</span>";
AC+="</a></div>"
});
return AC
}function F(){W=0;
H.version_updated=false;
H.new_doc=false;
CookieManager.set("offline","true")
}function D(){CookieManager.remove("offline");
CookieManager.remove("shouldCreateShortcut");
H.version_updated=false;
H.new_doc=false;
delete Offline.offlineCookie;
W=0
}return{timerId:null,downloadInProgress:false,isOffline:function(){if(typeof Offline.offlineCookie=="undefined"){Offline.offlineCookie=CookieManager.get("offline")
}if(Offline.offlineCookie&&R()!==false){return true
}return false
},init:function(){H.new_doc=false;
H.version_updated=false;
this.loadOfflineSettings();
if(this.isOffline()&&typeof google!="undefined"&&google.gears.factory.hasPermission===true){if(!H.document_id){Offline.goOnline()
}else{G();
a();
Navbar.View.goOffline()
}}else{if(CookieManager.get("download")){var AA=setInterval(function(){if(Drawer.View.firstTabLoaded===true){clearInterval(AA);
CookieManager.remove("download");
Offline.goOffline()
}},500)
}else{if(Offline.offlineCookie){CookieManager.remove("offline");
delete Offline.offlineCookie
}}}},goOffline:function(){if(!Offline.isOffline()){if(l()){Offline.downloadInProgress=true;
PageView.setAutoZoom(false);
d();
p();
C()
}else{document.location.reload(true)
}}else{F()
}},goOnline:function(){$("#button_link_offline_options").remove();
$("#button_link_offline_options_dialog").dialog("close");
Navbar.View.goOnline();
D()
},goBackOnline:function(){Offline.goOnline();
Navbar.View.goOnline();
B()
},getSearchResults:function(AB,AA){return u(AB,AA)
},getSearchHighlights:function(AC,AD,AA,AB){return r($.trim(AC),AD,AA,AB)
},loadDocuments:function(){return Y()
},getOfflineSettings:function(){return H
},getZoomLevel:function(){return H.z
},getZoomInLevel:function(){return H.zin
},getDocumentPageUrl:function(AC,AA,AB,AE,AD){return c(AC,AA,AB,AE,AD,false)
},loadOfflineSettings:function(){if(R()&&google.gears.factory.hasPermission===true){H=m(DocumentProperties.getDocumentUrl());
if(H!=null){$("#start_download_installed_text p strong").text("offline mode");
$("#download_offline_text").html("view in offline mode");
$("#download_message_text").hide()
}}}}
})();