// PRELOADS ALL THE BUTTONS NEEDED
// Works: IE4+, NS4+, Opera

function preButtons() {
    loadingFlag = true;
    
    // footer preloads
    createObject('heartoff','/_img/generic/foot_heart_off.gif');
    createObject('hearton','/_img/generic/foot_heart_on.gif');
    createObject('antioff','/_img/generic/foot_anti_off.gif');
    createObject('antion','/_img/generic/foot_anti_on.gif');
    createObject('catoff','/_img/generic/foot_cat_off.gif');
    createObject('caton','/_img/generic/foot_cat_on.gif');
    
    // button preloads
    createObject('btnsubmitoff','/_img/buttons/submit_off.gif');
    createObject('btnsubmiton','/_img/buttons/submit_on.gif');
    createObject('btnprevoff','/_img/buttons/previous_off.gif');
    createObject('btnprevon','/_img/buttons/previous_on.gif');
    createObject('btnnextoff','/_img/buttons/next_off.gif');
    createObject('btnnexton','/_img/buttons/next_on.gif');
    createObject('btnmoreloff','/_img/buttons/more_light_off.gif');
    createObject('btnmorelon','/_img/buttons/more_light_on.gif');
        
    loadingFlag = false;
    preloadFlag = true;
}

var PhotosPagination = new Class({
    initialize: function() {
        this.clickEvent = this.handleClick.bindWithEvent(this);
        //this.scrollEffect = new Fx.Scroll(window, {transition: Fx.Transitions.Cubic.easeInOut, duration: 800});
        this.scrollEffect = new Fx.Scroll(window);
        
        this.loader = new Element("div", {"class": "photospageloading"});
        this.loader.setText("Loading...");
        
        this.findButton();
    },
    handleClick: function(e) {
        e.preventDefault();
        
        this.loader.injectAfter(this.btn);
        this.btn.remove();
        
        this.loadPage(this.btn.getProperty("href") + "&ajax=1");
    },
    loadPage: function(url) {
        if (this.pageRequest) {
            this.pageRequest.cancel();
        }
        this.pageRequest = new Ajax(url, {
            method: "get",
            onComplete: this.handleLoadPage.bind(this),
            onFailure: this.handleLoadFailed.bind(this)
        }).request();
    },
    handleLoadPage: function(result) {
        var scrollPoint = this.loader.getTop();
        var windowCurrentPoint = window.getScrollTop();
        
        var p = this.loader.getParent();
        this.loader.remove();
        
        var temp = new Element("div");
        temp.innerHTML = result;
        temp.getChildren().each(function (el) {
            el.injectInside(p);
        })
        
        this.scrollEffect.scrollTo(0, scrollPoint);
        
        this.onRefresh();
        this.findButton();
    },
    handleLoadFailed: function() {
        this.loader.remove();
    },
    onRefresh: function() {
        
    },
    findButton: function() {
        if (this.btn) this.btn.removeEvents("click");
        this.btn = $E(".photosmorebtn");
        if (this.btn) {
            this.btn.addEvent("click", this.clickEvent);
        } 
    }
})

var TextFieldPlaceHolder = new Class({
    initialize: function(el, placeHolder) {
        this.el = $(el);
        this.placeHolder = placeHolder;
        this.isEmpty = true;
        this.el.addEvents({
            focus: this.handleFocus.bindWithEvent(this),
            blur: this.handleBlur.bindWithEvent(this),
            change: this.handleChange.bindWithEvent(this)
        });
        
        $(this.el.form).addEvents({
            submit: this.handleFormSubmit.bindWithEvent(this)
        })
        
        this.addPlaceHolder();
    },
    handleFocus: function(e) {
        if (this.isEmpty) {
            this.removePlaceHolder();
        }
    },
    handleBlur: function(e) {
        if (this.el.getProperty("value") == "") {
            this.addPlaceHolder();
        }
    },
    handleChange: function(e) {
        if (this.el.getProperty("value") != "") {
            this.isEmpty = false;
        }
    },
    handleFormSubmit: function(e) {
        if (this.isEmpty) {
            this.removePlaceHolder();
        }
    },
    addPlaceHolder: function () {
        this.isEmpty = true;
        this.el.addClass("placeholder");
        this.el.setProperty("value", this.placeHolder);
    },
    removePlaceHolder: function() {
        this.el.removeClass("placeholder");
        this.el.setProperty("value", "");
    }
    
})

