function toggleBoxoutMore(moreLink)
{
   more = $('.more', moreLink.parent());
   (more.css('display')=='block')?moreLink.text('Read more...'):moreLink.text('Close');
   more.slideToggle();
}

$(document).ready(function () {
   $('.carousel').height(192).after('<img src="/images/loader.gif" class="loader" style="margin-left: 50%;" />')

   // Add dashes to tripple nested menu items
   $("ul ul ul > li").prepend("- ");

   //Flash text replacement
   $('.hilight').sifr( {
      font: '/flash/fsalbert-bold'
   });

   //"Read more" in boxouts
   $('.boxout .more').hide().after(
      '<a class="expand" onclick="toggleBoxoutMore($(this))">Read more...</a>'
   )

   //Features
   $('#features').accordion({
      event: "mouseover",
      header: "h2",
      autoHeight: true,
      alwaysOpen: false,
      active: false
   });

   $('#features li').click(function(){
      $('#features').accordion("activate", $('#features ul').index($(this)));
   });

   $('.showcase ul').cycle();

   //Tabs
   //Automatically generate the links and IDs so the editing environment doesn't have to
   tabs = $('.tabs');
   $('li', tabs).each(function(){
      if ($('a', $(this)).length == 0)
         $(this).wrapInner('<a href="#tab'+$('li', tabs).index(this)+'"></a>');
   });

   $('.more', tabs).each(function(){
      $(this).attr('id', 'tab'+$('.more', tabs).index(this)).addClass('ui-tabs-hide');
   });

//   $(".tabs > ul").tabs();
///////////////////////////////////////////////////////////////////////////////
   $('ul', tabs).addClass('ui-tabs-nav');
   prevTab = $('li', tabs).eq(0).addClass('ui-tabs-selected');
   prevContent = $('.more', tabs).eq(0).removeClass('ui-tabs-hide');

   $('.tabs > ul li a').click(function() {
      hLoc = $(this).attr('href').indexOf('#')
      if (hLoc != -1)
      {
         prevTab.removeClass('ui-tabs-selected');
         prevContent.addClass('ui-tabs-hide');
         prevTab = $(this).parent().addClass('ui-tabs-selected');
         prevContent = $($(this).attr('href').substring(hLoc), tabs).removeClass('ui-tabs-hide');
	  return false;
      }
   });
///////////////////////////////////////////////////////////////////////////////
});


$(window).load(function(){
   //Carousel to view work
   carousel = $('.carousel');

   if (carousel.size() != 0)
   {

      carousel.height($('img', carousel).eq(0).height());

      //Clone more info items
      info = $('.more', carousel).clone();

      carousel.after('<div class="carouselInfo"></div>').after('<div class="carouselNav">\
         <span id="client"></span>\
         <div class="panel">\
         <a href="#" class="prev"><img src="/images/workscroll_left.png" /></a>\
         <a href="#" class="next"><img src="/images/workscroll_right.png" /></a>\
         <img class="stick" src="/images/workscroll_stick.png" /></a>\
         </div>\
         <a href="#" class="expand">Details...</a>\
      </div>');

      //Set the client line
      $('#client').text('Client: '+$('.carousel ul img').eq(0).attr('alt'));

      $('.next').click(function () {
         $('.stick').animate({marginLeft: '+=10'}, 50).animate({marginLeft: '-=10'})
      });

      $('.prev').click(function () {
         $('.stick').animate({marginLeft: '-=10'}, 50).animate({marginLeft: '+=10'})
      });

      carouselInfo = $('.carouselInfo')

      //Add more info items to the outside of the list
      carouselInfo.append(info);
      info = $('.more', carousel).empty()

      maxHeight = 0;

      $('.more', carouselInfo).each(function () {
         maxHeight = Math.max(maxHeight, $(this).outerHeight());
      }).hide();


      carouselInfo.height(maxHeight);

      $('.carouselNav .expand').click(function () {
         $('.carouselInfo .more:eq('+carouselOn+')').slideToggle();
         carouselInfo = (carouselInfo) ? false : true;
         return false;
      });

      carouselOn = 0;
      carouselInfo = false;

      //On the testimonial we want the detail showing already
      if ($('body.how').size() != 0)
      {
         $('body.how .carouselInfo .more:eq(0)').show();
         carouselInfo = true;
         $('.carouselNav .expand').hide();
      }

      carousel.jCarouselLite({
         btnNext: ".next",
         btnPrev: ".prev",
         visible: 1,
         circular: false,
         beforeStart: function(a, curr) {
            $('.carouselInfo .more:eq('+a+')').slideUp();
         },
         afterEnd: function(a) {
            carouselOn = a;
            if (carouselInfo) {
               $('.carouselInfo .more:eq('+carouselOn+')').slideDown();
            }
            //Set the client line
            var client = $('.carousel ul img').eq(a).attr('alt');
            if (client !== '') { $('#client').text('Client: '+client); }
            else { $('#client').text(''); }
         }
      });
      $('.loader').remove();
   }
});
