
var menoCarousel

$(function(){
  var totalLi = $("#moklist li").length;
  for (var i = 0; i < totalLi; i++) {
    $(".moknavi").append("<span></span>");
  }
  
  totalElm = $(".moknavi span").length;
  $(".moknavi span:first-child").addClass("active");
//
//  $(".carousel").jCarouselLite({
//    btnNext: "#gonext",
//    btnPrev: "#goback",
//    visible: 1,
//    scroll:1
//  });

if($(".carousel").length) {
    jQuery.jQueryRandom = 0;
    jQuery.extend(jQuery.expr[":"], {
        random: function(a, i, m, r) {
            if (i == 0) {
                jQuery.jQueryRandom = Math.floor(Math.random() * r.length);
            }
            return i == jQuery.jQueryRandom;
        }
    });

    var $items = $(".moknavi span");
    var $images = $("#moklist li");
    $items.css({
        cursor: "pointer"
    });

    // $images.find("img").show().css("opacity", "0").load(function(){
        // $(this).css("opacity", "1");
    // });

    $items.click(function(e){
        e.preventDefault();
        menoCarousel($(this).index());
        attachInterval();
    });

    var nextIndex, currentIndex = 0;
    var prevIndexCache = new Array();
    var imageLoading = true;
    function menoCarousel(index, movePhoto) {
        movePhoto = (typeof movePhoto == 'undefined') ? 1 : 0;

        if(prevIndexCache.length > 3) {
            prevIndexCache = new Array();
        }

        prevIndexCache.push(currentIndex);

        $items.removeClass("active");

        if("next" == index) {
            nextIndex = (currentIndex + 1 < $items.length) ? currentIndex + 1 : 0;
        }
        else if("prev" == index) {
            nextIndex = (currentIndex - 1 >= 0) ? currentIndex - 1 : $items.length - 1;
        }
        else if("random" == index) {
            do {
                nextIndex = $items.filter(":not(.active)").filter(":random").index();
            } while($.inArray(nextIndex, prevIndexCache) >= 0);
        }
        else if(typeof index == "number" && index < $items.length && index >= 0) {
            nextIndex = index;
        }
        if(typeof nextIndex == 'number') {
            $($items.get(nextIndex)).addClass("active");

            if(movePhoto == 1) {
                var $currentImage = $($images.get(nextIndex));
				
                $currentImage.addClass("loading");
				$currentImage.find("img").show();
//                $currentImage.find("img").load(function(){
//                    $(this).css('display', 'block');
//                });

                var offset = $currentImage.offset();
                var left = -1 * nextIndex * 689;

                $("#moklist").css("left", left + "px")
            }

            currentIndex = nextIndex;
        }
    }

    var menoCarouselInterval;
    function attachInterval() {
        clearInterval(menoCarouselInterval);
        menoCarouselInterval = setInterval(function(){
            menoCarousel('random');
        }, 3000)
    }

    menoCarousel(0);
    attachInterval();

    
}

    $("#gonext").click(function(e){
        e.preventDefault();
        menoCarousel("next");
        attachInterval();
    });
    $("#goback").click(function(e){
        e.preventDefault();
        menoCarousel("prev");
        attachInterval();
    });
  
  $(".mg-car").jCarouselLite({
    btnNext: ".next",
    btnPrev: ".prev",
    visible: 3,
    scroll:3,
    mouseWheel: true
  });
    $('#mg-img img').load(function(){
      $("#mg-img img").fadeIn("fast");
    });
        
    $(".mg-car a").click(function(){
      var largePath = $(this).attr("href");
      var largeAlt = $(this).find("img").attr("alt");
      if(largePath != $("#mg-img img").attr("src")) {
      $("#mg-img img").fadeOut("slow", function() {
        $("#mg-img img").attr({src: largePath, alt: largeAlt});
      });
      }
      return false;
    });
  
//select  
//$("select.select").css({opacity:'0', filter:'alpha(opacity=0)'});
$("select.select").css('opacity', 0);
$("<span/>").addClass("styled").insertBefore("select.select");
$("select.select").change(function () {
  var str = "";
  var selectWidth = $(this).width();
  var dataVals = "";
  $(this).closest('.mokselect').find('select.select option:selected').each(function () {
    dataVals += $(this).text() + " ";
  });
  if(!$.trim(dataVals)) dataVals = 'Ыўсющ';

  $(this).closest('.mokselect').find('span.styled').width(selectWidth + 2 + 'px').html(
  "<span class='selecttext'>" + dataVals + "</span><span class='selectbutton'></span>"
  );
}).change();
//end style select begin style input type="radio"
var $mockRadio = $("label.mokradio");
$mockRadio.append("<span></span>");
$mockRadio.find('input').css({display:'none'})
$mockRadio.click(function(){
  $("label.mokradio span").removeClass("active");
  $(this).find("span").addClass("active");
});
$mockRadio.find('input:checked').parent().find("span").addClass("active")
//end style input type="radio" begin style input type="checkbox"

var $mockCheckbox = $("label.mokcheckbox");

$mockCheckbox.append("<span></span>");
$mockCheckbox.find('input').css({display:'none'})
$mockCheckbox.find('input:checked').parent().find("span").addClass("active")
$mockCheckbox.toggle(
  function () {
    $(this).find('span').addClass("active");
    $(this).find('input').attr('checked', true);
  },
  function () {
    $(this).find('span').removeClass("active");
    $(this).find('input').attr('checked', false);
  }
);
// end

    if($.browser.msie && $.browser.version=="6.0") {
        $(".style_select_ie").live("click", function(e){
            if('LI' == e.target.tagName) {
                var $li = $(e.target);
                var $option = $($(this).parent().find("select").find("option").get($li.index()));
                $option.attr("selected", "selected")

                $(this).parent().find("select").trigger("change")

                $(this).hide();
            }
        })
        .live("mouseout", function(e){
//            console.log(e.target.tagName)
            if(e.target && (e.target.tagName != "LI")) {
                $(this).hide();
            }
        })


        $(".mokselect select").css("visibility", "hidden");

        $(".mokselect .styled").click(function(){
            var $select = $(this).parent().find("select");
            var selectPosition = $select.position();

            if(!$(this).parent().has("ul").length) {

                var $optionsList = $("<ul />").addClass("style_select_ie");
                $optionsList.css({
                    width: $select.width() + 2 - 8 - 10,
                    left: selectPosition.left - 40 + "px",
                    top: selectPosition.top + $select.height() + 2 - 20
                });

               var li = $("<li />").css({
                   backgroundColor: "transparent"
               });
               $optionsList.append(li);


               $.each($(this).parent().find("option"), function(i){
                    var t = $(this).text();
                    if(t) {
                        var li = $("<li />");
                        li.text(t)
                        $optionsList.append(li);
                    }
                })

                    $optionsList.css("backgroundColor", "#ffffff");
                $(this).parent().prepend($optionsList).mouseover();
            }
            else {
                $(this).parent().find("ul").show().mouseover();
            }
        })
    }

  
  $(".tab li:nth-child(even)").addClass("color");
  $(".table tr:nth-child(even)").addClass("color");
  /*
  $(".feedback input, .feedback textarea").focus(function(){
         $(this).addClass("light");
    });
  $(".feedback input, .feedback textarea").focusout(function(){
         $(this).removeClass("light");
    });
  */
});


