var imageviewers = {};

var ListingImageViewer = Class.create({
    aiv_main : null,
    listingid : null,

    images : null,
    comments : null,
    current : null,
    initialized : false,

    references : {
        'images' : null,
        'comments' : null,
        'current' : null,
        'thumbs' : null,
        'image' : null
    },

    initialize : function(parentpanel) {
        if (parentpanel.id == undefined)
            parentpanel = $("aivfor_" + parentpanel);
        this.aiv_main = parentpanel;

        this.references['images'] = parentpanel.getElementsByClassName("aiv_images")[0];
        this.references['current'] = parentpanel.getElementsByClassName("aiv_initial")[0];
        this.references['image'] = parentpanel.getElementsByClassName("aiv_image")[0];

        this.images = this.references['images'].value.split(",");
        this.preload();

        this.current = this.references['current'].value * 1;
        this.initialized = true;
        
        aivid = parentpanel.id.split("_").pop();
        this.listingid = aivid;
        imageviewers[aivid] = this;
        
        this.comments = $("commentsfor_" + aivid).value.split("|||");
        this.references['comments'] = $("commentfor_" + aivid); 
        
        this.jumpto(0);
    },
    preload : function() {
        for (x = 0; x < this.images.length; x++) {
            img = document.createElement("img");
            img.src = this.images[x];
        }
    },
    jumpto : function(index) {
        if (this.initialized && this.images[index]) {
            // sender
            sender = $("thumbfor_" + this.listingid + "_" + this.current);
            target = $("thumbfor_" + this.listingid + "_" + index);

            sender.removeClassName("active");
            target.addClassName("active");

            if (this.comments[index] != undefined && this.comments[index].length > 0) {
                this.references['comments'].style.display = "block";
                this.references['comments'].innerHTML = this.comments[index];
            }
            else
                this.references['comments'].style.display = "none";
            
            this.references['image'].setAttribute("src",this.images[index]);
            this.current = index;
            
            try_update_height("aivfor_" + this.listingid, this.references['image'].id);
        }
    }
});

function try_update_height(aivid, imageid) {
    if ($(imageid).height < 1) {
        setTimeout("try_update_height('" + aivid + "','" + imageid + "');",1000);
    }
    else {
        $(aivid).style.height = ($(imageid).height + 50) + "px";
    }
}

function aiv_jumpto(sender) {
    // find the sender's parent
    senderid = sender.id.split("_");
    index = senderid.pop();
    aivid = senderid.pop();
    aiv = imageviewers[aivid];
    aiv.jumpto(index);
}

/*
function aiv_advance(sender, amount) {
    while (!sender.hasClassName("aimageviewer"))
        sender = sender.parentNode;
    if (imageviewers[sender.id] != undefined) {
        imageviewers[sender.id].advance(amount);
    }
}
*/
