$(document).ready(function() {
	var open = false;

  initScroller();

  $("#tools input[type='submit']").click(function(){

    var id = "spinner"+Math.random();
    $(this).before("<div id='" + id + "' style='float:right;margin-left: 5px;vertical-align:middle;height: 20px;width: 20px;'></div>");

    var opts = {
      lines: 8, // The number of lines to draw
      length: 5, // The length of each line
      width: 3, // The line thickness
      radius: 2, // The radius of the inner circle
      color: '#000', // #rgb or #rrggbb
      speed: 1.5, // Rounds per second
      trail: 10, // Afterglow percentage
      shadow: false // Whether to render a shadow
    };
    var target = document.getElementById(id);
    var spinner = new Spinner(opts).spin(target);

  });

  $("#langWrap a").click(function(){

  	if(open){

  	}else{
  		$(this).parent().find("a").css('display', 'block');
  		open = true;
  	}

    return false;
  });

	$("#langWrap a").not(".active").click(function(){
  		var lang = $(this).attr("href");
			window.location = lang;
    	open = false;
      return false;
	});


	$("#langWrap").mouseleave(function(){
  	$(this).find("a").not(".active").css('display','none');
		open = false;
	});


	$("#langWrap a.active").prependTo("#langWrap");

	$("#position-check-mark").hover(function(){
  	$("#positions-check-description").show();
	},function(){
		$("#positions-check-description").hide();
	});

	$("#metrics_check_mark").hover(function(){
  	$("#metrics_check_tooltip").show();
	},function(){
		$("#metrics_check_tooltip").hide();
	});


});


function init_pos_check_form()
{

  $('form.keyword-position-check').each(function() {

    var add_new = function(e) {

      var wrap = $(this).parent();

      if (wrap.next().is('div.keyword')) {
        return;
      }

      var wrap_tpl = wrap.clone();
      wrap.removeClass('last');
      $('input', wrap_tpl).val('');

      var keyword_no = $('input', wrap).attr('id').substr(12);

      keyword_no ++;

      $('label', wrap_tpl).attr('for', 'fpos_keyword' + keyword_no);
      $('input', wrap_tpl).attr('id', 'fpos_keyword' + keyword_no);


      wrap.after(wrap_tpl);

      if ($('form.keyword-position-check div.keyword').length <= 2) {
        $('input', wrap_tpl).keyup(add_new);
      }

      $('input', wrap).unbind('keyup', add_new);

    };

    $('div.last input', this).keyup(add_new);

  });
          /*
  $('form.position-compare-form').each(function() {

    var add_new = function(e) {

      var wrap = $(this).parent();

      if (wrap.next().is('div.domain')) {
        return;
      }

      var wrap_tpl = wrap.clone();
      wrap.removeClass('last');
      $('input', wrap_tpl).val('');

      var domain_no = $('input', wrap).attr('id').substr(12);

      domain_no ++;

      $('label', wrap_tpl).attr('for', 'fdomains' + domain_no);
      $('input', wrap_tpl).attr('id', 'fdomains' + domain_no);


      wrap.after(wrap_tpl);

      if ($('form.position-compare-form div.domain').length <= 1) {
        $('input', wrap_tpl).keyup(add_new);
      }

      $('input', wrap).unbind('keyup', add_new);

    };

    $('div.last input', this).keyup(add_new);

  });  */

  var descr = $('p#positions-check-description').hide().html();

  $("h4#positions-check img").wTooltip({
      content: descr,
      className: "tooltip"
  });

}

function init_pr_check_form()
{

  var descr = $('p#pr-check-description').hide().html();

  $("span.title img").wTooltip({
      content: descr,
      className: "tooltip"
  });

}

function init_reg_form()
{

  $('input[name="type"]').each(function() {

    if (this.checked && $(this).val() == 'individual')
    {
      $('div#reg-form-legal').hide();
      $('div#reg-form-individual').show();
    }
    else if (this.checked && $(this).val() == 'legal')
    {
      $('div#reg-form-individual').hide();
      $('div#reg-form-legal').show();
    }

  });

  $('input[name="type"]').click(function()
  {
    if ($(this).val() == 'individual')
    {
      $('div#reg-form-legal').hide();
      $('div#reg-form-individual').show();
    }
    else
    {
      $('div#reg-form-legal').show();
      $('div#reg-form-individual').hide();
    }
  })

}

function init_contacts()
{
  $(function(){

    $('#contactstop').css('overflow', 'hidden');

    function each_a(i, e)
    {

      $(e).click(function()
      {
        var h = $('#contactstop').height();

        $('#contactstop').animate({ height: 0 }, h * 4, null, function()
        {

          $('#contactstop').css('height', 'auto').css('display', 'none');
          $('#contactstop').html( $('.full', $(e).parents()[4] ).html() );

          var h = $('#contactstop').height();

          $('#contactstop').css('height', 0).css('display', 'block');

          $('#contactstop').animate({ height: h }, h * 4);

        });

        return false;
      });

    }

    $('#contacts td.c3 a').each(each_a);
    $('#contacts td.c1 a').each(each_a);

  });

}


