WebFontConfig = {
    google: { families: [ 'PT+Sans:400,700,400italic:latin,cyrillic' ] }
};
(function() {
    var wf = document.createElement('script');
    wf.src = ('https:' == document.location.protocol ? 'https' : 'http') +
        '://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js';
    wf.type = 'text/javascript';
    wf.async = 'true';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(wf, s);
})();

function slider() {
    $slider = $('#pr_slider');
    $viewport = $slider.find('.viewport');
    $container = $slider.find('.slides');
    $slides = $slider.find('.slide');

    var max_height = 0;
    $slides.each(function() {
        $el = $(this).find('.slide_content');
        if ($el.height() > max_height) max_height = $el.height();
    });
    $slides.each(function() {
        $(this).find('.slide_content').height(max_height);
    });


    if( $slides.length == 0 ) { return; }
    var width = 199;

    $viewport.height($slides.first().height());
    $viewport.width(Math.floor($viewport.width() / width) * width);
    $container.css({ position: 'absolute', top: 0, left: 0, width: $slides.length * width });

    var switch_slide = function(direction) {
        console.log(direction);
        var left = $container.position().left;
        var new_left = left;
        if (direction == 'next') {
            var tmp = $container.width() + left - $viewport.width();
            if (tmp > 0) new_left = left - width;
            if (tmp == 0) new_left = 0;
        } else {
            if (left < 0) new_left = left + width;
            if (left == 0) new_left = $viewport.width() - $container.width();
        }
        $container.animate({ left: new_left }, 300);
    };

    interval = window.setInterval(function() {
        switch_slide('next');
    }, 5000);

    $slider.find('.controls .prev').click(function(e) {
        e.preventDefault();
        window.clearInterval(interval);
        switch_slide('prev');
    });
    $slider.find('.controls .next').click(function(e) {
        e.preventDefault();
        window.clearInterval(interval);
        switch_slide('next');
    });

    $slides.click(function(e) {
        window.location = $(this).find('.link').attr('href');
    });
}

function show_random_slide() {
    $slider = $('#pr_slider');
    $slides = $slider.find('.slide');
    $slides.hide();
    var random = Math.round(Math.random() * 100) % $slides.length;
    $($slides[random]).show();
}

$(function(){
    if ($('body').hasClass('index')) { slider() } else { show_random_slide() }
});
