﻿var searchboxDefault = "Trefwoord";

$().ready(function () {

	//setHomeColumnHeight();

	initHomeSlider();

	startLogoScroller();

	initLightbox();

	$('a.animation_button').click(function () {
		var email = $('#inputFastEmail');
		var shop = $('#inputFastWebshop');
		var agree = $('#chkFastAgree');

		PageMethods.CreateSubscriptionLink($(email).val(), $(shop).val(), $(agree).attr('checked') != undefined, function (response) {
			if (response != '') {
				document.location.href = response;
			}
		});
	});

	$(function () { $('#searchBox').keydown(OnSearchKeyPress); });
	$(function () { $('#btnSearch').click(OnSearchButtonClick); });
	$(function () { $('#searchBox').focus(OnSearchFocusBlur); });
	$(function () { $('#searchBox').blur(OnSearchFocusBlur); });
});

var slideIndex = 1;
var slideAnimationDuration = 2000;
var slideAnimationTimeout = 5000;
var slideOffset = 1100;
function initHomeSlider() {
	var slider = $('#homeslider');

	if ($(slider).length > 0) {
		setTimeout(function () {
			animateNextSlide(slider);
		}, slideAnimationTimeout);
	}
}

function animateNextSlide(slider) {
	var visibleImage = $(slider).find('img:visible');
	
	if ($(visibleImage).length > 0) {
		var nextImage = $(visibleImage).next();

		if ($(nextImage).length == 0) {
			nextImage = $(slider).find('img:first');
		}

		if ($(nextImage).length > 0) {
			$(visibleImage).animate({
				left: '-1100px'
			}, slideAnimationDuration, function () {
				$(visibleImage).hide();
			});

			$(nextImage).show(function () {
				$(nextImage).animate({
					left: '0px'
				}, slideAnimationDuration, function () {
					promoteSlides(slider);
					initHomeSlider();
				});
			});
		}
	}
}

function promoteSlides(slider) {
	var images = $(slider).find('image');
	$(images).each(function () {
		var left = parseInt($(this).css('left').replace('px', ''));
		
		if (left > 0) {
			$(this).css('left', (left - slideOffset) + 'px');
		}
		if (left < 0) {
			$(this).css('left', '4400px');
		}
	});
}

//function animateNextSlide(slider) {
//	var visibleImage = $(slider).find('img:visible');

//	if ($(visibleImage).length > 0) {
//		var nextImage = $(visibleImage).next();

//		if ($(nextImage).length > 0) {
//			$(visibleImage).animate({
//				left: '-1100px'
//			}, 3000, function () {
//				$(visibleImage).hide();
//			});

//			$(nextImage).show(function () {
//				$(nextImage).animate({
//					left: '0px'
//				}, 3000, function () {
//					initHomeSlider();
//				});
//			});
//		}
//	}
//}

function initLightbox() {
	
	if ($('#producttable').length > 0) {
		$(".optioninfolink").colorbox({ iframe: true, width: "540px", height: "490px", current: "" });
	}
}

function initLogoScroller() {
	var scroller = $('#scroller');
	if ($(scroller).length > 0) {
		var imgs = $(scroller).find('img');
		var totalHeight = $(scroller).height();

		$(imgs).each(function () {
			var imgHeight = $(this).height();
			
			if (imgHeight > 0) {
				$(this).css('margin-top', (totalHeight - imgHeight) / 2 + 'px');
			}
		});
	}
}

