$(function()
{
  $(".more").each(function()
  {
    var $this = $(this);
    $this.children().children("span.dropdown").click(function()
    {
      $this.children(".hidden").slideToggle("def");
      $(this).toggleClass("true");
    });
  });
  if ($.browser.msie && parseInt(jQuery.browser.version) < 7) {
    try {
      DD_belatedPNG.fix(".png");
    } catch (error) { }
  }
  setPageBreak();
  setCasualImagePreviewIndex();
  setCasualImagePreviewCustom();
});

function setPageBreak()
{
  var text =
  {
    "ru": ["Посмотреть все", "Свернуть"],
    "en": ["Show all", "Close"]
  }
  $(".news_block").find(".item").each(function(i)
  {
    var item = $(this);
    item.find("div").each(function()
    {
      var split = $(this);
      var style = split.css("page-break-after");
      if (style == "always") {
        split.nextAll().wrapAll('<div id="sh' + i + '" class="hidden"></div>');
        var a = $('<a href="" class="js">' + text[lang][0] + '</a>');
        a.toggle(function()
        {
          this.innerHTML = text[lang][1];
        }, function()
        {
          this.innerHTML = text[lang][0];
        });
        a.bind("click", function()
        {
          $("#sh" + i).slideToggle("def");
          return false;
        });
        $("#sh" + i).after(a.wrap("<div></div>"));
      }
    });
  });
}

function setCasualImagePreviewIndex()
{
  $(".news_block.news_block_index").find(".item").each(function(i)
  {
    var last = null;
    var item = $(this);
    item.find("img").each(function()
    {
      var img = $(this);
      if (!/\/upload\/image\/casual\/thumbs\//.test(this.src)) return;
      var link = img.parent("a:not(.clicked)"); last = link;
      var untrack = 0;
      link.addClass("casual").bind("click", function()
      {
        if ($("#nbp" + i).is(":visible") && link.children("div").hasClass("this")) {
          item.find("a.clicked").removeClass("clicked").children("div").removeClass("this");
          $("#nbp" + i).slideUp("def");
          return false;
        }
        item.find("a.clicked").removeClass("clicked").children("div").removeClass("this");
        link.addClass("clicked").children("div").addClass("this");
        var view = $("#nbp" + i).html('\
          <b><img src="' + link.attr("href") + '" alt=""></b>\
          <i>' + img.attr("alt") + '</i>\
        ').find("img")[0];
        function load()
        {
          if ((view.width == 0 || view.height == 0) && untrack < 5) {
            untrack++; link.click();
          }
          var span = document.createElement("span");
          span.className = "png hidden";
          span.style.width = 96;
          span.style.height = 34;
          span.style.left = (view.width / 2 - 48);
          span.style.top = (view.height / 2 - 17);
          span.style.backgroundImage = "url(" + host + "/design/images/website/close_" + lang + ".png)";
          span.onmouseover = function() { this.className = "over"; }
          span.onmouseout = function() { this.className = ""; }
          span.onclick = function()
          {
            item.find("a.clicked").removeClass("clicked").children("div").removeClass("this");
            $("#nbp" + i).slideUp("def");
          }
          var div = $("<div></div>").css({"width": view.width, "height": view.height}).html(span);
          div.css("background-image", "url(" + view.src + ")");
          div.hover(function() { div.children().show() }, function() { div.children().hide() });
          $(view).replaceWith(div);
          $("#nbp" + i).slideDown("def");
        }
        if (navigator.userAgent.toLowerCase().indexOf("chrome") > -1) load(); else view.onload = load;
        return false;
      });
      img.wrap("<div><i></i></div>");
    });
    if (last != null) last.after('<div id="nbp' + i + '" class="casual"></div>');
  });
}

function setCasualImagePreviewCustom()
{
  function explore(block, perform, i, dir)
  {
    i += dir;
    if (dir < 0 && i < 0) i = perform.length - 1;
    if (dir > 0 && i > perform.length - 1) i = 0;
    block.find("i").html((i + 1) + (lang == "ru" ? " из " : " of ") + perform.length);
    block.find("span").html(perform[i]['alt']);
    var img = block.find("img");
    img.fadeTo(125, 0, function()
    {
      img.attr({
        "src": perform[i]['src'],
        "alt": perform[i]['alt']
      });
    });
    if (navigator.userAgent.toLowerCase().indexOf("chrome") > -1) img.fadeTo(125, 1);
    else img.load(function() { img.fadeTo(125, 1); });
    return i;
  }
  
  $(".news_block.news_block_custom").find(".item").each(function()
  {
    var first = null;
    var perform = new Array(); var i = 0;
    $(this).find("img").each(function()
    {
      var img = $(this);
      if (!/\/upload\/image\/casual\/thumbs\//.test(this.src)) return;
      var link = img.parent("a");
      perform.push({"src": link.attr("href"), "alt": this.alt});
      if (first == null) first = link; else link.remove();
    });
    if (first != null && perform.length > 0) {
      for (var k in perform) {
        var preload = new Image();
        preload.src = perform[k]['src'];
      }
      var embed = '\
        <div class="casual2">\
          <div>\
            <b class="back"></b>\
            <i>1 из ' + perform.length + '</i>\
            <b class="forward"></b>\
            <span>' + perform[i]['alt'] + '</span>\
          </div>\
          <img src="' + perform[i]['src'] + '" alt="' + perform[i]['alt'] + '">\
        </div>\
      ';
      var parent = first.parent();
      first.replaceWith(embed);
      parent.find("b.back").bind("click", function() { i = explore(parent, perform, i, -1) });
      parent.find("b.forward, img").bind("click", function() { i = explore(parent, perform, i, 1) });
    }
  });
}