var page_inactive_html = '<span><a href="javascript:;" onclick="showPage({x});">•</a></span>';
var page_active_html = '<span>•</span>';
var active_page = 0;
var page_anim = 0;
var timer_anim = 0;

function initPages(automatic)
    {
    if($('.page').length>1){
    var n = 0;
    $('.teasers .page')
      .click(function(){showPage(-1);})
      .hover(function(){$(this).children('.description').fadeIn();},function(){$(this).children('.description').fadeOut();});
    $('#pagination .page').each(function(){this.id="page_num"+n;n++;});
    updateNavigation('#pagination_navi',active_page,$('#pagination .page').length);

    if(automatic>0)
        {
        timeranim(automatic);
        page_anim = window.setInterval("showPage();",automatic);
        timer_anim = window.setInterval("timeranim("+automatic+")",automatic);
        }
    }}

function timeranim(automatic)
    {
    $('#pageswitch_timer').stop();
    $('#pageswitch_timer').css('width','0px');
    if(automatic>0) $('#pageswitch_timer').animate({'width':100},automatic,'linear',function(){$('#pageswitch_timer').css('width','0px');});
    }

function showPage(num)
    {
    if(num==-1 || num>=0)
        {
        timeranim(0)
        window.clearInterval(page_anim);
        window.clearInterval(timer_anim);
        }
    if(num==-1) num = null;
    $('#pagination .page .description').hide();
  	$('#pagination .page:not(#page_num'+active_page+')').hide();
    $('#pagination #page_num'+active_page)
      .prependTo('#pagination')
      .fadeOut(350);
    if(num == null)
        {
        if(active_page+1<$('#pagination .page').length) active_page++;
        else active_page = 0;
        }
    else
        {
        active_page = num;
        }
    $('#pagination #page_num'+active_page).fadeIn(700);
    updateNavigation('#pagination_navi',active_page,$('#pagination .page').length);
    }

function updateNavigation(where,x,len)
    {
    if(len>1){
    var resulthtml ='';
    for(var b=0;b<len;b++)
        {
        resulthtml += (b==x ? page_active_html : page_inactive_html.replace(/{x}/,b));
        }
    $(where).html('<span class="pager">'+$('.pager').html()+'</span>' + resulthtml);
    }}