var $j = jQuery.noConflict();

$j(document).ready(function(){
   
   var images  = new Array();
   images[1]   = '/templates/gmi_content/images/gasdistribution.jpg';
   images[2]   = '/templates/gmi_content/images/marine.jpg';
   images[3]   = '/templates/gmi_content/images/generalindustry.jpg';
   images[4]   = '/templates/gmi_content/images/fixed.jpg';
   
   $j('a.switcheroo').hover(
      function(e)
      {
         if(!$(this).hasClass('live'))
         {
            var imageNum = parseInt($j(this).attr('rel'))+1;
            $j(this).children('img').attr('src', '/templates/gmi_content/images/home-tab-'+ imageNum +'-up.gif');
            $j(this).animate({marginTop: "-4px"}, 200);
         }
      },
      function(){
         if(!$(this).hasClass('live'))
         {
            var imageNum = parseInt($j(this).attr('rel'))+1;
            $j(this).animate({marginTop: "0"}, 200, '', function(){
               $j(this).children('img').attr('src', '/templates/gmi_content/images/home-tab-'+ imageNum +'-down.gif');
            });   
         }
      }
   )
   
   $j('div.lbContent').not(":first").hide();
   $j('ul#switch-menu li a:first').animate({marginTop: "-4px"}, 200).children('img').attr('src', '/templates/gmi_content/images/home-tab-1-up.gif');
   var lastClicked = 0;
   nextClick();

   $j("ul#switch-menu li a").click(function(e){
      e.preventDefault();
      if(!$(this).hasClass('live'))
      {
         if(e.originalEvent!=undefined)
         {
            /* User has clicked - not a simulated click! Stop rotating */
            clearTimeout(bannerTimer);
         }
         
         /* clear all links */
         var links = $j("ul#switch-menu li a");
         $j(links).each(function(){
            var imageNum = parseInt($j(this).attr('rel'))+1;
            
            if($j(this).hasClass('live'))
            {
               $j(this).animate({marginTop: "0"}, 200, '', function(){
                  $j(this).children('img').attr('src', '/templates/gmi_content/images/home-tab-'+ imageNum +'-down.gif');
               });
            }
            $j(this).removeClass('live').addClass('switcheroo');            
         })
         
         $(this).removeClass('switcheroo').addClass('live');
         
         var divs = $j("div.lbContent");
         $j(divs).each(function(){
            $j(this).hide();
         })
         
         var clicked = parseInt($j(this).attr('rel'))+1;
         
         $j(this).animate({marginTop: "-4px"}, 200);
         $j(this).children('img').attr('src', '/templates/gmi_content/images/home-tab-'+ clicked +'-up.gif');
         
         var myDiv = divs[clicked - 1];
         $j(myDiv).fadeIn(1500);
         
         /*******************************************************************************************************************
          * The script is running into problems because the click() method isn't being called first time through (the correct
          * DOM element is being found however.) This gives us problems with hiding and showing the first tab. 
          * The following is a slightly hacky way of checking the previous list item to make sure it has dropped
          * and that the src is ok.
          * ****************************************************************************************************************/
         if($j("ul#switch-menu li:first a").css('marginTop')=="-4px" && $j(this).attr('rel') != 0)
         {
            $j("ul#switch-menu li:first a").animate({marginTop: "0"}, 200, '', function(){
               if($j("ul#switch-menu li:first a img").attr('src') == '/templates/gmi_content/images/home-tab-1-up.gif')
               {
                  $j("ul#switch-menu li:first a img").attr('src', '/templates/gmi_content/images/home-tab-1-down.gif')
               }
            });
         }
         
         //$j('img#mainImage').animate({opacity: '0.3'}, 'fast', '', function(){
         //   $j(this).attr('src', images[clicked]).animate({opacity: '1'}, 'slow');
         //})
         
         $j('img#mainImage').fadeOut('fast', function(){
            $j(this).attr('src', images[clicked]).fadeIn('slow');
         });
      }
   })
   
   function nextClick()
   {      
      var links = $j("ul#switch-menu li a");
      /* Back to the start */
      lastClicked  = lastClicked > links.length - 1 ? 0:lastClicked;
      
      var myLink = links[lastClicked];
      
      
      $j(myLink).click();
      lastClicked = lastClicked + 1;

      bannerTimer = setTimeout(nextClick, 7 * 1000);
   }
   
   function sleep(seconds) {      
      var start = new Date().getTime();
      while (new Date() < start + seconds*1000) {}
      return 0;
   }


})
