/*
   Filename: image_gallery.js
   Author:   Falko Zander, falko@falko-zander.de
   Created:  2007-11-04
   Update:   2007-11-26

   Abstract: image gallery control

   Notes:
*/

var image_gallery_div_ = null;
var image_gallery_collection_ = [];

function image_gallery(id, parentid) {
    if (this.registered_(id) != -1) return;

    this.id = id || "";
    this.parentid = parentid || "";

    var e = $(this.id);
    if (e == null) error("image_gallery: Your ID does not exists: " + id);

    var img = e.getElementsByTagName("img");
    if (img.length == 0) error("image_gallery: No images in gallery found: " + id);

    var data = [];
    this.max_width = 0;
    this.max_height = 0;
    for (var i = 0; i < img.length; i++) {
        var info = {
            src: img[i].src,
            alt: img[i].alt,
            width: parseInt(img[i].width),
            height: parseInt(img[i].height)
        };
        if (info.width > this.max_width) this.max_width = info.width;
        if (info.height > this.max_height) this.max_height = info.height;
        img[i].style.display = "none";
        data.push(info);
    }

    this.elem = e;
    this.parent = $(this.parentid) || null;
    this.images = data;
    this.active_img = 0;
    image_gallery_collection_.push(this);
    
    var THIS = this;

    var span = e.getElementsByTagName("span")[0];
    if (typeof(span) == 'undefined') error("image_gallery: You have to define a span-section in your gallery: " + id);
    span.innerHTML = "<a href='javascript:void(0);' class='open'>" + e.title + "</a><br />" + span.innerHTML;
    span.onclick = function() { THIS.open_(); }

    this.fader = null;
}

/* end of public interface */

image_gallery.prototype.registered_ = function(id) {
    for (var i = image_gallery_collection_.length - 1; i >= 0; i--) {
        if (image_gallery_collection_[i].id == id) return i;
    }
    return -1;
}

image_gallery.prototype.draw_ = function(nbr, reposition) {
    if (this.elem == null) error("image_gallery: elem undefined");
    if (typeof(reposition) == 'undefined') reposition = 0;

    var data = this.images[nbr];
    if (typeof(data) == 'undefined') return;
    var p = this.parent;
    var w = data.width;
    var h = data.height;
    var s = data.src;
    var a = data.alt;

    if (this.fader == null) {
        this.fader = ViewUtils.show_fader();
    }

    var e = image_gallery_div_;
    if (e == null) {
        e = document.createElement("div");
        document.body.appendChild(e);
        e.id = "image_gallery";
        e.style.zIndex = '2';
        image_gallery_div_ = e;
    } else {
        e.style.display = "block";
    }

    e.style.width = w + 30 + "px";
    e.style.height = h + 50 + "px";
    if (reposition == 1) {
        if (p != null) {
            var d_p = getAbsoluteDimensions(p);
            e.style.left = (p.offsetWidth - w) / 2 + d_p.offsetLeft + "px";
            e.style.top = (p.offsetHeight - h) / 2 + d_p.offsetTop + "px";
        } else {
            e.style.left = ((window.innerWidth || document.body.clientWidth) - w) / 2 + "px";
            e.style.top = ((window.innerHeight || document.body.clientHeight) - h) / 2 + "px";
        }
    }

    var THIS = this;
    e.innerHTML = "<img id='" + this.id + "_img" + "' src='" + s + "' width='" + w + "' height='" + h + "' alt='" + a + "' />"
                + "<table style='width:" + w + "px'><tr>"
                + "<td style='width:" + (w - 48) + "px' id='label'>" + a + "</td><td>"
                + "<img id='" + this.id + "_left' src='/images/basic/arrow_left.png' width='12' height='12' alt='left' />"
                + "</td><td>"
                + "<img id='" + this.id + "_right' src='/images/basic/arrow_right.png' width='12' height='12' alt='right' />"
                + "</td><td>"
                + "<img id='" + this.id + "_close' src='/images/basic/close.png' width='12' height='12' alt='close' />"
                + "</td></tr></table>";
    
    if (this.active_img != 0) {
        var el = $(this.id + '_left');
        el.onmouseover = function() { this.src='/images/basic/arrow_left_hover.png'; };
        el.onmouseout = function() { this.src='/images/basic/arrow_left.png'; };
        el.onclick = function() { THIS.left_(); };
    }
    
    if (this.active_img != this.images.length - 1) {
        var er = $(this.id + '_right');
        er.onmouseover = function() { this.src='/images/basic/arrow_right_hover.png'; };
        er.onmouseout = function() { this.src='/images/basic/arrow_right.png'; };
        er.onclick = function() { THIS.right_(); };
    }
    
    var ec = $(this.id + '_close');
    ec.onmouseover = function() { this.src='/images/basic/close_hover.png'; };
    ec.onmouseout = function() { this.src='/images/basic/close.png'; };
    ec.onclick = function() { THIS.close_(); };
    
    this.dragdrop = new cDndWidget(e, $(this.id + '_img'));
}

image_gallery.prototype.open_ = function() {
    this.active_img = 0;
    this.draw_(0, 1);
}

image_gallery.prototype.left_ = function() {
    this.active_img--;
    this.draw_(this.active_img);
}

image_gallery.prototype.right_ = function() {
    this.active_img++;
    this.draw_(this.active_img);
}

image_gallery.prototype.close_ = function() {
    this.fader = ViewUtils.remove_fader(this.fader);
    if (image_gallery_div_ != null) {
        image_gallery_div_.style.display = 'none';
    }
}

/*
   Copyright Notice:

   Copyright 2007, Falko Zander
   ALL RIGHTS RESERVED

   UNPUBLISHED -- Use of a copyright notice is precautionary only and
   does not imply publication or disclosure.

   THE CONTENT OF THIS WORK CONTAINS CONFIDENTIAL AND PROPRIETARY
   INFORMATION OF FALKO ZANDER. ANY DUPLICATION, MODIFICATION,
   DISTRIBUTION, OR DISCLOSURE IN ANY FORM, IN WHOLE, OR IN PART, IS
   STRICTLY PROHIBITED WITHOUT THE PRIOR EXPRESS WRITTEN PERMISSION OF
   FALKO ZANDER.
*/

