$(document).ready(function()
{
  var
    keywLoaded = [],
    graphLoaded = [],
    keywOpened = {},
    graphOpened = {}
    ;
  
  $("a.keyw_down").click(function()
  {
    var id = parseInt($(this).attr("rel"));
    
    if ($.inArray(id, keywLoaded) > -1)
    {
      $("#keyw_" + id).slideDown("fast");
    }
    else
    {
      $("#keyw_" + id)
        .width (32)
        .height(32)
        .css("background", "url('/media/images/lightbox-ico-loading.gif') top left no-repeat")
        .load($(this).attr("href")+'?dc=1', function()
        {
          $(this)
            .css("width"     , "")
            .css("height"    , "")
            .css("background", "")
            .hide()
            .slideDown("fast")
            ;
          keywLoaded.push(id);
        });
    }
    
    if ((typeof graphOpened[id] != 'undefined') && (graphOpened[id] == 1))
    {
      $("a.graph_up[rel=" + id + "]").click();
    }

    $("a.keyw_up[rel=" + id + "]").show();
    $("#kol_" + id).hide();
    keywOpened[id] = 1;
    
    return false;
  });
  
  $("a.keyw_up").click(function()
  {
    var id = parseInt($(this).attr("rel"));
    
    $("#keyw_" + id).slideUp("fast");
    $("#kol_" + id).show();
    $(this).hide();
    keywOpened[id] = 0;
    
    return false;
  });
  
  $("a.graph_down").click(function()
  {
    var id = parseInt($(this).attr("rel"));
    
    if ($.inArray(id, graphLoaded) == -1)
    {
      swfobject.embedSWF(
        "/media/open-flash-chart.swf",
        "fgraph_inner_" + id,
        "100%",
        "300",
        "9.0.0",
        "expressInstall.swf",
        {
          "data-file": "/stats/project_graph_data/" + id,
          "loading": "Загрузка данных диаграммы..."
        }
      );
      $("<b style=\"display:block;padding-bottom:4px\">Видимость сайта по целевым запросам</b>")
        .prependTo("#fgraph_" + id);
      $.getJSON(
        "/stats/project_graph_data/" + id,
        function(data)
        {
          var cur, legend = {}, out, first = true;
          
          for (j = data["elements"].length, i = 0; i < j; ++i)
          {
            cur = data["elements"][i];
            legend[cur["dot-style"]["tip"].match(/^([^<]+)/)[1]] =
              cur["dot-style"]["colour"];
          }
          
          out = "<div class=\"legend\">";
          
          for (var i in legend)
          {
            out += "<b style=\"line-height:16px;color:" + legend[i] + "\">"
              + "<span style=\""
              + (first ? "vertical-align:-3px;" : "")
              + "font-size:" + (first ? 29 : 16) + "px\">"
              + (first ? "–" : "—") + "</span> "
              + i + "</b><br />";
              
            first = false;
          }
          
          out += "</div>";
          $(out).appendTo("#fgraph_" + id);
        }
      );
      $("#fgraph_" + id).hide();
      graphLoaded.push(id);
    }
    
    if ((typeof keywOpened[id] != 'undefined') && (keywOpened[id] == 1))
    {
      $("a.keyw_up[rel=" + id + "]").click();
    }

    $("#fgraph_" + id).slideDown("fast");
    $("a.graph_up[rel=" + id + "]").show();
    $("#gol_" + id).hide();
    graphOpened[id] = 1;
    
    return false;
  });
  
  $("a.graph_up").click(function()
  {
    var id = parseInt($(this).attr("rel"));
    
    $("#fgraph_" + id).slideUp("fast");
    $("#gol_" + id).show();
    $(this).hide();
    graphOpened[id] = 0;
    
    return false;
  });
  
  var anc = document.location.href.match(/#(.+)$/);
  
  if (anc !== null && anc.length > 1)
  {
    //alert(anc[1]);
    $("a.keyw_down", $("a[name=\"" + anc[1] + "\"] + table")).click();
  }
});
