var l,
t,
relink,
oldhash,
oldgrid,
settings =
  {
    gap: 15,
    width: 200,
    easing: 'easeInOutQuart',
    duration: 200,
    speed: 50
  };

var prevh, prevw, h, thumb;

function sorty(l, prevh, prevw, relink)
  {
    $(l).empty();
    $(l).animate
      (
        {
          height: prevh,
          width: prevw
        },
        settings.speed,
        function()
          {
            $(this).hide();
            $(this).append(relink).delay(settings.duration*4).fadeIn(settings.speed);
          }
      );

    $(l).removeClass('open postwide');
  }

function pageload(hash, tn)
  {
    if (tn === false)
      {
        if (hash)
          {
            $("#grid_loader").fadeIn(100);

            $.ajax
              (
                {
                  url: $('#c-'+hash+' a[grid]').attr('grid'),
                  cache: true,
								  type: 'POST',
									data: 'ajax=1',
                  success: function(html)
                    {
                      $("#grid_loader").fadeOut
                        (
                           200,
                           function()
                             {
                               $("#grid_thumbs").append(html);
                               $("#grid_thumbs").css('width', settings.width*3);
                               $(".project").fadeIn(200);
                             }
                        );
                    }
                }
              );
          }
        else
          {
            $(t).empty();
          }
      }
    else
      {
        l = t;
        t = '#c-' + hash;
        relink = '<a href="' + oldhash + '" grid="' + oldgrid + '" rel="history"><img class="thumbimg" src="' + thumb + '" /></a>';

        h = $(t).height();
        w = $(t).width();

        $(t).prepend('<div class="ploader"></div>');
        $(t + ' .ploader').css
          (
            {
              width: w,
              height: h
            }
          );

        if (hash)
          {
            $("#grid_loader").fadeIn(100);

            $.ajax
              (
                {
                  url: $('#c-'+hash+' a[grid]').attr('grid'),
                  cache: true,
                  type: 'POST',
									data: 'ajax=1',
                  success: function(html)
                    {
                      sorty(l, prevh, prevw, relink);
                      prevh = $(t).height();
                      prevw = $(t).width();

                      $("#grid_loader").fadeOut
                        (
                          200,
                          function()
                            {
                              thumb = $(t).find('img').attr('src');
                              oldhash = $(t).find('a').attr('href');
                              oldgrid = $(t).find('a').attr('grid');
                              $(t).addClass('open postwide');
                              $(t).append(html);

                              _height = $("#c-" + hash + ' .project').height(); // - 30;
                              $(t).css('height', _height);

                              $("#grid_thumbs").grid(settings, t, false, true);

                              $(".thumbimg").each
                                (
                                  function()
                                    {
                                      //$(this).fadeTo('slow', 0.7);
                                    }
                                );

                              $(t).removeClass('border');
                            }
                        );
                    }
                }
              );
          }
        else
          {
            $(t).empty();
          }
      }
  }

$(function()
  {
    $(".thumbimg").each
      (
        function()
          {
            //$(this).fadeTo('slow', 0.7);
          }
      );

    //on window resize, call again
    var resizeTimer = null;
    $(window).bind
      (
        'resize',
        function()
          {
            if (resizeTimer) clearTimeout(resizeTimer);
            resizeTimer = setTimeout
              (
                function()
                  {
                    if (!$("#grid_thumbs").length) return false;
                    $("#grid_thumbs").grid(settings, '', false, true);
                  },
                100
              );
          }
      );

    $(".scrolltopbutton").live
      (
        "click",
        function()
          {
            $.scrollTo(0, 800);
          }
      );

    $(".thumbimg").live
      (
        "mouseenter",
        function()
          {
            //$(this).fadeTo('fast', 1);
						$(this).fadeTo('fast', 0.7);
          }
      ).live
        (
          "mouseleave",
          function()
            {
              //$(this).fadeTo('slow', 0.7);
							$(this).fadeTo('slow', 1);
            }
        );

    settings.animateUpdate = false;

    if (!$("#grid_thumbs").length) return false;
    $("#grid_thumbs").grid(settings);

    settings.animateUpdate = true;

    $("#menu div > a[rel='history']").live
      (
        'click',
        function()
          {
            var hash = this.href;
            hash = hash.replace(/^.*#/, '');
            $("#grid_thumbs").fadeOut
              (
                 200,
                 function()
                   {
                     window.location = hash;
                   }
              );
          }
      );

    $("#grid_thumbs div > a[rel='history']").live
      (
        'click',
        function()
          {
            var hash = this.href;
            hash = hash.replace(/^.*#/, '');
            $.historyLoad(hash);
            //return false;
          }
      );

    $.historyInit(pageload);
  }
);
