//
//  iweb - navbar.js
//  copyright (c) 2007 apple Inc. all rights reserved.
//

function navbar(instanceid)
{if(instanceid)
{widget.apply(this,arguments);if(!this.preferenceforkey("usestaticfeed")&&this.preferenceforkey("dotmacaccount"))
{var depthprefix=this.preferenceforkey("path-to-root");if(!depthprefix||depthprefix=="")
depthprefix="./";this.xml_feed=depthprefix+"?webdav-method=truthget&depth=infinity&ns=iweb&filterby=in-navbar";}
else
{this.xml_feed="feed.xml";if(this.sitepath)
{this.xml_feed=this.sitepath+"/"+this.xml_feed;}}
this.changedpreferenceforkey("navbar-css");this.regenerate();}}
navbar.prototype=new widget();navbar.prototype.constructor=navbar;navbar.prototype.widgetidentifier="com-apple-iweb-widget-navbar";navbar.prototype.regenerate=function()
{var xml_handler=this.populatenavitems.bind(this);ajaxrequest(this.xml_feed,xml_handler);return true;}
navbar.prototype.getstyleelement=function(key)
{if(!this.styleelement)
{var head=document.getelementsbytagname("head")[0];if(head)
{var newelement=document.createelement("style");newelement.type="text/css";head.appendchild(newelement);this.styleelement=newelement;}}
return this.styleelement;}
navbar.prototype.substwidgetpath=function(text)
{var result=text.replace(/\$widget_path/gm,this.widgetpath);return result;}
navbar.prototype.addcssselectorprefix=function(text)
{var prefix="div#"+this.instanceid+" ";text=text.replace(/\/\*[^*]*\*+([^/][^*]*\*+)*\//gm,"");text=text.replace(/(^\s*|\}\s*)([^{]+)({[^}]*})/gm,function(match,beforeselectorList,selectorList,propertylist){var result=beforeselectorList;var selectors=selectorList.split(",");for(var i=0;i<selectors.length;i++){result+=prefix+selectors[i];if(i+1<selectors.length)result+=",";}
result+=propertyList;return result;});return text;}
navbar.prototype.changedpreferenceforkey=function(key)
{if(key=="navbar-css")
{var text=this.preferenceforkey(key);if(!text)
{text="";}
text=this.substwidgetpath(text);text=this.addcssselectorprefix(text);var styleelement=this.getstyleelement();if(styleelement)
{detectbrowser();if(!windowsinternetexplorer)
{var node=document.createtextnode(text);if(node)
{while(styleelement.haschildnodes())
{styleelement.removechild(styleelement.firstchild);}
styleelement.appendchild(node);}}
else
{styleelement.stylesheet.csstext=text;}}}}
navbar.prototype.populatenavitems=function(req)
{var items;var feedRoot;if(isajaxrequestready(req)&&(feedroot=ajaxgetdocumentelement(req))){var parsedfeed=this.getatomfeeditems(feedRoot);var items=parsedFeed.resultarray;var currentpageguid=null;var isCollectionpage="no";var curpagepat=null;if(this.runninginapp)
curpagepat=/\.#current#.$/;else
{currentpageguid=this.preferenceforkey("current-page-guid");iscollectionpage=this.preferenceforkey("iscollectionpage");}
var navdiv=this.div("navbar-list");var navbgdiv=navdiv.parentnode;$(navbgdiv).ensurehaslayoutforie();while(navdiv.firstchild){navdiv.removechild(navdiv.firstchild);}
var depthprefix=this.preferenceforkey("path-to-root");if(!depthprefix||depthprefix=="")
depthprefix="./";for(var x=0;x<items.length;x++){var navItem=document.createelement("li");var anchor=document.createelement("a");var title=items[x].title;var pageguid=items[x].guid;title=title.replace(/ /g,"\u00a0")+" ";var url=items[x].url;if(!this.runninginapp&&!url.match(/^http:/i))
url=depthprefix+url;var inappcurpage=this.runninginapp&&curpagepat.exec(unescape(new string(url)));if(inappcurpage)
{url=url.replace(curpagepat,"");}
if(pageguid==currentpageguid||inappcurpage){navitem.classname='current-page';if(!this.runninginapp&&iscollectionpage!="yes"){url="";}}
else
navItem.classname='noncurrent-page';anchor.setattribute("href",url);anchor.appendchild(document.createtextnode(title));navitem.appendchild(anchor);navdiv.appendchild(navitem);}
if(this.preferences&&this.preferences.postnotification){this.preferences.postnotification("blwidgetissafetodrawnotification",1);}}}
navbar.prototype.getatomfeeditems=function(feednode)
{var results=new array;var pageorder=new array;if(feednode)
{var generator="";var generatorelt=getfirstelementbytagname(feednode,"generator");if(generatorelt&&generatorelt.firstchild){generator=alldata(generatorelt);}
var pageguids,pageguidselt;for(var entryelt=feednode.firstchild;entryelt;entryelt=entryelt.nextsibling){var isinnavbarelt=null;if(!pageguids&&(pageguidselt=findchild(entryelt,"site-navbar","urn:iweb:"))){pageguids=alldata(pageguidselt).split(",");for(var x=0;x<pageguids.length;x++){var pageguid=pageguids[x];pageorder[""+pageguid]=x;}}
if(entryelt.nodename=="entry"&&(isinnavbarelt=findchild(entryelt,"in-navbar","urn:iweb:"))){if(!isinnavbarelt)
continue;var pageguid="";if(isinnavbarelt.firstchild){pageguid=""+alldata(isinnavbarelt);}else{iwlog("no navbarelt child");}
if(pageguid=="navbar-sort")
continue;var title="";var titleelt=findchild(entryelt,"title","urn:iweb:");if(!titleElt){iwlog("No iweb title");titleelt=findchild(entryelt,"title");}
if(titleelt&&titleelt.firstchild){title=alldata(titleelt);}
var linkelt=getfirstelementbytagname(entryelt,'link');url=linkelt.getattribute("href");if(!url&&linkelement.firstchild){url=alldata(linkelement);}
results[results.length]={title:title,url:url,guid:pageguid};}}}
if(pageguids){results=$(results).reject(function(result){return(pageorder[result.guid]===undefined);});results.sort(function(lhs,rhs){return pageorder[lhs.guid]-pageorder[rhs.guid];});}
return{resultarray:results};}
function findchild(element,nodename,namespace)
{var child;for(child=element.firstchild;child;child=child.nextsibling){if(child.localname==nodename||child.basename==nodename){if(!namespace){return child;}
var childnamespace=child.namespaceuri;if(childnamespace==namespace){return child;}}}
return null;}
function getfirstelementbytagname(node,tag_name){var elements=node.getelementsbytagname(tag_name);if(elements.length){return elements[0];}
else{return findchild(node,tag_name);}}
function alldata(node)
{node=node.firstchild;var data=node.data;while((node=node.nextsibling)){data+=node.data;}
return data;}
navbar.prototype.onload=function()
{}
navbar.prototype.onunload=function()
{}

