// Define mini-basket "flyout" on hover function globally
primeMiniBasket = function ( autoShow )
{
  function displayMiniBasket () {$('div#flyout').slideDown();}
  function hideMiniBasket    () {$('div#flyout').slideUp();}

  $('div#miniBasket').hoverIntent( {over:displayMiniBasket,timeout:500,out:hideMiniBasket} );

  if( autoShow )
  {
    displayMiniBasket();
  }
}

var panelTimer;

function randomFromTo ( from,to )
{
  return Math.floor( Math.random() * ( to - from + 1 ) + from );
}

function switchFeaturedPanels ( panelCount )
{
  panelCount = typeof(panelCount) != 'undefined' ? panelCount : 3;

  var count  = $('div#featured div.panel').length;

  var pick   = false;
  var target = false;

  if ( panelCount == 3 )
  {
    pick   = randomFromTo( 4,count ); // we want to pick a random tile outside of the top 3 (which are currently being displayed!)
    target = randomFromTo( 1,3 );     // we need to randomly choose which of the top 3 panels to replace
  }
  else if ( panelCount == 5 )
  {
    pick   = randomFromTo( 6,count ); // we want to pick a random tile outside of the top 5 (which are currently being displayed!)
    target = randomFromTo( 1,5 );     // we need to randomly choose which of the top 5 panels to replace
  }
  
  if ( pick !== false && target !== false )
  {
    //alert( pick + ':' + target );

    var originalHTML = $('div#featured div.panel:nth-child('+target+')').html();
    var newHTML      = $('div#featured div.panel:nth-child('+pick+')').html();

    $('div#featured div.panel:nth-child('+pick+') a').remove();

    $('div#featured div.panel:nth-child('+target+') a').fadeOut( 'fast',function()
    {
      $('div#featured div.panel:nth-child('+target+') a').remove();
      $('div#featured div.panel:nth-child('+target+')').html( newHTML ).hide().fadeIn( 'slow' );
      $('div#featured div.panel:nth-child('+pick+')').html( originalHTML );
    } );
    
    panelTimer = window.setTimeout( 'switchFeaturedPanels('+panelCount+')',6000 );
  }
}

