 $(document).ready(function() {

$('#gallery').cycle({ 
    fx:     'fade', 
    timeout: 5000, 
    after:   onAfter,
    before:  onBefore,
	next:   '.next, #gallery', 
    prev:   '.prev', 
    pause:	1,
    pager:  '#gallery-controls li.center'      
 });


// needs sorting out changed to class
//$('.gallery').cycle({ 
//    fx:     'fade', 
//    timeout: 5000, 
//    after:   onAfter,
//    before:  onBefore,
//	next:   '.next, .gallery', 
//    prev:   '.prev', 
//    pause:	1,
//    pager:  '.gallery-controls li.center'      
// });

function onBefore() { 
    $("#caption").html($(this).children().attr("title"));
	$("#caption").html($(this).attr("alt"));	
	$("#caption").addClass("active");
} 


   $('.gallery').each(function() {
    	var $nav = $('<div class="nav"></div>').appendTo($(this).next().next().children('li.center'));
//    	var $nav = $('<div class="nav"></div>').insertAfter(this);
  		var $prev = $(this).next().next().children('li.left');
  		var $next = $(this).next().next().children('li.right');  
  		
  		if ($(this).children('li').size() == 1){
  			// hide next button
  			$(this).next().next().children('li.right, li.left').children('a').hide();
  		} else {
  			// more than one image start cycle	
  			$(this).cycle({
   				fx:     'fade', 
   				timeout: 0, 
	        	pager:   $nav,
	        	next:	 $next, 
	        	prev:	 $prev,
	        	after:   onAfter
    		});
    	}
    });
    
    function onAfter(curr, next, opts) {
        var src = '&nbsp;';
        if (next.src)
    		src = next.src.match(/([a-zA-Z0-9\.]+$)/)[1];
    	$(curr).parent().nextAll('div.caption:first').html(src);
    }


 $(".gallery-container").hoverIntent(
      function () {
         $("#caption").addClass("hover");
      	 $('#caption:not(.active)').slideDown(500);
      }, 
      function () {
        $("#caption").removeClass("hover");      
        $('#caption').slideUp(250)
      }
    );


function onAfter() { 
	$("#caption").removeClass("active");
}
 
	//suggested products
	$('.suggested-products h2').toggle(
      function () {
        $(this).parent().css({height:'auto'});
        $(this).parent().addClass('open');
        //$(this).css({overflow:'auto'});
      },
      function () {
        $(this).parent().animate({height:'30'});
        $(this).parent().removeClass('open');      
      }
    );
	
	
	// remove alt tags from gallery
	$("#gallery img, #gallery a").attr({'title' : '', 'alt' : ''});

});



