//
//  iWeb - WidgetCommon.js
//  Copyright (c) 2007-2008 Apple Inc. All rights reserved.
//

var widgets=[];var identifiersToStringLocalizations=[];var Widget=Class.create({initialize:function(instanceID,widgetPath,sharedPath,sitePath,preferences,runningInApp)
{if(instanceID)
{this.instanceID=instanceID;this.widgetPath=widgetPath;this.sharedPath=sharedPath;this.sitePath=sitePath;this.preferences=preferences;this.runningInApp=(runningInApp===undefined)?false:runningInApp;this.onloadReceived=false;if(this.preferences&&this.runningInApp==true)
{this.preferences.widget=this;setTransparentGifURL(this.sharedPath.stringByAppendingPathComponent("None.gif"));}
this.div().widget=this;window[instanceID]=this;widgets.push(this);widgets[instanceID]=this;if(!this.constructor.instances)
{this.constructor.instances=new Array();}
this.constructor.instances.push(this);}},div:function()
{var divID=this.instanceID;if(arguments.length==1)
{divID=this.instanceID+"-"+arguments[0];}
return $(divID);},onload:function()
{this.onloadReceived=true;},onunload:function()
{},didBecomeSelected:function()
{},didBecomeDeselected:function()
{},didBeginEditing:function()
{},didEndEditing:function()
{},setNeedsDisplay:function()
{},preferenceForKey:function(key)
{var value;if(this.preferences)
value=this.preferences[key];return value;},initializeDefaultPreferences:function(prefs)
{var self=this;$H(prefs).each(function(pair)
{if(self.preferenceForKey(pair.key)===undefined)
{self.setPreferenceForKey(pair.value,pair.key,false);}});},setPreferenceForKey:function(preference,key,registerUndo)
{if(this.runningInApp)
{if(registerUndo===undefined)
registerUndo=true;if((registerUndo==false)&&this.preferences.disableUndoRegistration)
this.preferences.disableUndoRegistration();this.preferences[key]=preference;if((registerUndo==false)&&this.preferences.enableUndoRegistration)
this.preferences.enableUndoRegistration();}
else
{this.preferences[key]=preference;this.changedPreferenceForKey(key);}},changedPreferenceForKey:function(key)
{},postNotificationWithNameAndUserInfo:function(name,userInfo)
{if(window.NotificationCenter!==undefined)
{NotificationCenter.postNotification(new IWNotification(name,null,userInfo));}},sizeWillChange:function()
{},sizeDidChange:function()
{},widgetWidth:function()
{var enclosingDiv=this.div();if(enclosingDiv)
return enclosingDiv.offsetWidth;else
return null;},widgetHeight:function()
{var enclosingDiv=this.div();if(enclosingDiv)
return enclosingDiv.offsetHeight;else
return null;},getInstanceId:function(id)
{var fullId=this.instanceID+"-"+id;if(arguments.length==2)
{fullId+=("$"+arguments[1]);}
return fullId;},getElementById:function(id)
{var fullId=this.getInstanceId.apply(this,arguments);return $(fullId);},localizedString:function(string)
{return LocalizedString(this.widgetIdentifier,string);},showView:function(viewName)
{var futureView=this.m_views[viewName];if((futureView!=this.m_currentView)&&(futureView!=this.m_futureView))
{this.m_futureView=futureView;if(this.m_fadeAnimation)
{this.m_fadeAnimation.stop();}
var previousView=this.m_currentView;this.m_currentView=futureView;var currentView=this.m_currentView;this.m_futureView=null;this.m_fadeAnimation=new SimpleAnimation(function(){delete this.m_fadeAnimation;}.bind(this));this.m_fadeAnimation.pre=function()
{if(previousView)
{previousView.ensureDiv().setStyle({zIndex:0,opacity:1});}
if(currentView)
{currentView.ensureDiv().setStyle({zIndex:1,opacity:0});currentView.show();currentView.render();}}
this.m_fadeAnimation.post=function()
{!previousView||previousView.hide();!currentView||currentView.ensureDiv().setStyle({zIndex:'',opacity:1});!currentView||!currentView.doneFadingIn||currentView.doneFadingIn();}
this.m_fadeAnimation.update=function(now)
{!currentView||currentView.ensureDiv().setOpacity(now);!previousView||previousView.ensureDiv().setOpacity(1-now);}.bind(this);this.m_fadeAnimation.start();}}});Widget.onload=function()
{for(var i=0;i<widgets.length;i++)
{widgets[i].onload();}}
Widget.onunload=function()
{for(var i=0;i<widgets.length;i++)
{widgets[i].onunload();}}
function RegisterWidgetStrings(identifier,strings)
{identifiersToStringLocalizations[identifier]=strings;}
function LocalizedString(identifier,string)
{var localized=undefined;var localizations=identifiersToStringLocalizations[identifier];if(localizations===undefined)
{iWLog("warning: no localizations for widget "+identifier+", (key:"+string+")");}
else
{localized=localizations[string];}
if(localized===undefined)
{iWLog("warning: couldn't find a localization for '"+string+"' for widget "+identifier);localized=string;}
return localized;}
function WriteLocalizedString(identifier,string)
{document.write(LocalizedString(identifier,string));}
var JSONFeedRendererWidget=Class.create(Widget,{initialize:function($super,instanceID,widgetPath,sharedPath,sitePath,preferences,runningInApp)
{if(instanceID)
{$super(instanceID,widgetPath,sharedPath,sitePath,preferences,runningInApp);}},changedPreferenceForKey:function(key)
{try
{var value=this.preferenceForKey(key);if(key=="sfr-shadow")
{if(value!=null)
{this.sfrShadow=eval(value);}
else
{this.sfrShadow=null;}
this.renderFeedItems("sfr-shadow");}
if(key=="sfr-stroke")
{if(value!==null)
this.sfrStroke=eval(value);else
this.sfrStroke=null;this.invalidateFeedItems("sfr-stroke");}
if(key=="sfr-reflection")
{if(value!==null)
{this.sfrReflection=eval(value);}
else
{this.sfrReflection=null;}
this.invalidateFeedItems("sfr-reflection");}}
catch(e)
{iWLog("JSONFeedRendererWidget: exception");debugPrintException(e);}},invalidateFeedItems:function(reason)
{trace('invalidateFeedItems(%s)',reason);if(this.pendingRender!==null)
{clearTimeout(this.pendingRender);}
this.pendingRender=setTimeout(function()
{this.pendingRender=null;this.renderFeedItems(reason);}.bind(this),50);},rerenderImage:function(imgGroupDiv,imgDiv,imageUrlString,entryHasImage,photoProportions,imageWidth,positioningHandler,onloadHandler)
{imgGroupDiv.update();if(entryHasImage)
{imgGroupDiv.strokeApplied=false;imgGroupDiv.reflectionApplied=false;imgGroupDiv.shadowApplied=false;imgGroupDiv.setStyle({marginLeft:0,marginTop:0,marginRight:0,marginBottom:0});imgGroupDiv.style.position='relative';imgDiv.style.position='relative';var imageUrl=imageUrlString||transparentGifURL();var image=IWCreateImage(imageUrl);image.load(function(image,imgDiv,imgGroupDiv,positioningHandler,onloadHandler)
{var cropDiv=this.croppingDivForImage(image,photoProportions,imageWidth);imgGroupDiv.appendChild(cropDiv);if(positioningHandler)
{positioningHandler();}
if(image.sourceURL()!==transparentGifURL())
{this.applyEffects(imgGroupDiv);}
if(onloadHandler)
{onloadHandler();}}.bind(this,image,imgDiv,imgGroupDiv,positioningHandler,onloadHandler));}},croppingDivForImage:function(image,kind,width)
{var croppedSize=function(originalSize,cropKind,width)
{if(cropKind=="Square")
{return new IWSize(width,width);}
else if(cropKind=="Landscape")
{return new IWSize(width,width*(3/4));}
else if(cropKind=="Portrait")
{return new IWSize(width,width*(4/3));}
else
{var scaleFactor=width/originalSize.width;return originalSize.scale(scaleFactor,scaleFactor,true);}};var cropDiv=null;if(image.loaded())
{var img=$(document.createElement('img'));img.src=image.sourceURL();var natural=image.naturalSize();cropDiv=$(document.createElement("div"));cropDiv.appendChild(img);var croppingDivForImage_helper=function(loadedImage)
{if(loadedImage)
{natural=new IWSize(loadedImage.width,loadedImage.height);}
var cropped=croppedSize(natural,kind,width);var scaleFactor=cropped.width/natural.width;if(natural.aspectRatio()>cropped.aspectRatio())
{scaleFactor=cropped.height/natural.height;}
var scaled=natural.scale(scaleFactor);var offset=new IWPoint(Math.abs(scaled.width-cropped.width)/2,Math.abs(scaled.height-cropped.height)/2);img.setStyle({width:px(scaled.width),height:px(scaled.height),marginLeft:px(-offset.x),marginTop:px(-offset.y),position:'relative'});cropDiv.setStyle({width:px(cropped.width),height:px(cropped.height),overflow:"hidden",position:'relative'});cropDiv.className="crop";}
if(windowsInternetExplorer&&effectiveBrowserVersion<7&&img.src.indexOf(transparentGifURL())!=-1)
{var originalImage=new Image();originalImage.src=img.originalSrc;if(originalImage.complete)
{croppingDivForImage_helper(originalImage);}
else
{originalImage.onload=croppingDivForImage_helper.bind(null,originalImage);}}
else
{croppingDivForImage_helper(null);}}
return cropDiv;},applyEffects:function(div)
{if(this.sfrShadow||this.sfrReflection||this.sfrStroke)
{if((div.offsetWidth===undefined)||(div.offsetHeight===undefined)||(div.offsetWidth===0)||(div.offsetHeight===0))
{setTimeout(JSONFeedRendererWidget.prototype.applyEffects.bind(this,div),0)
return;}
if(this.sfrStroke&&(div.strokeApplied==false))
{this.sfrStroke.applyToElement(div);div.strokeApplied=true;}
if(this.sfrReflection&&(div.reflectionApplied==false))
{this.sfrReflection.applyToElement(div);div.reflectionApplied=true;}
if(this.sfrShadow&&(!this.disableShadows)&&(div.shadowApplied==false))
{this.sfrShadow.applyToElement(div);div.shadowApplied=true;}
if(this.runningInApp&&(window.webKitVersion<=419)&&this.preferences.setNeedsDisplay)
{this.preferences.setNeedsDisplay();}}
if(windowsInternetExplorer)
{var cropDivs=div.select(".crop");var cropDiv=cropDivs[cropDivs.length-1];if(cropDiv)
{cropDiv.onclick=function()
{var anchorNode=div.parentNode;var targetHref=locationHRef();while(anchorNode&&(anchorNode.tagName!="A"))
{anchorNode=anchorNode.parentNode}
if(anchorNode)
{targetHref=anchorNode.href;}
window.location=targetHref;};cropDiv.onmouseover=function()
{this.style.cursor='pointer';}}}},summaryExcerpt:function(descriptionHTML,maxSummaryLength)
{var div=document.createElement("div");div.innerHTML=descriptionHTML;if(maxSummaryLength>0)
{var model=new HTMLTextModel(div);model.truncateAroundPosition(maxSummaryLength,"...");}
else if(maxSummaryLength===0)
{div.innerHTML="";}
return div.innerHTML;}});var PrefMarkupWidget=Class.create(Widget,{initialize:function($super,instanceID,widgetPath,sharedPath,sitePath,preferences,runningInApp)
{if(instanceID)
{$super(instanceID,widgetPath,sharedPath,sitePath,preferences,runningInApp);}},onload:function()
{if(!this.runningInApp)
{this.setUpSubDocumentOnLoad();}},setUpSubDocumentOnLoad:function()
{var self=this;var oIFrame=this.getElementById("frame");if(oIFrame)
{setTimeout(function(){self.loadedSubDocument()},250);}},loadedSubDocument:function()
{var oIFrame=this.getElementById("frame");var oSubDocument=oIFrame.contentWindow||oIFrame.contentDocument;if(oSubDocument.document)
{oSubDocument=oSubDocument.document;}
if(oSubDocument.body)
{this.fixTargetOnElements(oSubDocument,"a");this.fixTargetOnElements(oSubDocument,"form");}
else
{var self=this;setTimeout(function(){self.loadedSubDocument()},250);}},fixTargetOnElements:function(doc,tagName)
{var elements=doc.getElementsByTagName(tagName);for(var i=0;i<elements.length;i++)
{var target=elements[i].target;if(target===undefined||target=="")
elements[i].target="_top";}}});function IWScrollbar(scrollbar)
{}
IWScrollbar.prototype._init=function()
{var style=null;var element=null;this._track=$(document.createElement("div"));style=this._track.style;style.height="100%";style.width="100%";this.scrollbar.appendChild(this._track);element=$(document.createElement("div"));element.style.position="absolute";this._setObjectStart(element,0);this._track.appendChild(element);element=$(document.createElement("div"));element.style.position="absolute";this._track.appendChild(element);element=$(document.createElement("div"));element.style.position="absolute";windowsInternetExplorer||this._setObjectEnd(element,0);this._track.appendChild(element);this._thumb=$(document.createElement("div"));style=this._thumb.style;style.position="absolute";this._setObjectSize(this._thumb,this.minThumbSize);this._track.appendChild(this._thumb);element=$(document.createElement("div"));element.style.position="absolute";this._setObjectStart(element,0);this._thumb.appendChild(element);element=$(document.createElement("div"));element.style.position="absolute";this._thumb.appendChild(element);element=$(document.createElement("div"));element.style.position="absolute";windowsInternetExplorer||this._setObjectEnd(element,0);this._thumb.appendChild(element);this.setSize(this.size);this.setTrackStart(this.trackStartPath,this.trackStartLength);this.setTrackMiddle(this.trackMiddlePath);this.setTrackEnd(this.trackEndPath,this.trackEndLength);this.setThumbStart(this.thumbStartPath,this.thumbStartLength);this.setThumbMiddle(this.thumbMiddlePath);this.setThumbEnd(this.thumbEndPath,this.thumbEndLength);this._thumb.style.display="none";Event.observe(this._track,"mousedown",this._mousedownTrackHandler,false);Event.observe(this._thumb,"mousedown",this._mousedownThumbHandler,false);}
IWScrollbar.prototype.remove=function()
{this.scrollbar.removeChild(this._track);}
IWScrollbar.prototype._captureEvent=function(event)
{event.stopPropagation();event.preventDefault();}
IWScrollbar.prototype._mousedownThumb=function(event)
{Event.observe(document,"mousemove",this._mousemoveThumbHandler,true);Event.observe(document,"mouseup",this._mouseupThumbHandler,true);Event.observe(document,"mouseover",this._captureEventHandler,true);Event.observe(document,"mouseout",this._captureEventHandler,true);this._thumbStart_temp=this._getMousePosition(event);this._scroll_thumbStartPos=this._getThumbStartPos();event.stopPropagation();event.preventDefault();}
IWScrollbar.prototype._mousemoveThumb=function(event)
{var delta=this._getMousePosition(event)-this._thumbStart_temp;var new_pos=this._scroll_thumbStartPos+delta;this.scrollTo(this._contentPositionForThumbPosition(new_pos));event.stopPropagation();event.preventDefault();}
IWScrollbar.prototype._mouseupThumb=function(event)
{Event.stopObserving(document,"mousemove",this._mousemoveThumbHandler,true);Event.stopObserving(document,"mouseup",this._mouseupThumbHandler,true);Event.stopObserving(document,"mouseover",this._captureEventHandler,true);Event.stopObserving(document,"mouseout",this._captureEventHandler,true);delete this._thumbStart_temp;delete this._scroll_thumbStartPos;event.stopPropagation();event.preventDefault();}
IWScrollbar.prototype._mousedownTrack=function(event)
{this._track_mouse_temp=this._getMousePosition(event)-this._trackOffset;if(event.altKey)
{this.scrollTo(this._contentPositionForThumbPosition(this._track_mouse_temp-(this._thumbLength/2)));delete this._track_mouse_temp;}
else
{this._track_scrolling=true;Event.observe(this._track,"mousemove",this._mousemoveTrackHandler,true);Event.observe(this._track,"mouseover",this._mouseoverTrackHandler,true);Event.observe(this._track,"mouseout",this._mouseoutTrackHandler,true);Event.observe(document,"mouseup",this._mouseupTrackHandler,true);this._trackScrollOnePage(this);this._track_timer=setInterval(this._trackScrollDelay,500,this);}
event.stopPropagation();event.preventDefault();}
IWScrollbar.prototype._trackScrollDelay=function(self)
{if(!self._track_scrolling)return;clearInterval(self._track_timer);self._trackScrollOnePage(self);self._track_timer=setInterval(self._trackScrollOnePage,150,self);}
IWScrollbar.prototype._mousemoveTrack=function(event)
{this._track_mouse_temp=this._getMousePosition(event)-this._trackOffset;event.stopPropagation();event.preventDefault();}
IWScrollbar.prototype._mouseoverTrack=function(event)
{this._track_mouse_temp=this._getMousePosition(event)-this._trackOffset;this._track_scrolling=true;event.stopPropagation();event.preventDefault();}
IWScrollbar.prototype._mouseoutTrack=function(event)
{this._track_scrolling=false;event.stopPropagation();event.preventDefault();}
IWScrollbar.prototype._mouseupTrack=function(event)
{clearInterval(this._track_timer);Event.stopObserving(this._track,"mousemove",this._mousemoveTrackHandler,true);Event.stopObserving(this._track,"mouseover",this._mouseoverTrackHandler,true);Event.stopObserving(this._track,"mouseout",this._mouseoutTrackHandler,true);Event.stopObserving(document,"mouseup",this._mouseupTrackHandler,true);delete this._track_mouse_temp;delete this._track_scrolling;delete this._track_timer;event.stopPropagation();event.preventDefault();}
IWScrollbar.prototype._trackScrollOnePage=function(self)
{if(!self._track_scrolling)return;var deltaScroll=Math.round(self._trackLength*self._getViewToContentRatio());if(self._track_mouse_temp<self._thumbStart)
self.scrollByThumbDelta(-deltaScroll);else if(self._track_mouse_temp>(self._thumbStart+self._thumbLength))
self.scrollByThumbDelta(deltaScroll);}
IWScrollbar.prototype.setScrollArea=function(scrollarea)
{if(this.scrollarea)
{Event.stopObserving(this.scrollbar,"mousewheel",this.scrollarea._mousewheelScrollHandler,true);Event.stopObserving(this.scrollbar,"DOMMouseScroll",this.scrollarea._mousewheelScrollHandler,true);}
this.scrollarea=scrollarea;Event.observe(this.scrollbar,"mousewheel",this.scrollarea._mousewheelScrollHandler,true);Event.observe(this.scrollbar,"DOMMouseScroll",this.scrollarea._mousewheelScrollHandler,true);}
IWScrollbar.prototype.refresh=function()
{this._trackOffset=this._computeTrackOffset();this._trackLength=this._computeTrackLength();var ratio=this._getViewToContentRatio();if(ratio>=1.0||!this._canScroll())
{if(this.autohide)
{this.hide();}
this._thumb.style.display="none";this.scrollbar.style.appleDashboardRegion="none";}
else
{this._thumbLength=Math.ma
