function MediaGallery(divSelector, sectionId){
    var mediaGallery = this;
    
    mediaGallery._div = $(divSelector);
    mediaGallery._sectionId = sectionId;
    mediaGallery._intervalId = 0;
    mediaGallery._sectionIndex = 0;
    mediaGallery._imageIndex = 0;
    mediaGallery._slideshowEnabled = false;
    mediaGallery._sections = Array();
    mediaGallery._flashPlayer = null;
    
    $.ajax({
        url: '/toyota/main/actions/getImages/0,,' + mediaGallery._sectionId + ',.html',
        dataType: 'json',
        async: true,
        error: function(){
        },
        success: mediaGallery._imageGroupsCallback,
        mediaGalleryObject: mediaGallery
    });
    
    var docHeight;
    var pageHeight = document.documentElement.clientHeight;
    
    if (document.compatMode && document.compatMode != 'BackCompat') {
        docHeight = document.documentElement.scrollHeight;
    }
    else 
        if (document.body && typeof document.body.scrollHeight != 'undefined') {
            docHeight = document.body.scrollHeight;
        }
    
    if (docHeight < pageHeight) {
        docHeight = pageHeight;
    }
    
    if (navigator.appName == 'Microsoft Internet Explorer') {
        position = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
    }
    else {
        position = window.pageYOffset;
    }
    
    // Find the height of the viewport
    var viewPortHeight = self.innerHeight || (document.documentElement && document.documentElement.clientHeight) || document.body.clientHeight;
    if (viewPortHeight > pageHeight) {
        mediaGallery._div.find('.black_overlay').css({
            height: 100 + '%',
            opacity: .80
        });
    }
    else {
        mediaGallery._div.find('.black_overlay').css({
            height: docHeight + 'px',
            opacity: .80
        });
    }
    
    mediaGallery._div.find('.controller .btn-prev a').click(function(){
        mediaGallery._prevImage();
        return false;
    });
    mediaGallery._div.find('.controller .btn-next a').click(function(){
        mediaGallery._nextImage();
        return false;
    });
}

MediaGallery.prototype._imageGroupsCallback = function(data, textStatus){
    var mediaGallery = this.mediaGalleryObject;
    
    mediaGallery._sections = data;
    mediaGallery._div.find('.thumbnails').empty();
    
    for (var i = 0; i < mediaGallery._sections.length; i++) {
        var thumbHTML = String();
        thumbHTML += '<div class="video-category-row">';
        thumbHTML += '<p class="thumbnails-category">' + mediaGallery._sections[i].title + '</p>';
        for (var j = 0; j < mediaGallery._sections[i].imageGroups.length; j++) {
        
            thumbHTML += '<a href="#" class="linkImg" rel="' + i + ':' + j + '">';
            thumbHTML += '<img src="' + mediaGallery._sections[i].imageGroups[j].xSmallImage.url + '" border=0 title="' + mediaGallery._sections[i].imageGroups[j].xSmallImage.altText + '" alt="' + mediaGallery._sections[i].imageGroups[j].xSmallImage.altText + '"class="imgBox"/>';
            thumbHTML += '</a>';
            //mediaGallery._div.find('.thumbnails').append(thumbHTML);
        }
        thumbHTML += '</div>';
        mediaGallery._div.find('.thumbnails').append(thumbHTML);
    }
    
    GlossaryTips_initGlobal();
    
    mediaGallery._div.find('.thumbnails a.linkImg').each(function(i, n){
        $(n).click(function(){
            var index = $(n).attr('rel').split(':');
            mediaGallery._changeImage(index[0], index[1]);
            mediaGallery._sectionIndex = Number(index[0]);
            mediaGallery._imageIndex = Number(index[1]);
            return false;
        });
    });
    
    mediaGallery._changeImage(0, 0);
}

