jQuery(document).ready(function(){

var collectionsThumbObject = jQuery("#collections_thumblist").find("li>a>img");
var collectionsLargeObject = jQuery("#collections_bigimglist").find("li>img");

var collectionsThumb = new Array();
var collectionsLarge = new Array();

for(i=0;i<collectionsThumbObject.length;i++){
	collectionsThumb[i] = jQuery(collectionsThumbObject[i]).attr("src");
}
for(i=0;i<collectionsLargeObject.length;i++){
	collectionsLarge[i] = jQuery(collectionsLargeObject[i]).attr("src");
}
	
	// mtpl9 collections effect
//	var collectionsThumb = new Array("public/_images/pdo/collection-thumb-1.jpg",
//								"public/_images/pdo/collection-thumb-2.jpg",
//								"public/_images/pdo/collection-thumb-3.jpg",
//								"public/_images/pdo/collection-thumb-4.jpg",
//								"public/_images/pdo/collection-thumb-5.jpg",
//								"public/_images/pdo/collection-thumb-6.jpg",
//								"public/_images/pdo/collection-thumb-7.jpg",
//								"public/_images/pdo/collection-thumb-8.jpg",
//								"public/_images/pdo/collection-thumb-9.jpg",
//								"public/_images/pdo/collection-thumb-10.jpg",
//								"public/_images/pdo/collection-thumb-11.jpg",
//								"public/_images/pdo/collection-thumb-12.jpg",
//								"public/_images/pdo/collection-thumb-13.jpg",
//								"public/_images/pdo/collection-thumb-14.jpg",
//								"public/_images/pdo/collection-thumb-15.jpg",
//								"public/_images/pdo/collection-thumb-16.jpg",
//								"public/_images/pdo/collection-thumb-17.jpg",
//								"public/_images/pdo/collection-thumb-18.jpg",
//								"public/_images/pdo/collection-thumb-19.jpg",
//								"public/_images/pdo/collection-thumb-20.jpg",
//								"public/_images/pdo/collection-thumb-21.jpg",
//								"public/_images/pdo/collection-thumb-22.jpg",
//								"public/_images/pdo/collection-thumb-23.jpg",
//								"public/_images/pdo/collection-thumb-24.jpg",
//								"public/_images/pdo/collection-thumb-25.jpg",
//								"public/_images/pdo/collection-thumb-26.jpg",
//								"public/_images/pdo/collection-thumb-27.jpg",
//								"public/_images/pdo/collection-thumb-28.jpg",
//								"public/_images/pdo/collection-thumb-29.jpg",
//								"public/_images/pdo/collection-thumb-30.jpg");
//								
//var collectionsLarge = new Array("public/_images/pdo/collection-gallery-1.jpg",
//							"public/_images/pdo/collection-gallery-2.jpg",
//							"public/_images/pdo/collection-gallery-3.jpg",
//							"public/_images/pdo/collection-gallery-4.jpg",
//							"public/_images/pdo/collection-gallery-5.jpg",
//							"public/_images/pdo/collection-gallery-6.jpg",
//							"public/_images/pdo/collection-gallery-7.jpg",
//							"public/_images/pdo/collection-gallery-8.jpg",
//							"public/_images/pdo/collection-gallery-9.jpg",
//							"public/_images/pdo/collection-gallery-10.jpg",
//							"public/_images/pdo/collection-gallery-11.jpg",
//							"public/_images/pdo/collection-gallery-12.jpg",
//							"public/_images/pdo/collection-gallery-13.jpg",
//							"public/_images/pdo/collection-gallery-14.jpg",
//							"public/_images/pdo/collection-gallery-15.jpg",
//							"public/_images/pdo/collection-gallery-16.jpg",
//							"public/_images/pdo/collection-gallery-17.jpg",
//							"public/_images/pdo/collection-gallery-18.jpg",
//							"public/_images/pdo/collection-gallery-19.jpg",
//							"public/_images/pdo/collection-gallery-20.jpg",
//							"public/_images/pdo/collection-gallery-21.jpg",
//							"public/_images/pdo/collection-gallery-22.jpg",
//							"public/_images/pdo/collection-gallery-23.jpg",
//							"public/_images/pdo/collection-gallery-24.jpg",
//							"public/_images/pdo/collection-gallery-25.jpg",
//							"public/_images/pdo/collection-gallery-26.jpg",
//							"public/_images/pdo/collection-gallery-27.jpg",
//							"public/_images/pdo/collection-gallery-28.jpg",
//							"public/_images/pdo/collection-gallery-29.jpg",
//							"public/_images/pdo/collection-gallery-30.jpg");
//							
//var collectionsTitle = new Array("<strong>TAMARRO</strong> PICENO MID BisonSnow White",
//							"<strong>Tamarro</strong> <span>Colore Mid</span> Snow White True Red",
//							"<strong>Sardinia</strong> <span>Piceno Low </span>Jet Set",
//							"<strong>Sardinia</strong> <span> Piceno</span> Low Bison",
//							"<strong>Modena</strong> <span>Piceno </span>Turkish Sea",
//							"<strong>Marinella</strong> <span>Piceno Low </span>Snow White",
//							"<strong>Marinella</strong> <span>Piceno Low </span>jet set",
//							"<strong>Marinella</strong> <span>Piceno Low </span>Bison",
//							"<strong>Pesaro</strong> <span>Piceno Low </span>Snow White",
//							"<strong>Pesaro</strong> <span>Piceno Low</span> WMS Jet Set",
//							"<strong>Pesaro</strong> <span>Piceno Low</span> WMS Bison",
//							"<strong>Pesaro</strong> <span>Piceno Low</span> WMS Snow White",
//							"<strong>Ascoli</strong> <span>Rome </span>Low Tan",
//							"<strong>Ascoli</strong> <span>Rome Low </span>Potting Soil",
//							"<strong>Ascoli</strong> <span>Piceno Low </span>Snow White",
//							"<strong>Ascoli</strong> <span>Piceno Low </span>BisonSnow White",
//							"<strong>Pesaro</strong> <span>Piceno Low</span> Jet set",
//							"<strong>Pesaro</strong> <span>Piceno Low </span>Insignia Blue",
//							"<strong>Pesaro</strong> <span>Piceno Low </span>Bison",
//							"<strong>Persaro</strong> <span>low</span> wms",
//							"<strong>Ascoli</strong> <span>low </span> wms",
//							"<strong>Ascoli</strong> <span>Jet-Set Low </span> Insignia Blue",
//							"<strong>Ascoli</strong> <span>Ethnis Low</span> BisonTrue red",
//							"<strong>Ascoli</strong> <span>Ethnic Low</span> Snow White WMS",
//							"<strong>Pasaro</strong> <span>Floral Low </span> WMS Salmon Rose",
//							"<strong>Modena</strong> <span>Piceno </span> True Red",
//							"<strong>Modena</strong> <span>Piceno </span> Greenery",
//							"<strong>Modena</strong> <span>Piceno </span> Starlight Blue",
//							"<strong>Ascoli</strong> <span> Ethnic Low</span> Snow White Turkish Sea",
//							"<strong>Ascoli</strong> <span>Colore Low</span> Turkish sea Snow White");
								
	
	
var collectionsThumbLength = collectionsThumb.length;
var collectionsLargeLength = collectionsLarge.length;
//jQuery(".thumbnails ul").html("");
//jQuery(".collections ul").html("");
//for(i=0; i<=(collectionsThumbLength-1); i++){
//	jQuery(".thumbnails ul").append("<li><a href='javascript:void(0);'><img src='"+collectionsThumb[i]+"' width='85' height='52' alt='' /></a></li>");
//}
//
//for(i=0; i<=(collectionsLargeLength-1); i++){
//	jQuery(".collections ul").append("<li><img src='"+collectionsLarge[i]+"' width='299' height='199' alt='' /><p>"+collectionsTitle[i]+"</p></li>");
//}

var animSpeed = 1000;
var itemWidth = jQuery(".collections>ul>li").width();
var outerWidth = 41;
var ulWidth = (itemWidth+outerWidth)*collectionsLargeLength;

if (collectionsLargeLength%2 != 0){
	jQuery(".collections ul").append("<li><img src='public/_images/pdo/collection-gallery-blank.jpg' width='299' height='199' alt='' /></li>");
	ulWidth = (ulWidth+itemWidth+outerWidth);
}

jQuery(".collections ul").css({"width":ulWidth+"px"});
jQuery(".prev_btn").css({"display":"none"});

jQuery(".next_btn").click(function(){
	jQuery(".prev_btn,.next_btn").css({"visibility":"hidden"});
	jQuery(".arrows").append("<a href='javascript:void(0);' class='unbindPrev'>next</a><a href='javascript:void(0);' class='unbindNext'>next</a>");
	ulLeft = parseInt(jQuery(".collections ul").css("left"));
	if(Math.abs(ulLeft)<(ulWidth-680)){
		jQuery(".collections ul").animate({"left":-(Math.abs(ulLeft)+680)+"px"},animSpeed,function(){
			jQuery(".unbindNext,.unbindPrev").remove();
			jQuery(".next_btn,.prev_btn").css({"visibility":"visible"});
			ulLeft = parseInt(jQuery(".collections ul").css("left"));
			if(Math.abs(ulLeft)==(ulWidth-680)){
				jQuery(".next_btn").css({"display":"none"});
			}
			if(ulLeft!=0){
				jQuery(".prev_btn").css({"display":"block"});
			}
		});
	}
});

jQuery(".prev_btn").click(function(){
	jQuery(".prev_btn,.next_btn").css({"visibility":"hidden"});
	jQuery(".arrows").append("<a href='javascript:void(0);' class='unbindPrev'>next</a><a href='javascript:void(0);' class='unbindNext'>next</a>");
	ulLeft = parseInt(jQuery(".collections ul").css("left"));
	if(ulLeft!=0){
	jQuery(".collections ul").animate({"left":(680+ulLeft)+"px"},animSpeed,function(){
		jQuery(".unbindNext,.unbindPrev").remove();
		jQuery(".next_btn,.prev_btn").css({"visibility":"visible"});
		ulLeft = parseInt(jQuery(".collections ul").css("left"));
		if(ulLeft==0){
			jQuery(".prev_btn").css({"display":"none"});
			jQuery(".next_btn").css({"display":"block"});
		}
		if(ulLeft!=0){
			jQuery(".prev_btn").css({"display":"block"});
			jQuery(".next_btn").css({"display":"block"});
		}
	});
	}
});
var clickedElement;
jQuery(".thumbnails ul li img").click(function(){
	jQuery(".prev_btn,.next_btn").css({"visibility":"hidden"});
	jQuery(".arrows").append("<a href='javascript:void(0);' class='unbindPrev'>next</a><a href='javascript:void(0);' class='unbindNext'>next</a>");
	var thisElementSrc = jQuery(this).attr("src");
	for(i=1; i<=collectionsThumbLength; i++){
		var thisSrc = jQuery(".thumbnails ul li:nth-child("+i+")").find("img").attr("src");
		if(thisSrc==thisElementSrc){
			clickedElement = i;
			break;
		}
	}
	//var thisArrayElement = jQuery.inArray(thisElementSrc, collectionsThumb);
	//var clickedElement = thisArrayElement+1;
	var presentUlLeft = parseInt(jQuery(".collections ul").css("left"));
	var presentShown = (Math.abs(presentUlLeft)/(itemWidth+outerWidth))+1;
	if(clickedElement>presentShown){
		var differenceItems = clickedElement-presentShown;
		if(clickedElement%2 != 0){
			jQuery(".collections ul").animate({"left":-(Math.abs(presentUlLeft)+(differenceItems*340))+"px"},{duration:animSpeed,queue:false,complete:function(){
				jQuery(".unbindNext,.unbindPrev").remove();
				jQuery(".next_btn,.prev_btn").css({"visibility":"visible"});
				presentUlLeft = parseInt(jQuery(".collections ul").css("left"));
				if(presentUlLeft==0){
					jQuery(".prev_btn").css({"display":"none"});
				}
				if(presentUlLeft!=0){
					jQuery(".prev_btn").css({"display":"block"});
				}
				if(Math.abs(presentUlLeft)==(ulWidth-680)){
					jQuery(".next_btn").css({"display":"none"});
				}
				if(Math.abs(presentUlLeft)<(ulWidth-680)){
					jQuery(".next_btn").css({"display":"block"});
				}
				}
			});		
		}
		else{
			jQuery(".collections ul").animate({"left":-(Math.abs(presentUlLeft)+((differenceItems-1)*340))+"px"},{duration:animSpeed,queue:false,complete:function(){
				jQuery(".unbindNext,.unbindPrev").remove();
				jQuery(".next_btn,.prev_btn").css({"visibility":"visible"});
				presentUlLeft = parseInt(jQuery(".collections ul").css("left"));
				if(presentUlLeft==0){
					jQuery(".prev_btn").css({"display":"none"});
				}
				if(presentUlLeft!=0){
					jQuery(".prev_btn").css({"display":"block"});
				}
				if(Math.abs(presentUlLeft)==(ulWidth-680)){
					jQuery(".next_btn").css({"display":"none"});
				}
				if(Math.abs(presentUlLeft)<(ulWidth-680)){
					jQuery(".next_btn").css({"display":"block"});
				}
				}
			});
		}
	}
	else{
		var differenceItems = clickedElement-presentShown;
		if(clickedElement%2 != 0){
			jQuery(".collections ul").animate({"left":(presentUlLeft+(Math.abs(differenceItems)*340))+"px"},{duration:animSpeed,queue:false,complete:function(){
				jQuery(".unbindNext,.unbindPrev").remove();
				jQuery(".next_btn,.prev_btn").css({"visibility":"visible"});
				presentUlLeft = parseInt(jQuery(".collections ul").css("left"));
				if(presentUlLeft==0){
					jQuery(".prev_btn").css({"display":"none"});
				}
				if(presentUlLeft!=0){
					jQuery(".prev_btn").css({"display":"block"});
				}
				if(Math.abs(presentUlLeft)==(ulWidth-680)){
					jQuery(".next_btn").css({"display":"none"});
				}
				if(Math.abs(presentUlLeft)<(ulWidth-680)){
					jQuery(".next_btn").css({"display":"block"});
				}
				}
			});
		}
		else{
			jQuery(".collections ul").animate({"left":(presentUlLeft+((Math.abs(differenceItems)+1)*340))+"px"},{duration:animSpeed,queue:false,complete:function(){
				jQuery(".unbindNext,.unbindPrev").remove();
				jQuery(".next_btn,.prev_btn").css({"visibility":"visible"});
				presentUlLeft = parseInt(jQuery(".collections ul").css("left"));
				if(presentUlLeft==0){
					jQuery(".prev_btn").css({"display":"none"});
				}
				if(presentUlLeft!=0){
					jQuery(".prev_btn").css({"display":"block"});
				}
				if(Math.abs(presentUlLeft)==(ulWidth-680)){
					jQuery(".next_btn").css({"display":"none"});
				}
				if(Math.abs(presentUlLeft)<(ulWidth-680)){
					jQuery(".next_btn").css({"display":"block"});
				}
				}
			});
		}
	}
});

});