var all_width = 708;
var all_bottom_width = 674;
var nav_block_width = 236;
//if (location.pathname != '/') {
//	nav_block_width = 334;
//}

var top_list_width = $(".carousel-top ul li").size() * all_width;
var top_left_pos = 0;

var bottom_list_width = $(".carousel-bottom ul li").size() * all_bottom_width;
var bottom_left_pos = 0;

var nav_items = $(".carousel-nav ul li").size();

var nav_list_width = nav_items * nav_block_width;
var nav_left_pos = 0;
var nav_item = 1;


$(document).ready(function() {
  $(".carousel-top ul").css("width", top_list_width + "px");
  $(".carousel-top ul").css("left", top_left_pos + "px");
  
  $(".carousel-bottom ul").css("width", bottom_list_width + "px");
  $(".carousel-bottom ul").css("left", bottom_left_pos + "px");
  
  $(".carousel-nav ul").css("width", nav_list_width + "px");
  $(".carousel-nav ul").css("left", nav_left_pos + "px");
  
  $(".carousel-nav ul li").append('<span class="nav-arrow"></span>');
  if (bottom_list_width > 0) $(".carousel-nav ul li").append('<span class="nav-arrow-down"></span>');
  $("#carousel-nav-" + nav_item).addClass("active");
  
  
  if (nav_items > 3){
    $(".home-gallery").append('<span class="nav-arrow-left"></span>').append('<span class="nav-arrow-right"></span>');
  }
  
  arrowChange();
  
  $(".nav-arrow-left").click(function(){
    nav_left_pos = 0;
    $(".carousel-nav ul").animate({
      left: 0
    }), 3000;
    $('.nav-arrow-left').removeClass("gray");
    $('.nav-arrow-right').removeClass("gray");
    $('.nav-arrow-left').addClass("gray");
  });
  
  $(".nav-arrow-right").click(function(){
    if (nav_left_pos < (nav_items - 3) * nav_block_width) {
      nav_left_pos += nav_block_width;
      $(".carousel-nav ul").animate({
        left: -nav_left_pos + "px"
      }), 3000; }
    
    $('.nav-arrow-left').removeClass("gray");
    $('.nav-arrow-right').removeClass("gray");
    if (nav_left_pos >= (nav_items - 3) * nav_block_width) {
        $('.nav-arrow-right').addClass("gray");
    }
  });
  
  var intervalID = setInterval(function(){ 
    
    top_left_pos = (top_left_pos < top_list_width - all_width) ? top_left_pos + all_width : 0;
    bottom_left_pos = (bottom_left_pos < bottom_list_width - all_bottom_width) ? bottom_left_pos + all_bottom_width : 0;
    
    $(".carousel-top ul").animate({
      left: -top_left_pos + "px"
      }), 3000;
    $(".carousel-bottom ul").animate({
      left: -bottom_left_pos + "px"
      }), 3000;
    $(".carousel-nav ul li").removeClass("active");
    nav_item = nav_item < nav_items ? nav_item + 1 : 1;
    if (nav_item > 3) {
      if (nav_left_pos == 0){
        nav_left_pos = (nav_items - nav_item ) <= 2  ? nav_left_pos + nav_block_width : nav_left_pos + nav_block_width*2;
      }
      nav_left_pos = ((nav_list_width - nav_item * nav_block_width) >= nav_block_width) && (nav_left_pos < (nav_items - 3) * nav_block_width) ? nav_left_pos + nav_block_width : nav_left_pos;
      $(".carousel-nav ul").animate({
        left: -nav_left_pos + "px"
      }), 3000;
    }
    else if (nav_item == 1) {
      nav_left_pos = 0;
      $(".carousel-nav ul").animate({
        left: 0
      }), 3000;
    }
    
    arrowChange();
    
    $("#carousel-nav-" + nav_item).addClass("active");
    }, 10000);
    
    $(".carousel-nav ul li").click(function(){
      clearInterval(intervalID);
      var nav_id = $(this).index();
      $(".carousel-nav ul li").removeClass("active");
      $(this).addClass("active");
      $(".carousel-top ul").animate({
        left: -nav_id*all_width + "px"
        }), 3000;
      $(".carousel-bottom ul").animate({
        left: -nav_id*all_bottom_width + "px"
        }), 3000;
      intervalID = setInterval(function(){ 
        top_left_pos = (top_left_pos < top_list_width - all_width) ? top_left_pos + all_width : 0;
        bottom_left_pos = (bottom_left_pos < bottom_list_width - all_bottom_width) ? bottom_left_pos + all_bottom_width : 0;
        $(".carousel-top ul").animate({
          left: -top_left_pos + "px"
          }), 3000;
        $(".carousel-bottom ul").animate({
          left: -bottom_left_pos + "px"
          }), 3000;
        $(".carousel-nav ul li").removeClass("active");
        nav_item = nav_item < nav_items ? nav_item + 1 : 1;
        if (nav_item > 3) {
          if (nav_left_pos == 0){
            nav_left_pos = (nav_items - nav_item) <= 2  ? nav_left_pos + nav_block_width : nav_left_pos + nav_block_width * 2; 
          }
          nav_left_pos = ((nav_list_width - nav_item * nav_block_width) >= nav_block_width) && (nav_left_pos < (nav_items - 3) * nav_block_width) ? nav_left_pos + nav_block_width : nav_left_pos;
          $(".carousel-nav ul").animate({
            left: -nav_left_pos + "px"
          }), 3000;
        }
        else if (nav_item == 1) {
          nav_left_pos = 0;
          $(".carousel-nav ul").animate({
            left: 0
          }), 3000;
        }
        $("#carousel-nav-" + nav_item).addClass("active");
        }, 15000);
    });
});

function arrowChange() {
    if (nav_items > 3) {
        $('.nav-arrow-left').removeClass("gray");
        $('.nav-arrow-right').removeClass("gray");
        
        if (nav_item == 1) {
            $('.nav-arrow-left').addClass("gray");
        }
        if (nav_item == nav_items) {
            $('.nav-arrow-right').addClass("gray");
        }
    }
}
