var timerId;
var timeDelay = 8000;
var fading = false;
var fadingPending = false;
var hiddenActive = false;
var fadingPaused = false;
var navigateHide = false;
var navigateToIndex = -1;
var articleSize = 0;
var footerTimerId;
var menutriggerTimerId;

$(document).ready(function() {
		articleSize = $(".articleinnernavigation a").size();
		var articleWidth = 0;
		for(i=0; i<articleSize; i++) {
			articleWidth += $(".articleinnernavigation a").eq(i).width() + 29;
		}
		
		$(".submenu a:last").css("border-right", "none");
		
		if($(".articleboxsub").size() > 0) {
			$(".articlescroller").css("overflow", "visible");
		}
		
		var maxHeight = $(document).height();
		$(".articlebox").each(function() {
				maxHeight = Math.max(maxHeight, $(this).height() + $(this).position().top + 150);
		});
		
		$(".articlebox").height(maxHeight);
		$(".articleboxsub").height(maxHeight - 235);
		
		$(".articleboxes").height(maxHeight);
		$(".articlescroller").height(maxHeight);
		
		$(".maincontainer").height(maxHeight);
		
		$(".footernews:even").css("clear", "left");
		
		$(".newsletterscroll").height($(".newsletter").height());
		$(".newslettersubscribed").height($(".newsletter").height());
		$(".newsletterunsubscribed").height($(".newsletter").height());
		
		$(".footer").hover(function() {
				clearTimeout(timerId);
				clearTimeout(footerTimerId);
				
				$(this).addClass("footershow");
				footerTimerId = setTimeout("handleFooter()", 350);
				
		}, function() {
			clearTimeout(footerTimerId);
			$(this).removeClass("footershow");
			
			footerTimerId = setTimeout("handleFooter()", 350);
		});
		
		$(".articleinnernavigation a").hover(function() {
				clearTimeout(timerId);
				navigateToIndex = $(this).prevAll().size();
				setTimeout("navigateTo(" + $(this).prevAll().size() + ")", 500);
		}, function() {
			navigateToIndex = -1;
			clearTimeout(timerId);
			timerId = setTimeout("articleNavigate(1)", timeDelay);
		});
		
		if(articleSize > 1) {
			articleNavigate(0);
			timerId = setTimeout("articleNavigate(1)", timeDelay);
		}
		
		handleMenu(true);
		$(".menutrigger").hover(function() {
				$(".menutrigger").removeClass("level1active").removeClass("level1open").removeClass("menutriggerborder");;
				$(this).addClass("level1active");
				clearTimeout(menutriggerTimerId);
				menutriggerTimerId = setTimeout("handleMenu(false);", 350);
		}, function() {
		});
		
		$(window).resize(function() {
				var maxHeight = $(document).height();
				$(".articlebox").each(function() {
						maxHeight = Math.max(maxHeight, $(this).height());
				});
				
				$(".articlebox").height(maxHeight);
				
				$(".articleboxes").height(maxHeight);
				$(".articlescroller").height(maxHeight);
				
				$(".maincontainer").height(maxHeight);
		});

		
		$("input[type=text], textarea").each(function() {
				var defaultValue = $(this).val();
				$(this).blur(function() {
						if($(this).val() == '') {
							$(this).val(defaultValue);
						}
				});
				
				$(this).focus(function() {
						if($(this).val() == defaultValue) {
							$(this).val('');
						}
				});
		});
		
 
		$('.newsletterform').submit(function() { 
				$(this).ajaxSubmit({success: showThankyou}); 

				return false; 
		});
		
		$("#newsletterbutton").click(function() {
				$('.newsletterform').submit();
				return false;
		});
 
		$('.contactform').submit(function() { 
				$(this).ajaxSubmit({success: showThankyouContact}); 

				return false; 
		});
		
		$("#contactbutton").click(function() {
				$('.contactform').submit();
				return false;
		});
});

