// Variables
var prevThumbInterval;
var nextThumbInterval;

// When DOM is ready
$(function() {
	
    // Thumbnail click change image
    $('.thumbnailClickMain').click(function(e) {
        
    	// Change main image
    	return changeMainImage($(this));
    	
    });

    // Thumbnail hover change image
    $('.thumbnailHoverMain').mouseover(function(e) {
        
    	// Change main image
    	return changeMainImage($(this));
    	
    });
    
    // Change main image
    function changeMainImage(thumbnailObj)
    {
    	
        // Update full image link
        $('#images a').attr('href', thumbnailObj.attr('href'));
        
        // Update main image
        $('#images a img').attr('src', thumbnailObj.attr('rel'));
        
        // Check thumbnails format
        if (thumbnailObj.parents('ul').attr('class') == 'slider') {
	        
	        // Get thumb list items
	        var thumbsLi = $('#thumbnails ul').find('li');
	        
	        // Start counter for number of hidden thumbs
	        var numHiddenCount = 0;
	        
	        // Loop
	        for (i = 0; i < thumbsLi.length; i++) {
	            
	            // Check if visibile
	            if (thumbsLi[i].style.fontSize == '0em') {
	                
	                // Increment counter
	                numHiddenCount = numHiddenCount + 1;
	                
	            }
	            
	        }
	        
	        // Get parent id
	        var parentId = thumbnailObj.parent().attr('id');
	        
	        // Check if first visible is thumb clicked
	        if (thumbsLi[numHiddenCount].id == parentId) {
	            
	            // Previous thumb
	            prevThumb();
	            
	        } else {
	
	            // Next thumb
	            nextThumb();
	            
	        }
	        
        }
        
        // Return false (stop link from opening)
        return false;
        
    }
    
    // Mouse over for prev thumb button
    $('#prevThumbnail').mouseover(function(e) {
        
        // Move to prev thumb
        prevThumb();
        
        // Enable interval
        prevThumbInterval = setInterval(prevThumb, 1000);
        
    });

    // Mouse click for prev thumb button
    $('#prevThumbnail').click(function(e) {
        
        // Move to prev thumb
        prevThumb();
        
    });

    // Mouse out for prev thumb button
    $('#prevThumbnail').mouseout(function(e) {
        
        // Enable interval
        clearInterval(prevThumbInterval);
        
    });

    // Mouse over for next thumb button
    $('#nextThumbnail').mouseover(function(e) {

        // Move to next thumb
        nextThumb();
        
        // Enable interval
        nextThumbInterval = setInterval(nextThumb, 1000);
        
    });

    // Mouse click for next thumb button
    $('#nextThumbnail').click(function(e) {

        // Move to next thumb
        nextThumb();
        
    });

    // Mouse out for next thumb button
    $('#nextThumbnail').mouseout(function(e) {
        
        // Enable interval
        clearInterval(nextThumbInterval);
        
    });
    
    // Move prev thumb
    function prevThumb() {
        
        // Get thumb list itesm
        var thumbsLi = $('#thumbnails ul').find('li');
        
        // Check if at least 3 thumbs
        if (thumbsLi.length > 3) {
            
            // Start counter for number of hidden thumbs
            var numHiddenCount = 0;
            
            // Loop
            for (i = 0; i < thumbsLi.length; i++) {
                
                // Check if visibile
                if (thumbsLi[i].style.fontSize == '0em') {
                    
                    // Increment counter
                    numHiddenCount = numHiddenCount + 1;
                    
                }
                
            }
    
            // Check if at least one hidden
            if (numHiddenCount > 0) {
                
                // Get last hidden image element id
                var elementId = thumbsLi[numHiddenCount - 1].id;
                
                // Unhide last hidden image
                $('#' + elementId).css('font-size', '1em');
                var thumbWidth = $('#' + elementId).width();
                var oldMarginLeftPx = $('#thumbnails ul').css('margin-left');
                oldMarginLeft = eval(oldMarginLeftPx.replace('px', ''));
                var adjustPx = oldMarginLeft + thumbWidth;
                $('#thumbnails ul').animate({marginLeft: adjustPx + 'px'});
                
            }
            
            // Check if prev button should be hidden
            if ((numHiddenCount - 1) <= 0) {
                
                // Hide
                $('#prevThumbnail').hide();
                
                // Check if timer exists
                if (prevThumbInterval) {
                    
                    // Clear interval
                    clearInterval(prevThumbInterval);
                    
                }
                
            }
            
            // Make sure next button visibile
            $('#nextThumbnail').show();
            
        }
        
    };

    // Move next thumb
    function nextThumb() {
        
        // Get thumb list itesm
        var thumbsLi = $('#thumbnails ul').find('li');
        
        // Check if at least 3 thumbs
        if (thumbsLi.length > 3) {
            
            // Start counter for number of hidden thumbs
            var numHiddenCount = 0;
            
            // Loop
            for (i = 0; i < thumbsLi.length; i++) {
                
                // Check if visibile
                if (thumbsLi[i].style.fontSize == '0em') {
                    
                    // Increment counter
                    numHiddenCount = numHiddenCount + 1;
                    
                }
                
            }
            
            // Check if at least three left
            if (numHiddenCount < (thumbsLi.length - 3)) {
                
                // Get first visibile image element id
                var elementId = thumbsLi[numHiddenCount].id;
                
                // Hide first visible image
                var thumbWidth = $('#' + elementId).width();
                var oldMarginLeftPx = $('#thumbnails ul').css('margin-left');
                oldMarginLeft = eval(oldMarginLeftPx.replace('px', ''));
                var adjustPx = oldMarginLeft - thumbWidth;
                $('#thumbnails ul').animate({marginLeft: adjustPx + 'px'});
                $('#' + elementId).css('font-size', '0em');
                
            }
            
            // Check if next button should be hidden
            if ((numHiddenCount + 1) >= (thumbsLi.length - 3)) {
                
                // Hide
                $('#nextThumbnail').hide();
    
                // Check if timer exists
                if (nextThumbInterval) {
                    
                    // Clear interval
                    clearInterval(nextThumbInterval);
                    
                }
                
            }
    
            // Make sure prev button visibile
            $('#prevThumbnail').show();
            
        }
        
    };
    
});

