// init
$(function() {

	var cache = [];
  	// Arguments are image paths relative to the current page.
	$.preLoadImages = function() {
		var args_len = arguments.length;
		for (var i = args_len; i--;) {
			var cacheImage = document.createElement('img');
			cacheImage.src = arguments[i];
			cache.push(cacheImage);
		}
	}

	

	//fancybox
	//$('.fancybox').fancybox();
	$(".fancybox, .fancybox2").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'inside',
		'titleFormat'       : function(title, currentArray, currentIndex, currentOpts) {
			titleOut = "";
			if (title != bildbezeichnung)
				titleOut = title;
						
			return '<span id="fancybox-title-inside">' + titleOut + '</span>';
		},
		'callbackOnClose'	: function() {
			$("#fancy_content").empty();
       } 
	});
	
	//$(".fancybox2").css("position", "relative").css("background-image","url(img/anwenderbericht-lupe.jpg)").css("background-position", "right bottom");
	//$(".fancybox2").append('<img src="img/anwenderbericht-lupe.jpg" class="fbLupe" />');
	//$(".fancybox2").each('<img src="img/anwenderbericht-lupe.jpg" class="fbLupe" />');
	
	$('.fancybox2').each(function(index) {
				//				  console.log($(this).find("img"));
		if ($(this).children("img").size() > 0)
			$(this).append('<img src="img/anwenderbericht-lupe.jpg" class="fbLupe" />');
	});
	
	

	startParallax(false);
	$(window).resize(function() { startParallax(true) });
})


function startParallax(resize) {
	doParallax = false;
	
	if ($(window).width()-35 > 860) {
		parallaxWidth = $(window).width()-35;
		doParallax = true;	
	}

	if (doParallax) {
		$("#parallax").width(parallaxWidth);
		$("#parallax .cnt").width(parallaxWidth);
	
		$("#parallax").css("left", ($(window).width()-parallaxWidth)/2);
		$(".parallax-titelbild").css("left", (parallaxWidth-881)/2);
		
		$('#parallax .cnt a').each(function(index) {
			curLeft = parseFloat($(this).css("left").replace(/px$/, ""));
			newLeft = parseFloat(curLeft - parseFloat(parallaxOldWidth-parallaxWidth)/2);

			$(this).css("left", newLeft + "px");
		});
		parallaxOldWidth = parallaxWidth;

	
		$("#parallax").show();		
		jQuery('#parallax .cnt').parallax({mouseport: jQuery('#parallax'), mouseResponse: true}, 
			{yparallax: false, xparallax: '50px'}, 
			{yparallax: false, xparallax: '150px'},
			{yparallax: false, xparallax: '100px'}
		);
		
	}
	else {
		$("#parallax").hide();
	}			

}
			
function pageListScroll (richtung, elementeSichtbar) {
	cntElements = $('.pagelistScroll a').size();
	curLeft = $(".pagelistScroll").css("left").replace(/px$/, "");
	leftDistance = 0;
	
	if (richtung == "next") {
		newLeft = curLeft - elementeSichtbar*29;
		if (newLeft > cntElements * (-29)) {		
			leftDistance = newLeft-curLeft;
		}
	}
	else if (richtung == "prev") {
		newLeft = parseInt(curLeft) + parseInt(elementeSichtbar*29);
		if (newLeft <= 0) {		
			leftDistance = newLeft-curLeft;
		}
	}
	
	if (leftDistance < 0) {
		leftDistance *= -1;
		$(".pagelistScroll").animate({"left": "-="+leftDistance+"px"});
	}
	else if (leftDistance > 0) {
		$(".pagelistScroll").animate({"left": "+="+leftDistance+"px"});
	}
	
	if (leftDistance != 0) {
		if (newLeft == 0)
			$(".pagelistPrev img").hide();
		else if (cntElements > elementeSichtbar)
			$(".pagelistPrev img").show();
		else 
			$(".pagelistPrev img").hide();
			
		console.log(newLeft);
		
		if (newLeft-elementeSichtbar*29 <= cntElements * (-29))
			$(".pagelistNext img").hide();
		else if (cntElements > elementeSichtbar)
			$(".pagelistNext img").show();
		else
			$(".pagelistNext img").hide();
	}
	
	if (cntElements <= elementeSichtbar) {
		$(".pagelistPrev img, .pagelistNext img").hide();
	}
}

function newsHomeScroll(richtung) {
	cntElements = $('.newsKlein-ScrollAreaInner a.newsKlein-Eintrag').size();	
	curTop = $(".newsKlein-ScrollAreaInner").css("top").replace(/px$/, "");
	
	if (richtung == "next") {
		newTop = parseInt(curTop) - 64; 
		if (newTop <= 0 && newTop >= (cntElements-3)*(-64)) {		
			$('.newsKlein-ScrollAreaInner').animate({top: '-=64'});
		}
	}
	else if (richtung == "prev") {
		newTop = parseInt(curTop) + 64; 
		if (newTop <= 0 && newTop >= (cntElements-3)*(-64)) {		
			$('.newsKlein-ScrollAreaInner').animate({top: '+=64'});
		}
	}
}

function ajaxNewsletteranmeldung() {	
	$.ajax({
		type: "POST",
		url: "ajaxServer.php",
		data: "action=newsletteranmeldung&nl_anrede=" + $("#footerNlAnrede").val() + "&nl_vorname=" + $("#nl_vorname").val() + "&nl_nachname=" + $("#nl_nachname").val() + "&nl_email=" + $("#nl_email").val() + "&email=" + $("#email").val(),
		success: function(response){
			//alert(response);
			jQuery.noticeAdd({text: response});
		}
	});
}

function resizeFancybox(width, height) {
	width2 = width+20;
	height2 = height+42;
	
	$("#fancybox-wrap").width(width2+"px").height(height2+"px");
	$("#fancybox-inner").width(width+"px").height(height+"px");
}