$(document).ready(function()
{

  jQuery('form ol.category-filter').each(function(){
		if( jQuery(this).find('li').length > 10 )
		{
			jQuery(this).addClass('scroll');
			jQuery(this).after('<a class="scroll-more" href="#">More&hellip;</a>');
		}
	});

  jQuery('.scroll-more').live('click',function(e){
		e.preventDefault();
		jQuery(this).parent().find('ol').removeClass('scroll');
		jQuery(this).html('Less&hellip;');
		jQuery(this).attr('class','scroll-less');
		return false;
	});

  jQuery('.scroll-less').live('click',function(e){
		e.preventDefault();
		jQuery(this).parent().find('ol').addClass('scroll');
		jQuery(this).html('More&hellip;');
		jQuery(this).attr('class','scroll-more');
		return false;
	});

  // Mini Basket
  if ( $('div#flyout').length )
  {
    // Activate basket show/hide functionality
    primeMiniBasket();

    // Allow clicking of product thumbnails
    $('div#flyout li img').live( 'mouseover',function()
    {
      $(this).css('cursor','pointer');
    } ).live( 'mouseout',function()
    {
      $(this).css('cursor','auto');
    } ).live( 'click',function()
    {
      var link = $(this).parent().find('h6 a').attr('href');

      if ( link != undefined )
      {
        window.location.href=link;
      }
    } );
  }
  
  // Rotate featured panels randomly
  if ( $('div#featured').length )
  {
    $([window,document]).focusin( function()
    {
      if ( $('div#featured').hasClass( 'fivefold' ) ) // 5 panels
      {
        if ( $('div#featured div.panel').length > 5 )
        {
          panelTimer = window.setTimeout( 'switchFeaturedPanels( 5 )',6000 );
        }
      }
      else // 3 panels
      {
        if ( $('div#featured div.panel').length > 3 )
        {
          panelTimer = window.setTimeout( 'switchFeaturedPanels( 3 )',6000 );
        }
      }
    } ).focusout( function() // Stop panel rotating upon losing tab focus...
    {
      window.clearTimeout( panelTimer );
    } );
  }

  // Product Page -> highlight size selection
  if ( $('ul#selectSize').length )
  {
    $('ul#selectSize label').click( function()
    {
      $('ul#selectSize label').removeClass('current');
      $(this).addClass('current');
    } );
  }
  
  // Products text background rollover and ability to click anywhere...
  if ( $('ul.product-line').length )
  {
    if ( $('ul.product-line').hasClass('mini') ) // product page mini 'you may also like'
    {
      $('ul.product-line div.product').live( 'mouseover',function()
      {
        $(this).css('cursor','pointer');
        $(this).find('p').addClass('hover');
      } ).live( 'mouseout',function()
      {
        $(this).css('cursor','auto');
        $(this).find('p').removeClass('hover');
      } ).live( 'click',function()
      {
        var link = $(this).find( 'a' ).attr('href');
        
        if ( link != undefined )
        {
          window.location.href=link;
        }
      } );
    }
    else // standard category/featured products overview
    {
      $('ul.product-line div.product').live( 'mouseover',function()
      {
        $(this).css('cursor','pointer');
        $(this).find('ins').addClass('hover');
      } ).live( 'mouseout',function()
      {
        $(this).css('cursor','auto');
        $(this).find('ins').removeClass('hover');
      } ).live( 'click',function()
      {
        var link = $(this).find( 'h6 a' ).attr('href');
        
        if ( link != undefined )
        {
          window.location.href=link;
        }
      } );
    }
  }

  // Image Cycles
  if ( $( '.article div.imagery' ).length )
  {
    $('.article div.imagery').cycle();
  }

  // Carousels
  if ( $('div.scroller').length )
  {
    var i = 0;
    
    $('div.scroller').each( function()
    {
      i++;
      
      $(this).attr( 'id','carousel'+i );
      
      if ( $('div#carousel'+i+' ul').hasClass( 'mini' ) ) // product page mini 'you may also like' carousel
      {
        if ( $('div#carousel'+i+' ul.mini li').length > 9 )
        {
          $('div#carousel'+i+' ul.mini')
            .after( '<a id="carouselPrev'+i+'" class="left"  href="" title="See More">See More</a>' )
            .after( '<a id="carouselNext'+i+'" class="right" href="" title="See More">See More</a>' );
          
          $('div#carousel'+i).jCarouselLite(
          {
            btnNext:  'a#carouselNext'+i,
            btnPrev:  'a#carouselPrev'+i,
            visible:  9,
            scroll:   9,
            circular: true,
            speed:    1000
          });
        }
      }
      else if ( $('div#carousel'+i+' ul').hasClass( 'product-line' ) ) // standard products carousel
      {
        if ( $('div#carousel'+i+' ul.product-line li').length > 5 )
        {
          $('div#carousel'+i+' ul.product-line')
            .after( '<a id="carouselPrev'+i+'" class="left"  href="" title="See More">See More</a>' )
            .after( '<a id="carouselNext'+i+'" class="right" href="" title="See More">See More</a>' );
          
          $('div#carousel'+i).jCarouselLite(
          {
            btnNext:  'a#carouselNext'+i,
            btnPrev:  'a#carouselPrev'+i,
            visible:  5,
            scroll:   5,
            circular: true,
            speed:    1000
          });
        }
      }
      else if ( $('div#carousel'+i+' ul').hasClass( 'brands-showreel' ) ) // brands carousel
      {
        if ( $('div#carousel'+i+' ul.brands-showreel li').length > 8 )
        {
          $('div#carousel'+i)
            .after( '<a id="carouselPrev'+i+'" class="left"  href="" title="See More">See More</a>' )
            .after( '<a id="carouselNext'+i+'" class="right" href="" title="See More">See More</a>' );
          
          $('div#carousel'+i).jCarouselLite(
          {
            btnNext:  'a#carouselNext'+i,
            btnPrev:  'a#carouselPrev'+i,
            visible:  8,
            scroll:   8,
            circular: true,
            speed:    1000
          });
        }
      }
    } );
  }
  
  
  /* WW - BELOW */
  
  // Departments.
  $('div#departments-list h3').toggle(
  	function(){
  		$(this).addClass('open');
  		$(this).parents('li').find('div').show();	
  	},
  	function(){
  		$(this).removeClass('open');
  		$(this).parents('li').find('div').hide();
  	}
  );
  
  // Homepage Highlights Rotaion.
  $('#masthead').cycle({ 
    fx: 'fade',
    pause: 1, 
    timeout: 6000,
    speed: 2000  
  });
  
  
  
});
