var scroll = -1;
var ie6 = function()
{
  $("#overlay, #loading").width($("body").width()).height($("body").height());
  $("#layout").width($(window).width()).height($(window).height());
}

$(function()
{
  if (typeof host == "undefined" || typeof lang == "undefined" || typeof cmd == "undefined") return;
  var langs = {"en": "English", "ru": "Русский"};
  var a = $('<a href="' + host + '/' + lang + '/">' + langs[lang] + '</a>');
  a.css("background-image", 'url("' + host + '/world/design/images/website/flag_' + lang + '.gif")');
  a.click(function()
  {
    $("#lang > .drop").slideToggle("def");
    return false;
  });
  a.blur(function(){ $("#lang > .drop").slideUp("def"); });
  $("#lang > .draw > div").hover(function() { $(this).addClass("over") }, function() { $(this).removeClass("over") }).html(a);
  for (var _lang in langs) {
    if (_lang != lang) {
      var vhost = _lang == "ru" ? "http://www.helixadvance.com" : host;
      a = $('<a href="' + vhost + (_lang == "ru" ? '/" class="lang-ru' : '/' + _lang + '/' + cmd) + '">' + langs[_lang] + '</a>');
      a.css("background-image", 'url("' + host + '/world/design/images/website/flag_' + _lang + '.gif")');
      $("#lang > .drop").append(a.wrap("<div></div>"));
    }
  }
  $("#layout > .close").click(function()
  {
    $("#layout > .body").empty();
    $("#overlay, #layout").fadeOut("def");
    $("html, body").animate({scrollTop: scroll}, 500, "linear");
    return false;
  });
  if ($.browser.msie && $.browser.version < 7) $(window).resize(function() { ie6(); });
  get_page(); get_layout(); get_mail();
  $.history.init(page);
});

function page(href)
{
  var logo = $("#logo > a").unbind()[0];
  if (href == "") {
    if (cmd != "" && cmd != "manifest") return preview();
    href = "manifest";
  }
  scroll = 0;
  $("#loading").show();
  $("#layout > .close").click();
  if (href == "manifest") {
    $("#logo, #copyright").hide();
    $("#page, #content").addClass("index");
  } else {
    logo.className = "get-page";
    logo.href = "manifest";
    $("#logo, #copyright").show();
    $("#page, #content").removeClass("index");
  }
  $.post(href, {"ajax": 1}, function(data)
  {
    var query = "#" + href.replace(/^.*\//, "");
    $("#lang > .drop").find("a:not(.lang-ru)").each(function()
    {
      var split = this.href.split("#");
      this.href = split[0] + (query == "#" || query == "#manifest" ? "" : query);
    });
    $("#content").html(data);
    get_page(); get_layout(); get_mail(); preview();
    $("#loading").hide();
  });
}

function layout(href, opacity, content, _scroll)
{
  $("#layout > .page > .body").html("");
  if (typeof opacity == "undefined") opacity = 1;
  $("#overlay").fadeTo("def", opacity);
  if ($.browser.msie && $.browser.version < 7) ie6();
  $("#layout").fadeIn("def");
  if (typeof _scroll == "undefined") {
    scroll = $(window).scrollTop();
    $("html, body").animate({scrollTop: 0}, 500, "linear");
  }
  if (typeof content == "undefined") {
    $.post(href, {"ajax": 1}, function(data)
    {
      $("#layout > .page > .body").html(data);
      get_mail();
    });
  } else $("#layout > .page > .body").html(content);
  $("html").bind("keydown", function(event)
  {
    if (event.keyCode == 27) {
      $(this).unbind("keydown");
      $("#layout > .close").click();
    }
  });
}

function get_mail()
{
  $(".get-mail:not(.fixed)").each(function()
  {
    this.innerHTML = this.innerHTML.replace(" at ", "@");
    if (this.tagName.toLowerCase() == "a") this.href = "mailto:" + this.innerHTML;
    $(this).addClass("fixed");
  });
}

function get_layout()
{
  $(".get-layout:not(.fixed)").bind("click", function()
  {
    layout(this.href);
    return false;
  }).addClass("fixed");
}

function get_page()
{
  $(".get-page:not(.fixed)").bind("click", function()
  {
    $.history.load(this.href.replace(/^.*\//, ""));
    return false;
  }).addClass("fixed");
}

function _preview(seq, i, scroll)
{
  var splitted = seq.split("|");
  if (i < 0) i = splitted.length - 1;
  else if (i >= splitted.length) i = 0;
  var link = document.getElementById(splitted[i]);
  var prev = document.getElementById(splitted[i-1] || splitted[splitted.length-1]);
  var next = document.getElementById(splitted[i+1] || splitted[0]);
  
  $("html").unbind("keydown").bind("keydown", function(event)
  {
    if (event.keyCode == 37) _preview(seq, i - 1, 1);
    if (event.keyCode == 39) _preview(seq, i + 1, 1);
  });
  
  var alt = $(link).find("img")[0].alt.split(" / ");
  var javascript = '\
    <script type="text/javascript">\
      var seq = "' + seq + '";\
    </script>\
  ';
  var content = '\
    <div class="preview">\
      <div class="header">\
        <span class="title">' + (alt[0] || '') + '</span>\
        <span class="label">' + (alt[1] || '') + '</span>\
      </div>\
      <div style="padding-bottom: 60px">\
        <table align="center">\
        <tr>\
          <td class="middle" style="width: 50px">\
            <a href="' + prev.href + '" class="prev"\
             onclick="return _preview(seq, ' + (i - 1) + ', 1)"></a>\
          </td>\
          <td><img src="' + link.href + '" alt="" id="image"\
           onclick="return _preview(seq, ' + (i + 1) + ', 1)"></td>\
          <td class="middle" style="width: 50px">\
            <a href="' + next.href + '" class="next"\
             onclick="return _preview(seq, ' + (i + 1) + ', 1)"></a>\
          </td>\
        </tr>\
        </table>\
      </div>\
    </div>\
  ';
  layout(undefined, 1.0, javascript + content, scroll);
  return false;
}

function preview()
{
  if (!document.getElementById("preview")) return;
  var blocks = $("#preview > .box > div > a");
  var seq = new Array();
  blocks.each(function(i)
  {
    var pic = $(this).find("img")[0];
    var alt = pic.alt.split(" / ");
    var mask = $('<div class="mask"></div>');
    var over = $('<table />',
    {
      className: "over",
      width: parseInt(pic.width) + 10,
      height: parseInt(pic.height) + 10,
      html: '\
        <tbody>\
          <tr>\
            <td>\
              <div class="title">' + (alt[0] || '') + '</div>\
              <div class="label">' + (alt[1] || '') + '</div>\
              <div class="zoom"></div>\
            </td>\
          </tr>\
        </tbody>'
    });
    mask.css("opacity", 0.8);
    mask.width(parseInt(pic.width) + 10).height(parseInt(pic.height) + 10);
    $(this).append(mask, over);
    $(this).hover(function(){ mask.show(); over.show(); },
     function() { mask.hide(); over.hide(); });
    $(this).click(function()
    {
      _preview(seq.join("|"), i);
      return false;
    });
    seq.push(this.id);
  });
  if ($("#preview").hasClass("autoshow")) {
    if ($("#preview > .box").length > 0) {
      $("#preview > .box:eq(0) > div > a").click();
    }
  }
}

