﻿
$(document).ready(function () {
    $('.slideshow-wrapper').each(function () {
        var thumbs = new Array();

        var width = 364;
        var imageWidth = 73;

        $(this).find('.ad-thumb-list').children('li').each(function (i) {
            thumbs[i] = $(this);
        });

        if (thumbs.length > 0) {
            width = imageWidth * thumbs.length;

            if (width < 364)
                width = 364;

            $(this).find('.ad-thumb-list').css('width', width + 'px');
        }

        $(this).find('.ad-thumb-list').children('li').each(function (i) {
            if (i == 0) {
                $(this).parents('.ad-gallery').find('.slideshow-image').attr('src', $(this).find('.thumb-trigger').attr('href'));
            }
            //Preload the image so when changing images it goes smooooooth
            MM_preloadImages($(this).children().attr('href'));

            $(this).children().click(function () {
                $(this).parents('.ad-gallery:first').find('.slideshow-image').stop().animate({ opacity: 0.1 }, 200);
                $(this).parents('.ad-gallery:first').find('.slideshow-image').attr('src', $(this).attr('href'));
                $(this).parents('.ad-gallery:first').find('.slideshow-image').animate({ opacity: 1.0 }, 200);

                //Get the left position of the current image
                var imageLeft = ((i * imageWidth));
                //The gallery holds 5 images, 2 either side 1 in the middle!
                var middle = imageLeft - (2 * imageWidth);

                if (middle <= 0) {
                    //Set the left arrow to disabled
                    var srcOld = $(this).parents('.ad-thumbs').prev().find('img').attr('src');
                    if (srcOld.indexOf("_o.jpg") == -1) {
                        $(this).parents('.ad-thumbs').prev().find('img').addClass('no-overstate');
                        $(this).parents('.ad-thumbs').prev().find('img').attr('src', srcOld.replace(".jpg", "_o.jpg"));
                    }
                    middle = 0;
                }
                else if (middle >= width - (5 * imageWidth)) {
                    //Set the right arrow to disabled
                    var srcOld = $(this).parents('.ad-thumbs').next().find('img').attr('src');
                    if (srcOld.indexOf("_o.jpg") == -1) {
                        $(this).parents('.ad-thumbs').next().find('img').addClass('no-overstate');
                        $(this).parents('.ad-thumbs').next().find('img').attr('src', srcOld.replace(".jpg", "_o.jpg"));
                    }
                    middle = width - (5 * imageWidth);
                }
                else {
                    //Set the left arrow to enabled
                    var srcOld = $(this).parents('.ad-thumbs').prev().find('img').attr('src');
                    if (srcOld.indexOf("_o.jpg") != -1) {
                        $(this).parents('.ad-thumbs').prev().find('img').addClass('no-overstate');
                        $(this).parents('.ad-thumbs').prev().find('img').attr('src', srcOld.replace("_o.jpg", ".jpg"));
                    }
                    //Set the right arrow to enabled
                    var srcOld = $(this).parents('.ad-thumbs').next().find('img').attr('src');
                    if (srcOld.indexOf("_o.jpg") != -1) {
                        $(this).parents('.ad-thumbs').next().find('img').removeClass('no-overstate');
                        $(this).parents('.ad-thumbs').next().find('img').attr('src', srcOld.replace("_o.jpg", ".jpg"));
                    }
                }

                $(this).parents('.ad-thumb-list').animate({ left: -middle + 'px' }, 400);
            });
        });

        $(this).find('.nav-left').click(function () {
            var oldLeft = parseInt($(this).next().find('.ad-thumb-list').css('left'));

            var moveTo = oldLeft + (imageWidth * 3);

            if (moveTo >= 0) {
                moveTo = 0;
                //Set the left arrow to disabled
                /*var srcOld = $(this).find('img').attr('src');
                if (srcOld.indexOf("_o") == -1) {
                $(this).find('img').addClass('no-overstate');
                $(this).find('img').attr('src', srcOld.replace(".jpg", "_o."));
                }*/
            }
            else {
                //Set the left arrow to enabled
                /*var srcOld = $(this).find('img').attr('src');
                if (srcOld.indexOf("_o.jpg") != -1) {
                $(this).find('img').removeClass('no-overstate');
                $(this).find('img').attr('src', srcOld.replace("_o.jpg", ".jpg"));
                }*/
            }

            $(this).next().find('.ad-thumb-list').stop().animate({ left: moveTo + 'px' }, 400);
        });

        $(this).find('.nav-right').click(function () {
            var oldLeft = parseInt($(this).prev().find('.ad-thumb-list').css('left'));
            var moveTo = oldLeft - (imageWidth * 3);
            if (moveTo <= (-width + 364)) {
                moveTo = (-width + 364);
                //Set the right arrow to disabled
                /*var srcOld = $(this).find('img').attr('src');
                if (srcOld.indexOf("_o") == -1) {
                $(this).find('img').addClass('no-overstate');
                $(this).find('img').attr('src', srcOld.replace(".jpg", "_o.jpg"));
                }*/
            }
            else {
                //Set the right arrow to enabled
                /*var srcOld = $(this).find('img').attr('src');
                if (srcOld.indexOf("_o") != -1) {
                $(this).find('img').removeClass('no-overstate');
                $(this).find('img').attr('src', srcOld.replace("_o.jpg", ".jpg"));
                }*/
            }

            $(this).prev().find('.ad-thumb-list').stop().animate({ left: moveTo + 'px' }, 400);
        });
    });
});

