GM.namespace('GM.cms.demonfuzz');

//Init

GM.cms.demonfuzz.init = function ()
{
  $('div.product div.thumbnail a').removeAttr('href');
  
  $('div.product h1 a').click(function (event) {
    event.preventDefault();
        
    GM.cms.demonfuzz.ProductManager.open($(this));
  });
  
  $('div#shop-search select').change(function () {
    $(this).closest('form').submit();
  });
};

GM.cms.demonfuzz.ProductManager = function ()
{
  var node = null;
  
  function close()
  {
    if (node) {
      node.remove();
      node = null;
    }
  }
  
  return {
    
    open: function (link)
    {
      $('*').css('cursor', 'progress');
      
      close();
      
      var href = link.attr('href').replace(/site/, 'async');
            
      $.get(href, function (data) {
        node = $(data);
        
        node.attr('class', 'shop product detail');
        
        $('h1', node).click(close);
        
        $('div.image a', node).click(function (event) {
          event.preventDefault();
          
          var img = $('> img', $(this).parent().parent() );
          
          $('*').css('cursor', 'progress');
          
          img.bind('load', function () { 
            $('*').css('cursor', '');
          });
          
          img.attr('src', img.attr('src').replace(/[^\?]*\?/, $('img', $(this) ).attr('src').match(/[^\?]*\?/) ) );
        });
        
        link.parent().parent().parent().parent().append(node);
        
        var positionTarget = link.parent().parent().parent().position();
        var positionNode   = node.position();
        
        node.css('position', 'relative');
        node.css('left', positionTarget.left - positionNode.left);
        node.css('top', positionTarget.top - positionNode.top);
        node.css('margin-bottom', (positionTarget.top - positionNode.top) );
        
        node.animate({height: '298px'}, 350);
        
        $('*').css('cursor', '');
      });
    }    
  };
}();

GM.event.register(window, 'load', GM.cms.demonfuzz.init);

