rgrLogoPadding = 40;
rgrLogoSmallSpacing = 160 + 2 * rgrLogoPadding;
rgrLogoLargeSpacing = 320 + 2 * rgrLogoPadding;
rgrLogoSpeed = 500;
rgrTabSpeed = 150;
convertedImages = false;

/**
 * Set up the styles on the logos and menu for easy jquery manipulation.
 */ 
function rgrInit ()
{
	/* Convert artist logos to absolute positions. */
	$("#artists a").css ({ 
      'float': 'none', 
      'position': 'absolute',
      'left': '-1000px' 
   });
   
   setUpLogos ();
   setUpDialog ();
   
   $(".menu").css('visibilty', 'hidden');
   var imagesLoaded = true;
   var $menuImages = $(".menu img");
   for (var i = 0; i < $menuImages.length; i++)
   {
      imagesLoaded = imagesLoaded && $menuImages[i].complete;
   }
   if (imagesLoaded)
   {
      convertMenuImages();
   }
   else
   {
      var menuImgCount = 0;
      var menuImgTotal = 8;
      $(".menu img").load (function() 
      {
         menuImgCount ++;
         if (menuImgCount == menuImgTotal)
         {
            convertMenuImages(); 
         }
      });
   }
}

function convertMenuImages()
{
   /* Convert menu images to absolute positions. */
   if (convertedImages)
   {
      return;
   }
   convertedImages = true;
   
   $menuImages = $(".menu img");
   var offsets = new Array ();
   for (var i = 0; i < $menuImages.length; i++)
   {
      offsets[i] = $menuImages.eq(i).offset ();
   }

   for (var i = 0; i < $menuImages.length; i++)
   {
      $menuImages.eq(i).css ({'left': offsets[i].left - 50, 'margin': 0}); 
      $menuImages.eq(i).css ('position', 'absolute');
   }
   $(".menu img.tab").css ({'width': 1, 'height': 32 });
   setUpMenu ();
   $(".menu").hide().css('visibility', 'visible').fadeIn (rgrTabSpeed);
}

function setUpLogos ()
{
   var $logos = $("#artists a");
   $logos.unbind ('click');
   
   // Left image: rotate right when clicked.
   $logos.eq(0).css ('left', 0).click (function (event) {
      $logos.eq(0).css('z-index', 5);
      $logos.eq(2).css('z-index', 3);
      $logos.eq(0).animate ( {'left': rgrLogoSmallSpacing }, rgrLogoSpeed)
	     .find ('img').animate ({'width' : 320, 'height': 462}, rgrLogoSpeed);
	  
      $logos.eq(1).animate ({ 'left': rgrLogoSmallSpacing + rgrLogoLargeSpacing}, rgrLogoSpeed)
         .find ('img').animate ({'width' : 160, 'height': 231}, rgrLogoSpeed);
         
      $logos.eq(2).animate ({ 'left': 0 }, rgrLogoSpeed, function () {
         $logos.eq(1).removeClass ('selected');
         $logos.eq(2).prependTo ("#artists");
         setUpLogos ();
      });
      event.preventDefault ();
   });

   // Right image: rotate right when clicked.
   $logos.eq(2).css ('left', rgrLogoSmallSpacing + rgrLogoLargeSpacing).click (function (event) {
      $logos.eq(2).css('z-index', 5);
      $logos.eq(0).css('z-index', 3);
      $logos.eq(2).animate ( {'left': rgrLogoSmallSpacing}, rgrLogoSpeed)
	    .find ('img').animate ({'width' : 320, 'height': 462}, rgrLogoSpeed);

	 $logos.eq(1).animate ({ 'left': 0}, rgrLogoSpeed)
	    .find ('img').animate ({'width' : 160, 'height': 231}, rgrLogoSpeed);

     $logos.eq(0).animate ({ 'left': rgrLogoSmallSpacing + rgrLogoLargeSpacing}, rgrLogoSpeed, 
        function () {
	       $logos.eq(1).removeClass ('selected');
           $logos.eq(0).appendTo ("#artists");
           setUpLogos ();
        });
        event.preventDefault ();
     });

   if ($("#container.visible").length == 0)
   {
      $logos.eq(1).addClass('selected');
   }
   else
   {
      processContent ("");
   }
   
   // Centre image.
   var centerImgSpacing = rgrLogoSmallSpacing;
   if (!$logos.eq(1).hasClass ("selected"))
   {
      centerImgSpacing += 80;
   }
   $logos.eq(1).css ({'left': centerImgSpacing, 'z-index': 4})
   
   
}

