var global_pos = 1;
var count_mainbg = 0;

//$(document).ready(function(){
function StartListatel()
  {
    $('#prev').css('visibility','visible');
    $('#next').css('visibility','visible');
  
  $('div.mainbg').each(function(){count_mainbg++;});
  $('#prev').click(function(){
    var prev_pos = 0;
    if(global_pos==0) prev_pos=count_mainbg; else prev_pos = global_pos-1;
    if(prev_pos==0) prev_pos = count_mainbg;
    ShowPos(prev_pos);
    $("#timer").stopTime();
    $("#timer").oneTime("4s", function() {StartTimer();});
    return false;
  });
  $('#next').click(function(){
    var new_pos = 0;
    if(global_pos==count_mainbg) new_pos=1; else new_pos = global_pos+1;
    ShowPos(new_pos);
    $("#timer").stopTime();
    $("#timer").oneTime("20s", function() {StartTimer();});
    return false;
  });
  StartTimer();
  }
//});

// Запускалка таймера
function StartTimer()
  {
    var new_pos = 0;
    $("#timer").everyTime(7000, function(i) {
      if(global_pos==count_mainbg) new_pos=1; else new_pos = global_pos+1;
      ShowPos(new_pos);
    });
  }
function ShowPos(pos)
  {
    var old_pos = global_pos-1;
    global_pos = pos;
    var new_pos = global_pos-1;
    if(navigator.appName=="Microsoft Internet Explorer")
      {
        $("div.mainbg:eq("+old_pos+")").hide();
        $("div.mainbg:eq("+new_pos+")").show();
      }
      else
      {
        $("div.mainbg:eq("+old_pos+")").fadeOut(2000);
        $("div.mainbg:eq("+new_pos+")").fadeIn(2000);
      }


    $(".frtitle:eq("+old_pos+")").hide();
    $(".frtitle:eq("+new_pos+")").show();
    $(".frtext:eq("+old_pos+")").hide();
    $(".frtext:eq("+new_pos+")").show();
    
  }