var Photobox = new Class({
    initialize: function() {
        this.container = new Element("div", {id: "photobox"});
        this.navRequest = new Ajax("/photos/nav", {onComplete: this.loadedNav.bind(this)})
        this.navRequest.request();
        this.loadPhotoEvent = this.loadedPhoto.bind(this);
    },
    loadedNav: function(txt) {
        this.container.setHTML(txt);
        this.container.inject($E("body"));
        $("photooverlay").addEvent("click", this.hide.bindWithEvent(this));
        $("photolarge").addEvent("click", this.hide.bindWithEvent(this))
        $("photoclosebtn").addEvent("click", this.hide.bindWithEvent(this))
        $("photonextbtn").addEvent("click", this.next.bindWithEvent(this));
        $("photoprevbtn").addEvent("click", this.prev.bindWithEvent(this));
        $("photosharebtn").addEvents({
            click: function(e) {
                e.preventDefault();
                return addthis_open($("photosharebtn"), 'more', $("photosharebtn").getProperty("href") || '', $("photosharebtn").getProperty("title") || '');
            }
        })
        
        this.navRequest = null;
        this.updateLinks();
    },
    updateLinks: function() {
        if (this.navRequest != null) return;
        
        this.links = $$("a[rel=photobox]");
        
        for (var i=0; i < this.links.length; i++) {
            this.links[i].removeEvents("click");
            this.links[i].addEvent("click", this.clickLink.bindWithEvent(this, this.links[i]));
        }
    },
    clickLink: function(e, target) {
        if (e.shift || e.control || e.alt || e.meta) {
            return
        } else {
            e.preventDefault();
        }
        
        var n = this.links.indexOf(target);
        // make sure we actually have a link
        if (n >= 0) {
            this.getPhoto(n);
        }
    },
    getPhoto: function(n) {
        this.selectedIndex = n;
        this.selectedLink = this.links[this.selectedIndex];
        
        var photoDiv = this.selectedLink.getParent();
        
        var data = {};
        data.shareLink = this.selectedLink.getProperty("href");
        data.img = this.selectedLink.getElement("img").getProperty("src").replace(/_m(?=\.\w+$)/i, "");
        data.title = photoDiv.getElement(".thumbtitle").getText();
        data.date = photoDiv.getElement(".photodate").getText();
        data.download = photoDiv.getElements(".photodownload a").clone();
        
        this.show();
        
        this.updatePhoto(data);
    },
    loadedPhotoData: function(txt) {
        var data = Json.evaluate(txt);
        this.photoRequest = null;
        this.updatePhoto(data);
    },
    updatePhoto: function(data) {
        this.cancelPhoto();
        
        $("photoloader").setStyle("display", "block");
        
        $("photosharebtn").setProperty("href", data.shareLink);
        $("photosharebtn").setProperty("title", data.title);
        
        $E("#photonavbar h1").setText(data.title);
        $E("#photonavbar .photodate").setText(data.date);
        var photoDownload = $E("#photonavbar .photodownload");
        photoDownload.setHTML("Download: ");
        for (var i = 0; i < data.download.length; i++) {
            data.download[i].inject(photoDownload);
        }
        
        this.preloadedImage = new Element("img");
        this.preloadedImage.addEvent("load", this.loadedPhoto.bind(this));
        this.preloadedImage.setProperty("src", data.img);
        
    },
    loadedPhoto: function() {
        this.clearPhoto();
        $("photoloader").setStyle("display", "none");

        this.preloadedImage.inject($("photolarge"));
        var photoHeight = $("photolarge").getSize().size.y;
        var navBarHeight = $("photonavbar").getSize().size.y;
        var stageHeight = window.getHeight() - navBarHeight;
        
        var photoTop = Math.max(Math.round((stageHeight - photoHeight) * 0.5) + window.getScrollTop(), 0);
        
        $("photolarge").setStyle("height", (photoHeight + navBarHeight) + "px");
        $("photolarge").setStyle("top", photoTop + "px");
        
        this.preloadedImage = null;
    },
    show: function() {
        this.container.setStyle("display", "block");
        $$("object", "select", "embed").each(function(el) {
            el.style.visibility = "hidden";
        })
    },
    hide: function(e) {
        if (e) e.preventDefault();
        if (e == null || e.target.getTag() != "img") {
            $("photoloader").setStyle("display", "none");
            this.container.setStyle("display", "none");
            $$("object", "select", "embed").each(function(el) {
                el.style.visibility = "visible";
            })
            this.clearPhoto();
        }
    },
    next: function(e) {
        if (e) e.preventDefault();
        
        var n = this.selectedIndex + 1;
        if (n < this.links.length) {
            this.getPhoto(n);
        } else {
            this.hide();
        }
    },
    prev: function(e) {
        if (e) e.preventDefault();
        var n = this.selectedIndex - 1;
        if (n >= 0) {
            this.getPhoto(n);
        } else {
            this.hide();
        }
    },
    clearPhoto: function() {
        $("photolarge").setHTML("");
        $("photolarge").setStyle("height", "auto");
    },
    cancelPhoto: function() {
        if (this.preloadedImage) {
            this.preloadedImage.removeEvent("load", this.loadPhotoEvent);
            this.preloadedImage = null;
        }
    }
})

var addthis_pub="epitaphrecords";
var addthis_offset_left = -10000;

window.addEvent("domready", function () {
    
    var photosPagination  = new PhotosPagination();
    
    if (!window.ie6) {
        var photoBox = new Photobox();
        photosPagination.onRefresh = function() {
            photoBox.updateLinks();
        }
    } else {
        $$("a[rel=photobox]").addEvent("click", function(e){
            window.location.href = $(this).getProperty("href");
        })
    }
    
    // share link code for photo view page
    var shareLink = $E(".photoview .photosharelink");
    if (shareLink) {
        shareLink.addEvents({
            "click": (function(e) {
                e.preventDefault();
                addthis_open($E(".photoview .photosharelink"), 'more', '', '');
            }).bindWithEvent(shareLink)
        })
    }
    
    if ($("photosearchfield")) new TextFieldPlaceHolder($("photosearchfield"), "Show me photos of...");
    
})

