$(document).ready(function(){


  $(".galleryImage a").fancybox();
  height();

  $("#vards,#tel,#text,#email").focus(function(){
   $(this).val('');


  });
});

function height(){
  if($("#col2").height()<$("#col1").height()){
    b = $("#col1").height();
    c = $("#calendar").height();
    rm = $("#rightMenu").height();
    p = $("#poll").height();

    dif = b - c - rm - p;

    $("#suggestions").height(dif);
    if($("#col2").height()>b){
      s = $("#suggestions").height()
      s -= $("#col2").height() - b;
      $("#suggestions").height(s);

    }
  }else{
     d = $("#daily").height();
     s = $("#static").height();
     b = $("#col2").height();
     dif = b - s - d;
     if(dif>0){
        $("#static").height(s+dif-5);

     }
  }

	if($("#col2").height()<$("#col1").height()){
    b = $("#col1").height();
    c = $("#calendar").height();
    rm = $("#rightMenu").height();
    p = $("#poll").height();

    dif = b - c - rm - p;

    $("#suggestions").height(dif);
    if($("#col2").height()>b){
      s = $("#suggestions").height()
      s -= $("#col2").height() - b;
      $("#suggestions").height(s);

    }
  }else{
     d = $("#daily").height();
     s = $("#static").height();
     b = $("#col2").height();
     dif = b - s - d;
     if(dif>0){
        $("#leftCol").height(s+dif-170);

     }


  }

  initScroller();
}
var index = 0;


function initScroller()
{
  var step = 2;
  var pos = 0;

  var win_h = $('#bannerBox').height();
  var wait_frames = 100;

  function setPos(anim)
  {
    var h = $('#bannerBox .inner').height();
    if (h < win_h) return;

    var max_pos = h - win_h;

    if (pos > max_pos)
    {
      step = -step;
      pos = max_pos;
      wait_frames = 100;
    }
    else if (pos < 0)
    {
      step = -step;
      pos = 0;
      wait_frames = 100;
    }


    if (anim)
    {
      var diff = parseInt($('#bannerBox .inner').css('top'));
      if (isNaN(diff)) diff = 0;
      diff -= pos;

      $('#bannerBox .inner').animate({ top: -pos + 'px' });//, Math.abs(diff))
    }
    else
      $('#bannerBox .inner').css('top', -pos + 'px');

  }

  function scroll()
  {
    if (wait_frames > 0)
    {
      wait_frames--;
      return;
    }

    pos += step;

    setPos();
  }

  setInterval(scroll, 30);

  $('#arrowUp').click(function()
  {
    pos -= 50;
    setPos(true);
    wait_frames = 80;
    return false;
  });

  $('#arrowDown').click(function()
  {
    pos += 50;
    setPos(true);
    wait_frames = 80;
    return false;
  });

}


var webroot = '/';
var images_path = webroot + 'images/html/';


function preload_images(images)
{
  for (var i=0; i<images.length; i++)
  {
    var im = new Image();
    im.src = images[i];
  }

}


function init_slides(slides)
{
  var sl = [];
  for (var i=0; i<slides.length; i++)
    sl.push(slides[i].image);

  preload_images(sl);

  var pic_i = 0;
  var act_i = 0;
  var lis;
  var bgs = [];
  var act_bg = 0;

  function showPic(i)
  {
    if (act_i == i) return;

    $(lis[act_i]).toggleClass('active');
    $(lis[i]).toggleClass('active');

    act_i = i;

    var bg2 = $('#midbg2').css('backgroundRepeat', 'no-repeat').css('backgroundImage');

    if (bg2 != '')
    {
      $('#midbg').css('backgroundImage', bg2);
    }

    $('#midbg2').stop().fadeTo(0, 0)
      .attr('href', slides[i].fullimage)
      .css('backgroundRepeat', 'no-repeat')
      .css('backgroundImage', 'url("'+slides[i].image+'")').
      css('backgroundPosition', 'center center')
      .fadeTo(1000, 1.0);

    $('#bannerText').html(slides[i].desc);

  }

  function switchSlide()
  {
    pic_i++;
    if (pic_i >= slides.length) pic_i = 0;

    showPic(pic_i);
  }

  var tmr;
  var interval = 7500;

  function setUserPic(i)
  {
    clearInterval(tmr);
    tmr = setInterval(switchSlide, interval);
    showPic(i);
    pic_i = i;
    return false;
  }

  $(document).ready(function()
  {

    tmr = setInterval(switchSlide, interval);
    bgs[0] = $('#midbg')[0];
    bgs[1] = $('#midbg2')[0];

    lis = $('#slider li');

    $('#slider li').click(function()
    {
      var i = $(lis).index( $(this));

      setUserPic(i);

      return false;
    });

  });
}
