$(document).ready(function() {

    $('#mapPlayerExpose').each(function(i) {

        // get player sources via id from json
        confPLEX = $(this).attr('id');
        confPLEX = eval(confPLEX);
        srcPLEX = confPLEX['playerSources'];

        // load sources if we have sources
        if (srcPLEX) {
            overlay = $('.player_images', $(this));
            jQuery.each(srcPLEX,
            function(index, valueOfElement) {
                overlay.append(valueOfElement);
            });
        }
    });

    // prefetch objects via jQuery direct access
    function prefetch(tar) {
        thumbs = $('li', tar + ' ul.player_thumbs');
        captions = $('li', tar + ' ul.player_captions');        
        images = $('div', tar + '  div.player_images');
    }
    prefetch('#mapPlayerExpose');

    // define rollover funtion on singles
    var lastOverPLEX = '';
    function overItPlex(i) {
        if (lastOverPLEX) {
            clearItPlex(lastOverPLEX)
        }
        lastOverPLEX = i + '';
        thumbs.css('opacity', 0.5);
        thumbs.eq(i).addClass('over').css('opacity', 1);
        images.eq(i).addClass('over').show();
        captions.eq(i).show();
        playerPosition = i + '';
    }
    function overItPlexSmooth(i) {
            if (lastOverPLEX) {
                // clearItPlex(lastOverPLEX)
                thumbs.eq(lastOverPLEX).removeClass('over');
                images.eq(lastOverPLEX).fadeOut('slow');
                captions.eq(lastOverPLEX).fadeOut();
            }
            lastOverPLEX = i + '';
            thumbs.css('opacity', 0.5);
            thumbs.eq(i).addClass('over').css('opacity', 1);
            images.eq(i).addClass('over').hide().fadeIn('slow');
            captions.eq(i).fadeIn();
            playerPosition = i + '';
    }

    // define clear function for singles
    function clearItPlex(i) {
        thumbs.eq(i).removeClass('over');
        images.eq(i).removeClass('over').show();
        captions.eq(i).hide();
    }

    // get indexOfTheLastImage
    function prefetchAdd(tar) {
        indexOfTheLastImage = '';
        thumbs.each(function(i) {
            indexOfTheLastImage = i;
        })
    }
    prefetchAdd('#mapPlayerExpose');



    // rollover
    var DOMthumbs = thumbs.each(function(i) {
        $(this).mouseover(function() {
            overItPlex(i);
        })
    });


    $('#mapPlayerExpose').mouseenter(function() {
        window.clearTimeout(timer);
    }).mouseleave(function() {
        autoplay(playerPosition);
    });

    var playerPosition = 0 + '';

    // do play in loop
    var timer = null;
    function autoplay(playerPosition) {
        // if (tfMapPlayerAutoplay == true){
        if (confPLEX['autoplay'] == 'true') {
            timer = window.setTimeout(function() {
                playerPosition++;
                curPos = playerPosition;
                if (playerPosition > indexOfTheLastImage) playerPosition = 0;

                overItPlexSmooth(playerPosition);
                window.clearTimeout(timer);
                timer = null;
                autoplay(playerPosition);
            },
            5000);
        }
    }
    
    captions.hide();

    
    var thumbsLength = DOMthumbs.length;
    


    if (confPLEX['selectFirst'] == 'true') {
        overItPlex(playerPosition);
    }

    if (confPLEX['autoplay'] == 'true') {
        // 2010.07.20 only do if more than one thumb
        if (thumbsLength>1) {
            autoplay(playerPosition);
        }
    }
    
    // 2010.07.20 hide thumbs if less than 2
    if (thumbsLength<=1) {
        $('.player_thumbs').hide();
    }
    
});

