function resizePopup(obj)
{
  $("#popup_hint").width($(obj).width()).height($(obj).height());
}

function setPhoto(src, sub)
{
  document.getElementById("mainphoto").src = src;
  document.getElementById("mainphotosub").innerHTML = sub;
}

(function($)
{
  $(document).ready(function()
  {
    // Pop-up hints
    $(".popup_pic")
      .hover(
        function(ev)
        {
          $("#popup_hint")
            .html("<img src=\"" + $(this).attr("href") + "\" onload=\"resizePopup(this);\">")
            .show()
            .css("top", ev.pageY + 20)
            .css("left", ev.pageX + 20 + (thisIsIE7 ? 483 : 0))
            ;
        },
        function(ev)
        {
          $("#popup_hint").hide();
        }
      )
      .mousemove(function(ev)
      {
        $("#popup_hint")
          .css("top", ev.pageY + 20)
          .css("left", ev.pageX + 20 + (thisIsIE7 ? 483 : 0))
          ;
      })
      .click(function(ev)
      {
        ev.preventDefault();
      });
    // =========================================================================
    
    var maxhei = 0, buf = [], ptr = 1;
    
    function slideBuf()
    {
      var oldptr = ptr;
      ++ptr;
      
      if (ptr >= buf.length)
      {
        ptr = 0;
      }
      
      $(buf[oldptr]).fadeOut(
        500,
        function()
        {
          $(buf[ptr]).fadeIn(500);
        }
      );
    }
    
    buf = $("div[rel=rand]");

    $(buf).height(320).hide();
    $("div[rel=rand]:first-child").show();
    ptr = 0;
    if (buf.length > 1)
    {
      setInterval(slideBuf, 7000);
    }

    var maxhei2 = 0, buf2 = [], ptr2 = 1;
    
    function slideBuf2()
    {
      var oldptr2 = ptr2;
      ++ptr2;
      
      if (ptr2 >= buf2.length)
      {
        ptr2 = 0;
      }
      
      $(buf2[oldptr2]).fadeOut(
        500,
        function()
        {
          $(buf2[ptr2]).fadeIn(500);
        }
      );
    }
    
    buf2 = $("div.rand2");

    buf2.height(200).hide();
    $("div.rand2:first-child").show();
    ptr2 = 0;
    if (buf2.length > 1)
    {
      setInterval(slideBuf2, 5000);
    }

    $("a[rel=lightbox]").lightBox({
      imageLoading : "/media/lightbox/images/lightbox-ico-loading.gif",
      imageBtnClose: "/media/lightbox/images/lightbox-btn-close.gif",
      imageBtnPrev : "/media/lightbox/images/lightbox-btn-prev.gif",
      imageBtnNext : "/media/lightbox/images/lightbox-btn-next.gif",
      imageBlank   : "/media/lightbox/images/lightbox-blank.gif"
    });
    
    // copy-paste is our everything...
  
    // =======================================================================
    // My Lightbox
    // =======================================================================
    
    var lightIsOpened = false, lightSpeed = "normal";
    
    lightOpen = function(x, y, content)
    {
      var worker = function()
      {
        $("#my_lightbox")
          .css("width", x)
          .css("height", y)
          .css("left", Math.floor(($(window).width() - x - 60) / 2)  + (thisIsIE7 ? 483 : 0))
          .css("top", $(document).scrollTop() + Math.floor(($(window).height() - y - 60) / 2))
          .fadeIn(lightSpeed, function()
          {
            $("#my_lightbox_close").click(function(ev)
            {
              ev.preventDefault();
              lightClose();
            });

            lightIsOpened = true;
          });
        $("#my_lb_content").html(content);
      }
      
      if (lightIsOpened)
      {
        $("#my_lightbox").fadeOut(lightSpeed, worker);
      }
      else
      {
        $("#gray_bg")
          .css("width" , $(document).width())
          .css("height", $(document).height())
          .css("top", "0px")
          .css("left", thisIsIE7 ? "483px" : "0px")
          .click(lightClose)
          .fadeIn(lightSpeed, worker);
      }
    }
    
    lightClose = function()
    {
      if (!lightIsOpened)
      {
        return;
      }
      
      $("#my_lightbox").fadeOut(lightSpeed, function()
      {
        $("#my_lightbox_prev").css("display", "block");
        $("#my_lightbox_next").css("display", "block");
        $("#gray_bg").fadeOut(lightSpeed);
      });
      
      lightIsOpened = false;
    }
    
    // =======================================================================
    // Lightbox replacement
    // =======================================================================
    
    var photos = {}, photoPtr = {}, curHash;
    
    function lightRegister(objs, hash, renderer, onclick)
    {
      onclick = onclick || function(obj, id)
      {
        var data = renderer(obj, id);
        lightOpen(data.width, data.height, data.content);
      }
      
      if (typeof photos[hash] == "undefined")
      {
        photos[hash]        = [];
        photoPtr[hash]      = 0;
        $(objs).each(function()
        {
          photos[hash].push(this);
          var photoId = photos[hash].length - 1;
          
          $(this)
            .click(function(ev)
            {
              ev.preventDefault();
              photoPtr[hash] = photoId;
              curHash = hash;
              onclick(this, photoId);
            });
        });
      }
    }
    
    lightMove = function(delta)
    {
      photoPtr[curHash] += delta;
      
      if (photoPtr[curHash] < 0)
      {
        photoPtr[curHash] = photos[curHash].length - 1;
      }
      else if (photoPtr[curHash] >= photos[curHash].length)
      {
        photoPtr[curHash] = 0;
      }
      
      $(photos[curHash][photoPtr[curHash]]).click();
    }
    
    // Register projects handler
    
    lightRegister(
      $("a[rel=mylight]"),
      "mylight",
      function(){},
      function(obj, id)
      {
        $.get(
          $(obj).attr("href"),
          function(data)
          {
            data = data.replace(
              /^[^\x01]+CONTENT_START\x20\-\-\x3E([^\x01]+)\x3C\x21\-\-\x20CONTENT_END[^\x01]+$/,
              "$1"
            );
            
            lightOpen(
              1000,
              560,
              "<div id=\"projects_popup\">" + data + "</div>"
            );
          },
          "text"
        );      
      }
    );
  });
  

  
  
  
  
  
})(jQuery);

