var play;

$(document).ready(function () {
	// value text in search fields dissapears when focus on
	$("input.search").focus(function () {
		$(this).val("");
	});
	$("input.search").focusout(function () {
		if ($(this).val().length < 1) {
			$(this).val("to search, type and hit enter");
		}
	});
	$("#top-bar-search input").focus(function () {
		$(this).val("");
	});
	$("#top-bar-search input").focusout(function () {
		if ($(this).val().length < 1) {
			$(this).val("to search, type and hit enter");
		}
	});
	// slidedown animation for menu
	$("#navigation ul li.has-menu").hover(function () {
		$(this).each(function () {
			var isActive = $(this).hasClass('extended');
			$('.extended div').hide();
			$('.extended').removeClass('extended');
			if (!isActive) {
				$(this).addClass('extended');
				$('.extended div').show();
			}
		});
	}, function () {
		$('.extended div').hide();
	});
	// slidedown animation for more-menu
	$("a.more-menu").click(function () {
		$("#top-bar-search").removeClass("trigger");
		var isActive = $("#more-menu").hasClass('extend');
		$('.extend').hide();
		$('.extend').removeClass('extend');
		if (!isActive) {
			$("#more-menu").addClass('extend');
			$('.extend').show();
		}
	});
	$('body').bind('click', function (e) {
		if ($(e.target).closest('#top-bar').length == 0) {
			$("#top-bar-search").removeClass("trigger");
			$('.extend').hide();
			$('.extend').removeClass('extend');
		}
	});
	$(".top-search-submit").val("");
	// search field on top bar
	$("a.top-bar-search").click(function () {
		$("#top-bar-search").toggleClass("trigger");
		$('.extend').hide();
		$('.extend').removeClass('extend');
	});

	// carousel animation
	$(".slide-paging").show();
	$(".slide-paging a:first").addClass("active");

	var imgw = $(".window").width();
	var imgSum = $(".imageReel img").size();
	var imgReelw = imgw * imgSum;

	$(".imageReel").css({ 'width': imgReelw });

	rotate = function (active, manSpeed) {
		var triggerID = active.attr("rel") - 1;
		var image_reelPosition = triggerID * imgw;

		$(".slide-paging a").removeClass('active');
		active.addClass('active');

		$(".desc").stop(true, true).delay(800).hide();
		$(".desc").eq($('.slide-paging a.active').attr("rel") - 1).fadeIn();
		$(".desc-title").stop(true, true).delay(900).hide();
		$(".desc-title").eq($('.slide-paging a.active').attr("rel") - 1).fadeIn();
		$(".desc-link").stop(true, true).delay(900).hide();
		$(".desc-link").eq($('.slide-paging a.active').attr("rel") - 1).fadeIn();

		var defSpeed = $(".slideSpeed").text();
		var speed;
		speed = manSpeed;

		if (speed != 0) {
			speed = defSpeed;
		} else {
			speed = manSpeed;
		}
		$(".imageReel").animate({
			left: -image_reelPosition
		}, speed);
	};

	rotateStart = function () {
		$(".desc").eq($('.slide-paging a.active').attr("rel") - 1).fadeIn();
		$(".desc-title").eq($('.slide-paging a.active').attr("rel") - 1).fadeIn();
		$(".desc-link").eq($('.slide-paging a.active').attr("rel") - 1).fadeIn();
		window.clearInterval(document.play);
		document.play = window.setInterval(function () {
			var active = $('.slide-paging a.active').next();
			var manSpeed;

			if (active.length === 0) {
				active = $('.slide-paging a:first');
				manSpeed = 0;
			}
			rotate(active, manSpeed);
		}, 8000);
	};
	rotateStop = function () {
		window.clearInterval(document.play);
	}

	rotateStart();

	$(".slide-paging a").click(function () {
		rotateStop();
		rotate($(this));
		//rotateStart();
		return false;
	});

	$("#carousel").hover(function () {
		rotateStop();
	}, function () {
		rotateStart();
	});

	$(window).blur(function () {
		rotateStop();
	});

	$(window).focus(function () {
		rotateStart();
	});
});