MediaGallery.prototype._changeImage = function(sectionIndex, imageIndex){
    var mediaGallery = this;
    if (mediaGallery._sections.length <= sectionIndex || mediaGallery._sections[sectionIndex].imageGroups.length <= imageIndex) 
        return;
    
    var imageGroup = mediaGallery._sections[sectionIndex].imageGroups[imageIndex];
    
    mediaGallery._div.find('.thumbnails a').attr('class', 'linkImg');
    mediaGallery._div.find('.thumbnails a[rel="' + sectionIndex + ':' + imageIndex + '"]').attr('class', 'linkImgSelected');
    mediaGallery._div.find('.sideInfo').html(imageGroup.title);
    
    if (imageGroup.videoLink.length > 0) {
        if (mediaGallery._flashPlayer == null) {
            mediaGallery._createFlashPlayer();
            mediaGallery._flashPlayer.addVariable('thumbnail', imageGroup.xLargeImage.url);
            mediaGallery._flashPlayer.addVariable('video', imageGroup.videoLink);
            mediaGallery._flashPlayer.write('videoPlayerDiv');
        }
        else {
            try {
                mediaGallery._flashPlayer.addVariable('video', imageGroup.videoLink);
                mediaGallery._flashPlayer.addVariable('autoPlay', 'true');
                mediaGallery._flashPlayer.addVariable('thumbnail', imageGroup.xLargeImage.url);
                mediaGallery._flashPlayer.write('videoPlayerDiv');
            } 
            catch (e) {
            }
        }
        mediaGallery._div.find('.mainImage').hide();
        mediaGallery._div.find('.mainVideo').show();
        mediaGallery._div.find('.galleryAreaNew').addClass('video-gallery');
    }
    else {
        mediaGallery._div.find('.mainImage img').attr('src', imageGroup.xLargeImage.url);
        mediaGallery._div.find('.mainImage img').attr('alt', imageGroup.xLargeImage.altText);
        mediaGallery._div.find('.mainVideo').hide();
        mediaGallery._div.find('.mainImage').show();
    }
}

MediaGallery.prototype._nextImage = function(){
    var mediaGallery = this;
    mediaGallery._imageIndex++;
    if (mediaGallery._imageIndex >= mediaGallery._sections[mediaGallery._sectionIndex].imageGroups.length) {
        mediaGallery._imageIndex = 0;
        mediaGallery._sectionIndex++;
    }
    if (mediaGallery._sectionIndex >= mediaGallery._sections.length) {
        mediaGallery._sectionIndex = 0;
    }
    mediaGallery._changeImage(mediaGallery._sectionIndex, mediaGallery._imageIndex);
}

MediaGallery.prototype._prevImage = function(){
    var mediaGallery = this;
    mediaGallery._imageIndex--;
    if (mediaGallery._imageIndex < 0) {
        mediaGallery._sectionIndex--;
        if (mediaGallery._sectionIndex < 0) {
            mediaGallery._sectionIndex = mediaGallery._sections.length - 1;
        }
        mediaGallery._imageIndex = mediaGallery._sections[mediaGallery._sectionIndex].imageGroups.length - 1;
    }
    mediaGallery._changeImage(mediaGallery._sectionIndex, mediaGallery._imageIndex);
}

MediaGallery.prototype._createFlashPlayer = function(){
    var mediaGallery = this;
    mediaGallery._flashPlayer = new SWFObject('/toyota/vehicles/'+vehicleSection+'/video/videoPlayer_dark.swf', 'videoPlayer', '650', '398', '9.0.0', '');
    mediaGallery._flashPlayer.addParam('allowFullscreen', 'true');
    mediaGallery._flashPlayer.addParam('menu', 'false');
    mediaGallery._flashPlayer.addParam('loop', 'false');
    mediaGallery._flashPlayer.addParam('scale', 'noscale');
    mediaGallery._flashPlayer.addParam('quality', 'best');
    mediaGallery._flashPlayer.addParam('wmode', 'transparent');
    mediaGallery._flashPlayer.addParam('allowScriptAccess', 'always');
    mediaGallery._flashPlayer.addVariable('sizeToContainer', 'true');
    mediaGallery._flashPlayer.addVariable('autoPlay', 'false');
    mediaGallery._flashPlayer.addVariable('carModel', vehicleSection);
}

/*
 * Focus Vehicles - Media Gallery thumbnails tooltips
 * By Ronald Jusuf
 * Hothouse 2009
 */
