/* 
Joe Tan (joetan54@gmail.com)
http://tantannoodles.com/toolkit/flickr-dhtml-badge/

Version 0.2

This is a Javascript class library used to display thumbnails. This library is
based on the popular Flickr flash badge and is in no way endorsed by Flickr or Yahoo.

Copyright (C) 2005  Joe Tan

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA

*/
/* commonly used functions, based on 1k DHTML API (http://www.dithered.com/javascript/1kdhtml/) */
d=document;l=d.layers;op=navigator.userAgent.indexOf('Opera')!=-1;px='px';
function gE(e,f){if(l){f=(f)?f:self;var V=f.document.layers;if(V[e])return V[e];for(var W=0;W<V.length;)t=gE(e,V[W++]);return t;}if(d.all)return d.all[e];return d.getElementById(e);}
function sZ(e,z){l?e.zIndex=z:e.style.zIndex=z;}
function sX(e,x){l?e.left=x:op?e.style.pixelLeft=x:e.style.left=x+px;}
function sX2(e,x){l?e.right=x:op?e.style.pixelright=x:e.style.right=x+px;}
function sY(e,y){l?e.top=y:op?e.style.pixelTop=y:e.style.top=y+px;}
function sY2(e,y){l?e.bottom=y:op?e.style.pixelBottom=y:e.style.bottom=y+px;}
function sW(e,w){l?e.clip.width=w:op?e.style.pixelWidth=w:e.style.width=w+px;}
function sH(e,h){l?e.clip.height=h:op?e.style.pixelHeight=h:e.style.height=h+px;}
function getElem(e, f) { return gE(e,f) }
function setZ(e, z) { return sZ(e,z) }
function setLeft(e, x) { return sX(e,x) }
function setRight(e, x) { return sX2(e,x) }
function setTop(e, y) { return sY(e,y) }
function setBottom(e, y) { return sY2(e,y) }
function setWidth(e, w) { return sW(e,w) }
function setHeight(e, h) { return sH(e,h) }

var BadgeAnimateRandom = function() {
    function BadgeAnimateRandom(e) {
        BadgeAnimateRandom.initialize()
    }
    var self = BadgeAnimateRandom
    function setBadge(obj) {
        this.badge = obj
    }
    function initialize() {
        BadgeAnimateFlickr.badge = this.badge
        BadgeAnimateScroll.badge = this.badge
        BadgeAnimateExplode.badge = this.badge
        this.initAnimation(this.badge.numPhotos, this.badge.numPhotos)
    }
    function animate(which, pic) {
        var rand = Math.random()
        if (rand < 0.3) {
            BadgeAnimateFlickr.animate(which, pic)
        } else if (rand < 0.6) {
            BadgeAnimateScroll.animate(which, pic)
        } else {
            BadgeAnimateExplode.animate(which,pic)
        }
    }
    
    self.badge = false
    self.animate = animate
    self.initialize = initialize
    self.setBadge = setBadge
    function initAnimation(inc, pic) {
        inc = parseInt(inc)
        pic = parseInt(pic)
        var which = this.badge.orderList[inc]
        if (pic >= this.badge.photosList.length) {
            pic = 0;
        }
        this.animate(which, pic)
        if (inc >= this.badge.orderList.length) { 
            inc = 0;
        } else {
            setTimeout("BadgeAnimateRandom.initAnimation("+(inc+1)+", "+(pic+1)+")", this.badge.delay);
        }
        
    }
    
    self.initAnimation = initAnimation
    
    return self    
}();

