//
// iWeb - iWebImage.js
// Copyright 2007 Apple Inc.
// All rights reserved.
//

var IWAllImages={};var IWAllImageObjects={};function IWCreateImage(url)
{var image=IWAllImages[url];if(image==null)
{image=new IWImage(url);}
return image;}
var IWNamedImages={};function IWImageNamed(name)
{var image=null;var url=IWNamedImages[name];if(url)
{image=IWCreateImage(url);}
return image;}
function IWRegisterNamedImage(name,url)
{IWNamedImages[name]=url;}
function IWImage(url)
{if(IWAllImages.hasOwnProperty(url))
{iWLog("warning -- use IWCreateImage rather than new IWImage and you'll get better performance");}
this.mPreventUnloading=0;this.mLoading=false;this.mLoaded=false;this.mURL=url;this.mCallbacks=[];IWAllImages[url]=this;}
IWImage.prototype.sourceURL=function()
{return this.mURL;}
IWImage.prototype.loaded=function()
{return this.mLoaded;}
IWImage.prototype.load=function(callback,delayCallbackIfLoaded)
{if(this.mLoaded)
{if(callback!=null)
{delayCallbackIfLoaded?setTimeout(callback,0):callback();}}
else
{if(callback!=null)
{this.mCallbacks.push(callback);}
if(this.mLoading==false)
{this.mLoading=true;var img=new Image();IWAllImageObjects[this.sourceURL()]=img;img.onload=this.p_onload.bind(this);img.src=this.mURL;}}}
detectBrowser();var IWImageEnableUnload=isiPhone;IWImage.prototype.unload=function(evenIfNotEnabled)
{if((evenIfNotEnabled||IWImageEnableUnload)&&this.mLoaded)
{if(this.mPreventUnloading<=0)
{this.mLoaded=false;this.mLoading=false;IWAllImageObjects[this.sourceURL()]=null;}
else
{this.mPreventedUnload=true;}}}
IWImage.prototype.preventUnloading=function()
{if(this.mPreventUnloading==0)
{this.mPreventedUnload=false;}
++this.mPreventUnloading;}
IWImage.prototype.allowUnloading=function()
{--this.mPreventUnloading;if(this.mPreventUnloading<=0&&this.mPreventedUnload)
{this.unload();}}
IWImage.prototype.naturalSize=function()
{(function(){return this.mNaturalSize!==undefined}).bind(this).assert();return this.mNaturalSize;}
IWImage.prototype.imgObject=function()
{return IWAllImageObjects[this.sourceURL()];}
IWImage.prototype.p_onload=function()
{this.preventUnloading();this.mLoaded=true;if(this.mNaturalSize===undefined)
{var imgObject=this.imgObject();(function(){return imgObject!==undefined}).assert();this.mNaturalSize=new IWSize(imgObject.width,imgObject.height);}
for(var i=0;i<this.mCallbacks.length;++i)
{this.mCallbacks[i]();}
this.mCallbacks=[];this.allowUnloading();}
IWImage.prototype.toString=function()
{return"IWImage("+this.mNaturalSize+", "+this.mURL+")";}
function IWCreateLoadingArea()
{if(IWSharedLoadingAreaManager==null)
{IWSharedLoadingAreaManager=new IWLoadingAreaManager();}
return IWSharedLoadingAreaManager.createLoadingArea();}
function IWLoadingAreaManager()
{var div=document.createElement("div");div.style.visibility="hidden";div.style.position="absolute";div.style.width="0px";div.style.height="0px";div.style.overflow="hidden";document.body.appendChild(div);this.mCurrentLoadingArea=div;}
IWLoadingAreaManager.prototype.createLoadingArea=function()
{var loadingArea=document.createElement('div');this.mCurrentLoadingArea.appendChild(loadingArea);return loadingArea;}
var IWSharedLoadingAreaManager=null;var IWSharedEffectRegistry=null;function IWCreateEffectRegistry()
{if(IWSharedEffectRegistry==null)
{IWSharedEffectRegistry=new IWEffectRegistry();}
return IWSharedEffectRegistry;}
function IWEffectRegistry()
{this.mEffects=null;}
IWEffectRegistry.prototype.registerEffects=function(effects)
{this.mEffects=effects;}
IWEffectRegistry.prototype.applyEffects=function()
{var effectQueue=[];effectQueue=effectQueue.concat(this.p_generateQueueForEffectType("crop"));effectQueue=effectQueue.concat(this.p_generateQueueForEffectType("stroke"));effectQueue=effectQueue.concat(this.p_generateQueueForEffectType("reflection"));effectQueue=effectQueue.concat(this.p_generateQueueForEffectType("shadow"));this.p_applyEffectsFromQueue(effectQueue);}
IWEffectRegistry.prototype.p_generateQueueForEffectType=function(effectType)
{var effectQueue=[];var i=0;var effectClass=effectType+"_"+i++;while(effect=this.mEffects[effectClass])
{effectQueue=effectQueue.concat(this.p_generateQueueForEffectClass(effect,effectClass));effectClass=effectType+"_"+i++;}
return effectQueue;}
IWEffectRegistry.prototype.p_generateQueueForEffectClass=function(effect,effectClass,elementList)
{var effectQueue=[];var elements=elementList||$$('.'+effectClass);while(elements&&elements.length>0)
{var element=elements.shift();var children=$(element).select('.'+effectClass);if(children.length>0)
{elements.minusArray(children);effectQueue=effectQueue.concat(this.p_generateQueueForEffectClass(effect,effectClass,children));}
effectQueue.push({element:element,effect:effect});}
return effectQueue;}
var allStyleSheetsLoaded=false;var timeStyleSheetsAppearedInDOM=undefined;IWEffectRegistry.prototype.p_allStyleSheetsLoaded=function()
{if(isCamino||isFirefox)
{if(timeStyleSheetsAppearedInDOM!==undefined)
{duration=(new Date().getTime())-timeStyleSheetsAppearedInDOM;if(duration>100)
{allStyleSheetsLoaded=true;timeStyleSheetsAppearedInDOM=undefined;}}
else if(!allStyleSheetsLoaded)
{for(var i=0,sheetCount=document.styleSheets.length;i<sheetCount;i++)
{var styleSheet=document.styleSheets[i];if(styleSheet.href&&styleSheet.href.indexOf("Moz.css")!=-1)
{timeStyleSheetsAppearedInDOM=new Date().getTime();}}}}
else
{allStyleSheetsLoaded=true;}
return allStyleSheetsLoaded;}
IWEffectRegistry.prototype.p_applyEffectsFromQueue=function(queue)
{var startTime=new Date().getTime();var duration=0;var readyToApplyEffects=this.p_allStyleSheetsLoaded();while(queue.length>0&&duration<100&&readyToApplyEffects)
{var queueEntry=queue.shift();if(queueEntry&&queueEntry.effect&&queueEntry.element)
{queueEntry.effect.applyToElement(queueEntry.element);}
duration=(new Date().getTime())-startTime;}
if(queue.length>0)
{setTimeout(this.p_applyEffectsFromQueue.bind(this,queue),0);}
else
{performPostEffectsFixups();}}
function IWChildOffset(child,parent,positionedOnly)
{var l=0;var t=0;if(parent)
{var current=child;while(current&&current!=parent)
{if(!positionedOnly||(current.style.position=="absolute")||(current.style.position=="relative"))
{l+=current.offsetLeft;t+=current.offsetTop;}
current=current.parentNode;}}
return new IWPoint(l,t);}
function IWImageExtents(ancestor,images,left,top,right,bottom)
{var unionedBounds=new IWRect(left,top,right-left,bottom-top);for(var e=0;e<images.length;++e)
{var imageClippedBounds=new IWRect(images[e].offsetLeft,images[e].offsetTop,images[e].offsetWidth,images[e].offsetHeight);if(ancestor)
{var current=images[e].parentNode;while(current&&current!=ancestor)
{if((current.style.position=="absolute")||(current.style.position=="relative"))
{imageClippedBounds.origin.x+=current.offsetLeft||0;imageClippedBounds.origin.y+=current.offsetTop||0;}
var testForHidden=function(str)
{return str=='hidden';};var clipX=[current.style.overflow,current.style.overflowX].any(testForHidden);var clipY=[current.style.overflow,current.style.overflowY].any(testForHidden);if(clipX||clipY)
{var currentRect=new IWRect(clipX?current.offsetLeft:imageClippedBounds.origin.x,clipY?current.offsetTop:imageClippedBounds.origin.y,clipX?current.offsetWidth:imageClippedBounds.size.width,clipY?current.offsetHeight:imageClippedBounds.size.height);imageClippedBounds=imageClippedBounds.intersection(currentRect);}
current=current.parentNode;}}
if((imageClippedBounds.size.width>0)&&(imageClippedBounds.size.height>0))
{if((unionedBounds.size.width>0)&&(unionedBounds.size.height>0))
{unionedBounds=unionedBounds.union(imageClippedBounds);}
else
{unionedBounds=imageClippedBounds.clone();}}}
var extents={left:unionedBounds.origin.x,top:unionedBounds.origin.y,right:unionedBounds.origin.x+unionedBounds.size.width,bottom:unionedBounds.origin.y+unionedBounds.size.height};return extents;}
function IWImageChildren(element)
{var allImgs=element.getElementsByTagName("img");var inlineRegex=new RegExp('\\b'+'inline-block'+'\\b');var badgeRegex=new RegExp('\\b'+'badge-overlay'+'\\b');var imgs=new Array();for(var e=0;e<allImgs.length;++e)
{var current=allImgs[e];var isIgnored=(current.className.match(badgeRegex)?true:false);while(!isIgnored&&curren
