// Blog Navigator v0.2
// Greg Hill http://www.greg-hill.id.au
// visit http://blogfresh.blogspot.com for details

// event handler codage via ecmanaut
var then=window.onload||function(){};window.onload=function(){doNav();then();};

function buildAnchors(){anchors=new Array();var a=document.anchors;for(var i=0;i<a.length;i++) anchors.push('#'+a[i].name);return;} function ItemPostNav(archFile,postID,dir){var targetURL=buildNavURL(archURL+archFile,dir,postID);return targetURL;} function jumpNav(postID,dir){var currPos=getPos(NavLadder,postID);if (currPos==-1)currPos=1;var count=NavLadder.length;var targPos=1;switch (dir){case "top":targPos=1;break;case "bottom":targPos=count-2;break;case "up":targPos=currPos-1;break;case "down":targPos=currPos+1;break;case "earlier":targPos=count-1;break;case "later":targPos=0;break;}var targURL=NavLadder[targPos];if (targURL.charAt(0)=='#'){window.location.hash=targURL;return;}window.location.href=targURL;return;} function doNav(){buildLadder();var dir=getNavDir();if (!dir)return;var postID=window.location.hash;if (!postID)postID=NavLadder[1];jumpNav(postID,dir);return;} function buildLadder(){var i,currPos,targURL;if (typeof(PageType)=='undefined')PageType="Archive";NavLadder=new Array();buildAnchors();targURL=buildNavURL('',"top",'');switch(PageType){case "Item": targURL=buildNavURL('',"bottom",'');break;case "Main":buildArchives();currPos=-1;break;case "Archive":buildArchives();var archFile=getArchFile();if (archFile){currPos=getPos(archives,archFile);if (currPos<archives.length-1)targURL=buildNavURL(archives[currPos+1],"bottom",'');}else PageType="Main";break;}NavLadder.push(targURL);for(var i=0;i<anchors.length;i++)NavLadder.push(anchors[i]);switch(PageType){case "Item": targURL=buildNavURL('',"top",'');break;case "Main":if (typeof(mainArch)=='undefined'|| typeof(mainPostID)=='undefined')currPos=archives.length-1;else {currPos=getPos(archives,mainArch);if (!currPos)currPos=archives.length-1;}targURL=buildNavURL(archives[currPos],"down",mainPostID);break;case "Archive":targURL=buildNavURL(archives[currPos-1],"bottom",'');break;}NavLadder.push(targURL);return;} function getNavDir(){var dir;var query=window.location.search;var rexp=/[?&]Nav=(\w+)/i;var result=rexp.exec(query);if (result)dir=result[1];else dir='';return dir;} function eatNavDir( query ){var rexp=/[?&]Nav=(\w+)/i;return query.replace(rexp,'');} function getArchFile(){var archFile;var query=window.location.search;var url=window.location.href;var rexp=new RegExp(archURL+'([^?^#]+)');var result=url.match(rexp);if (result)archFile=result[1];return archFile;} function getPos(list,val){if(!list)return -1;for(var i=0;i<list.length;i++){if(list[i].search(val.replace(/#.*/,''))>=0)return i;}return -1;} function buildNavURL( path,dir,hash ){var targURL;var query=eatNavDir(window.location.search);if (hash)hash='#'+hash;if (!dir)targURL=path+query+hash;else {if (!query)targURL=path+'?Nav='+dir+hash;else targURL=path+query+'&Nav='+dir+hash;}return targURL;}