var BadgeAnimateFlickr = function() {
    function BadgeAnimateFlickr(e) {
        BadgeAnimateFlickr.initialize()
    }
    var self = BadgeAnimateFlickr
    
    function setBadge(obj) {
        this.badge = obj
    }

    function initialize() {
        this.initAnimation(this.badge.numPhotos, this.badge.numPhotos)
    }
    self.badge = false
    self.animate = animate
    self.initialize = initialize
    self.setBadge = setBadge

    function animate(which, pic) {
        this.animateThumb(which, pic);
    }

    /* Badge Library functions */
    
    // choose the next thumbnail to display, from a randomized list
    function initAnimation(inc, pic) {
        inc = parseInt(inc)
        pic = parseInt(pic)
        var which = this.badge.orderList[inc]
        if (pic >= this.badge.photosList.length) {
            pic = 0;
        }

        this.animate(which, pic)
        if (inc >= this.badge.orderList.length) { 
            inc = 0;
        } else {
            setTimeout("BadgeAnimateFlickr.initAnimation("+(inc+1)+", "+(pic+1)+")", this.badge.delay);
        }
        
    }
    
    function animateThumb(imgLocation, pic) {
        var img = getElem('badgeImage-'+imgLocation)
        var href = getElem('badgeHref-'+imgLocation)
        var imgTmp = getElem('badgeImage-tmp-'+imgLocation)
        imgTmp.src = img.src
        imgTmp.className = 'tmp tmp2'
        img.src = this.badge.photos[this.badge.photosList[pic]]
        href.href = this.badge.links[this.badge.photosList[pic]]
        setZ(href, 2)
        setWidth(img, this.badge.previewSize)
        setHeight(img, this.badge.previewSize)
        this.revealThumb(imgLocation, 0);
    }

    function revealThumb(which, opacity) {
        var badge = getElem('badgeHref-'+which);
        badge.className = 'set opacity'+opacity
        if (opacity < 100) {
            setTimeout("BadgeAnimateFlickr.revealThumb("+which+", "+(opacity + 10)+")", 100);
        } else {
            setTimeout("BadgeAnimateFlickr.animateThumbHelp("+which+", "+this.badge.previewSize+")", 750);
        }
    }
    
    function animateThumbHelp(which, size) {
        size = parseInt(size)
        var img = getElem('badgeImage-'+which)
        if (size > this.badge.size) {
            setWidth(img, size-1);
            setHeight(img, size-1);
            setTimeout("BadgeAnimateFlickr.animateThumbHelp('"+which+"', "+(size-2)+")", 1);
        } else {
            var href = getElem('badgeHref-'+which)
            setZ(href, 0)
            setWidth(img, this.badge.size);
            setHeight(img, this.badge.size);
        }
    }
    self.initAnimation = initAnimation
    self.animateThumb = animateThumb
    self.revealThumb = revealThumb
    self.animateThumbHelp = animateThumbHelp    
    
    return self
    
}();