function init_rightpanel()
{
  $(function() {
    var is_open = false;
    var rp_right = parseInt($('#rightpanel').css('right'));

    $('#rightpanel').mouseenter(function()
    {
//      if ($('#rightpanel').data('animating')) return;

      $('#rightpanel').data('animating', true);

      $('#rightpanelwrap').stop(true).animate({ width: 270 }, 500);
      $('#rightpanel').stop(true).animate({ right: 0 }, 500, null, function()
      {
        is_open = true;
        $('#rightpanel').data('animating', false);
      });

      return false;
    });

    $('#rightpanel').mouseleave(function()
    {
//      if ($('#rightpanel').data('animating')) return;

      $('#rightpanelwrap').stop(true).animate({ width: 70 }, 500);
      $('#rightpanel').stop(true).animate({ right: rp_right }, 500, null, function()
      {
        is_open = false;
        $('#rightpanel').data('animating', false);
      });

    });

  });
}


function init_sitemaplink()
{
  $(function(){
    var animating = false;
    var h = $('#sitemap').height() + 90;
//    $('#sitemap').css('overflow', 'hidden').height(0);
    var min_h = 0;
    var is_open = ($('#sitemapwrap').height() > min_h);

//    if ($.browser.msie) h -= 15;

    $('#sitemapwrap').css('overflow', 'hidden');
    $('#sitemap').css('overflow', 'hidden');

    $('#sitemaphead a.sitemaplink').click(function()
    {
      if (animating) return false;

      animating = true;
      if (is_open)
      {
        $('#sitemapwrap').animate({ height: min_h }, (h - min_h) * 4, null, function()
        {
          is_open = false;
          animating = false;
        });
      }
      else
      {
        $('#sitemapwrap').animate({ height: h }, (h - min_h) * 4, null, function()
        {
          is_open = true;
          animating = false;
        });
      }

      return false;
    });
  });
}


function easeOutCubic(x, t, b, c, d)
{
  return c*((t=t/d-1)*t*t + 1) + b;
}

function init_brands_scroller()
{
//  return;
  $(function(){

    var step = 2;
    var pos = 0;
    var scr = $('#brands .mid');
    if (!scr) return;

    $('#brands .left a').click(function(){
      pos -= 50;
      if (pos < 0) pos = 0;
      return false;
    });

    $('#brands .right a').click(function(){
      pos += 50;
      var max = scr.scrollWidth() - scr.innerWidth();
      if (pos > max) pos = max;
      return false;
    });

    function scrollSlide()
    {

      var min = 0;
      var max = scr.scrollWidth() - scr.innerWidth();

//      var val = easeOutCubic(0, pos - min, min, max - min, 30);

      pos += step;
      if (pos >= max) step = -step;
      else if (pos <= min) step = -step;

      scr.scrollLeft(pos);
    }

    setInterval(scrollSlide, 30);
  });
}

try {
  // Create scrollWidth method
  $.fn[ 'scrollWidth' ] = function(val) {
  if (!this[0]) return;

  return val != undefined ?

  // Set the scrollWidth
  this.each(function() {
  this == window || this == document ?
  window.scrollWidth( val ) :
  this[ 'scrollWidth' ] = val;
  }) :

  // Return the scrollWidth
  this[0] == window || this[0] == document ?
  self[ 'scrollWidth' ] ||
  $.boxModel && document.documentElement[ 'scrollWidth' ] ||
  document.body[ 'scrollWidth' ] :
  this[0][ 'scrollWidth' ];
  };
}
catch (e) {}


function init_labelify()
{
  $(function(){
    $(".labelify").labelify({
      labelledClass: 'labelHighlight'
    });
  });
}


function init_voted_box()
{
  $(function(){
    var animating = false;
    var div = $('#votebox .rwrap');
    var is_open = (div.height() != 0);

    var h = $('#votebox .rwrap .results').outerHeight();
    var min_h = 0;

    if ($.browser.msie) h += 10;

    div.css('overflow', 'hidden');

    $('#votebox a.voted').click(function()
    {
      if (animating) return false;

      animating = true;
      if (is_open)
      {
        div.animate({ height: min_h }, (h - min_h) * 4, null, function()
        {
          is_open = false;
          animating = false;
        });
      }
      else
      {
        div.animate({ height: h }, (h - min_h) * 4, null, function()
        {
          is_open = true;
          animating = false;
        });
      }

      return false;
    });
  });

}



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

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

  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 = 10;
    }
    else if (pos < 0)
    {
      step = -step;
      pos = 0;
      wait_frames = 10;
    }


    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, 50);

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

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

}

