/**
 * Display sizes
 */
jQuery(document).ready(function() {
	
	// move the login box to the top menu item
	var loginLinkItem = jQuery('#popup_link_login').parent();
	var loginForm = jQuery('#popup_login');
	
	loginLinkItem.append(loginForm);
	
	// move the email box to the top menu item
	var emailLinkItem = jQuery('#popup_link_email').parent();
	var emailForm = jQuery('#popup_email');
	
	emailLinkItem.append(emailForm);

	/* Drop down cart functionality
	 * Works with Ajax Cart Pro, see customized ajaxcartpro-community-1.4.6.js in skin directory
	 * Dependencies:  catalog.xml, checkout/cart/topcart.phtml, page/template/links.phtml, styles.css
	 * 
	 */
	// move the cart block to the topcartWrapper	

	/* End top cart code */
	/* ------------------*/

	// load more views slider
	if (jQuery(".more-views")) {
		loadMoreViews();
	}
	
	if (jQuery('.catalog-category-view')) {

		jQuery('.products-grid .product-image img').hover(
			function() {
				jQuery(this).addClass('highlight');
			},
			function() {
				jQuery(this).removeClass('highlight');
		});
	}
	
		
});	

function loadMoreViews() {

	  var slideInterval = 3000; // 3 seconds
	  var lastSlideInterval = 10000; // 10 seconds
	  var intId;
	  var currentPosition = 0;
	  var slideWidth = 140;
	  var slides = jQuery('.more-views li');
	  var numberOfSlides = slides.length;
	  var pageSize = 3;

	  // Remove scrollbar in JS
	  jQuery('.more-views ul').css('overflow', 'hidden');

	  // Wrap all .slides with #slideInner div
	  slides
	  	.wrapAll('<div id="slideInner"></div>')
		// Float left to display horizontally, readjust .slides width
		.css({
		    'float' : 'left',
		    'width' : slideWidth
	  });

	  // Set #slideInner width equal to total width of all slides
	  jQuery('#slideInner').css('width', slideWidth * numberOfSlides);

	  // Hide left arrow control on first load
	  manageControls(currentPosition);
	  
	  // load the first slide
	  loadSlide(0);
	  
	  // Create event listeners for .controls clicks
	  jQuery('#rightControl').click(function() { 
		  
		// Determine new position
		currentPosition = currentPosition + 1;
		
		// load the slide
		loadSlide(currentPosition); 
		
	  });

	  jQuery('#leftControl').click(function() { 
		 
		// Determine new position
		currentPosition = currentPosition - 1;
		
		// load the slide
		loadSlide(currentPosition); 
		
	  });
	  
	  // manageControls: Hides and shows controls depending on currentPosition
	  function manageControls(position) {
	    // Hide left arrow if position is first slide
	    if(position==0){ 
	    	jQuery('#leftControl').hide();
	    } else { 
	    	jQuery('#leftControl').show();
	    }
	    // Hide right arrow if position is last slide
	    if ( (numberOfSlides - position) <= pageSize ) {
	    	jQuery('#rightControl').hide();
	    } else { 
	    	jQuery('#rightControl').show();
	    }
	  }
	  
	  function loadSlide(position) {

		// Hide / show controls
		manageControls(position);
			
		if (position == 0) {	// at the first slide

			  // reset back to first slide
			  jQuery('#slideInner').css('marginLeft', 0);
			
		} else if (position == numberOfSlides - 1) {	
			
			// advance to last slide
			jQuery('#slideInner').animate({
				'marginLeft' : slideWidth*(-position)
			});
		  
		} else {
			
			// Move slideInner using margin-left
			jQuery('#slideInner').animate({
				'marginLeft' : slideWidth*(-position)
			});
		}
	  }
}