var BadgeAnimateScroll = function() {
    function BadgeAnimateScroll(e) {
        BadgeAnimateScroll.initialize()
    }
    var self = BadgeAnimateScroll
    
    function setBadge(obj) {
        this.badge = obj
    }

    function initialize() {
        this.initAnimation(this.badge.numPhotos, this.badge.numPhotos)
    }
    function initAnimation(inc, pic) {
        inc = parseInt(inc)
        pic = parseInt(pic)
        var which = this.badge.orderList[inc]
        if (pic >= this.badge.photosList.length) {
            pic = 0;
        }
        this.animate(which, pic)
        //this.animateThumb(which, pic);
        if (inc >= this.badge.orderList.length) { 
            inc = 0;
        } else {
            setTimeout("BadgeAnimateScroll.initAnimation("+(inc+1)+", "+(pic+1)+")", this.badge.delay);
        }
        
    }

    self.badge = false
    self.initialize = initialize
    self.animate = animate
    self.setBadge = setBadge

    function animate(which, pic) {
        if (!this.timer) {
            if (Math.random() < 0.5) {
                this.moveRow(Math.floor(Math.random()*(this.badge.numPhotosY)), pic)
            } else {
                this.moveColumn(Math.floor(Math.random()*(this.badge.numPhotosX)), pic)
            }
        }
    }


    // choose the next thumbnail to display, from a randomized list
    function moveColumn(x, pic) {
        var dir = Math.floor(Math.random() * 4)%2==0 ? 'up' : 'down'
        //dir = 'down'
        var href, img


        var imgTmp
        for (var i=0;i<this.badge.numPhotosY;i++) {
            imgTmp = getElem('badgeImage-tmp-'+((i*this.badge.numPhotosX)+x))
            setZ(imgTmp, 2)
        }
        if (dir == 'up') {
            for (var i=0;i<this.badge.numPhotosY-1;i++) {
                img = getElem('badgeImage-'+((i*this.badge.numPhotosX)+x))
                imgTmp = getElem('badgeImage-tmp-'+(((i+1)*this.badge.numPhotosX)+x))
                img.src = imgTmp.src
                img.className = 'opacity0'
            }
            // photo spot to rotate into
            href = getElem('badgeHref-'+(x+((this.badge.numPhotosY-1) * this.badge.numPhotosX)))
            img = getElem('badgeImage-'+(x+((this.badge.numPhotosY-1) * this.badge.numPhotosX)))
        } else {
            for (var i=1;i<this.badge.numPhotosY;i++) {
                img = getElem('badgeImage-'+((i*this.badge.numPhotosX)+x))
                imgTmp = getElem('badgeImage-tmp-'+(((i-1)*this.badge.numPhotosX)+x))
                img.src = imgTmp.src
                img.className = 'opacity0'
            }

            href = getElem('badgeHref-'+x)
            img = getElem('badgeImage-'+x)
        }
                
        this.oldHref = href.href
        img.src = this.badge.photos[this.badge.photosList[pic]]
        href.href = this.badge.links[this.badge.photosList[pic]]

        this.moveColumnHelp(x, this.badge.size, dir)
    }
    
    function moveColumnHelp(x, dist, dir) {
        var columnHref
        var columnImg
        var columnImgTmp
        if (dir == 'up') {
            for (var i=0;i<this.badge.numPhotosY;i++) {
                columnImgTmp = getElem('badgeImage-tmp-'+((i*this.badge.numPhotosX)+x))
                setTop(columnImgTmp, columnImgTmp.offsetTop - 1)
                
            }
        } else {
            for (var i=0;i<this.badge.numPhotosY;i++) {
                columnImgTmp = getElem('badgeImage-tmp-'+((i*this.badge.numPhotosX)+x))
                setTop(columnImgTmp, columnImgTmp.offsetTop + 1)
                
            }
        }
        if (dist > 0) {
            this.timer = setTimeout("BadgeAnimateScroll.moveColumnHelp("+x+", "+(dist-1)+", '"+dir+"')", 10)
        } else {
            if (dir == 'up') {
                for (var i=0;i<this.badge.numPhotosY-1;i++) {
                    columnHref = getElem('badgeHref-'+((i*this.badge.numPhotosX)+x))
                    columnImg = getElem('badgeImage-'+((i*this.badge.numPhotosX)+x))
                    columnImgTmp = getElem('badgeImage-tmp-'+((i*this.badge.numPhotosX)+x))
                    
                    var tmpImg = getElem('badgeImage-tmp-'+(((i+1)*this.badge.numPhotosX)+x))
                    var tmpHref = getElem('badgeHref-'+(((i+1)*this.badge.numPhotosX)+x))
                    columnHref.href = tmpHref.href
                    setZ(columnHref, 1)
                }
            } else {
            
                for (var i=this.badge.numPhotosY-1;i>0;i--) {
                    columnHref = getElem('badgeHref-'+((i*this.badge.numPhotosX)+x))
                    columnImg = getElem('badgeImage-'+((i*this.badge.numPhotosX)+x))
                    columnImgTmp = getElem('badgeImage-tmp-'+((i*this.badge.numPhotosX)+x))
                    
                    var tmpImg = getElem('badgeImage-tmp-'+(((i-1)*this.badge.numPhotosX)+x))
                    var tmpHref = getElem('badgeHref-'+(((i-1)*this.badge.numPhotosX)+x))
                    columnHref.href = tmpHref.href
                    setZ(columnHref, 1)
                }
            }
            // reset imgTmp
            var y = 0
            for (var j=0;j<this.badge.numPhotosY;j++) {
                var imgTmp = getElem('badgeImage-tmp-'+((j*this.badge.numPhotosX)+x))
                var href = getElem('badgeHref-'+((j*this.badge.numPhotosX)+x))
                var img = getElem('badgeImage-'+((j*this.badge.numPhotosX)+x))
                imgTmp.src = img.src
                setTop(imgTmp, y)
                setZ(imgTmp, 0)
                setZ(href, 1)
                img.className='opacity100'
                y += this.badge.size + this.badge.margin
            }
            if (dir == 'up') {
                var href = getElem('badgeHref-'+(x+((this.badge.numPhotosY-2) * this.badge.numPhotosX)))
                href.href = this.oldHref
            } else {
                var href = getElem('badgeHref-'+(x+this.badge.numPhotosX))
                href.href = this.oldHref
            }
            
            this.timer = false
        }
        
    }
    

    function moveRow(y, pic) {
        var dir = Math.floor(Math.random() * 4)%2==0 ? 'left' : 'right'
        dir = 'left'
        var href, img


        var imgTmp
        for (var i=0;i<this.badge.numPhotosX;i++) {
            imgTmp = getElem('badgeImage-tmp-'+((y*this.badge.numPhotosX)+i))
            setZ(imgTmp, 2)
        }
        if (dir == 'left') {
            for (var i=0;i<this.badge.numPhotosX-1;i++) {
                img = getElem('badgeImage-'+((y*this.badge.numPhotosX)+i))
                imgTmp = getElem('badgeImage-tmp-'+((y*this.badge.numPhotosX)+i+1))
                img.src = imgTmp.src
                img.className = 'opacity0'
            }
            // photo spot to rotate into
            href = getElem('badgeHref-'+((this.badge.numPhotosX-1) + (y* this.badge.numPhotosX)))
            img = getElem('badgeImage-'+((this.badge.numPhotosX-1) + (y* this.badge.numPhotosX)))
        } else {
            for (var i=1;i<this.badge.numPhotosX;i++) {
                img = getElem('badgeImage-'+((y*this.badge.numPhotosX)+i))
                imgTmp = getElem('badgeImage-tmp-'+((y*this.badge.numPhotosX)+i-1))
                img.src = imgTmp.src
                img.className = 'opacity0'
            }

            href = getElem('badgeHref-'+(y*this.badge.numPhotosX))
            img = getElem('badgeImage-'+(y*this.badge.numPhotosX))
        }
                
        this.oldHref = href.href
        img.src = this.badge.photos[this.badge.photosList[pic]]
        href.href = this.badge.links[this.badge.photosList[pic]]

        this.moveRowHelp(y, this.badge.size, dir)
    }
    
    function moveRowHelp(y, dist, dir) {
        var columnHref
        var columnImg
        var columnImgTmp
        if (dir == 'left') {
            for (var i=0;i<this.badge.numPhotosX;i++) {
                columnImgTmp = getElem('badgeImage-tmp-'+((y*this.badge.numPhotosX)+i))
                setLeft(columnImgTmp, columnImgTmp.offsetLeft - 1)
                
            }
        } else {
            for (var i=0;i<this.badge.numPhotosX;i++) {
                columnImgTmp = getElem('badgeImage-tmp-'+((y*this.badge.numPhotosX)+i))
                setLeft(columnImgTmp, columnImgTmp.offsetLeft + 1)
                
            }
        }
        if (dist > 0) {
            this.timer = setTimeout("BadgeAnimateScroll.moveRowHelp("+y+", "+(dist-1)+", '"+dir+"')", 10)
        } else {
            if (dir == 'left') {
                for (var i=0;i<this.badge.numPhotosX-1;i++) {
                    columnHref = getElem('badgeHref-'+((y*this.badge.numPhotosX)+i))
                    columnImg = getElem('badgeImage-'+((y*this.badge.numPhotosX)+i))
                    columnImgTmp = getElem('badgeImage-tmp-'+((y*this.badge.numPhotosX)+i))
                    
                    var tmpImg = getElem('badgeImage-tmp-'+((y*this.badge.numPhotosX)+i+1))
                    var tmpHref = getElem('badgeHref-'+((y*this.badge.numPhotosX)+i+1))
                    columnHref.href = tmpHref.href
                    setZ(columnHref, 1)
                }
            } else {
            
                for (var i=this.badge.numPhotosX-1;i>0;i--) {
                    columnHref = getElem('badgeHref-'+((y*this.badge.numPhotosX)+i))
                    columnImg = getElem('badgeImage-'+((y*this.badge.numPhotosX)+i))
                    columnImgTmp = getElem('badgeImage-tmp-'+((y*this.badge.numPhotosX)+i))
                    
                    var tmpImg = getElem('badgeImage-tmp-'+((y*this.badge.numPhotosX)+i-1))
                    var tmpHref = getElem('badgeHref-'+((y*this.badge.numPhotosX)+i-1))
                    columnHref.href = tmpHref.href
                    setZ(columnHref, 1)
                }
            }
            // reset imgTmp
            var x = 0
            for (var j=0;j<this.badge.numPhotosX;j++) {
                var imgTmp = getElem('badgeImage-tmp-'+((y*this.badge.numPhotosX)+j))
                var href = getElem('badgeHref-'+((y*this.badge.numPhotosX)+j))
                var img = getElem('badgeImage-'+((y*this.badge.numPhotosX)+j))
                imgTmp.src = img.src
                setLeft(imgTmp, x)
                setZ(imgTmp, 0)
                setZ(href, 1)
                img.className='opacity100'
                x += this.badge.size + this.badge.margin
            }
            if (dir == 'left') {
                var href = getElem('badgeHref-'+((y*this.badge.numPhotosX)+(this.badge.numPhotosX-2)))
                href.href = this.oldHref
            } else {
                var href = getElem('badgeHref-'+((y*this.badge.numPhotosX)+1))
                href.href = this.oldHref
            }
            
            this.timer = false
        }
        
    }
    
    self.moveColumn = moveColumn
    self.moveColumnHelp = moveColumnHelp
    self.moveRow = moveRow
    self.moveRowHelp = moveRowHelp
    self.initAnimation = initAnimation
    return self

}();

