function rand() {
  return Math.floor(Math.random()*$(".preview").length);
}
function iegogo() {
  $("a[rel=fb]").fancybox({'overlayShow': false});
  $("#mycarousel-collaction").jcarousel();
}
$(document).ready(function(){
  var _coll = false;
  $(".preview:first").animate({"opacity": 1}, 500);
  $(".theshow").hide();

  $('#mycarousel-txt').jcarousel({
    scroll: 1,
    vertical: false,
    auto: 1000
  });

  $('#mycarousel-blog').jcarousel({
    scroll: 2,
    vertical: false,
    auto: 1000
  });

  $('#mycarousel-collaction').jcarousel({
    scroll: 7,
    vertical: false,
    auto: 1000
  });

 /*$('#in_show').jcarousel({
    scroll: 1,
    vertical: false,
    auto: 1000
  }); */

  $('#mycarousel-collaction-big').jcarousel({
    scroll: 1,
    vertical: false,
    auto: 1000
  });



  $('.in-collaction-big .close').live('click',function(){
    $('.in-collaction-big').hide();
  });

  $('.in-blog-big .close').live('click',function(){
    $('.in-blog-big').hide();
  });

  // пункты главного меню
  $(function() {
    var $list   = $('#st_nav');

    $list.find('.left-menu-down').live('click',function(){
      var $this = $(this);
      hideThumbs();
      $this.addClass('left-menu-up');
      var $elem = $this.closest('li');
      if (!$elem.hasClass("current"))
        $elem.addClass('current').animate({'height':'230px'}, 200);
      $(".txt-home").fadeOut("slow");
      $.fancybox.close();
      // переключение фоновых девочек
      var r = rand();

      $(".in-blog-big").fadeOut("slow");// прячем блог
      $('#bg-slider').remove();// убиваем слайдер фона

      $(".theshow").animate({"opacity": 0}, 500);
      if ($this.parents("li").index() == 1) {
        // мы в коллекциях
        _coll = !_coll;
        if (_coll) {// мы открываем коллекции
          $(".gallery:first").trigger("click").addClass("gallery-active");
          $(".gallery:not(:first)").removeClass("gallery-active");
          $(".preview").animate({opacity: 0}, 500).removeClass("active_bg");// прячем четких телок
          $(".blur:eq("+r+")")
            .addClass("active_bg")
            .animate({"opacity": 1}, 3000)
            .siblings(".blur")
            .animate({"opacity": 0}, 1500)
            .removeClass("active_bg");
        } else { // мы закрываем коллекции
          var _idx = $(".active_bg").attr("alt");
          $(".blur").animate({opacity: 0}, 500).removeClass("active_bg");// прячем четких телок
          $(".preview[alt="+_idx+"]").animate({opacity: 1}, 1500).addClass("active_bg");
        }
      }
      else {
        // мы в обычном разделе
        $(".blur").animate({opacity: 0}, 500).removeClass("active_bg");
        $(".preview:eq("+r+")")
          .addClass("active_bg")
          .animate({"opacity": 1}, 3000)
          .siblings(".preview")
          .animate({"opacity": 0}, 1500)
          .removeClass("active_bg");
      }

    });

    function hideThumbs(){
          $list.find('li.current')
          .animate({'height':'45px'},400,function(){$(this).removeClass('current');})
          .find('.st_thumbs_wrapper')
          .hide(200)
          .andSelf()
          .find('.left-menu-down')
          .removeClass('left-menu-up');
    }

    $list.find('.left-menu-up').live('click',function(){
      var $this = $(this);
      $this.removeClass('left-menu-up');
      hideThumbs();
    });
  });

  //blog
  $('#mycarousel-blog a, .blog_navi a').live("click", function(){
    var ID  = $(this).attr("name");
    if(ID){
      $.ajax({
        type: "POST",
        url: '/index-ajax.php',
        data: {q: 'assets/snippets/mysnippets/ajax_blog.php','ajax':'blog','lastindex': ID},
        cache: false,
        success: function(html){
          $(".in-blog-big").css("display", "block");
          json = $.parseJSON(html); // парсим ответ на поля
          $(".in-blog-big .in-blog-big-small").html(json.content).fadeIn("slow"); // педалим контент
          $('.in-blog-big .in-blog-big-small #mycarousel-blog').jcarousel({ // вешаем на него прокрутку
            scroll: 2,
            vertical: false,
            auto: 1000
          });
          // СЕО
          $("title").html(json.pagetitle);
          $("meta[name=keywords]").attr("content", json.keywords);
          $("meta[name=description]").attr("content", json.description);
          // фанси на фотки блога
          $("a[rel=zoom]").fancybox({'overlayShow': false});
          return false;
        }
      });
    }
    return false;
  });

  //images
  $('.gallery').live("click", function(){
    var ID  = $(this).attr("name");
    $("#fancybox-wrap").fadeOut('slow');
    $(this).addClass("gallery-active");
    $(".gallery").not(this).removeClass("gallery-active");
    if(ID){
      $.ajax({
        type: "POST",
        url: '/index-ajax.php',
        data: {q: 'assets/snippets/mysnippets/ajax_blog.php','ajax':'gallery','id': ID},
        cache: false,
        success: function(html){
          json = $.parseJSON(html);
          $(".in-collaction:eq(0)").html(json.gallery);
          $("title").html(json.pagetitle);
          $("meta[name=keywords]").attr("content", json.keywords);
          $("meta[name=description]").attr("content", json.description);
          $("a[rel=fb]").fancybox({'overlayShow': false});
          $("#mycarousel-collaction").jcarousel();
          return false;
        }
      });
    }
    if($.browser.msie) {
      //alert("Sorry your browser not supported");
      setTimeout(function(){
        $("a[rel=fb]").fancybox({'overlayShow': false});
        $("#mycarousel-collaction").jcarousel();
      }, 200);
    }
    return false;
  });

  $(".theshowsrc").live("click", function(){
    $(".theshow").attr("src", $(this).attr("href")).load(function(){
      $(".preview, .blur").animate({opacity: 0}, 1000).removeClass("active_bg");
      $(".theshow").show().animate({"opacity": 1}, 1500);
      return false;
    });
    return false;
  });


  $(".google-map").live("click", function (){
    $(".google-content").fadeOut();
    $(this).next(".google-content").fadeToggle("slow");
    return false;
  });

  $(".google-content .close").live("click", function(){
    $(this).parents(".google-content").fadeOut("slow");
  });


  $("a[rel='zoom'], a[rel='fb'], .zoom_contact, .zoom, .cont-link, #mycarousel-blog-big img").fancybox({'overlayShow': false});
  $('#fancybox-close').html("Закрыть");

  $("ul#mycarousel-collaction li").live("mouseover mouseout", function(event) {
  if ( event.type == "mouseover" ) {
    $(this).find("img").addClass("click").stop().animate({'margin-top': '-5px', 'width': '80px', 'height': '120px', 'opacity': '1', 'border': '1px solid orange', 'position': 'absolute', 'z-index': '200'});
  } else {// возвращаем обратно
    $(this).find("img").removeClass("click").stop().animate({marginTop: '0px', width: '70px', height: '104px', opacity: '0.6', border: '0'});
  }
});

});
