$(function() {

  var inputs = $(".botsearch input[type='text']");
  inputs.focus(function(){
      $("label[for='"+$(this).attr('id')+"']").hide();
      });
  inputs.blur(function(){
      var that = $(this);
      if (that.val() == ""){
      $("label[for='"+that.attr('id')+"']").show();
      }
      });
  inputs.blur();

  function carouselDo(){
    var liWidth = $(".h-carousel3").width() / 4;
    $(".jcarousellite li").width(liWidth);

    $(".jcarousellite").jCarouselLite({
      btnNext: ".h-carousel .next",
      btnPrev: ".h-carousel .prev",
      circular: false,
      visible: 4
    });
  }

  carouselDo();
  $(window).bind("resize", carouselDo);
  
  function objectSize() {
      if(
          $('.h-models-wrap object')
      ) {
          var width = $('.h-models-wrap').innerWidth();
          var height = Math.round(310 / (1000 / width));
          
          $('.h-models-wrap object').attr('height', height);
          $('.h-models-wrap object embed[width=100%]').attr('height', height);
      }
  }
  
  objectSize();
  
  $(window).bind("resize", objectSize);

  $(".testdrive img").bind("click", function() {
      $(".testdrive .choose").removeClass("choose");
      $(this).parents(".testdrive li").addClass("choose");
      });

  $(".testdrive2 a").bind("click", function() {
      $(".testdrive2 .choose").removeClass("choose");
      $(this).parents(".testdrive2 li").addClass("choose");
      return false;
      });

  $(".h-content ol li").each(function(){
      var index = $(this).index()+1;
		  var list = $('<span class="ol-list">'+ index +'</span>');
			$(this).prepend(list);
			});

  $(".cc-popup").each(function() {$(this).css({"margin-left": - $(this).width()/2})});
  $(".cc-color").bind(
    "click",
    function() {
      $(".cc-change").removeClass("cc-change");
      $(this).parents(".cc-wrap").addClass("cc-change");

      var imgUri = $(this).attr('rel');
      var img = new Image();
      img.src = imgUri;
      $(img).load(
        function () {
          $('.b-prod-color img').attr('src', $(this).attr('src'));
        }
      );
    }
  );

  if($(".resume").length) $(".b-send").hide();
  $(".resume").bind("click", function() {
      $(".b-send").show();
      $(this).hide();
      return false;
      })

    $("a.full").bind(
        "click",
        function() {
            $(this).parents('p.tiny-content').addClass('hidden');
            $(this).parents('div.b-pack-col').find('p.full-content').removeClass('hidden');
            return false;
        }
    );

    if(
        $('table.equipment tr[rel="additional"]').length
    ) {
        $('table.equipment tr[rel="additional"]').hide();

        $('div.full-list a').bind(
            'click',
            function() {
                $('table.equipment tr[rel="additional"]').show();
                $(this).parent('div.full-list').hide();
                return false;
            }
        );
    } else {
        $('div.full-list').hide();
    }

    $(".calc-popup").hide();
    $(".calclink").bind("click", function() {
        $(".calc-popup").show();
        $(this).parents(".h-content").css("z-index","5");
        return false;
    });
    $(".calc-close").bind("click", function() {
        $(".calc-popup").hide();
        $(this).parents(".h-content").removeAttr("style");
    });

    $(".subsite_ic").bind("click", function() {
        $(this).parents(".subsite").toggleClass("subsite-open");
    });

    $('input.autocomplete').each(
        function() {
            $(this).attr('value', $(this).attr('rel'));

            $(this).bind(
                'focus',
                function() {
                    var rel = $(this).attr('rel');
                    var val = $(this).attr('value');

                    if(rel == val) {
                        $(this).attr('value', '');
                    }
                }
            );

            $(this).bind(
                'blur',
                function() {
                    var rel = $(this).attr('rel');
                    var val = $(this).attr('value');

                    if(val == '') {
                        $(this).attr('value', rel);
                    }
                }
            );
        }
    );

    if(
        $('.trade-in').length
    ) {
        if(
            getCookie('show_trade_in') != 1
        ) {
            setTimeout(
                function() {
                    setCookie('show_trade_in', 1, null, '/');

                    $('.trade-in').animate({
                        width: 500
                    }, 1200, function() {
                        setTimeout(
                            function() {
                                $('.trade-in').animate({
                                    width: 150
                                }, 1200);
                            },
                            2000
                        );
                    });
                },
                1000
            );

        }
    }
    
    $(".mod-nav a").bind("mouseenter", function(ele) {
        if($.browser.msie && parseInt($.browser.version) == 7 ){ 
            $(".h-mn-container").css({"width":"100%"});
        };
        $(".mn-popup").hide();
        $("#b-"+$(this).attr("id")).show();
        $(".h-mn-popup-wrap").attr({"id" : $(this).attr("id") + "-pw"});
    });
    $(".mn-popup .close").bind("click", function() {
        if($.browser.msie && parseInt($.browser.version) == 7 ){ 
            $(".h-mn-container").removeAttr("style");
        };
        $(".h-mn-popup-wrap").removeAttr("id");
        $(this).parents(".mn-popup").hide();
    });

    $(".show-sub").bind("click", function() {
        $(this).parents(".models-nav li").toggleClass("active");
        return false;
    });
});

function calculateCredit() {
    var fields = new Array('price', 'perv_vznos', 'stavka', 'period');

    var error = false;

    for(var i = 0; i < fields.length; i++) {
        if(!checkCalculateField(fields[i])) {
            error = true;
            markAsError(fields[i]);
        } else {
            clearMark(fields[i]);
        }
    }

    if(error) {
        $('fieldset.field3 table.restable').hide();
    } else {
        var price = parseFloat($('#price').attr('value').replace(",", "."));
        var perv_vznos = parseFloat($('#perv_vznos').attr('value').replace(",", "."));
        var startPrice = price - ((price / 100) * perv_vznos);
        var stavka = parseFloat($('#stavka').attr('value').replace(",", "."));
        var period = parseFloat($('#period').attr('value').replace(",", "."));

        var monthPercent = (stavka / 12) / 100;

        var k = (monthPercent * Math.pow(1 + monthPercent, period)) / (Math.pow(1 + monthPercent, period) - 1);
        var s1 = (startPrice * k) . toFixed(2);

        var s2 = (s1 * period) . toFixed(2);
        // total_expenditures
        $('#total_expenditures').html(s2 + ' ' + $('#valuta').val());
        // monthly_payment
        $('#monthly_payment').html(s1 + ' ' + $('#valuta').val());
        $('fieldset.field3 table.restable').show();
    }

    return false;
}

function checkCalculateField(id) {
    var value = $('#' + id).attr('value');

    if(!value) {
        return false;
    }

    value = value.replace(",", ".");

    if(parseFloat(value) != value) {
        return false;
    }

    return true;
}

function markAsError(id) {
    $('#' + id).parents('tr').find('label').addClass('error');
}

function clearMark(id) {
    $('#' + id).parents('tr').find('label').removeClass('error');
}