var BadgeAnimateExplode = function() {
    function BadgeAnimateExplode(e) {
        BadgeAnimateExplode.initialize()
    }
    var self = BadgeAnimateExplode
    
    function setBadge(obj) {
        this.badge = obj
    }

    function initialize() {
        this.initAnimation(this.badge.numPhotos, this.badge.numPhotos)
    }
    function initAnimation(inc, pic) {
        inc = parseInt(inc)
        pic = parseInt(pic)
        var which = this.badge.orderList[inc]
        if (pic >= this.badge.photosList.length) {
            pic = 0;
        }
        this.animate(which, pic)
        //this.animateThumb(which, pic);
        if (inc >= this.badge.orderList.length) { 
            inc = 0;
        } else {
            setTimeout("BadgeAnimateExplode.initAnimation("+(inc+1)+", "+(pic+1)+")", this.badge.delay);
        }
        
    }

    function animate(which, pic) {
        this.explodeThumb(which, pic);
    }
    self.badge = false
    self.initialize = initialize
    self.initAnimation = initAnimation
    self.animate = animate
    self.setBadge = setBadge

    function explodeThumb(imgLocation, pic) {
        var img = getElem('badgeImage-'+imgLocation)
        var href = getElem('badgeHref-'+imgLocation)
        var imgTmp = getElem('badgeImage-tmp-'+imgLocation)
        imgTmp.src = img.src
        imgTmp.className = 'tmp tmp2'
        imgTmp._origOffsetLeft = imgTmp.offsetLeft
        imgTmp._origOffsetTop = imgTmp.offsetTop
        setZ(imgTmp, 2)

        img.src = this.badge.photos[this.badge.photosList[pic]]
        href.href = this.badge.links[this.badge.photosList[pic]]
        this.explodeThumbHelp(imgLocation, 100, this.badge.size);
    }

    function explodeThumbHelp(which, opacity, size) {
        var badge = getElem('badgeHref-'+which)
        var img = getElem('badgeImage-'+which)
        var imgTmp = getElem('badgeImage-tmp-'+which)
        
        setWidth(imgTmp, size)
        setHeight(imgTmp, size)
        setLeft(imgTmp, imgTmp.offsetLeft - 1)
        setTop(imgTmp, imgTmp.offsetTop - 1)


        var opacity2 = 100 - opacity
    
        imgTmp.className = 'tmp tmp2 opacity'+opacity
        badge.className = 'set opacity'+opacity2
        if (opacity > 0) {
            setTimeout("BadgeAnimateExplode.explodeThumbHelp("+which+", "+(opacity - 5)+", "+(size+2)+")", 50);
        } else {
            badge.classname = 'set opacity100'
            imgTmp.classname = 'tmp tmp2 opacity100'
            imgTmp.src = img.src
            setWidth(imgTmp, this.badge.size)
            setHeight(imgTmp, this.badge.size)
            setLeft(imgTmp, imgTmp._origOffsetLeft)
            setTop(imgTmp, imgTmp._origOffsetTop)
            setZ(imgTmp, 1)
            setZ(badge, 2)
        }
    }
    self.explodeThumb = explodeThumb
    self.explodeThumbHelp = explodeThumbHelp
        
    return self
}();