function handleFooter() {
	if($(".footer").hasClass("footershow")) {
		var footerHeight = $(".footercontent").height();	
		$(".footer").animate({ 
				height: footerHeight
		}, 750, "easeInOutQuint", function() {
			footerAnimation = 0;
		});
	} else {
		$(".footer").animate({
					height: 40
		}, 500, "easeInOutSine", function() {footerAnimation = 0});
		
		if(articleSize > 1) {
			timerId = setTimeout("articleNavigate(1)", timeDelay);
		}
	}
}

function handleMenu(instant) {
	$(".menutrigger").each(function() {
			var nextmenu = $(this).next();
			while(nextmenu.attr("href") && !nextmenu.hasClass("menutrigger")) {
				if($(this).hasClass("level1active")) {
					$(this).addClass("level1open");
					nextmenu.slideDown("slow");
					var testmenu = nextmenu.next();
					if(testmenu.attr("href") && testmenu.hasClass("menutrigger")) {
						testmenu.addClass("menutriggerborder");
					}
				} else {
					if(instant) {
						nextmenu.hide();
					} else {
						nextmenu.slideUp("slow");
					}
				}
				nextmenu = nextmenu.next();
			}
	});
}

function navigateTo(index) {
	if(index == navigateToIndex) {
		$(".articleinnernavigation a").eq(index).addClass("active").siblings().removeClass("active");
		articleNavigate(0);
	}
}


function articleNavigate(offset) {
	var thisNav = $(".articleinnernavigation a.active");
	fading = true;
	var prevCount = thisNav.prevAll().size();
	
	if(offset == 1) {
		thisNav.removeClass('active').next().addClass("active");
		if($(".articleinnernavigation a.active").size() == 0) {
			$(".articleinnernavigation a").eq(0).addClass("active");
		}
		
		if(!fadingPaused) {
			clearTimeout(timerId);
			timerId = setTimeout("articleNavigate(1)", timeDelay);
		}
		articleNavigate(0)
		return;
	} else if(offset == -1) {
		thisNav.removeClass('active').prev().addClass("active");
		if($(".articleinnernavigation a.active").size() == 0) {
			$(".articleinnernavigation a:last").addClass("active");
		}
		if(!fadingPaused) {
			clearTimeout(timerId);
			timerId = setTimeout("articleNavigate(1)", timeDelay);
		}
		articleNavigate(0)
		return;
	}
	
	$(".articlebox").eq(prevCount).siblings().animate({ 
			marginLeft: 0,
			marginTop: $(document).height()
	}, 1500, "easeInOutQuint", function() {
		
	});
	
	var indekatorOffset = 0;
	for(q=0; q <= prevCount; q++) {
		if(q == 0) {
			indekatorOffset += 14;
		} else {
			indekatorOffset += $(".articleinnernavigation a").eq(q - 1).width() + 14 * 2;
		}
	}
	$("#indekator").animate({ 
			marginLeft: indekatorOffset,
			width: $(".articleinnernavigation a").eq(prevCount).width()
	}, 1500, "easeInOutQuint");
	
	$(".articlebox").eq(prevCount).css("marginTop", -$(document).height()).css("marginLeft", "0px");
	$(".articlebox").eq(prevCount).animate({ 
			marginLeft: 0,
			marginTop: 0
	}, 1500, "easeInOutQuint", function() {
		fading = false;
		if(fadingPending) {
			fadingPending = false;
			articleNavigate(0);
		} 
	});
}

function showThankyou(responseText, statusText) {
	if(responseText == "subscribed") {
		$(".newsletter").animate({ 
				marginLeft: 375
		}, 750, "easeInOutBack", function() {
			$(".newslettersubscribed").animate({ 
					marginLeft: 0
			}, 750, "easeInOutBack");
		});
	} else if(responseText == "unsubscribed") {
		$(".newsletter").animate({ 
				marginLeft: -375
		}, 750, "easeInOutBack", function() {
			$(".newsletterunsubscribed").animate({ 
					marginLeft: 0
			}, 750, "easeInOutBack");
		});
	} else {
		alert(responseText);
	}
}

function showThankyouContact(responseText, statusText) {
	if(responseText.indexOf("<h1>") != -1) {
		$(".articleboxsub").html(responseText);
	} else {
		alert(responseText);
	}
}
