//
//  iweb - widgetcommon.js
//  copyright (c) 2007 apple inc. all rights reserved.
//

var widgets=[];var identifierstostringLocalizations=[];function widget(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;settransparentGifurlL(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);}}
widget.prototype.div=function()
{var divid=this.instanceid;if(arguments.length==1)
{divid=this.instanceid+"-"+arguments[0];}
return $(divid);}
widget.prototype.onload=function()
{this.onloadreceived=true;}
widget.prototype.onunload=function()
{}
widget.prototype.didbecomeselected=function()
{}
widget.prototype.didbecomedeselected=function()
{}
widget.prototype.didbeginediting=function()
{}
widget.prototype.didendediting=function()
{}
widget.prototype.setneedsdisplay=function()
{}
widget.prototype.preferenceforkey=function(key)
{var value;if(this.preferences)
value=this.preferences[key];return value;}
widget.prototype.initializedefaultpreferences=function(prefs)
{var self=this;object.keys(prefs).foreach(function(pref)
{if(self.preferenceforkey(pref)===undefined)
{self.setpreferenceforkey(prefs[pref],pref);}});}
widget.prototype.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);}}
widget.prototype.changedpreferenceforkey=function(key)
{}
widget.prototype.postnotificationwithnameanduserinfo=function(name,userinfo)
{if(window.notificationcenter!==undefined)
{notificationCenter.postnotification(new iwnotification(name,null,userinfo));}}
widget.prototype.sizewillchange=function()
{}
widget.prototype.sizedidchange=function()
{}
widget.prototype.widgetwidth=function()
{var enclosingdiv=this.div();if(enclosingdiv)
return enclosingdiv.offsetwidth;else
return null;}
widget.prototype.widgetheight=function()
{var enclosingdiv=this.div();if(enclosingdiv)
return enclosingdiv.offsetheight;else
return null;}
widget.prototype.getinstanceid=function(id)
{var fullid=this.instanceiD+"-"+id;if(arguments.length==2)
{fullid+=("$"+arguments[1]);}
return fullid;}
widget.prototype.getelementbyid=function(id)
{var fullId=this.getinstanceId.apply(this,arguments);return $(fullid);}
widget.prototype.localizedstring=function(string)
{return localizedstring(this.widgetidentifier,string);}
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));}