var currentLogoIndex = 0;
function startLogoScroller() {

	initLogoScroller();

	var scroller = $('#scroller');
	var scrollerImages = $(scroller).find('img');
	var totalWidth = 0;
	
	$(scrollerImages).each(function () {
		totalWidth +=  $(this).width();
	});
	
	if (totalWidth == 0) {
		totalWidth = 870;
	}

	$('#scroller').width(totalWidth + 'px');

	var scroller = $('#logos_scroller');
	if ($(scroller).length > 0) {
		var logos = $(scroller).find('a');
		if ($(logos).length > 0) {

			var animationSpeed = 3000;

			//remove js-disabled class
			$("#scroller").removeClass("js-disabled");

			//create new container for images
			//$("<div>").attr("id", "container").css({ position: "absolute" }).width($(".scrolling").length * 170).height(170).appendTo("div#scroller");
			$("<div>").attr("id", "container").css({ position: "absolute" }).width(totalWidth).height(170).appendTo("div#scroller");

			//add images to container
			$(".scrolling").each(function () {
				$(this).appendTo("div#container");
			});

			//work out duration of anim based on number of images (1 second for each image)
			//var duration = $(".scrolling").length * animationSpeed;
			var duration = 42000;
			
			//store speed for later (distance / time)
			var speed = (parseInt($("div#container").width()) + parseInt($("div#scroller").width())) / duration;
			
			//set direction
			var direction = "rtl";

			//set initial position and class based on direction
			//(direction == "rtl") ? $("div#container").css("left", $("div#scroller").width()).addClass("rtl") : $("div#container").css("left", 0 - $("div#container").width()).addClass("ltr");
			(direction == "rtl") ? $("div#container").css("left", 0).addClass("rtl") : $("div#container").css("left", 0 - $("div#container").width()).addClass("ltr");

			//animator function
			var animator = function (el, time, dir) {
				//add direction class
				el.removeClass("ltr").addClass("rtl");

				//animate the el
				el.animate({ left: "-" + el.width() + "px" }, time, "linear", function () {

					//reset container position
					//$(this).css({ left: $('#scroller').width(), right: "" });
					$(this).css({ left: '1100px', right: "" });
					
					//restart animation
					animator($(this), duration, "rtl");

					//hide controls if visible
					($("div#controls").length > 0) ? $("div#controls").slideUp("slow").remove() : null;

				});
			}

			//start anim
			animator($("div#container"), duration, direction);

			//pause on mouseover
			$("a.scrolling").live("mouseover", function () {
				$("div#container").stop(true);
			});

			//restart on mouseout
			$("a.scrolling").live("mouseout", function (e) {
				
				//hide controls if not hovering on them
				(e.relatedTarget == null) ? null : (e.relatedTarget.id != "controls") ? $("div#controls").slideUp("slow").remove() : null;

				//work out total travel distance
				var totalDistance = parseInt($("div#container").width()) + parseInt($("div#scroller").width());

				//work out distance left to travel
				var distanceLeft = ($("div#container").hasClass("ltr")) ? totalDistance - (parseInt($("div#container").css("left")) + parseInt($("div#container").width())) : totalDistance - (parseInt($("div#scroller").width()) - (parseInt($("div#container").css("left"))));

				//new duration is distance left / speed)
				var newDuration = distanceLeft / speed;
				
				//restart anim
				//animator($("div#container"), newDuration, $("div#container").attr("class"));
				animator($("div#container"), duration, $("div#container").attr("class"));
			});
		}
	}
}

function setHomeColumnHeight() {
	var columns = $('div.home_contentblock');
	var maxHeight = 0;
	var maxTop = 0;

	if ($(columns).length > 1) {
		$(columns).each(function () {
			var button = $(this).children().last();

			if ($(this).height() > maxHeight) {
				maxHeight = $(this).height();
			}

			if ($(button).position().top > maxTop) {
				maxTop = $(button).position().top;
			}
		});

		$(columns).height(maxHeight);

		$(columns).each(function () {
			var button = $(this).children().last();
			$(button).css({ 'top': maxTop + 'px' });
		});
	}
}

/*SEARCH*/
function OnSearchKeyPress(e) {
	if (e.keyCode == 13 || e.which == 13) {
		Search($('#searchBox').val());

		e.stopImmediatePropagation();
	}
}

function OnSearchButtonClick(e) {
	Search($('#searchBox').val());

	e.stopImmediatePropagation();
}

function Search(searchTerm) {
	if (searchTerm.length > 0) {
		PageMethods.GetSearchUrl(searchTerm, GetSearchUrl_CallBack);
		//document.location.href = '/Search.aspx?query=' + searchTerm;
	}
}

function GetSearchUrl_CallBack(r) {
	location.href = r;
}

function OnSearchFocusBlur(e) {
	if ($(this).val() == searchboxDefault) {
		$(this).val('');
	}
	else if ($(this).val() == '') {
		$(this).val(searchboxDefault);
	}
}
