
function cs_right_click(){
  $("#rightCasestudy").unbind("click");
  if($("#slideCasestudy").children().size() > 3)
      $("#leftCasestudy").css('cursor', 'pointer');

    max = ($("#slideCasestudy").children().size()-3) * 171;
    if(max > parseInt($("#slideCasestudy > div:last").css('right'))){
      $("#slideCasestudy").children().animate({right:parseInt($("#slideCasestudy").children().css('right')) + 171 + 'px'}, 500, "", function(){
        $("#rightCasestudy").bind("click",function() { cs_right_click(); });

      });
} else {
      $("#rightCasestudy").css('cursor', 'default');
      $("#rightCasestudy").bind("click",function() { cs_right_click(); });
    }

}

function cs_left_click(){
  $("#leftCasestudy").unbind("click");
  if($("#slideCasestudy").children().size() > 3)
        $("#rightCasestudy").css('cursor', 'pointer');

      if(parseInt($("#slideCasestudy > div:first").css('right')) != 0){
        $("#slideCasestudy").children().animate({right:parseInt($("#slideCasestudy").children().css('right')) - 171 + 'px'}, 500, "", function(){
          $("#leftCasestudy").bind("click",function() { cs_left_click(); });
        });
      } else {
        $("#leftCasestudy").css('cursor', 'default');
        $("#leftCasestudy").bind("click",function() { cs_left_click(); });
      }
}


$().ready(function() {

  $(".linkList > li").hover(
    function() {
      $(this).css('color', '#D07A00');
      $(this).children('a').css('color', '#D07A00');
    },
    function() {
      $(this).css('color', '#444')
      $(this).children('a').css('color', '#444');
    }
  );

  $(".rightbarBlock > .text").css('display', 'none');
  $(".rightbarBlock > .text:first").css('display', 'block');
  $(".open_close:first").attr('src', '/img/rightbar/close.jpg');

  $(".open_close").click(
    function() {
      if($(this).attr('src') == '/img/rightbar/close.jpg') {
        $(this).parent().children('.text').slideUp('solw');
        $(this).attr('src', '/img/rightbar/open.jpg');
      } else {
        $(this).parent().children('.text').slideDown('solw');
        $(this).attr('src', '/img/rightbar/close.jpg');
      }
    }
  );

  $(".rightbarBlock > h3").click(function() {
      if($(this).parent().children('.open_close').attr('src') == '/img/rightbar/close.jpg') {
        $(this).parent().children('.text').slideUp('solw');
       $(this).parent().children('.open_close').attr('src', '/img/rightbar/open.jpg');
      } else {
        $(this).parent().children('.text').slideDown('solw');
        $(this).parent().children('.open_close').attr('src', '/img/rightbar/close.jpg');
      }
  });

  $(".rightbarBlock > h3").css('cursor','pointer');

  $("#hotline > div").css('display', 'none');
  $("#hotline > div:first").css('display', 'block').addClass('hotSel');

  var t = setInterval("SlideHotNews()", 5000);

  $(".casestudyBlock").hover(
    function() {
      $(this).css('background-position', 'bottom')
    },
    function() {
      $(this).css('background-position', 'top')
    }
  );

  $(".casestudyBlock").click(function() {
    window.location.href = $(this).children('input').val();
  });

  $("#CasestudySectionId").change(function() {
    $("#load").css('display', 'block');
    $("#casestudy_blocks").load(
      '/casestudy/index #casestudy_blocks > div',
      {'data[Casestudy][section_id]':$(this).val(), 'data[Casestudy][category]':$("#CasestudyCategory").val()},
      function() {
        $("#load").css('display', 'none');
        $("#CasestudySectionId").change(function() {
          $("#load").css('display', 'block');
          $("#casestudy_blocks").load(
            '/casestudy/index #casestudy_blocks > div',
            {'data[Casestudy][section_id]':$(this).val(), 'data[Casestudy][category]':$("#CasestudyCategory").val()},
            function() {
              $("#load").css('display', 'none');
            }
          );
        });
      }
    );
  });

  $("#CasestudyCategory").change(function() {
    $("#load").css('display', 'block');
    $("#casestudy_blocks").load(
      '/casestudy/index .casestudyBlock',
      {'data[Casestudy][category]':$(this).val(), 'data[Casestudy][section_id]':$("#CasestudySectionId").val()},
      function() {
        $("#load").css('display', 'none');
        $("#CasestudyCategory").change(function() {
          $("#load").css('display', 'block');
          $("#casestudy_blocks").load(
            '/casestudy/index .casestudyBlock',
            {'data[Casestudy][category]':$(this).val(), 'data[Casestudy][section_id]':$("#CasestudySectionId").val()},
            function() {
              $("#load").css('display', 'none');
            }
          );
        });
      }
    );
  });

  if($("#flashMessage").html() == 'Please enter correct code') {
    $("#ContactCapcha").css('border', '1px solid red');
  }


  $("#leftCasestudy").css('cursor', 'default');
  if($("#slideCasestudy").children().size() <= 3)
    $("#rightCasestudy").css('cursor', 'default');

  /* Casestydy на страницах сайта */
  $("#rightCasestudy").bind("click",function() { cs_right_click(); });

  $("#leftCasestudy").bind("click",function() { cs_left_click(); });


  $("#page_tabs").tabs();

  $(".articleBlock:even").css({background:'none'});
  $(".articleBlock:even").children('.articleBlockImgTop').remove();
  $(".articleBlock:even").children('.articleBlockImgBottom').remove();

  if($.browser.msie && $.browser.version == 6){
    $(".articleBlock").children('.articleBlockImgTop').remove();
    $(".articleBlock").children('.articleBlockImgBottom').remove();
  }

  $("#ContactIndexForm").submit(function() {
    return checkForm();
  });

});

var cur = '';

function SlideHotNews() {
  if($("#hotline").length <= 1) {
    return;
  }

  el = $("#hotline > .hotSel");
  el.hide('fast', function() {
    $(this).removeClass('hotSel').hide();

    if(el.next().length == 0)
      el = $("#hotline > div:first");
    else
      el = el.next();

    el.addClass('hotSel').show('fast');
  });

}

function checkForm() {
  var sub = 1;
  $(".mandatory").each(function() {
    if($(this).val() == "") {
      $(this).css('border-color', 'red');
      sub = 0;
    }
  });
  if(sub == 0) {
    alert('All mandatory fields must be field in.');
    return false;
  } else {
    _gaq.push(['_trackPageview', '/send_contact_form']);
    return true;
  }

}
