function preloadImg(image)
{
   var preloaded = Array();

   if (image.constructor != Array)
   {
      // Convert strings to an array
      image[0] = image;
   }
   
   for (i = 0, j = image.length; i < j; i++)
   {
      preloaded[i]     = new Image();
      preloaded[i].src = image[i];
   }
}

$(document).ready(function()
{
   var tempVar  = '';
   var homepage = 'http://localhost/sites/govid/';

   // Hearing imparied text
   $('div.hearingImpaired div.link span').css('display', 'none');
   $('div.hearingImpaired div.info').css('display', 'none');

   $('div.hearingImpaired div.link').hover(function()
   {
      if (!$(this).next('div.info').hasClass('opened'))
      {
         $(this).css("background-color", '#d3d8df');
         $(this).find('span').css('display', 'inline');

         var imageSrc = $(this).find('img').attr('src');
         var imageRel = $(this).find('img').attr('rel');
         
         $(this).find('img').attr('src', imageRel);
         $(this).find('img').attr('rel', imageSrc);
      }
   }, function()
   {
      if (!$(this).next('div.info').hasClass('opened'))
      {
         $(this).css("background-color", 'transparent');
         $(this).find('span').css('display', 'none');

         var imageSrc = $(this).find('img').attr('rel');
         var imageRel = $(this).find('img').attr('src');
         
         $(this).find('img').attr('src', imageSrc);
         $(this).find('img').attr('rel', imageRel);
      }
   });

   $('div.hearingImpaired div.link').click(function()
   {
      $info   = $(this).next('div.info');
      $link   = $(this);

      if ($info.hasClass('opened'))
      {
         $info.slideUp(null, function()
         {
            $link.css("background-color", 'transparent');
            $link.find('span').html('Hearing impaired or speakers not working?');
         });

         $info.removeClass('opened');
      }
      else
      {
         $info.slideDown();
         $link.css("background-color", '#d3d8df');
         $link.find('span').html('minimise text');

         $info.addClass('opened');
      }
   });

   // Gallery rollover
   $('#brandedGallery #images ul li a img').hover(function()
   {
      var src = $(this).attr('src');
      var rel = $(this).attr('rel');

      $(this).attr('src', rel);
      $(this).attr('rel', src);
   }, function()
   {
      var src = $(this).attr('rel');
      var rel = $(this).attr('src');

      $(this).attr('src', src);
      $(this).attr('rel', rel);
   });

   $('#brandedGallery #images ul li a img').each(function()
   {
      preloadImg([$(this).attr('rel')]);
   });

   // GoVid Footer Related Properies
   $('#footer.govid #relatedProperties li').hover(function()
   {
      tempVar = $(this).find('img').attr('src');
      
      $(this).addClass('hover');
      $(this).find('img').attr('src', $(this).find('img').attr('rel'));
   }, function()
   {
      $(this).removeClass('hover');
      $(this).find('img').attr('src', tempVar);
   });

   // Preload essential images
   $('input[type="checkbox"]').each(function()
   {
      if ($(this).attr('rel') !== undefined)
      {
         var imageOn  = $(this).attr('rel') + '.jpg';
         var imageOff = $(this).attr('rel') + '-selected.jpg';

         preloadImg([imageOn, imageOff]);
      }
   });

   preloadImg([$('.hearingImpaired .link img').attr('src'), $('.hearingImpaired .link img').attr('rel')]);
});


