(function(){
	$.fn.infiniteCarousel = function(){
		function repeat(str, n){
			return new Array( n + 1 ).join(str);
		}
	
		return this.each(function(){
			// magic!
			var $wrapper = $('> div', this).css('overflow', 'hidden'),
			$slider = $wrapper.find('> div').width(50000),
			$items = $slider.find('> div'),
			$single = $items.filter(':first')
			
			singleWidth = $single.outerWidth(),
			visible = Math.ceil($wrapper.innerWidth() / singleWidth),
			currentPage = 1,
			pages = Math.ceil($items.length / visible);
			/* TASKS */
			// 1. pad the pages with empty element if required
			if ($items.length % visible != 0) {
				// pad
				$slider.append(repeat('<div class="empty" />', visible - ($items.length % visible)));
				$items = $slider.find('> div');
			}

			// 2. create the carousel padding on left and right (cloned)
			$items.filter(':first').before($items.slice(-visible).clone().addClass('cloned'));
			$items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
			$items = $slider.find('> div');
	
			// 3. reset scroll
			$wrapper.scrollLeft(singleWidth * visible);

			// 4. 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 == last page - then reset position
					if(page > pages){
						$wrapper.scrollLeft(singleWidth * visible);
						page = 1;
					}else if(page == 0){
						page = pages;
						$wrapper.scrollLeft(singleWidth * visible * pages);
					}
					currentPage = page;
				});
			}
	
			// 5. insert the back and forward link
			//$wrapper.after('<a href="#" class="arrow back">&lt;</a><a href="#" class="arrow forward">&gt;</a>');
	
			// 6. bind the back and forward links
			$('a.car_back').click(function () {
				gotoPage(currentPage - 1);
				clearTimeout(window.car_timer);
				window.car_timer = setInterval(function(){
					if (window.autoscrolling){
						$('.infiniteCarousel').trigger('next');
					}
				}, 5000);
				return false;
			});
	
			$('a.car_forward').click(function () {
				gotoPage(currentPage + 1);
				clearTimeout(window.car_timer);
				window.car_timer = setInterval(function(){
					if (window.autoscrolling){
						$('.infiniteCarousel').trigger('next');
					}
				}, 5000);
				return false;
			});
	
			$(this).bind('goto', function (event, page) {
				gotoPage(page);
			});
	
			// THIS IS NEW CODE FOR THE AUTOMATIC INFINITE CAROUSEL
			$(this).bind('next', function () {
				gotoPage(currentPage + 1);
			});
		});
	};
})(jQuery);

$(document).ready(function(){
	//Preload images

	if($('#shipping_adres')){
		if($('#customers_shipping').attr('checked')==true){
			$('#shipping_adres').css('display','block');
		}else{
			$('#shipping_adres').css('display','none');
		}
	}
	$('#customers_shipping').bind('click',function(){
		if(this.checked==true){
			$('#shipping_adres').css('display','block');
		}else{
			$('#shipping_adres').css('display','none');
		}
	});
	if($('#customers_found')&&$('#customers_found')[0]){
		if($('#customers_found')[0].selectedIndex==2){
			$('#customers_found_description').parent('p').css('display','block');
		}else{
			$('#customers_found_description').parent('p').css('display','none');
		}
	}


	$('#customers_found').bind('change',function(){
		if(this.selectedIndex==2){
			$('#customers_found_description').parent('p').css('display','block');
		}else{
			$('#customers_found_description').parent('p').css('display','none');
		}
	});
	$("img.himg").each(function(i){
		var preimg = $(this).attr("src").replace('.jpg', '_hover.jpg');
		jQuery("<img>").attr("src", preimg);
		if($(this).hasClass("selected")){
			$(this).attr("src", $(this).attr("src").replace('.jpg', '_hover.jpg'));
		}
	});

	$("img.himg").mouseover(function(){
		if(!$(this).hasClass("selected")){
			$(this).attr("src", $(this).attr("src").replace('.jpg', '_hover.jpg'));
		}
	});
	
	$("img.himg").mouseout(function(){
		if(!$(this).hasClass("selected")){
			$(this).attr("src", $(this).attr("src").replace('_hover.jpg', '.jpg'));
		}
	});

	// THIS IS NEW CODE FOR THE AUTOMATIC INFINITE CAROUSEL
	window.autoscrolling = true;
	
	$('.infiniteCarousel').infiniteCarousel().mouseover(function (){
		window.autoscrolling = false;
	}).mouseout(function (){
		window.autoscrolling = true;
	});

	window.car_timer = setInterval(function(){
		if (window.autoscrolling){
			$('.infiniteCarousel').trigger('next');
		}
	}, 5000);

//	$('.sound').click(function(){
//	    //alert($(this).attr("href"));
//	    return false;
//	});
	$('.thumb').hover(function(){
	    $('.large>img').attr("src",'/userfiles/products/small/'+$(this).attr('href').substring($(this).attr('href').lastIndexOf('/'), $(this).attr('href').length));
	    $('a.large').attr("href",$(this).attr('href'));
	    return false;
	}).click(function(){return false;});

	if(typeof($('body').fancybox)=='function'){
		$(".large , .thumb").attr("rel","product").fancybox({'zoomSpeedIn': 300, 'zoomSpeedOut': 300});
	}
});