var Badge = function() {
    function Badge(e){
    }
    var self = Badge
    
    /*
       Public methods 
    */
    function addPhoto(src, link) {
        this.photos[this.photos.length] = src
        this.links[this.links.length] = link
        var img = new Image()
        img.src = src
        this.images[this.images.length] = img
        
    }
    function setSize(size) {
        this.size = size
    }
    function setMargin(margin) {
        this.margin = margin
    }
    function setDelay(delay) {
        this.delay = delay
    }
    function setLinkTarget(target) {
        this.target = target
    }
    
    function initialize(id) {
        this.badge = getElem(id)
        if (!this.size) this.size = 48
        if (!this.margin) this.margin = 2
        if (!this.delay) this.delay = 5000
        if (!this.target) this.target = "_top"
        
        this.previewSize = (this.size*2) +  this.margin; // previews are twice as big
        this.width = this.badge.offsetWidth // width of badge div
        this.height = this.badge.offsetHeight // height of badge div
        
        this.absWidth = Math.floor(this.width/this.size)*this.size // actual width of images
        this.absHeight = Math.floor(this.height/this.size)*this.size
        
        this.numPhotosX = Math.floor(this.width/this.size)
        this.numPhotosY = Math.floor(this.height/this.size)
        this.numPhotos = this.numPhotosX*this.numPhotosY // num photos contained in badge at one time
        
        // adjust with margins
        if ((this.absWidth + ((this.numPhotos-1)*this.margin)) > this.width) {
        }
        if ((this.absHeight + ((this.numPhotos-1)*this.margin)) > this.height) {
        }
        
        
        //alert(this.numPhotos)
        this.orderList = this.getOrderList(this.numPhotos, 10) // only loop thru photos 10 times
        this.photosList = this.getOrderList(this.photos.length, 10)
        
        var loading = document.createElement('div')
        loading.id=id+'-loading'
        loading.appendChild(document.createTextNode('loading...'))
        this.badge.appendChild(loading)
        
        this.layoutPictures()
    }

    function setAnimation(library) {
        library.setBadge(this)
        this.animate = library
    }
    function animate() { /* place holder stubb */ }

    
    
    /*
        Private methods - you shouldn't have to call any of these functions
    */
    function layoutPictures() {
        //var ahrefs = this.badge.getElementsByTagName('a');
        var x = 0;
        var y = 0;
        var ahref, img, imgTmp
        
        for (var i=0; i<this.numPhotos; i++) {
            imgTmp = document.createElement('img')
            imgTmp.id = 'badgeImage-tmp-'+i
            imgTmp.className = 'tmp'
            imgTmp.src = this.photos[this.photosList[i]]
            setWidth(imgTmp, this.size)
            setHeight(imgTmp,this.size)
            this.badge.appendChild(imgTmp)
            
        
            ahref = document.createElement('a')
            ahref.href = this.links[this.photosList[i]]
            ahref.id = 'badgeHref-'+i
            ahref.target = this.target
            //ahref.className = 'set'

            img = document.createElement('img')
            img.src = this.photos[this.photosList[i]]
            img.id = 'badgeImage-'+i
            setWidth(img, this.size)
            setHeight(img, this.size)
                        
            ahref.appendChild(img)
            this.badge.appendChild(ahref)
        
            if ((x + this.previewSize) >= this.width) {
                setRight(imgTmp, (this.width - (x + this.size)));
                setRight(ahref, (this.width - (x + this.size)));
            } else {
                setLeft(imgTmp, x);
                setLeft(ahref, x);
            }
            if ((y + this.previewSize) >= this.height) {
                setBottom(imgTmp, (this.height - (y + this.size)));
                setBottom(ahref, (this.height - (y + this.size)));
            } else {
                setTop(imgTmp, y);
                setTop(ahref, y);
            }

            x += this.size + this.margin;
            if ((x+this.size) > this.width) {
                x = 0;
                y += this.size + this.margin;
            }            
            // set variables 
            
            // show image
           // ahref.className = 'set'

        }


        //this.initAnimation(0, 0)
        var loading = getElem(this.badge.id + '-loading')
        loading.className='hidden'

        this.revealPhotosStart(0)
    }
    
    // fade in initial photo set
    function revealPhotosStart(inc) {
        inc = parseInt(inc)
        var which = this.orderList[inc]
        if (inc >= this.numPhotos) {
        //this.animate()
        //    setTimeout("Badge.initAnimation("+(inc)+", "+(this.numPhotos)+")", 2000)
        setTimeout("Badge.animate()", 2000)
        } else {
            this.revealPhotosStartHelp(which, 0)
            setTimeout("Badge.revealPhotosStart("+(inc+1)+")", 1)
        }
    }
    function revealPhotosStartHelp(which, opacity) {
        var badge = getElem('badgeHref-'+which);
        badge.className = 'set opacity'+opacity
        if (opacity < 100) {
            setTimeout("Badge.revealPhotosStartHelp("+which+", "+(opacity + 10)+")", 100);
        } else {
            var img = getElem('badgeImage-tmp-'+which)
            img.className = 'tmp tmp2'
        }
        
    }


    function _getNextZ() {
        return this._z++;
    }
    
    // determine the thumbnail order from which to randomly place the next thumbnail
    function getOrderList(max, times) {
        var rand = Array();
        var seed = Array();
        for (var i=0;i<max;i++) {
            seed[i] = i
        }
        for (var j=0;j<times;j++) {
            seed.sort(Badge.shuffle)
            for (var i=0;i<seed.length;i++) {
                rand[((j*seed.length)+i)] = seed[i]
            }    
        }

        return rand;
    }
    function shufflePhotos() {
    }
    
    // randomize shuffle
    function shuffle(list) {
        return (Math.round(Math.random())-0.5);
    }
    
    // methods
    self.initialize = initialize

    self.shuffle = shuffle
    self.animate = animate
    
    self.addPhoto = addPhoto
    self.setSize = setSize
    self.setMargin = setMargin
    self.setDelay = setDelay
    self.setLinkTarget = setLinkTarget
    self.setAnimation = setAnimation

    self.layoutPictures = layoutPictures
    self.revealPhotosStart = revealPhotosStart
    self.revealPhotosStartHelp = revealPhotosStartHelp

    
    // properties
    self.photos = Array()
    self.links = Array()
    self.images = Array()
    self.target = false
    
    self.numPhotosX = 0
    self.numPhotosY = 0
    self.numPhotos = 0
    
    self.badge = false
    self.orderList = false
    self.photosList = false

    self.getOrderList = getOrderList
    self._getNextZ = _getNextZ
    self._z = 5;
    
    return self;
}();

