// jQuery based Image Slider

jQuery(function(){					
    
    // init show / hide content
    jQuery('#text-switcher').click(function(){ 
            jQuery('#main-area-inner').slideToggle(400);
    });

    // init slider - only if we do have more than 1 image
    if (jQuery('#bg-main-area .tx-pvimagelist-pi1 li').size() > 1) {
        slider();
    }
    
});

function slideElement() {

    // presets
    var fadeSpeed = 1000;
    
    var $active = jQuery('#bg-main-area .tx-pvimagelist-pi1 li.active');
    if ( $active.length == 0 ) {
        $active = jQuery('#bg-main-area .tx-pvimagelist-pi1 li:last');
    }
    
    if ($active.next().length) {
        var $next = $active.next();
    } else {
        var $next = jQuery('#bg-main-area .tx-pvimagelist-pi1 li:nth-child(1)');
    }

    $active.addClass('last-active');
    
    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, fadeSpeed, function() {
            $active.removeClass('active last-active');
        });

    $active.removeClass('active');
    
    // update pagination
    var currentImage = ($active.index()) + 2;   // start + 2 as index starts 0 and we have place very first image already
    if (currentImage > jQuery('#bg-main-area .tx-pvimagelist-pi1 li').size()) {
        currentImage = 1;
    }
    jQuery('#CONTROL .current-image').text(currentImage);
}

function slider() {

    var rotationSpeed = 7000;
    
    jQuery('#bg-main-area .tx-pvimagelist-pi1 li:nth-child(1)').addClass('active');
    jQuery('#bg-main-area .tx-pvimagelist-pi1 li').css({opacity: 0.0});
    jQuery('#bg-main-area .tx-pvimagelist-pi1 li:nth-child(1)').css({opacity: 1.0});

    var totalElements = jQuery('#bg-main-area .tx-pvimagelist-pi1 li').size();
    jQuery('#CONTROL .total-images').text(totalElements);
    
    // init slide elements
    var run = setInterval('slideElement()', rotationSpeed);
    
    jQuery('#CONTROL .next').click(function () {   
        clearInterval(run); // stop rotation
        slideElement(); // call next rotation manually
        run = setInterval('slideElement()', rotationSpeed); // start rotation loop again
        return false;   
    });
    
}