function GlossaryTips_initGlobal(){

    var linkYCoord = 0;
    
    $('.thumbnails-gallery .video-category-row a').each(function(i, n){
        $(n).hover(function(e){
        
            // take out title text
            $(this).find('img').attr('title', '');
            
            //generate custom hovered alt text
            var hoveredThumbId = $(this).attr('rel');
            var altText = $(this).find('.imgBox').attr('alt');
            hoveredThumbId = hoveredThumbId.substr(2);
            $(this).attr('id', 'thumb' + hoveredThumbId);
            var linkXCoord = getAbsoluteLeft(hoveredThumbId);
            var hoveredPos = getHoverPos(hoveredThumbId); //looking for 'left''middle''right'
            if (hoveredPos == 'middle') {
                linkXCoord -= 56;
            }
            else 
                if (hoveredPos == 'right') {
                    linkXCoord -= 113;
                }
            
            var thumbPopupStruc;
            thumbPopupStruc = "<div class=\"thumb_description_popup " + hoveredPos + "\">";
            thumbPopupStruc += "<div class=\"thumb_description_container\">";
            thumbPopupStruc += "<div class=\"summary\">";
            thumbPopupStruc += "<div class=\"glossary_text\">" + altText;
            thumbPopupStruc += "</div></div></div></div>";
            
            $('body').prepend(thumbPopupStruc);
            $('.thumb_description_popup').css('display', 'block');
            var topCoordinate = getAbsoluteTop(hoveredThumbId);
            
            var mainContainerHeight = $('.thumb_description_popup .thumb_description_container').height();
            linkYCoord = topCoordinate - mainContainerHeight;
            $('.thumb_description_popup').css({
                left: linkXCoord + 'px',
                top: linkYCoord + 'px'
            });
            
        }, function(){
            linkYCoord = 0;
            $('.thumb_description_popup').css('display', 'none');
            $('.thumb_description_popup').remove();
            $(this).removeAttr('id');
        });
    });
}

function getHoverPos(linkIndex){
    var numberLinks = ($('.thumbnails-gallery a').length) - 1;
    var leftLinks = new Array();
    var middleLinks = new Array();
    var rightLinks = new Array();
    
    var leftLinksIndex = 0;
    var counter = 0;
    
    for (var i = 0; i <= numberLinks; i++) {
        if (counter < 3) {
            leftLinks[i] = leftLinksIndex;
            leftLinksIndex++;
            counter++;
        }
        else 
            if (counter == 3) {
                counter = 0;
                leftLinksIndex += 5;
            }
    }
    
    var middleLinksIndex = 3;
    var counter = 0;
    
    for (var i = 0; i <= numberLinks; i++) {
        if (counter < 2) {
            middleLinks[i] = middleLinksIndex;
            middleLinksIndex++;
            counter++;
        }
        else 
            if (counter == 2) {
                counter = 0;
                middleLinksIndex += 6;
            }
    }
    
    var rightLinksIndex = 5;
    var counter = 0;
    
    for (var i = 0; i <= numberLinks; i++) {
        if (counter < 3) {
            rightLinks[i] = rightLinksIndex;
            rightLinksIndex++;
            counter++;
        }
        else 
            if (counter == 3) {
                counter = 0;
                rightLinksIndex += 5;
            }
    }
    
    for (var i = 0; i <= leftLinks.length; i++) {
        if (linkIndex == leftLinks[i]) {
            return 'left';
        }
    }
    
    for (var i = 0; i <= middleLinks.length; i++) {
        if (linkIndex == middleLinks[i]) {
            return 'middle';
        }
    }
    
    for (var i = 0; i <= rightLinks.length; i++) {
        if (linkIndex == rightLinks[i]) {
            return 'right';
        }
    }
    
}

function getAbsoluteLeft(linkId){
    var xCoord = 0;
    var hoveredThumb = document.getElementById('thumb' + linkId);
    xCoord = hoveredThumb.offsetLeft;
    oParent = hoveredThumb.offsetParent;
    while (oParent != null) { //IE only takes the direct parent, needs to loop to the outer most parent
        xCoord += oParent.offsetLeft;
        oParent = oParent.offsetParent;
    }
    return xCoord;
}

function getAbsoluteTop(linkId){
    var yCoord = 0;
    var hoveredThumb = document.getElementById('thumb' + linkId);
    yCoord = hoveredThumb.offsetTop;
    oParent = hoveredThumb.offsetParent;
    while (oParent != null) {//IE only takes the direct parent, needs to loop to the outer most parent
        yCoord += oParent.offsetTop;
        oParent = oParent.offsetParent;
    }
    return yCoord;
}

function getPositionType(hoveredIndex){
    var linksLength = ($('.thumbnails a').length) - 1;
}

