﻿function WDCImageGallery(elemId,wrap){
    this.container = document.getElementById(elemId);
    this.images = [];
    this.selectedIndex = null;
    this.fullImage = null;
    this.wrap = (wrap==true);
    this.left=null;
    this.right=null;
    this.maxHeight = 240;
    this.maxWidth = 360;

    this.init();
}

WDCImageGallery.prototype.init = function(){
    var li = this.container.getElementsByTagName("li");
    if(li.length > 1){
        this.fullImage = this.container.getElementsByTagName("img")[0];
        for(var i=0;i<li.length;i++){
            this.addImage(li[i].getElementsByTagName("img")[0]);
        }
        
        var controls = document.getElementById(this.container.id+"_controls");
        if(controls){
            var context = this;
            this.left = document.createElement('a');
            this.left.className = 'left';
            this.left.href = 'javascript:void(0);';
            this.left.onclick = function(){context.showPreviousImage();};
            this.right = document.createElement('a');
            this.right.className = 'right';
            this.right.href = 'javascript:void(0);';
            this.right.onclick = function(){context.showNextImage();};
            controls.appendChild(this.left);
            controls.appendChild(this.right);
        }

        this.selectImage(0);
        li[0].className='selected';
    }
};
WDCImageGallery.prototype.addImage = function(img){
    var context = this;
    img.onclick=function(){
        var i = context.images.indexOf(this);
        context.selectImage(i);
        return false;
    };
    img.parentNode.parentNode.onmouseover=function(){
        this.className+=' hover';
    }
    img.parentNode.parentNode.onmouseout=function(){
        this.className=this.className.replace('hover','');
    }
    this.images.push(img);
};

WDCImageGallery.prototype.selectImage = function(index){
    if(this.images[index]){
        if(this.selectedIndex!=null&&this.images[this.selectedIndex]){
            this.images[this.selectedIndex].parentNode.parentNode.className='';
        }
        this.selectedIndex = index;
        var img = this.images[this.selectedIndex];
        img.parentNode.parentNode.className='selected';
        var imgurl;
        if(img.parentNode.href){
            imgurl=img.parentNode.href;
        }else{
            imgurl=img.src;
        }
       
        //resize image proportionally
        var copy = new Image();
        copy.src = imgurl;
        if(ImgResize(copy, this.maxHeight, this.maxWidth))
        {
            this.fullImage.style.height = copy.height + 'px';  
            this.fullImage.style.width = copy.width + 'px';  
        }
        this.fullImage.src=imgurl;
        this.fullImage.parentNode.href=imgurl;
        this.fullImage.alt=img.alt;
        
        if(this.wrap!=true){
            if(this.selectedIndex==this.images.length-1){
                this.right.className="rightDisabled"
            } else {
                this.right.className="right"
            }
            if(this.selectedIndex==0){
                this.left.className="leftDisabled"
            } else {
                this.left.className="left"
            }
        }
    }
};

WDCImageGallery.prototype.showNextImage = function(){
    var i = this.selectedIndex + 1;
    if(i >= this.images.length){
        if(this.wrap==true){
            i = 0;
        }else{
            return;
        }
    }
    this.selectImage(i);
};

WDCImageGallery.prototype.showPreviousImage = function(){
    var i = this.selectedIndex - 1;
    if(i < 0){
        if(this.wrap==true){
            i = this.images.length-1;
        }else{
            return;
        }
    }
    this.selectImage(i);
};
