

// Sportmarket js-scripts by Cocky Days / Kim Lindeberg //


$(document).ready(function () {
	if ($('.infiniteCarousel').length) {
		$('.infiniteCarousel').infiniteCarousel();

		var autoscrolling = true;

	$('.page-template-template-start-php .infiniteCarousel').mouseover(function () {
		autoscrolling = false;
	}).mouseout(function () {
		autoscrolling = true;
	});
	
	$('.page-id-20 .infiniteCarousel').mouseover(function () {
		autoscrolling = false;
	}).mouseout(function () {
		autoscrolling = true;
	});
	
	setInterval(function () {
		if (autoscrolling) {
			$('.page-template-template-start-php .infiniteCarousel').trigger('next');
			$('.page-id-20 .infiniteCarousel').trigger('next');
    	}
	}, 6000);
	}
});

$.fn.infiniteCarousel = function () 
{
	setDescription(0);
	
	function repeat(str, num) 
    {
        return new Array( num + 1 ).join( str );
    }
  
    return this.each(function () 
    {
		var $wrapper = $('.wrapper', this).css('overflow', 'hidden'),
			$slider = $wrapper.find('> ul'),
			$items = $slider.find('> li'),
			$single = $items.filter(':first'),

			singleWidth = $single.outerWidth(), 
			visible = Math.ceil($wrapper.innerWidth() / singleWidth), // note: doesn't include padding or border
			currentPage = 1,
			pages = Math.ceil($items.length / visible);            


        // 1. Pad so that 'visible' number will always be seen, otherwise create empty items
        if (($items.length % visible) != 0) 
        {
            $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
            $items = $slider.find('> li');
        }


        // 2. Top and tail the list with 'visible' number of items, top has the last section, and tail has the first
        $items.filter(':first').before($items.slice(- visible).clone().addClass('cloned'));
        $items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
        $items = $slider.find('> li'); // reselect
        
        
        // 3. Set the left position to the first 'real' item
        $wrapper.scrollLeft(singleWidth * visible);
        
        
        // 4. Set the local ID for each implementation on the page
        randID = Math.round(Math.random()*100000000);
        this.id = randID;
        var localID = this.id;
        
        function setBackPos(cp) 
        {
        	for(i=1;i<=pages;i++)
			{
				$('#thumb_'+localID+'_'+i).css({'background-position':'bottom'});
				if(i==cp) $('#thumb_'+localID+'_'+i).css({'background-position':'top'});
			}
        }
		
		
		// 5. paging function
        function gotoPage(page) 
        {
            var dir = page < currentPage ? -1 : 1,n = Math.abs(currentPage - page),left = singleWidth * dir * visible * n;
             
            $wrapper.filter(':not(:animated)').animate({
                scrollLeft : '+=' + left
            }, 500, function () 
            {
                if (page == 0) 
                {
                    $wrapper.scrollLeft(singleWidth * visible * pages);
                    page = pages;
                } 
                else if (page > pages) 
                {
                    $wrapper.scrollLeft(singleWidth * visible);
                    
                    // reset back to start position
                    page = 1;
                } 

                currentPage = page;

				setBackPos(currentPage);
            });                
            
            
            
			setDescription(page);
	
            return false;
        }
        
        $wrapper.after('<a class="arrow back" title="Go Back">&lt;</a><a class="arrow forward">&gt;</a>');
        
        
        // 6. Bind to the forward and back buttons
        $('a.back', this).click(function () {        
            return gotoPage(currentPage - 1);                
        });
        
        $('a.forward', this).click(function () {
            return gotoPage(currentPage + 1);
        });
        
        
        // 7. Create a public interface to move to a specific page    
        $(this).bind('goto', function (event, page) {
            gotoPage(page);
        });

		this.gotoPage = gotoPage       
        
        
        // 8. Build index viewer and index divs (Pagination)
		$wrapper.after('<div class="thumbfooter"><div id="thumbs'+randID+'" class="thumbholder"></div></div>');


		/* $wrapper.after('<div id="description"><div id="descriptionContent"><div id="descriptionText"></div><div><a id="descriptionLink"></a></div></div></div>'); */


		for(i=0;i<=pages-1;i++)
		{
			$('#thumbs'+randID).append('<a class="thumb" id="thumb_'+randID+'_'+(i+1)+'">'+(i+1)+'</a>');
			if(i==0) $('#thumb_'+randID+'_1').css({'background-position':'top'});
		}
        

		function thumbclick(event)
		{
			target_num = this.id.split('_');
			var ic = document.getElementById(target_num[1]);
			ic.gotoPage(parseInt(target_num[2]));
		};

		$('#thumbs'+randID+' a').bind('click', thumbclick);
		
		$(this).bind('next', function () {
  			gotoPage(currentPage + 1);
		});

    }); 


	function setDescription(pageNumber)
	{
		var item = $(".infiniteCarousel li")[pageNumber];
		
		//var itemLink = $($(".infiniteCarousel li a")[pageNumber]).attr("href");
		var itemLink = $(item).find("a").attr("href");
		var showDescription = ($(item).find("a").length != 0);	
		
		if(showDescription == false)
		{
			$("#descriptionContent").hide()
			return;
		}
		
		
		if($("#descriptionText").html().length > 0)
		{
			$("#descriptionContent").hide().fadeIn();
		}
		else
		{
			$("#descriptionContent").fadeIn();
		}
		
		$("#descriptionText").html($(item).attr("rel"));
		
		$("#descriptionLink").attr("href",itemLink);
		
		$("#description").click(function(){
			window.location = itemLink;
		});
	
}
	     
};