function setUpMenu ()
{
   var $tabs = $(".menu img.tab");
   $(".menu .icon").hover (
      function () {
         $(this).parent().find ("img.tab").animate ({ 'width': 102, 'height': 32 }, {queue: false, duration: rgrTabSpeed});
      },
	  function () {
         $(this).parent().find ("img.tab").animate ({ 'width': 1, 'height': 32 }, {queue: false, duration: rgrTabSpeed});
      }
   );
   
   $("a.dialog").click (function (event) {
      var $logos = $("#artists a");
      $logos.unbind ('click');
      
      var $currLink = $(this);
      var $logoImg = $("#artists a.selected img");
      var $logoLink = $("#artists a.selected");
      if ($logoLink.length != 0)
      {
         var newLeft = rgrLogoSmallSpacing + 80;
         $logoImg.animate ({ 'width': 160, 'height': 231}, rgrLogoSpeed);
         $logoLink.animate ({'left': newLeft}, rgrLogoSpeed, function () {
            $("div#container").fadeIn (rgrTabSpeed);
            $logoLink.removeClass('selected');
            internalLinkHandler ($currLink);
         });
      }
      else
      {
         internalLinkHandler ($currLink);
      }
      event.preventDefault ();
   });
}

function setUpDialog ()
{
	$("div#container-head a#a_close").click (function (event) {
	   $("div#container").fadeOut (rgrLogoSpeed, function () {
	      $(this).removeClass("visible");
	      $logos = $("#artists a");
	      $logoLink = $logos.eq(1);
	      $logoImg = $logoLink.find('img');
	      $logoImg.animate ({'width': 320, 'height': 462}, rgrLogoSpeed);
	      $logoLink.animate ({'left': rgrLogoSmallSpacing}, rgrLogoSpeed, function () {
	         $logos.eq(1).addClass ("selected");
	         
	         setUpLogos ();
	         //$("div#content").remove();
	         // $("div#container-body").append ('<img src="/img/loader.gif" alt="Loading..." id="loading"/>');
	      });
	   });
	   event.preventDefault ();
	});
}

function internalLinkHandler ($currLink)
{
   var title = $currLink.attr('title');
   $("#container-head .heading").attr('id', $currLink.attr('id') + "_img").html (title);
   $("div#content").remove();
   $("div#container-body").append ('<img src="/img/loader.gif" alt="Loading..." id="loading"/>');
   $("#container-body").load ($currLink.attr('href') + "?ajax #content", function () {
      processContent (title);
      
      // $("#content form").ajaxForm ({success : formHandler});
   });
}

function processContent (title)
{
   if (title == "")
   {
      $("#container-head .heading").html ($("#content h1:first").text());
      $("#content h1:first").hide();
   }
   $('#content a').filter(function() {
      return (this.hostname && this.hostname == location.hostname) &&
             ($(this).attr("href").match (".rss$") != ".rss");
   }).click (function (event) { 
      event.preventDefault ();
      internalLinkHandler ($(this)); 
   });
}

/*
function formHandler (responseText, statusText)
{
   $("div#content").text(responseText);
   $("#content a:not([href^=http])").click (function (event) { 
      internalLinkHandler ($(this)); 
      event.preventDefault ();
   });
   $("#content form").ajaxForm ({success : formHandler});
}
*/

