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

var IWAllFeeds={};function IWCreateFeed(url)
{var feed=IWAllFeeds[url];if(feed==null)
{feed=new IWFeed(url);}
return feed;}
var IWFeed=Class.create({initialize:function(url)
{if(url)
{if(IWAllFeeds.hasOwnProperty(url))
{iWLog("warning -- use IWCreateFeed rather than new IWFeed and you'll get better performance");}
this.mURL=url;this.mLoading=false;this.mLoaded=false;this.mCallbacks=[];this.mImageStream=null;IWAllFeeds[url]=this;}},sourceURL:function()
{return this.mURL;},load:function(baseURL,callback)
{if(this.mLoaded&&(callback!=null))
{callback(this.mImageStream);}
else
{if(callback!=null)
{this.mCallbacks.push(callback);}
if(this.mLoading==false)
{this.mLoading=true;this.p_sendRequest(baseURL);}}},p_sendRequest:function(baseURL)
{var url=this.mURL.toRelativeURL(baseURL);new Ajax.Request(url,{method:'get',onSuccess:this.p_onload.bind(this,baseURL),onFailure:this.p_requestFailed.bind(this,baseURL)});},p_requestFailed:function(baseURL,req)
{iWLog("There was a problem ("+req.status+") retrieving the feed:\n\r"+req.statusText);if(req.status==500)
{iWLog("working around status 500 by trying again...");window.setTimeout(this.p_sendRequest.bind(this,baseURL),100);}},p_onload:function(baseURL,req)
{var collectionItem;var doc=ajaxGetDocumentElement(req);var items=$A(doc.getElementsByTagName('item'));this.mImageStream=this.p_interpretItems(baseURL,items);this.p_postLoadCallbacks(this.mImageStream);},p_postLoadCallbacks:function(imageStream)
{for(var i=0;i<this.mCallbacks.length;++i)
{this.mCallbacks[i](imageStream);}
this.mLoaded=true;},p_applyEntryOrder:function(imageStream,entryGUIDs)
{var orderedStream=[];var guidToIndex=[];for(var i=0;i<imageStream.length;i++)
{var streamEntryGUID=imageStream[i].guid();if(streamEntryGUID)
{guidToIndex[streamEntryGUID]=i;}}
for(var i=0;i<entryGUIDs.length;i++)
{var index=guidToIndex[entryGUIDs[i]];if(index!==undefined)
{orderedStream.push(imageStream[index]);}}
(function(){return orderedStream.length==entryGUIDs.length}).assert();return orderedStream;},p_firstElementByTagNameNS:function(element,ns,tag)
{var child=null;for(child=element.firstChild;child!=null;child=child.nextSibling)
{if(child.baseName==tag||child.localName==tag)
{if(ns==null||ns==""||child.namespaceURI==ns)
{break;}}}
return child;}});var IWStreamEntry=Class.create({initialize:function(thumbnailURL,title,richTitle,guid)
{if(arguments.length>0)
{if(thumbnailURL)
{this.mThumbnail=IWCreateImage(thumbnailURL);}
if(title)
{this.mTitle=title.stringByEscapingXML().stringByConvertingNewlinesToBreakTags();}
if(richTitle)
{this.mRichTitle=richTitle;}
if(guid)
{this.mGUID=guid;}}},setThumbnailURL:function(thumbnailURL)
{this.mThumbnail=IWCreateImage(thumbnailURL);},loadThumbnail:function(callback)
{this.thumbnail().load(callback);},unloadThumbnail:function()
{this.thumbnail().unload();},thumbnailNaturalSize:function()
{return this.thumbnail().naturalSize();},thumbnail:function()
{return this.mThumbnail;},micro:function()
{return this.thumbnail();},mipThumbnail:function()
{return this.thumbnail();},title:function()
{return this.mTitle;},richTitle:function()
{return this.mRichTitle?this.mRichTitle:this.mTitle;},metric:function()
{return null;},guid:function()
{return this.mGUID;},isMovie:function()
{return false;},commentGUID:function()
{return null;},showCommentIndicator:function()
{return true;},badgeMarkupForRect:function(rect)
{return IWStreamEntryBadgeMarkup(rect,this.isMovie(),this.showCommentIndicator()?this.commentGUID():null);},thumbnailMarkupForRect:function(rect)
{return imgMarkup(this.thumbnail().sourceURL(),rect.position(),"",this.mTitle)+
this.badgeMarkupForRect(rect);},didInsertThumbnailMarkupIntoDocument:function()
{}});function IWStreamEntryBadgeMarkup(rect,isMovie,commentGUID)
{var kBadgeWidth=16.0;var kBadgeHeight=16.0;var badgeRect=new IWRect(rect.origin.x,rect.maxY()-kBadgeHeight,rect.size.width,kBadgeHeight);var markup="";if(isMovie)
{markup+='<div style="background-color: black; '+badgeRect.position()+iWOpacity(0.75)+'"></div>';}
var badgeSize=new IWSize(kBadgeWidth,kBadgeHeight);if(isMovie)
{var movieImage=IWImageNamed("movie overlay");var movieRect=new IWRect(badgeRect.origin,badgeSize);markup+=imgMarkup(movieImage.sourceURL(),movieRect.position(),'class="badge-overlay"');}
var commentLocation=new IWPoint(badgeRect.maxX()-kBadgeWidth,badgeRect.origin.y);if(commentGUID)
{var commentImage=IWImageNamed("comment overlay");var commentRect=new IWRect(commentLocation,badgeSize);markup+=imgMarkup(commentImage.sourceURL(),commentRect.position()+"display: none; ",'class="badge-overlay" id="comment-badge-'+commentGUID+'"');}
return markup;}
var IWCommentableStreamEntry=Class.create(IWStreamEntry,{initialize:function($super,assetURL,showCommentIndicator,thumbnailURL,title,richTitle,guid)
{$super(thumbnailURL,title,richTitle,guid);this.mAssetURL=assetURL;this.mShowCommentIndicator=showCommentIndicator;},commentGUID:function()
{return this.guid();},showCommentIndicator:function()
{return this.mShowCommentIndicator;},didInsertThumbnailMarkupIntoDocument:function()
{if(this.mAssetURL&&hostedOnDM())
{IWCommentCountForURL(this.mAssetURL,this.p_commentCountCallback.bind(this));}
else
{this.p_commentCountCallback(0);}},commentAssetURL:function()
{return this.mAssetURL;},p_commentCountCallback:function(commentCount)
{this.mCommentCount=commentCount;if(this.mCommentCount>0)
{$('comment-badge-'+this.commentGUID()).show();}}});var IWImageStreamEntry=Class.create(IWCommentableStreamEntry,{initialize:function($super,assetURL,showCommentIndicator,imageURL,thumbnailURL,microURL,mipThumbnailURL,title,richTitle,guid)
{$super(assetURL,showCommentIndicator,thumbnailURL,title,richTitle,guid);this.mImage=IWCreateImage(imageURL);if(microURL)
{this.mMicro=IWCreateImage(microURL);}
if(mipThumbnailURL)
{this.mMIPThumbnail=IWCreateImage(mipThumbnailURL);}},setImageURL:function(imageURL)
{this.mImage=IWCreateImage(imageURL);},image:function()
{return this.mImage;},micro:function()
{return this.mMicro?this.mMicro:this.thumbnail();},mipThumbnail:function()
{return this.mMIPThumbnail?this.mMIPThumbnail:this.thumbnail();},targetURL:function()
{return this.mImage.sourceURL();},slideshowValue:function(imageType)
{var image=this[imageType]();return{image:image,caption:this.title()};}});var IWMovieStreamEntry=Class.create(IWCommentableStreamEntry,{initialize:function($super,assetURL,showCommentIndicator,movieURL,thumbnailURL,title,richTitle,movieParams,guid)
{$super(assetURL,showCommentIndicator,thumbnailURL,title,richTitle,guid);this.mMovieURL=movieURL;this.mMovieParams=movieParams;},movieURL:function()
{return this.mMovieURL;},targetURL:function()
{return this.movieURL();},isMovie:function()
{return true;},setMovieParams:function(params)
{this.mMovieParams=params;},slideshowValue:function(imageType)
{return{image:this.thumbnail(),movieURL:this.movieURL(),caption:this.title(),params:this.mMovieParams};}});var IWMediaStreamPageEntry=Class.create(IWStreamEntry,{initialize:function($super,targetPageURL,thumbnailURL,title,richTitle,guid)
{if(arguments.length>0)
{$super(thumbnailURL,title,richTitle,guid);this.mTargetPageURL=targetPageURL;}},thumbnailNaturalSize:function()
{return new IWSize(4000,3000);},targetURL:function()
{return this.mTargetPageURL;},positionedThumbnailMarkupForRect:function(rect)
{return IWMediaStreamPageEntryPositionedThumbnailMarkupForRect(this.mThumbnail,rect);}});function IWMediaStreamPageEntryPositionedThumbnailMarkupForRect(thumbnail,rect)
{var thumbnailSize=thumbnail.naturalSize();var scale=Math.max(rect.size.width/thumbnailSize.width,rect.size.height/thumbnailSize.height);var imageSize=thumbnailSize.scale(scale,scale,true);var imagePosition=new IWPoint((rect.size.width-imageSize.width)/2,(rect.size.height-imageSize.height)/2);imagePosition=imagePosition.scale(1,1,true);var imageRect=new IWRect(imagePosition,imageSize);return imgMarkup(thumbnail.sourceURL(),imageRect.position());}
var IWMediaStreamPhotoPageEntryPrefs={};var IWMediaStreamPhotoPageEntries={};function IWMediaStreamPhotoPageSetPrefs(slideshowPrefs)
{IWMediaStreamPhotoPageEntryPrefs=slideshowPrefs;}
var IWMediaStreamPhotoPageEntryUniqueId=0;var IWMediaStreamPhotoPageEntry=Class.create(IWMediaStreamPageEntry,{initialize:function($super,streamScriptURL,targetPageURL,title,contentsFunction,guid)
{$super(targetPageURL,null,title,null,guid);this.mStreamScriptURL=streamScriptURL;this.mContentsFunction=contentsFunction;},loadThumbnail:function(callback)
{this.mThumbnailCallback=callback;this.mSlideshowId='gridEntry'+IWMediaStreamPhotoPageEntryUniqueId++;IWMediaStreamPhotoPageEntries[this.mSlideshowId]=this;var loadingArea=IWCreateLoadingArea();var uniqueId='iFrame_'+new Date().getTime()+IWMediaStreamPhotoPageEntryUniqueId;loadingArea.innerHTML='';},streamDidLoad:function(media)
{this.mMedia=media;if(this.mMedia&&this.mMedia.length>0)
{this.mThumbnail=this.mMedia[0].mipThumbnail();this.mThumbnail.load(this.mThumbnailCallback);}
else
{this.mThumbnailCallback();}},metric:function()
{var photoCount=0;var clipCount=0;if(this.mMedia)
{for(var index=0;index<this.mMedia.length;++index)
{if(this.mMedia[index].isMovie())
++clipCount;else
++photoCount;}}
return this.mContentsFunction(photoCount,clipCount);},thumbnailMarkupForRect:function(rect)
{var markup="";if(this.mThumbnail)
{markup='<div id="'+this.mSlideshowId+'" style="overflow: hidden; '+rect.position()+'" onclick="window.location.href = \''+this.targetURL()+'\'">'+
this.positionedThumbnailMarkupForRect(rect)+'<div id="'+this.mSlideshowId+'-slideshow_placeholder" style="position: absolute; left: 0px; top: 0px; height: 100%; width: 100%; overflow: hidden; ">'+'</div>'+'</div>';}
return markup;},didInsertThumbnailMarkupIntoDocument:function()
{if(this.mThumbnail)
{if(isiPhone==false)
{var prefs=IWMediaStreamPhotoPageEntryPrefs;prefs["mediaStreamObject"]={load:function(media,baseURL,callback){callback(media);}.bind(null,this.mMedia)};new SlideshowGlue(this.mSlideshowId,'../Scripts/Widgets/Slideshow','../Scripts/Widgets/SharedResources','..',prefs);}}}});function IWMediaStreamPhotoPageSetMediaStream(mediaStream,slideshowId)
{mediaStream.load(IWMediaStreamPhotoPageEntryPrefs.baseURL,function(slideshowId,media)
{IWMediaStreamPhotoPageEntries[slideshowId].streamDidLoad(media);}.bind(null,slideshowId));}
var IWMediaStreamMediaPageEntryUniqueId=0;var IWMediaStreamMediaPageEntry=Class.create(IWMediaStreamPageEntry,{initialize:function($super,targetPageURL,thumbnailURL,title,contents,isMovie,guid)
{$super(targetPageURL,thumbnailURL,title,null,guid);this.mContents=contents;this.mIsMovie=isMovie;},isMovie:function()
{return this.mIsMovie;},metric:function()
{return this.mContents;},thumbnailMarkupForRect:function(rect)
{var badgeRect=new IWRect(new IWPoint(0,0),rect.size);var thumbnailMarkup=this.positionedThumbnailMarkupForRect(rect)+this.badgeMarkupForRect(badgeRect);var idAttribute="";var playButtonMarkup="";if(this.isMovie())
{this.mPlayButtonId='movieEntry'+IWMediaStreamMediaPageEntryUniqueId++;idAttribute='id="'+this.mPlayButtonId+'"';playButtonMarkup='<div id="'+this.mPlayButtonId+'-play_button" class="play_button">'+'<div>'+'</div>'+'</div>';}
var markup='<div '+idAttribute+' style="overflow: hidden; '+rect.position()+'" onclick="window.location.href = \''+this.targetURL()+'\'">'+
thumbnailMarkup+
playButtonMarkup+'</div>';return markup;},didInsertThumbnailMarkupIntoDocument:function()
{if(this.isMovie())
{if(isiPhone==false)
{new PlayButton(this.mPlayButtonId,'../Scripts/Widgets/PlayButton','../Scripts/Widgets/SharedResources','..',{});}}}});var gPhotoFormats=[];var gClipFormats=[];function IWCreateMediaCollection(url,slideshowEnabled,transitionIndex,photoFormats,clipFormats)
{var feed=IWAllFeeds[url];if(feed==null)
{if(gPhotoFormats.length==0)
{gPhotoFormats=photoFormats;}
if(gClipFormats.length==0)
{gClipFormats=clipFormats;}
feed=new IWMediaCollection(url,slideshowEnabled,transitionIndex);}
return feed;}
var IWMediaCollection=Class.create(IWFeed,{initialize:function($super,url,slideshowEnabled,transitionIndex)
{$super(url);this.mSlideshowEnabled=slideshowEnabled;this.mTransitionIndex=transitionIndex;},p_interpretItems:function(baseURL,items)
{var iWebNamespace='http://www.apple.com/iweb';var thumbnailNamespace='urn:iphoto:property';var truthFeed=this.mURL.indexOf('?webdav-method=truthget')!=-1;if(truthFeed)
{iWebNamespace=thumbnailNamespace="urn:iweb:";}
if(IWMediaStreamPhotoPageSetPrefs)
{IWMediaStreamPhotoPageSetPrefs({slideshowEnabled:this.mSlideshowEnabled,fadeIn:true,showOnMouseOver:true,photoDuration:2,startIndex:1,scaleMode:"fill",transitionIndex:this.mTransitionIndex,imageType:"mipThumbnail",movieMode:kPosterFrameOnly,baseURL:baseURL});}
var mediaStream=[];for(var i=0;i<items.length;++i)
{var item=items[i];var link=null;if(truthFeed)
{link=this.p_firstElementByTagNameNS(item,iWebNamespace,'link');}
if(link==null)
{link=item.getElementsByTagName('link')[0];}
if(link!=null)
{var titleText='';var title=item.getElementsByTagName('title')[0];if(title!=null)
{titleText=title.firstChild.nodeValue;}
var skip=false;if(truthFeed)
{title=this.p_firstElementByTagNameNS(item,iWebNamespace,'title');if(title!=null)
{titleText=title.firstChild.nodeValue;}
else
{skip=true;}}
if(skip==false)
{var pageURL=link.firstChild.nodeValue.toRelativeURL(baseURL);var entry=null;var guidNode=item.getElementsByTagName('useritemguid')[0];var guid=null;if(guidNode!=null)
{guid=guidNode.firstChild.nodeValue;}
var thumbnail=this.p_firstElementByTagNameNS(item,thumbnailNamespace,'thumbnail')||item.getElementsByTagName('thumbnail')[0];if(thumbnail)
{var thumbnailURL=transparentGifURL();if(thumbnail.firstChild&&thumbnail.firstChild.nodeValue)
{thumbnailURL=thumbnail.firstChild.nodeValue.toRelativeURL(baseURL);}
var contentsText=null;var contents=this.p_firstElementByTagNameNS(item,iWebNamespace,'contents');if(contents)
{contentsText=contents.firstChild.nodeValue;}
var isMovie=false;var isMovieValue=this.p_firstElementByTagNameNS(item,iWebNamespace,'is-movie');if(isMovieValue&&isMovieValue.firstChild&&isMovieValue.firstChild.nodeValue)
{isMovie=(isMovieValue.firstChild.nodeValue=='true');}
entry=new IWMediaStreamMediaPageEntry(pageURL,thumbnailURL,titleText,contentsText,isMovie,guid);}
else
{var pageName=pageURL.lastPathComponent().stringByDeletingPathExtension();var pageScriptURL=pageURL.stringByDeletingLastPathComponent().stringByAppendingPathComponent(pageName+"_files").stringByAppendingPathComponent(pageName+".js");entry=new IWMediaStreamPhotoPageEntry(pageScriptURL,pageURL,titleText,albumContentsFunction,guid);}
mediaStream.push(entry);}}}
return mediaStream;}});function albumContentsFunction(photoCount,clipCount)
{var contents="";photoFormat=gPhotoFormats[Math.min(photoCount,2)];clipFormat=gClipFormats[Math.min(clipCount,2)];photoFormat=photoFormat.replace(/%d/,photoCount);clipFormat=clipFormat.replace(/%d/,clipCount);if(clipFormat&&clipFormat.length>0&&photoCount==0)
{contents=clipFormat;}
else
{contents=photoFormat;if(clipFormat&&clipFormat.length>0)
{contents+=", "+clipFormat;}}
return contents;}
function IWCreatePhotocast(url,showCommentIndicator)
{var feed=IWAllFeeds[url];if(feed==null)
{feed=new IWPhotocast(url,showCommentIndicator);}
return feed;}
var IWPhotocast=Class.create(IWFeed,{initialize:function($super,url,showCommentIndicator)
{$super(url);this.mShowCommentIndicator=showCommentIndicator;},p_interpretItems:function(baseURL,items)
{var imageStream=[];for(var i=0;i<items.length;++i)
{var item=items[i];enclosure=item.getElementsByTagName('enclosure')[0];if(enclosure!=null)
{var titleText='';var title=item.getElementsByTagName('title')[0];if(title&&title.firstChild&&title.firstChild.nodeValue)
{titleText=title.firstChild.nodeValue;}
var iWebNamespace='http://www.apple.com/iweb';var thumbnailNamespace='urn:iphoto:property';var richTitleHTML;var richTitle=this.p_firstElementByTagNameNS(item,iWebNamespace,'richTitle');if(richTitle&&richTitle.firstChild&&richTitle.firstChild.nodeValue)
{richTitleHTML=richTitle.firstChild.nodeValue.stringByUnescapingXML();}
var guidNode=item.getElementsByTagName('guid')[0];var guid=null;if(guidNode!=null)
{guid=guidNode.firstChild.nodeValue;}
var thumbnail=this.p_firstElementByTagNameNS(item,thumbnailNamespace,'thumbnail')||item.getElementsByTagName('thumbnail')[0];IWAssert(function(){return thumbnail!=null},"Could not get thumbnail from feed.  Server configuration may have changed.");if(thumbnail)
{var entry=null;var type=enclosure.getAttribute("type").split("/");var thumbnailURL=thumbnail.firstChild.nodeValue.toRebasedURL(baseURL);if(type[0]=="video")
{var movieURL=enclosure.getAttribute('url').toRebasedURL(baseURL);var movieParams=null;var movieParamsElement=this.p_firstElementByTagNameNS(item,iWebNamespace,'movieParams');if(movieParamsElement&&movieParamsElement.firstChild&&movieParamsElement.firstChild.nodeValue)
{var movieParams