// addEvent by John Resig | http://ejohn.org/
function addEvent( obj, type, fn ) {
        if ( obj.attachEvent ) {
                obj['e'+type+fn] = fn;
                obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
                obj.attachEvent( 'on'+type, obj[type+fn] );
        } else
                obj.addEventListener( type, fn, false );
}
function removeEvent( obj, type, fn ) {
        if ( obj.detachEvent ) {
                obj.detachEvent( 'on'+type, obj[type+fn] );
                obj[type+fn] = null;
        } else
                obj.removeEventListener( type, fn, false );
}



// Unobtrusive scroller object | http://www.frequency-decoder.com
var fdScroller = {
        scrollLoop:false,
        scrollInterval:null,
        currentBlock:null,
        lastY:0,
        requestedY:0,
        init: function() {
                var lnks = document.getElementsByTagName('a');
                var removeLoc = window.location + ' ';

                // strip any #target, force a String cast
                if(removeLoc.indexOf('#') != -1) {
                        removeLoc = removeLoc.substr(0, removeLoc.indexOf('#'));
                } else {
                        removeLoc = removeLoc.substr(0, removeLoc.length - 1);
                }

                for(var i = 0, lnk; lnk = lnks[i]; i++) {
                        tmploc = lnk.href.replace(removeLoc,'');
                        if(tmploc.charAt(0) == '#' && !lnk.onclick) lnk.onclick = fdScroller.initScroll;
                }
        },
        getWindowHeight: function(){
                var myHeight = 0;
                if( typeof( window.innerWidth ) == 'number' ) {
                        //Non-IE
                        myHeight = window.innerHeight;
                } else if( document.documentElement &&
                                ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
                        //IE 6+ in 'standards compliant mode'
                        myHeight = document.documentElement.clientHeight;
                } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
                        //IE 4 compatible
                        myHeight = document.body.clientHeight;
                }
                return myHeight;
        },
        getScrollLeft: function(){
                if(document.all) return (document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft;
                else return window.pageXOffset;
        },
        getScrollTop: function(){
                if(document.all) return (document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
                else return window.pageYOffset;
        },
        getElementYpos: function(el){
                var y = 0;
                while(el.offsetParent){
                        y += el.offsetTop
                        el = el.offsetParent;
                }
                return y;
        },
        initScroll: function(e){
                var targ;

                if (!e) var e = window.event;
                if (e.target) targ = e.target;
                else if (e.srcElement) targ = e.srcElement;
                if (targ.nodeType == 3) targ = targ.parentNode;

                if(targ.href && targ.href.indexOf('#') != -1) {
                        var where = targ.href.substr(targ.href.indexOf('#')+1,targ.href.length);

                        fdScroller.currentBlock = document.getElementById(where);
                        if(!fdScroller.currentBlock) return;

                        var documentHeight = document.getElementsByTagName('body').offsetHeight;
                        var windowHeight = fdScroller.getWindowHeight();
                        var ypos = fdScroller.getElementYpos(fdScroller.currentBlock);
                        if(ypos > documentHeight - windowHeight) ypos = documentHeight - windowHeight;

                        fdScroller.requestedY = ypos;
                        fdScroller.scroll();
                }

                return false;
        },
        scroll: function(){
                var left = fdScroller.getScrollLeft();
                var top  = fdScroller.getScrollTop();

                if(Math.abs(top-fdScroller.requestedY) <= 1){
                        window.scrollTo(0,fdScroller.requestedY);
                        fdScroller.lastY = -1;
                } else {
                        fdScroller.lastY = top;
                        window.scrollTo(0, Math.round(top+((fdScroller.requestedY-top)/2)));
                        setTimeout(fdScroller.scroll,50);
                }
        }
}

// Coded by Travis Beckham | http://www.squidfingers.com | http://www.podlob.com
Detect = function(){
        var agent = navigator.userAgent.toLowerCase();
        this._mac = agent.indexOf('mac') != -1;
        this._win = !this._mac;
        this._w3c = document.getElementById;
        this._iex = document.all;
        this._ns4 = document.layers;
};
Detect.prototype.getObj = function(name){
        if(this._w3c){
                return document.getElementById(name);
        }else if(this._iex){
                return document.all[name];
        }else if(this._ns4){
                return this.getObjNS4(document,name);
        }
};
Detect.prototype.getObjNS4 = function(obj, name){
        var d = obj.layers;
        var result;
        for(var i=0; i<d.length; i++){
                if(d[i].id == name){
                        result = d[i];
                }else if(d[i].layers.length){
                        var temp = this.getObjNS4(d[i],name);
                }
                if(temp){
                        result = temp;
                }
        }
        return result;
};
Detect.prototype.getStyle = function(obj){
        return (this._ns4) ? obj : obj.style;
};
HTMLobj = function(name){
        if(name){
                this._inherit = Detect; this._inherit(name);
                this._id  = name;
                this._el  = this.getObj(this._id);
                this._css = this.getStyle(this._el);
                this._obj = name+'Object'; eval(this._obj+'=this');
                this._timer = null;
                this._fadeRunning = false;
                this._randNum = null;
                this._startFade = false;
                return this;
        }
};
HTMLobj.prototype = new Detect();

HTMLobj.prototype.setStyle = function(prop, val){
        if(!this._ns4){
                this._el.style[prop] = val;
                if(this._iex && this._mac){
                        this._el.innerHTML = this._el.innerHTML;
                }
        }
};
HTMLobj.prototype.show = function(){
        this._css.visibility = 'visible';
};
HTMLobj.prototype.hide = function(){
        this._css.visibility = 'hidden';
};
HTMLobj.prototype.setOpacity = function(opacity){
        // Fix for math error in some browsers
        opacity = (opacity == 100)?99.999:opacity;
        // IE/Windows
        this._css.filter = "alpha(opacity:"+opacity+")";
        // Safari < 1.2, Konqueror
        this._css.KHTMLOpacity = opacity/100;
        // Older Mozilla and Firefox
        this._css.MozOpacity = opacity/100;
        // Safari 1.2, newer Firefox and Mozilla, CSS3
        this._css.opacity = opacity/100;
};
HTMLobj.prototype.fadeOut = function(opacity, change, speed){
        if (opacity >= 0){
          this._fadeRunning = true;
          this.setOpacity(opacity);
          opacity -= change;
          setTimeout(this._obj+'.fadeOut('+opacity+','+change+','+speed+')', speed);
        } else {
                this._fadeRunning = false;
                this.hide();
                load_image();
        }
};
HTMLobj.prototype.fadeIn = function(opacity, change, speed){
        if (opacity <= 100){
          this.show();
          this._fadeRunning = true;
          this.setOpacity(opacity);
          opacity += change;
          setTimeout(this._obj+'.fadeIn('+opacity+','+change+','+speed+')', speed);
        } else {
                this._fadeRunning = false;
                clickability = true;
                this.setOpacity(100);
        }
};
var API = new Detect();
var clickability        = false;
var currentImg          = 1;

function commence_fadeIn() {
        clickability = false;
        API.Container.fadeIn(0,10,33);
}
function commence_fadeOut() {
        clickability = false;
        t = 0;
        API.Container.fadeOut(100,10,33);
}
function load_image(img) {
        document.images['photo'].onload = commence_fadeIn;
        document.images['photo'].src = "./media/photo_" + currentImg + ".jpg";
}
function advanceImg(img) {
        if(!clickability) return;
        currentImg = img;
        if(currentImg > 8) { currentImg = 1; }
        var lnks = document.getElementById('photolist').getElementsByTagName('a');
        for(var i = 0, lnk; lnk = lnks[i]; i++) {
                if(i != (currentImg-1)) lnk.id = "";
                else {
                        lnk.id = "current";
                }

        }
        commence_fadeOut();
}
window.onload = function() {
        // Basic runtime environment check
        if(!document.getElementById) return;
        fdScroller.init();
        API.Container = new HTMLobj('photo');
        commence_fadeIn();
}