$(function(){
   if(true || !$.browser.mozilla) {
       return;
   }

    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "/template/js/jquery-ui-1.8.7.custom.min.js";

    $(script).load(function(){
        $(document.body).css("overflow", "hidden").css("min-width", "970px");

        var $scrollbar = $(".scrollbar");
        var $sliderDiv = $("<div />").addClass("slide_container");
        var $window = $(window);
        var $wrap = $("#wrap");
        var delta = $wrap.height() - $window.height();

        $scrollbar.prepend('<div class="scroll_up"><span>&uarr;</span></div>')
                  .append('<div class="scroll_down"><span>&darr;</span></div>');

        $window.resize(function(){
            delta = $wrap.height() - $window.height();

            if(delta > 0) {
                $scrollbar.show();

                $sliderDiv.slider({
                    orientation: "vertical",
                    value: delta,
                    min: 0,
                    max: delta,
                    change: function(e, ui){
                        $window.scrollTop(delta - ui.value);
                    },
                    slide: function(e, ui){
                        $(this).slider("value", ui.value);
                    }
                });
                $scrollbar.append($sliderDiv);


                $sliderDiv.height($window.height() - parseInt($sliderDiv.css("margin-top")) * 2);
            }
            else {
                $scrollbar.hide();
            }
        })
        .trigger("resize")

        $scrollbar.find(".scroll_down").click(function(e){
            e.preventDefault();
            $sliderDiv.slider("value", $sliderDiv.slider("value") - 30);
        })
        $scrollbar.find(".scroll_up").click(function(e){
            e.preventDefault();
            $sliderDiv.slider("value", $sliderDiv.slider("value") + 30);
        })
    })

    document.body.appendChild(script);
})
