/* Author: jfreeman | elevatoragency.com */

$(document).ready(function(){
// slideshow
	$('#homepageSlideWrap').cycle({
		fx : 'scrollHorz',
		easing : 'swing',
		speed : 800,
		next : '.slideNext',
		prev : '.slidePrev',
		pause : 1,
		timeout : 5000
	});

// carousel controls
	var carOffset = 0,
		carrWidth = 2000,
		carCurrentPos = 0,
		carCount = 1,
		carTotal = $(".carouselItem").size(),
		carStep = 0;
	
	function resizeCar() {
		var scherm = $(window).width();
		if(carTotal == 1) {
			$(".next").css("display", "none");
		}
		
		if(scherm < 1700) { 
			//alert("scale 1: " + carStep + " | " + carCount);
			
			$(".carouselItem").css("width", "2000px");
			
			carOffset = ((2000-scherm)/2)-0;
			carrWidth = 2000;
			carStep = (carrWidth)
			carCurrentPos = "-"+carOffset;
			var newPos = carOffset;
			if(carCount > 1) {
				//alert("groter dan 1 " + carStep + " | walk: ");
				newPos = carStep * (carCount-1);
				//alert("new steps: "+ carStep);
				carCurrentPos = "-"+(newPos+carOffset);
				newPos = (newPos+carOffset);
				
				//alert("newspos: " +newPos);
			}
			$("#carouselHolder").css("left", "-" + newPos + "px");
		} else {
			if(carOffset > 0) {
				//alert("scale 2");
				$("#carouselHolder").css("left", "0px");
				
				carCurrentPos = 0;//carCurrentPos - carOffset;
				carOffset = 0;
				carrWidth = scherm;
				carStep = (carrWidth+carOffset);
				
				var newPos = 0;
				if(carCount > 1) {
					//alert("groter dan 2");
					newPos = carStep * (carCount-1);
					//alert("new steps: "+ carStep);
					carCurrentPos = "-"+newPos
					$("#carouselHolder").css("left", "-" + newPos + "px");
					
				}
				$(".carouselItem").css("width", scherm+"px");
			} else {
				//alert("scale 3");
				$(".carouselItem").css("width", scherm+"px");
				carCurrentPos = carCurrentPos - carOffset;
				carrWidth = scherm;
				carOffset = 0;
				carStep = (carrWidth+carOffset)
			}
		}
	}
	
	/* Carousel commands */
	$(".previous").css("display", "none");
	$(".carRight").click(function() {
		if(carCount != carTotal) {
			//alert("currentStep: "+ carStep);
			//alert("currentPos1: "+ carCurrentPos);
			carCurrentPos = carCurrentPos - carStep;
			//alert("currentPos2: "+ carCurrentPos);
			
			
			$("#carouselHolder").animate({
				left: carCurrentPos+"px"
			}, 1000);
			
			$(".carLinkLeft").remove();
			$(".carLinkRight").remove();
			carCount = carCount + 1;
			
			$(".previous").css("display", "block");
			if(carCount == carTotal) {
				$(".next").css("display", "none");
			}
		}
	});
	$(".carLeft").click(function() {
		if(carCount != 1) {
			//alert("count: " + carCount);
			//alert("pos: " + carCurrentPos + " step: "+carStep);
			carCurrentPos = (carCurrentPos*1) + (carStep*1);

			//alert("pos2: " + carCurrentPos + " step2: "+carStep);
			$("#carouselHolder").animate({
				left: carCurrentPos+"px"
			}, 1000);
			$(".carLinkLeft").remove();
			$(".carLinkRight").remove();
			carCount = carCount - 1;
			$(".next").css("display", "block");
			if(carCount == 1) {
				$(".previous").css("display", "none");
			}
		} 
	});
		
	
	// Execute
	//resizeCar();
	//$(window).resize(resizeCar);	
});
