$(document).ready(function(){
/*
 * PARAMETRY:
 *  numberMovePhoto-ilosc zdjec przesuwanych przy jednym kliknieciu;
 *  imgSizeWithMargin- rozmiar zdjecia razem z prawym marginesem
 *  imgsNumber- ilosc wyswietlanych zdjec, tyle samo jest dociagane z ajaxa
 */
	odczyt=true;
	var offset=0;
	var move=true;
	var numberMovePhoto=14;
	var numberMoveCasting=7;
	var imgSizeWithMargin=58;
	var castingSizeWithMargin=116;
	var imgsNumber=14;
	var castingNumbers=7;
	var marginOffset=numberMovePhoto*imgSizeWithMargin;
	var castingMarginOffset=numberMoveCasting*castingSizeWithMargin;
	var countFZnajomi=0;
	var countFObserwuja=0;
	var countFGlosowali=0;
	var countFCasting=0;
	var allPhotosNumberZnajomi=parseInt($('#photosNumberZnajomi').text());
	var allPhotosNumberObserwuja=parseInt($('#photosNumberObserwuja').text());
	var allPhotosNumberGlosowali=parseInt($('#photosNumberGlosowali').text());
	var allCastingNumbers=parseInt($('#castingNumbers').text());
	var leftPhotosNumberZnajomiRside=allPhotosNumberZnajomi-imgsNumber;
	var leftPhotosNumberObserwujaRside=allPhotosNumberObserwuja-imgsNumber;
	var leftPhotosNumberGlosowaliRside=allPhotosNumberGlosowali-imgsNumber;
	var leftCastingNumbersRside=allCastingNumbers-castingNumbers;
	var leftPhotosNumberZnajomiLside=0;
	var leftPhotosNumberObserwujaLside=0;
	var leftPhotosNumberGlosowaliLside=0;
	var leftCastingNumbersLside=0;
	var fZnajomiMarginMax=0;
	var fGlosowaliMarginMax=0;
	var fObserwujaMarginMax=0;
	var fCastingNumbers=0;
	
	$('.fZnajomi').each(function(){
		var startMargin=5;
		margin=startMargin+offset
		$(this).css({'margin-left':margin });
		offset+=imgSizeWithMargin;
		countFZnajomi++;
			})
	if(allPhotosNumberZnajomi>imgsNumber){
		$('#moveLeftZnajomi').css({'color':'#4682B4', 'visibility':'visible'});
	}
	offset=0;
	$('.fObserwuja').each(function(){
		var startMargin=5;
		margin=startMargin+offset
		$(this).css({'margin-left':margin });
		offset+=imgSizeWithMargin
		countFObserwuja++;
			})
	if(allPhotosNumberObserwuja>imgsNumber){
		$('#moveLeftObserwuja').css({'color':'#4682B4', 'visibility':'visible'});
	}
	offset=0;
	$('.fGlosowali').each(function(){
		var startMargin=5;
		margin=startMargin+offset
		$(this).css({'margin-left':margin });
		offset+=imgSizeWithMargin
		countFGlosowali++;
			})		
	if(allPhotosNumberGlosowali>imgsNumber){
		$('#moveLeftGlosowali').css({'color':'#4682B4', 'visibility':'visible'});
	}
	var castingOffset=0;
	$('.fCasting').each(function(){
		var startMargin=5;
		margin=startMargin+castingOffset;
		$(this).css({'margin-left':margin})
		castingOffset+=castingSizeWithMargin;
		countFCasting++;
	})
	if(allCastingNumbers>castingNumbers){
		$('#moveLeftCastingi').css({'color':'#4682B4', 'visibility':'visible'});
	}
	
	$('.fObserwuja').each(function(){fObserwujaMarginMax=$(this).css('margin-left');})
		klik();
		
function klik(){
	$('.leftArrowSmallSlider').click(function(){
		var action=$(this).attr('id');
		var margin=0;
		if(action=='moveRightZnajomi' && move){
			margin=parseInt($('#photosMoveBoxZnajomi').css('margin-left'));
			maxMargin=allPhotosNumberZnajomi*(-imgSizeWithMargin)+(imgsNumber*imgSizeWithMargin);
			if(!margin)
				margin=0;
			if(leftPhotosNumberZnajomiLside>=numberMovePhoto){			
				leftPhotosNumberZnajomiRside+=numberMovePhoto;
				leftPhotosNumberZnajomiLside-=numberMovePhoto;
				margin+=marginOffset;
			}
			else{
				margin+=(leftPhotosNumberZnajomiLside*imgSizeWithMargin);
				leftPhotosNumberZnajomiRside+=leftPhotosNumberZnajomiLside;
				leftPhotosNumberZnajomiLside=0;
			}
			if(margin<=0 && margin>=maxMargin){
				move=false;
				//$('#rightArrowZnajomi').show();
				if(margin==0){
					$('#moveRightZnajomi').hide();
						if(allPhotosNumberZnajomi>imgsNumber)
							$('#moveLeftZnajomi').show();
				}
				
				$('#photosMoveBoxZnajomi').animate({'margin-left':margin},'slow',activeMove);
			}
		}
		else if(action=='moveRightObserwuja' && move){
			margin=parseInt($('#photosMoveBoxObserwuja').css('margin-left'));
			maxMargin=allPhotosNumberObserwuja*(-imgSizeWithMargin)+(imgsNumber*imgSizeWithMargin);
			if(!margin)
				margin=0;
			if(leftPhotosNumberObserwujaLside>=numberMovePhoto){			
				leftPhotosNumberObserwujaRside+=numberMovePhoto;
				leftPhotosNumberObserwujaLside-=numberMovePhoto;
				margin+=marginOffset;
			}
			else{
				margin+=(leftPhotosNumberObserwujaLside*imgSizeWithMargin);
				leftPhotosNumberObserwujaRside+=leftPhotosNumberObserwujaLside;
				leftPhotosNumberObserwujaLside=0;
			}

			if(margin<=0 && margin>=maxMargin){
				move=false;
				//$('#leftArrowObserwuja').show();
				if(margin==0){
					$('#moveRightObserwuja').hide();
						if(allPhotosNumberObserwuja>imgsNumber)
							$('#moveLeftObserwuja').show();
				}
				$('#photosMoveBoxObserwuja').animate({'margin-left':margin},'slow',activeMove);
			}
		}
		else if(action=='moveRightGlosowali' && move){
			margin=parseInt($('#photosMoveBoxGlosowali').css('margin-left'));
			maxMargin=allPhotosNumberGlosowali*(-imgSizeWithMargin)+(imgsNumber*imgSizeWithMargin);
			if(!margin)
				margin=0;
			if(leftPhotosNumberGlosowaliLside>=numberMovePhoto){			
				leftPhotosNumberGlosowaliRside+=numberMovePhoto;
				leftPhotosNumberGlosowaliLside-=numberMovePhoto;
				margin+=marginOffset;
			}
			else{
				margin+=(leftPhotosNumberGlosowaliLside*imgSizeWithMargin);
				leftPhotosNumberGlosowaliRside+=leftPhotosNumberGlosowaliLside;
				leftPhotosNumberGlosowaliLside=0;
			}

			if(margin<=0 && margin>=maxMargin){
				move=false;
				//$('#leftArrowGlosowali').show();
				if(margin==0){
					$('#moveRightGlosowali').hide();
						if(allPhotosNumberGlosowali>imgsNumber)
							$('#moveLeftGlosowali').show();
				}
				$('#photosMoveBoxGlosowali').animate({'margin-left':margin},'slow',activeMove);
			}
		}
		else if(action=='moveRightCastingi' && move){
			margin=parseInt($('#castingMoveBox').css('margin-left'));
			maxMargin=allCastingNumbers*(-castingSizeWithMargin)+(castingNumbers*castingSizeWithMargin);
			if(!margin)
				margin=0;
			if(leftCastingNumbersLside>=numberMoveCasting){			
				leftCastingNumbersRside+=numberMoveCasting;
				leftCastingNumbersLside-=numberMoveCasting;
				margin+=castingMarginOffset;
			}
			else{
				margin+=(leftCastingNumbersLside*castingSizeWithMargin);
				leftCastingNumbersRside+=leftCastingNumbersLside;
				leftCastingNumbersLside=0;
			}
			if(margin<=0 && margin>=maxMargin){
				move=false;
				//$('#leftArrowGlosowali').show();
				if(margin==0){
					$('#moveRightCastingi').hide();
						if(allCastingNumbers>castingNumbers)
							$('#moveLeftCastingi').show();
				}
				$('#castingMoveBox').animate({'margin-left':margin},'slow',activeMove);
			}
		}
		
	})
	
	$('.rightArrowSmallSlider').click(function(){
		var action=$(this).attr('id');
		if(action=='moveLeftZnajomi' && move){
			maxMargin=allPhotosNumberZnajomi*(-imgSizeWithMargin)+(imgsNumber*imgSizeWithMargin);
			margin=parseInt($('#photosMoveBoxZnajomi').css('margin-left'));
			if(!margin)
				margin=0;
				if(leftPhotosNumberZnajomiRside>=numberMovePhoto){
					leftPhotosNumberZnajomiLside+=numberMovePhoto;
					leftPhotosNumberZnajomiRside-=numberMovePhoto;
					margin-=marginOffset;
				}
				else{
						if(leftPhotosNumberZnajomiRside<0)
							leftPhotosNumberZnajomiRside+=numberMovePhoto;
					leftPhotosNumberZnajomiLside+=leftPhotosNumberZnajomiRside;
					marginOffset=leftPhotosNumberZnajomiRside*imgSizeWithMargin;
					margin-=marginOffset;
					leftPhotosNumberZnajomiRside=0;
				}				
				//alert("margin: "+margin+ " maxMargin: "+maxMargin);	
			if(margin<=0 && margin>=maxMargin){
				move=false;
				if(margin==maxMargin){
					$('#moveLeftZnajomi').hide();
				}
				$('#moveRightZnajomi').show().css({'visibility':'visible'});
				//$('#photosMoveBoxZnajomi').animate({'margin-left':margin},'slow',activeMove);
				wczytajZnajomych(margin);
			}
			marginOffset=numberMovePhoto*imgSizeWithMargin;
		}
		else if(action=='moveLeftObserwuja' && move){
			maxMargin=allPhotosNumberObserwuja*(-imgSizeWithMargin)+(imgsNumber*imgSizeWithMargin);
			margin=parseInt($('#photosMoveBoxObserwuja').css('margin-left'));
			if(!margin)
				margin=0;
				if(leftPhotosNumberObserwujaRside>=numberMovePhoto){
					leftPhotosNumberObserwujaLside+=numberMovePhoto;
					leftPhotosNumberObserwujaRside-=numberMovePhoto;
					margin-=marginOffset;
				}
				else{
						if(leftPhotosNumberObserwujaRside<0)
							leftPhotosNumberObserwujaRside+=numberMovePhoto;
					leftPhotosNumberObserwujaLside+=leftPhotosNumberObserwujaRside;
					marginOffset=leftPhotosNumberObserwujaRside*imgSizeWithMargin;
					margin-=marginOffset;
					leftPhotosNumberObserwujaRside=0;
				}				
				
			if(margin<=0 && margin>=maxMargin){
				move=false;
				if(margin==maxMargin)
					$('#moveLeftObserwuja').hide();
					
				$('#moveRightObserwuja').show().css({'visibility':'visible'});
				//$('#photosMoveBoxObserwuja').animate({'margin-left':margin},'slow',activeMove);
				wczytajObserwuja(margin);
			}
			marginOffset=numberMovePhoto*imgSizeWithMargin;
		}
		else if(action=='moveLeftGlosowali' && move){
			maxMargin=allPhotosNumberGlosowali*(-imgSizeWithMargin)+(imgsNumber*imgSizeWithMargin);
			margin=parseInt($('#photosMoveBoxGlosowali').css('margin-left'));
			if(!margin)
				margin=0;
				if(leftPhotosNumberGlosowaliRside>=numberMovePhoto){
					leftPhotosNumberGlosowaliLside+=numberMovePhoto;
					leftPhotosNumberGlosowaliRside-=numberMovePhoto;
					margin-=marginOffset;
				}
				else{
						if(leftPhotosNumberGlosowaliRside<0)
							leftPhotosNumberGlosowaliRside+=numberMovePhoto;
					leftPhotosNumberGlosowaliLside+=leftPhotosNumberGlosowaliRside;
					marginOffset=leftPhotosNumberGlosowaliRside*imgSizeWithMargin;
					margin-=marginOffset;
					leftPhotosNumberGlosowaliRside=0;
				}				
					
			if(margin<=0 && margin>=maxMargin){
				move=false;
				if(margin==maxMargin)
					$('#moveLeftGlosowali').hide();
				
				$('#moveRightGlosowali').show().css({'visibility':'visible'});
				wczytajGlosowali(margin);
			}
			marginOffset=numberMovePhoto*imgSizeWithMargin;
		}
		else if(action=='moveLeftCastingi' && move){
			maxMargin=allCastingNumbers*(-castingSizeWithMargin)+(castingNumbers*castingSizeWithMargin);
			margin=parseInt($('#castingMoveBox').css('margin-left'));
			if(!margin)
				margin=0;
				if(leftCastingNumbersRside>=numberMoveCasting){
					leftCastingNumbersLside+=numberMoveCasting;
					leftCastingNumbersRside-=numberMoveCasting;
					margin-=castingMarginOffset;
				}
				else{
						if(leftCastingNumbersRside<0)
							leftCastingNumbersRside+=numberMoveCasting;
							leftCastingNumbersLside+=leftCastingNumbersRside;
							castingMarginOffset=leftCastingNumbersRside*castingSizeWithMargin;
							margin-=castingMarginOffset;
							leftCastingNumbersRside=0;
				}				
					
			if(margin<=0 && margin>=maxMargin){
				move=false;
				if(margin==maxMargin)
					$('#moveLeftCastingi').hide();
				
				$('#moveRightCastingi').show().css({'visibility':'visible'});
				wczytajCastingi(margin);
			}
			castingMarginOffset=numberMoveCasting*castingSizeWithMargin;

		}
	})
}
function activeMove(){
		move=true;
	}

function wczytajZnajomych(margin){
			$('.fZnajomi').each(function(){fZnajomiMarginMax=$(this).css('margin-left');})
			
			var currentPhotoNumberZnajomi=$('#photosMoveBoxZnajomi').children().size();
			if(currentPhotoNumberZnajomi<allPhotosNumberZnajomi){
				var ile=imgsNumber;
				
				if(allPhotosNumberZnajomi-currentPhotoNumberZnajomi<imgsNumber)
					ile=allPhotosNumberZnajomi-currentPhotoNumberZnajomi+1;
				
				fZnajomiMarginMax=currentPhotoNumberZnajomi*imgSizeWithMargin-53;
				$.ajax({
						 url:adresURL+'portfolio/portfolio_wczytajznajomych.php',
						 data: 'startMargin='+fZnajomiMarginMax+'&ile='+ile+'&start='+currentPhotoNumberZnajomi,
						 type: "POST",
						 success:function(msg){
								$('#photosMoveBoxZnajomi').append(msg);
								$('#photosMoveBoxZnajomi').animate({'margin-left':margin},'slow',activeMove);
								klik();
								}
						});
			}else{
				$('#photosMoveBoxZnajomi').animate({'margin-left':margin},'slow',activeMove);
			}
}

function wczytajGlosowali(margin){
	$('.fGlosowali').each(function(){fGlosowaliMarginMax=$(this).css('margin-left');})
	
	var currentPhotoNumberGlosowali=$('#photosMoveBoxGlosowali').children().size();
	if(currentPhotoNumberGlosowali<allPhotosNumberGlosowali){
		var ile=imgsNumber;
		
		if(allPhotosNumberGlosowali-currentPhotoNumberGlosowali<imgsNumber)
			ile=allPhotosNumberGlosowali-currentPhotoNumberGlosowali+1;
		
		fGlosowaliMarginMax=currentPhotoNumberGlosowali*imgSizeWithMargin-53;
		$.ajax({
				 url:adresURL+'portfolio/portfolio_wczytajglosowali.php',
				 data: 'startMargin='+fGlosowaliMarginMax+'&ile='+ile+'&start='+currentPhotoNumberGlosowali,
				 type: "POST",
				 success:function(msg){
						$('#photosMoveBoxGlosowali').append(msg);
						$('#photosMoveBoxGlosowali').animate({'margin-left':margin},'slow',activeMove);
						klik();
						}
				});
	}else{
		$('#photosMoveBoxGlosowali').animate({'margin-left':margin},'slow',activeMove);
	}
}

function wczytajObserwuja(margin){
	
	var currentPhotoNumberObserwuja=$('#photosMoveBoxObserwuja').children().size();
	if(currentPhotoNumberObserwuja<allPhotosNumberObserwuja){
		var ile=imgsNumber;
		
		if(allPhotosNumberObserwuja-currentPhotoNumberObserwuja<imgsNumber){
			ile=allPhotosNumberObserwuja-currentPhotoNumberObserwuja;
		}
		fObserwujaMarginMax=currentPhotoNumberObserwuja*imgSizeWithMargin-53;
		$.ajax({
				 url:adresURL+'portfolio/portfolio_wczytajobserwuja.php',
				 data: 'startMargin='+fObserwujaMarginMax+'&ile='+ile+'&start='+currentPhotoNumberObserwuja,
				 type: "POST",
				 success:function(msg){
						$('#photosMoveBoxObserwuja').append(msg);
						$('#photosMoveBoxObserwuja').animate({'margin-left':margin},'slow',activeMove);
						klik();
						
		
						}
				});
	}else{
		$('#photosMoveBoxObserwuja').animate({'margin-left':margin},'slow',activeMove);
	}
}

function wczytajCastingi(margin){
	
	var currentCastingsNumber=$('#castingMoveBox').children().size();
	if(currentCastingsNumber<allCastingNumbers){
		var ile=castingNumbers;
		
		if(allCastingNumbers-currentCastingsNumber<castingNumbers){
			ile=allCastingNumbers-currentCastingsNumber;
		}
		fCastingMarginMax=currentCastingsNumber*castingSizeWithMargin+5;
		$.ajax({
				 url:'portfolio_wczytajcastingi.php',
				 data: 'startMargin='+fCastingMarginMax+'&ile='+ile+'&start='+currentCastingsNumber,
				 type: "POST",
				 success:function(msg){
						$('#castingMoveBox').append(msg);
						$('#castingMoveBox').animate({'margin-left':margin},'slow',activeMove);
						klik();
		
						}
				});
	}else{
		$('#castingMoveBox').animate({'margin-left':margin},'slow',activeMove);
	}
}

$('.rightArrowSmallSlider').mouseover(function(event){$(this).css('background','url('+adresURL+'images/strzakla-prawo-over-kllisza-glosowali.jpg)no-repeat 0px 37px'); return false;}).mouseout(function(event){$(this).css('background','url('+adresURL+'images/strzakla-prawo-kllisza-glosowali.jpg)no-repeat 0px 37px'); return false;})
$('.leftArrowSmallSlider').mouseover(function(event){$(this).css('background','url('+adresURL+'images/strzakla-lewo-over-kllisza-glosowali.jpg)no-repeat 0px 37px'); return false;}).mouseout(function(event){$(this).css('background','url('+adresURL+'images/strzakla-lewo-kllisza-glosowali.jpg)no-repeat 0px 37px'); return false;})			
})
