﻿/*
 * albumId: ID of photo album
 * nofPhotos: Number of photos in the thumbnails image
 */
AR.photoalbum.slider = function(albumId, nofPhotos) {
    this.img = document.getElementById("img_"+albumId);
    this.imglink = document.getElementById("imglink_"+albumId);
    this.imgdiv = document.getElementById("imgdiv_"+albumId);
    this.albumId = albumId;
    this.nofPhotos = nofPhotos;
    this.firstEventTime = 0;
    this.oldD = -1;

    var myGroup = new YAHOO.util.ImageLoader.group(this.imgdiv.id, 'mouseover', 2); 
    myGroup.registerBgImage(this.imgdiv.id, "/Fotos/Thumbnails/"+this.albumId);
    
    Evt.addListener(this.imgdiv, 'mousemove', this.showImage, this);
    Evt.addListener(this.imgdiv, 'mouseout', this.defaultImage, this);
};

AR.photoalbum.slider.prototype.showImage = function(e, instance) {
    Evt.preventDefault(e);
    var thisEventTime = new Date().getTime();
    YAHOO.log("bg: "+instance.imgdiv.style.backgroundImage);

    d = 0;
    if (instance.firstEventTime!=0) {
        if (thisEventTime-instance.firstEventTime>500) {
            var imgX = Dom.getX(instance.img);
            var x = (e.offsetX!=null) ? e.offsetX : e.pageX-imgX;
            d = Math.floor((instance.nofPhotos)*x/200.0);
        }
    } else {
        instance.firstEventTime = thisEventTime;
    }
    if (instance.oldD != d) {
        instance.img.style.visibility = "hidden";
        instance.imgdiv.style.backgroundPosition = "0px -"+(d*200)+"px";
        instance.imglink.href = "/Fotos/"+instance.albumId+"/"+d;
        instance.oldD = d;
    }
};

AR.photoalbum.slider.prototype.defaultImage = function(e, instance) {
    Evt.preventDefault(e);
    var imgX = Dom.getX(instance.img);
    var imgY = Dom.getY(instance.img);
    var x = (e.offsetX!=null) ? e.offsetX : e.pageX-imgX;
    var y = (e.offsetY!=null) ? e.offsetY : e.pageY-imgY;
    if (x<=0 || y<=0 || x>=200 || y>=200) {
        YAHOO.log("Setting main image visible");
        instance.img.style.visibility = "visible";
        instance.imgdiv.style.backgroundPosition = "";
        instance.imglink.href = "/Fotos/"+instance.albumId;
        instance.firstEventTime = 0;
        instance.oldD = -1;
    }
};
