
function SlideShowViewer (elementId, menuBarId, imageUrlList)
{
    this.fadeTime = 3000;
    this.showTime = 5000;
    this.container = jQuery('#'+elementId);
    this.menu = jQuery('#'+menuBarId);
    this.imageUrlList = imageUrlList;
    this.imageCount = imageUrlList.length;
    this.currentIndex = 0;
    this.preLoadedCount = 0;
    this.isPreLoaded = false;
    this.imageList = Array;
    thisViewer = this;
    this.playAction = function () {
        thisViewer._nextSlide();
    }
    this.timer = null;
}

SlideShowViewer.prototype.init = function ()
{
    thisObject = this;
    thisObject.menu.find('#menu_home_bg').css('opacity', '0.7');
    thisObject.menu.find('#real-estate_item_cell a').bind('mouseover', function(event) {
        thisObject._hoverMenuItem('real-estate');
    });
    thisObject.menu.find('#lifestyle-brands_item_cell a').bind('mouseover', function(event) {
        thisObject._hoverMenuItem('lifestyle-brands');
    });
    thisObject.menu.find('#equity-ventures_item_cell a').bind('mouseover', function(event) {
        thisObject._hoverMenuItem('equity-ventures');
    });
    thisObject.menu.find('a').bind('mouseout', function(event) {
        thisObject.unpause();
    });
    this._preLoadImages();
}


SlideShowViewer.prototype.play = function ()
{
    thisObject = this;
    clearTimeout(thisObject.timer);
    thisObject.timer = setInterval(thisObject.playAction, thisObject.showTime);
    return false;
}


SlideShowViewer.prototype.unpause = function ()
{
    thisObject = this;
    image = this.container.find('img:animated');
    if (image.length == 0) {
        thisObject._nextSlide();
    }
    thisObject.play();
    return false;
}

SlideShowViewer.prototype.pause = function ()
{
    thisObject = this;
    clearTimeout(thisObject.timer);
    return false;
}

SlideShowViewer.prototype._nextSlide = function ()
{
    nextImageSrc = this.imageUrlList[this.currentIndex]['url'];
    this.container.css({background: 'transparent url('+nextImageSrc+') left top no-repeat'});
    image = this.container.find('img:first');
    image.stop().animate({opacity: 0}, this.fadeTime, function(){
        $(this)
        .attr('src', nextImageSrc)
        .css({'opacity': '1'})
    });
    imageType = this.imageUrlList[this.currentIndex]['type'];
    this._showMenuItem(imageType);
    
    if (this.currentIndex < this.imageCount-1) {
        this.currentIndex++;
    } else {
        this.currentIndex = 0;
    }
    return false;
}

SlideShowViewer.prototype._showMenuItem = function(type)
{
    otherItemList = this.menu.find('>div:not(#'+type+'_item_cell)');
    otherItemCount = otherItemList.length;
    for(i = 0; i<otherItemCount; i++) {
        otherItem = jQuery(otherItemList.get(i));
        otherItem.find('.item:first').stop().animate({opacity: 0.1}, this.fadeTime);
    }
    
    menuItem = this.menu.find('#'+type+'_item_cell .item:first');
    menuItem.stop().animate({opacity: 1}, this.fadeTime);
}


SlideShowViewer.prototype._hoverMenuItem = function(type)
{
    thisObject = this;
    if (!thisObject.isPreLoaded) {
        return false;
    }
    var currentType;
    
    if (thisObject.currentIndex == 0) {
        currentType = thisObject.imageUrlList[thisObject.imageCount-1]['type'];
    } else {
        currentType = thisObject.imageUrlList[thisObject.currentIndex-1]['type'];
    }
    
    if (currentType != type) {
        while (thisObject.imageUrlList[thisObject.currentIndex]['type'] != type) {
            if (thisObject.currentIndex < thisObject.imageCount-1) {
                thisObject.currentIndex++;
            } else {
                thisObject.currentIndex = 0;
            }
        }
        thisObject._nextSlide();
    }
    thisObject.pause();
}

/*
SlideShowViewer.prototype._preloadImages = function()
{
    for(var i = 0; i<imageList.length; i++)
    {
      jQuery("<img>").attr("src", imageList[i]);
    }  
}
*/


SlideShowViewer.prototype._preLoadImages = function() {
    thisSlideShowViewer = this;
    for(var i = 0; i<this.imageUrlList.length; i++)
    {
        this.imageList[i] = new Image();
        this.imageList[i].onload = function () {
            thisSlideShowViewer.preLoadedCount++;
            if (thisSlideShowViewer.preLoadedCount == thisSlideShowViewer.imageCount) {
                thisSlideShowViewer.isPreLoaded = true;
                nextImageSrc = thisSlideShowViewer.imageUrlList[thisSlideShowViewer.currentIndex]['url'];
                image = thisSlideShowViewer.container.find('img:first');
                image.attr('src', nextImageSrc);
                if (thisSlideShowViewer.currentIndex < this.imageCount-1) {
                    thisSlideShowViewer.currentIndex++;
                } else {
                    thisSlideShowViewer.currentIndex = 0;
                }
                thisSlideShowViewer._showMenuItem(thisSlideShowViewer.imageUrlList[thisSlideShowViewer.currentIndex]['type']);
                thisSlideShowViewer.play();
            }
        };
        this.imageList[i].src = this.imageUrlList[i]['url'];
    }
}