2011-05-06 13:55:47 +02:00
$ ( document ) . ready ( function ( ) {
function cachePath ( path ) {
if ( path == "" )
return "root" ;
if ( path [ 0 ] == '/' )
path = path . substring ( 1 ) ;
2011-05-07 00:55:02 +02:00
path = path
. replace ( / /g , "_" )
. replace ( /\//g , "-" )
. replace ( /\(/g , "" )
. replace ( /\)/g , "" )
. replace ( /#/g , "" )
. replace ( /\[/g , "" )
. replace ( /\]/g , "" )
. replace ( /"/g , "" )
. replace ( /'/g , "" )
. replace ( /_-_/g , "-" )
. toLowerCase ( ) ;
while ( path . indexOf ( "--" ) != - 1 )
path = path . replace ( /--/g , "-" ) ;
while ( path . indexOf ( "__" ) != - 1 )
path = path . replace ( /__/g , "_" ) ;
return path ;
2011-05-06 13:55:47 +02:00
}
function imagePath ( image , path , size , square ) {
var suffix ;
if ( square )
suffix = size . toString ( ) + "s" ;
else
suffix = size . toString ( ) ;
return "cache/" + cachePath ( path + "/" + image + "_" + suffix + ".jpg" ) ;
}
2011-05-07 00:55:02 +02:00
function escapeId ( id ) {
return id . replace ( /\./g , "\\." ) . replace ( /,/g , "\\," ) ;
}
2011-05-06 13:55:47 +02:00
function loadAlbum ( ) {
if ( current _album _cache in album _cache ) {
albumLoaded ( album _cache [ current _album _cache ] ) ;
return ;
}
$ ( "#loading" ) . show ( ) ;
$ . ajax ( {
type : "GET" ,
url : "cache/" + current _album _cache + ".json" ,
error : function ( ) { $ ( document . body ) . html ( "Couldn't fetch it." ) ; } ,
success : albumLoaded
} ) ;
}
function albumLoaded ( album ) {
$ ( "#loading" ) . hide ( ) ;
album _cache [ cachePath ( album . path ) ] = album ;
current _album = album ;
if ( cachePath ( album . path ) == current _album _cache )
showAlbum ( ) ;
if ( current _photo _cache != null )
showPhoto ( ) ;
setTitle ( ) ;
}
function setTitle ( ) {
var title = "" ;
var components ;
if ( current _album . path . length == 0 )
components = [ document . title ] ;
else {
components = current _album . path . split ( "/" ) ;
components . unshift ( document . title ) ;
}
var last = "" ;
for ( var i = 0 ; i < components . length ; ++ i ) {
if ( i )
last += "/" + components [ i ] ;
if ( i < components . length - 1 || current _photo _cache != null )
title += "<a href=\"#" + ( i == 0 ? "" : cachePath ( last . substring ( 1 ) ) ) + "\">" ;
title += components [ i ] ;
if ( i < components . length - 1 || current _photo _cache != null ) {
title += "</a>" ;
title += " » " ;
}
}
if ( current _photo _cache != null )
title += current _photo . name ;
$ ( "#title" ) . html ( title ) ;
}
function showAlbum ( ) {
$ ( "html, body" ) . animate ( { scrollTop : 0 } , "slow" ) ;
var photos = "" ;
for ( var i = 0 ; i < current _album . photos . length ; ++ i )
photos += "<a href=\"#" + current _album _cache + "/" + cachePath ( current _album . photos [ i ] . name ) + "\"><img id=\"thumb-" + cachePath ( current _album . photos [ i ] . name ) + "\" src=\"" + imagePath ( current _album . photos [ i ] . name , current _album . path , 150 , true ) + "\" height=\"150\" width=\"150\"></a>" ;
$ ( "#thumbs" ) . html ( photos ) ;
if ( current _album . albums . length )
$ ( "#subalbums-title" ) . show ( ) ;
else
$ ( "#subalbums-title" ) . hide ( ) ;
var subalbums = "" ;
2011-05-07 00:55:02 +02:00
var thumbFinderList = new Array ( ) ;
for ( var i = current _album . albums . length - 1 ; i >= 0 ; -- i ) {
var path = cachePath ( current _album . path + "/" + current _album . albums [ i ] . path ) ;
var id = "album-" + path ;
subalbums += "<a href=\"#" + path + "\"><div id=\"" + id + "\" class=\"album-button\">" + current _album . albums [ i ] . path + "</div></a>" ;
thumbFinderList . push ( { path : path , id : escapeId ( id ) } ) ;
}
2011-05-06 13:55:47 +02:00
$ ( "#subalbums" ) . html ( subalbums ) ;
2011-05-07 00:55:02 +02:00
for ( var i = 0 ; i < thumbFinderList . length ; ++ i )
( function ( thumb ) {
albumThumbFinder ( thumb . path , function ( photo , album ) {
$ ( "#" + thumb . id ) . css ( "background-image" , "url(" + imagePath ( photo . name , album . path , 150 , true ) + ")" ) ;
} ) ;
} ) ( thumbFinderList [ i ] ) ;
2011-05-06 13:55:47 +02:00
$ ( "#album-view" ) . removeClass ( "photo-view-container" ) ;
$ ( "#subalbums" ) . show ( ) ;
$ ( "#photo-view" ) . hide ( ) ;
}
function showPhoto ( ) {
currentPhoto ( ) ;
if ( current _photo == null ) {
$ ( document . body ) . html ( "Wrong picture." ) ;
return ;
}
2011-05-06 14:52:46 +02:00
var maxSize = 800 ;
2011-05-06 13:55:47 +02:00
var width = current _photo . size [ 0 ] ;
var height = current _photo . size [ 1 ] ;
if ( width > height ) {
height = height / width * maxSize ;
width = maxSize ;
} else {
width = width / height * maxSize ;
height = maxSize ;
}
$ ( "#photo" )
. attr ( "width" , width ) . attr ( "height" , height )
. attr ( "src" , imagePath ( current _photo . name , current _album . path , maxSize , false ) )
. attr ( "alt" , current _photo . name )
2011-05-06 15:02:32 +02:00
. attr ( "title" , current _photo . name )
. load ( function ( ) { $ ( this ) . css ( "width" , "auto" ) . css ( "height" , "100%" ) ; } ) ;
2011-05-06 13:55:47 +02:00
var nextLink = "#" + current _album _cache + "/" + cachePath ( current _album . photos [
( current _photo _index + 1 >= current _album . photos . length ) ? 0 : ( current _photo _index + 1 )
] . name ) ;
$ ( "#next-photo" ) . attr ( "href" , nextLink ) ;
$ ( "#next" ) . attr ( "href" , nextLink ) ;
$ ( "#back" ) . attr ( "href" , "#" + current _album _cache + "/" + cachePath ( current _album . photos [
( current _photo _index - 1 < 0 ) ? ( current _album . photos . length - 1 ) : ( current _photo _index - 1 )
] . name ) ) ;
$ ( "#album-view" ) . addClass ( "photo-view-container" ) ;
$ ( "#subalbums" ) . hide ( ) ;
$ ( "#photo-view" ) . show ( ) ;
2011-05-07 00:55:02 +02:00
var thumb = $ ( "#thumb-" + escapeId ( current _photo _cache ) ) ;
2011-05-06 13:55:47 +02:00
var scroller = $ ( "#album-view" ) ;
scroller . stop ( ) ;
scroller . animate ( { scrollLeft : thumb . position ( ) . left + scroller . scrollLeft ( ) - scroller . width ( ) / 2 + thumb . width ( ) / 2 } , "slow" ) ;
}
function currentPhoto ( ) {
for ( current _photo _index = 0 ; current _photo _index < current _album . photos . length ; ++ current _photo _index ) {
if ( cachePath ( current _album . photos [ current _photo _index ] . name ) == current _photo _cache )
break ;
}
if ( current _photo _index >= current _album . photos . length ) {
current _photo = null ;
current _photo _index = - 1 ;
return ;
}
current _photo = current _album . photos [ current _photo _index ] ;
}
2011-05-07 00:55:02 +02:00
function albumForThumbIteration ( album , callback ) {
album _cache [ cachePath ( album . path ) ] = album ;
var index = Math . floor ( Math . random ( ) * ( album . photos . length + album . albums . length ) ) ;
if ( index >= album . photos . length ) {
index -= album . photos . length ;
fetchAlbumForThumb ( cachePath ( album . path + "/" + album . albums [ index ] . path ) , function ( fetchedAlbum ) {
albumForThumbIteration ( fetchedAlbum , callback ) ;
} ) ;
} else
callback ( album . photos [ index ] , album ) ;
}
function fetchAlbumForThumb ( album , callback ) {
if ( album in album _cache ) {
callback ( album _cache [ album ] ) ;
return ;
}
$ . ajax ( {
type : "GET" ,
url : "cache/" + album + ".json" ,
error : function ( ) { $ ( document . body ) . html ( "Couldn't fetch it." ) ; } ,
success : callback
} ) ;
}
function albumThumbFinder ( album , callback ) {
fetchAlbumForThumb ( album , function ( fetchedAlbum ) { albumForThumbIteration ( fetchedAlbum , callback ) ; } ) ;
}
2011-05-06 13:55:47 +02:00
var current _album _cache = null ;
var current _photo _cache = null ;
var current _album = null ;
var current _photo = null ;
var current _photo _index = - 1 ;
var album _cache = new Array ( ) ;
$ ( window ) . hashchange ( function ( ) {
var new _album _cache = location . hash . substring ( 1 ) ;
var index = new _album _cache . lastIndexOf ( "/" ) ;
if ( index != - 1 && index != new _album _cache . length - 1 ) {
current _photo _cache = new _album _cache . substring ( index + 1 ) ;
new _album _cache = new _album _cache . substring ( 0 , index ) ;
} else
current _photo _cache = null ;
if ( ! new _album _cache . length )
new _album _cache = cachePath ( "root" ) ;
if ( new _album _cache != current _album _cache ) {
current _album _cache = new _album _cache ;
loadAlbum ( ) ;
} else if ( current _photo _cache != null ) {
showAlbum ( ) ;
showPhoto ( ) ;
setTitle ( ) ;
} else {
showAlbum ( ) ;
setTitle ( ) ;
}
} ) ;
$ ( window ) . hashchange ( ) ;
$ ( document ) . keydown ( function ( e ) {
if ( current _photo _cache == null )
return true ;
if ( e . keyCode == 39 ) {
window . location . href = $ ( "#next" ) . attr ( "href" ) ;
return false ;
} else if ( e . keyCode == 37 ) {
window . location . href = $ ( "#back" ) . attr ( "href" ) ;
return false ;
}
return true ;
} ) ;
2011-05-07 00:55:02 +02:00
alert ( "Hello. This is an obnoxious alert message. PhotoFloat is a work in progress. There are many kinks to be worked out.\n\nTODO:\n* display EXIF info in json\n* link to hi-res\n* sizing bugs\n* random thumbnail for album links\n\nSuggestions?" ) ;
2011-05-06 13:55:47 +02:00
} ) ;