var WAM = {
  active : false,
  gallery : null,

  load : function() {
    var i, id, url;
    gallery = new Gallery(AJS.$('gallery_image'),'simpleFadeTransition');
    gallery.onImageCalc = function(image) {
      return image;
    };
    id = gallery.addCategory();
    for (i in galleryData) {
        url = galleryData[i].src;
        url = url.replace(/&amp;/g, '&');
        gallery.addImage(id, url,galleryData[i].width,galleryData[i].height);
    }
    gallery.setCategory(id);
    var elms = AJS.$bytc('a','gallery_image');
    for(i in elms) {
        elms[i].href = "#";
        AJS.AEV(elms[i], "click", AJS.bind(WAM.setImage, WAM, [elms[i]]))
        //elms[i].addEventListener('click', WAM.setImage, false);
    }
    WAM.gallery = gallery;

    
  },

  setImage : function (event, img) {
    if (WAM.active) {
        return;
    }
    WAM.active = true;
    event = event || window.event;
    img = img.id.replace('img_', '');
    new AJS.fx.fadeOut(AJS.$('gallery_legend'), {onComplete : function () {
        AJS.$('gallery_legend').innerHTML = galleryData[WAM.gallery.getCurrentImage()].legend;
        AJS.fx.fadeIn(AJS.$('gallery_legend'), {onComplete : function () {
            WAM.active = false;
        }});
    }});
    AJS.$('img_' + WAM.gallery.getCurrentImage()).className = 'gallery_image regular';
    AJS.$('img_' + img).className = 'gallery_image';
    WAM.gallery.setImage(img);
  },

  openPop : function(url, table, column, id, width, height) {
    url += 'admin/imgwindow.php?table=' + table + '&column=' + column + '&id=' + id + '&Bild&fuman_popupwidth=' + width + '&fuman_popupheight=' + height;
    var pop = window.open(url, "Bild", 'width=' + width + ',height=' + height + ',status=no,scrollbars=no,resizable=no');
    pop.focus();

  }
};

var WAMIMG = {

  fx : null,

  init : function() {
    var images, i, id;
    images = AJS.$bytc('a', 'project_list_image');
    for (i in images) {
        id = images[i].id.replace('img_', '');
        AJS.AEV(images[i], 'mouseover',  AJS.bind(WAMIMG.show, WAMIMG, [id, images[i]]), false, false);
        AJS.AEV(images[i], 'mouseout', AJS.bind(WAMIMG.closeImage, WAMIMG));
    }
  },

  show : function(event, id, elm) {
    var url, pos;
    url = document.fumanHttpRoot + 'dbproxy.php?table=project_image&column=image&id=' + id;
    pos = AJS.absolutePosition(elm);
    var loader = new ImageLoader(url);
    loader.loadEvent = AJS.bind(WAMIMG.loadImageComplete, WAMIMG, [pos]);
    loader.load();
  },

  loadImageComplete : function(src,image, pos) {
    AJS.$('lightbox_img').src = src;
    AJS.$('lightbox').style.top = (pos.y - image.height - 15) + 'px';
    AJS.$('lightbox').style.left = (pos.x - (image.width / 2) + 31) + 'px';
    if (WAMIMG.fx) {
        WAMIMG.fx.clearTimer();
    }
    AJS.setOpacity(AJS.$('lightbox'), 0);
    AJS.$('lightbox').style.display = 'block';
    WAMIMG.fx = AJS.fx.fadeIn(AJS.$('lightbox'));
  },

  closeImage : function(event) {
    if (WAMIMG.fx) {
        WAMIMG.fx.clearTimer();
    }
    WAMIMG.fx = AJS.fx.fadeOut(AJS.$('lightbox'), {onComplete : AJS.bind(WAMIMG.reset, WAMIMG)})
  },

  reset : function() {
     AJS.$('lightbox').style.display = 'none';
     AJS.$('lightbox').style.top = '0px';
     AJS.$('lightbox').style.left = '0px';
  }

  
};
