//gibt an, ob bereits geslidet wird (DU und ICH Bild-Tooltip)
var sliding = false;
var loading = false;
var blogMoving = false;
var bildSlidingSpeed = 200;

function Fensterhoehe () {
  if (window.innerHeight) {
    return window.innerHeight;
  } else if (document.documentElement.clientHeight) {
	  return document.documentElement.clientHeight;
  }else if (document.body && document.body.clientHeight) {
    return document.body.clientHeight;
  } else {
    return 0;
  }
}

function getCursurPosY(e)
{
	cursorPosY = e.pageY;
	if (!cursorPosY)
		cursorPosY = e.y + 20;
	if (document.body.scrollTop)
		cursorPosY = e.pageY-document.body.scrollTop;
	else if (document.documentElement.scrollTop)
		cursorPosY = e.pageY-document.documentElement.scrollTop;
	else if (window.pageYOffset)
		cursorPosY = e.pageY-window.pageYOffset;	
		
	return cursorPosY;
}

function toggleBlogEntry(restContent,bildBox)
{
	//Text und Bild offen
	if ($('.mySuperAktiv').html() && $('.aktivBild').html())
	{
		//alert('beides offen');
		$('.aktivBild').fadeOut(bildSlidingSpeed,function(){
			$('.aktivBild').removeClass('aktivBild');
			$('.mySuperAktiv').slideUp(500,function(){
				$('.mySuperAktiv').removeClass('mySuperAktiv')
					.parent('.content').removeClass('highlight');
				restContent.addClass('mySuperAktiv')
					.parent('.content').addClass('highlight');
				restContent.slideDown(500,function(){
					//falls Bilder vorhanden sind
					if (bildBox)
					{
						bildBox.fadeIn(bildSlidingSpeed);
						bildBox.addClass('aktivBild');
					}
					blogMoving = false;
				});
			});
		});
	}
	//Nur Text offen
	else if ($('.mySuperAktiv').html())
	{
		//alert('nur text offen');
		$('.mySuperAktiv').slideUp(500,function(){
			$('.mySuperAktiv').removeClass('mySuperAktiv')
				.parent('.content').removeClass('highlight');
			restContent.addClass('mySuperAktiv')
				.parent('.content').addClass('highlight');
			restContent.slideDown(500,function(){
				//falls Bilder vorhanden sind
				if (bildBox)
				{
					bildBox.fadeIn(bildSlidingSpeed);
					bildBox.addClass('aktivBild');
				}
				blogMoving = false;
			});
		});
	}
	//noch nichts offen
	else
	{
		//alert('nichts offen');
		restContent.addClass('mySuperAktiv')
			.parent('.content').addClass('highlight');
		restContent.slideDown(500,function(){
			//falls Bilder vorhanden sind
			if (bildBox)
			{
				bildBox.fadeIn(bildSlidingSpeed);
				bildBox.addClass('aktivBild');
			}
			blogMoving = false;
		});
	}
}

//Src-Angaben fuer die Navi-Elemente
var sliderRechtsActivSrc = '../bitmaps/du_und_ich/pfeilRechts.gif';
var sliderLinksActivSrc = '../bitmaps/du_und_ich/pfeilLinks.gif';
var sliderLinksDeactivSrc = '../bitmaps/du_und_ich/pfeilLinksGrau.gif';
var sliderRechtsDeactivSrc = '../bitmaps/du_und_ich/pfeilRechtsGrau.gif';
//Bild-Tooltip Bildslider Right
this.imageSlideRight = function(leftButton,rightButton){

	if (sliding == false)
	{
		sliding = true;
		slideLength = 62;
		showElements = 3;
		
		maxSlideLength = $('#mySuperSlide_'+id).css('width');
		maxSlideLength = parseInt(maxSlideLength.substr(0,(maxSlideLength.length-2)));
		
		pos = $('#mySuperSlide_'+id).css('margin-left');
		if (pos != 0)
			pos = parseInt(pos.substr(0,(pos.length-2)));
		newPos = pos - slideLength;
		//alert(newPos+' '+maxSlideLength);
		if ((pos * (-1)) >= maxSlideLength-(slideLength*showElements))
		{
			sliding = false;
		}
		else
		{
			$('#mySuperSlide_'+id).animate({marginLeft: newPos+'px'},300,'',function(){sliding = false;});
			
			leftButton.removeClass('sliderLeftDeaktiv');
			leftButton.addClass('sliderLeft');
			/*
			if (leftButton.children('img').attr('src') == sliderLinksDeactivSrc)
			{
				leftButton.children('img').attr('src',sliderLinksActivSrc)
					.css('cursor','pointer');
			}
			*/
		}
		
		//Falls nicht mehr weiter gesrollt werden kann, deaktiv-Symbol anzeigen
		if ((newPos * (-1)) >= maxSlideLength-(slideLength*showElements))
		{
			rightButton.removeClass('sliderRight');
			rightButton.addClass('sliderRightDeaktiv');
			/*
			rightButton.children('img').attr('src',sliderRechtsDeactivSrc)
				.css('cursor','auto');
			*/
		}
	}
}

//Bild-Tooltip Bildslider LINKS
this.imageSlideLeft = function(leftButton,rightButton){
	if (sliding == false)
	{
		sliding = true;
		slideLength = 62;
		showElements = 3;
		
		maxSlideLength = $('#mySuperSlide_'+id).css('width');
		maxSlideLength = parseInt(maxSlideLength.substr(0,(maxSlideLength.length-2)));
		
		pos = $('#mySuperSlide_'+id).css('margin-left');
		if (pos != 0)
			pos = parseInt(pos.substr(0,(pos.length-2)));
		newPos = pos + slideLength;
		//alert(newPos+' '+pos);
		if (newPos > 0)
		{
			sliding = false;
		}
		else
		{
			$('#mySuperSlide_'+id).animate({marginLeft: newPos+'px'},300,'',function(){sliding = false;});
		
			rightButton.removeClass('sliderRightDeaktiv');
			rightButton.addClass('sliderRight');
		}
		
		//Falls nicht mehr weiter gesrollt werden kann, deaktiv-Symbol anzeigen
		if (newPos+slideLength > 0)
		{
			leftButton.removeClass('sliderLeft');
			leftButton.addClass('sliderLeftDeaktiv');
		}
	}
}


this.imagePreview = function(){	
	/* CONFIG */
		
		xOffset = 110;
		yOffset = 30;		
		
		bildWidth = 245;
		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
		
	/* END CONFIG */
	$("a.preview").hover(function(e){
		//alert('breite = '+this.title);
		this.t = this.title;
		this.title = "";	

		var borderStyle = '';
		
		if (this.className.indexOf("giveBorder") != -1)
			borderStyle = 'border: 8px solid #988347;';
		
		hoehe = this.name;
		hoehe = parseInt(hoehe);
		if (hoehe)
		{
			cursorPosY = getCursurPosY(e);
			//alert('cursor = '+cursorPosY+'fenster ='+fensterhoehe)
			xOffset = hoehe / 2 + 6;
			fensterhoehe = Fensterhoehe();
			//alert(Fensterhoehe());
			if (cursorPosY && fensterhoehe && cursorPosY+xOffset >fensterhoehe)
			{
				xOffset = hoehe - (fensterhoehe-cursorPosY) + 12;
				//alert(xOffset);
			}
		}
		
		//var c = (this.t != "") ? "<br/><br/>" + this.t : "";
		var c = "";
		
		isDislayed = false;
		
		$("body").append('<div style="display:none;'+borderStyle+'" id="preview"><img src="'+ this.href +'" alt="" />'+ c +'</div>');	
		
		$("#preview").children('img').load(function(){
			$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");
			isDislayed = true;
		});
		
		if(isDislayed == false)
		{
			
			$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");
		}
		/*
		html = '<div style="width:'+bildWidth+'px" id="preview">';
		html += '	<div style="width:'+bildWidth+'px" class="blogBild iepngfix">';
        html += '		<div style="width:'+bildWidth+'px" class="blogBildTop iepngfix"></div>';
        html += '	        <div style="height:'+(hoehe+6)+'px;width:'+bildWidth+'px;" class="blogBildMiddle iepngfix">';
		html += '				<div class="iepngfix" style="position:absolute; top:'+(xOffset-18)+'px; left:-16px; height:42px;background:transparent url(/include/iepngfix/blank.gif) 0 0 no-repeat;">';
        html += '				    <img src="../bitmaps/basic/tooltip_arrow.png" width="21" height="42" class="iepngfix ieblock" />';
        html += '				</div>';
		html += '				<div style="margin: 0 12px;border-top:1px solid #988347;border-bottom:1px solid #988347;">';
		html += '					<img class="ieblock" style="margin: 2px 0;" src="'+ this.href +'" alt="Image preview" />';
		html += '				</div>';
		html += '			</div>';
        html += '		  <div style="overflow:hidden;left:0;top:'+(hoehe+16)+'px;width:'+(bildWidth)+'px" class="iepngfix blogBildBottom"></div>';
        html += '	</div>';
		html += '	</div>';
	
		$("body").append(html);
		*/
		
		
		
    },
	function(){
		this.title = this.t;	
		$("#preview").remove();
    });	
	$("a.preview").mousemove(function(e){
		hoehe = this.name;
		if (hoehe)
		{
			
			cursorPosY = getCursurPosY(e);
			fensterhoehe = Fensterhoehe();
			if (cursorPosY && fensterhoehe && cursorPosY+xOffset >fensterhoehe)
			{
				xOffset = hoehe - (fensterhoehe-cursorPosY) + 12;
				//alert(xOffset);
			}
		}
		$("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};

wellnessMove = false;
// starting the script on page load


$(document).ready(function(){
	
	//Wellness-Button Mouseover
//	$('#wellnessButton').mouseenter(
//		function(){
//			if (wellnessMove == false)
//			{
//				wellnessMove = true;
//				$(this).animate({width:"78px", height:"78px", top:"-=3px", right:"-=3px"}, 150, function(){
//					$(this).animate({width:"72px", height:"72px", top:"+=3px", right:"+=3px"}, 150, function(){
//						$(this).animate({width:"78px", height:"78px", top:"-=3px", right:"-=3px"}, 150, function(){
//							$(this).animate({width:"72px", height:"72px", top:"+=3px", right:"+=3px"}, 150, function(){
//								wellnessMove = false;																						 
//							});
//						});
//					});
//				});
//			}
//		});


	//Du und Ich / Eckartswelt: Blog-Bild Toggle
	$('.blogBildToggle').click(function(e){
		
		//Abfangen, dass nicht mehrere events geschickt werden können, bevor das erste ausgefuehrt wurde
		if (blogMoving == false)
		{
			blogMoving = true;
			bildBox = $(this).parent().parent().prev('.blogBildBox').children(' .blogBild');
			//alert(bildBox.html());
			restContent = $(this).next();
			
			//falls das aktuelle element eingeklappt ist
			if (restContent.css('display') == 'none')
			{
				id = this.id;
				
				//falls die bilder aus der DB geholt werden muessen
				if (bildBox.children('.blogBildMiddle').html() == 'LEER')
				{
					bildBox.children('.blogBildMiddle').html('');
					//AJAX Bildabfrage
					$.post("/du_und_ich/getTooltip.php", {blogID: id },function(data){
						if (data != "LEER")
						{
							bildBox.children('.blogBildMiddle').html(data);
							$('.bigThumb').colorbox({});
							$('.tmpThumb').colorbox({});
							$('.bigThumbVideo').colorbox({},function(data){
								loadVideoFlash();
							});
							//slider Rechts
							$('#slideRight_'+id).click(function(){imageSlideRight($('#slideLeft_'+id),$(this));});
							//slider links
							$('#slideLeft_'+id).click(function(){imageSlideLeft($(this),$('#slideRight_'+id));});
							
							$('#bigThumbBox_'+id).hover(
								function(){
									$('#bigThumbBox_'+id).children('.bigThumbBoxContent').children('.bigThumb').children('.thumbBigPlus').addClass('thumbBigHover');
								},
								function(){
									$('#bigThumbBox_'+id).children('.bigThumbBoxContent').children('.bigThumb').children('.thumbBigPlus').removeClass('thumbBigHover');
								}
							);
						}
						
					});
					
					toggleBlogEntry(restContent,bildBox);
				}
				else
					toggleBlogEntry(restContent,bildBox);
			}
			else
			{
				bildBox.fadeOut(bildSlidingSpeed,function(){
					restContent.slideUp(500,function(){
						restContent.removeClass('aktiv').parent('.content').removeClass('highlight');
						blogMoving = false;
					});
				});
				bildBox.toggleClass('aktivBild');
			}
		}
	});
	
	//Du und Ich / Eckartswelt: BlogToggle ohne Bild
	$('.blogToggle').click(function(){
									
		if (blogMoving == false)
		{
			restContent = $(this).next();
			bildBox = false;
			blogMoving = true;
			//alert(restContent.css('visibility'));
			if (restContent.css('display') == 'none')
				toggleBlogEntry(restContent,bildBox);
			else
			{
				restContent.slideUp(500,function(){
					restContent.removeClass('aktiv').parent('.content').removeClass('highlight');
					blogMoving = false;
				});
			}
		}
	});
	
	$('.ersterBild').trigger('click');
	$('.erster').trigger('click');

	imagePreview();
	//termine einklappen
	$('.toggleTrigger').click(function(e){
		id = this.id;
		if ($('#toggle_'+id).css('display') == 'none')
		{
			$('#toggle_'+id).slideToggle(250);
			$('#container_'+id).toggleClass('terminAktiv');
		}
		else
		{
			
			$('#toggle_'+id).slideToggle(250,function(){$('#container_'+id).toggleClass('terminAktiv');})
		}
	});
	//termin-tooltip
	$('.einladen_image').mouseenter(function(e){
		id = this.name;
		//myEvent = e;
		
		if(!isNaN($('#tID').val()))
			$('#tID').val(id);
		else{
			$(".tt_arrow").html("<div style='text-align:center;padding:5px;'><img src='/bitmaps/basic/loading.gif' alt='laden...' title='laden' /></div>");
			$.post("/termine/verabreden.php", { func: 'anzeigen', tID: id },
														function cb(data){
															$(".tt_arrow").html(data);	
														});
			
		}
		
		tipResize(e,375);
		
		//Mouse-out beim Tooltip
		$("#verabreden").mouseleave(function(e){
			$("#verabreden").hide();
		});
	});
	
});

function tipResize(e,hoehe,yOffset)
{
		posY = e.pageY;
		
		if (!yOffset)
			yOffset = 50;
		
		if (!hoehe)
			hoehe = $('#verabreden').height();
		
		cursorPosY = getCursurPosY(e);
		fensterhoehe = Fensterhoehe();
		if (cursorPosY && fensterhoehe && cursorPosY+hoehe- 80 >fensterhoehe)
		{
			yOffset = cursorPosY+hoehe - fensterhoehe - 60;
		}
		
		pfeilPos = yOffset-60;
		if (pfeilPos < 0)
			pfeilPos = 0;
		//$(".tt_arrow").css("background-position",'left '+(pfeilPos)+'px');
		$("#hitbox").css("top",(pfeilPos)+'px');
		
		$("#verabreden").css("top",(posY - yOffset) + "px").show();
}

function showBigThumb(blogID,bildID)
{
	if (loading == false)
	{
		loading = true;
		$.post("/du_und_ich/getBigThumb.php", { blogID: blogID, bildID: bildID },
			function(data){
				//alert(data);
				if (data != 'fehler')
				{
					$('#bigThumbBox_'+blogID).children('.bigThumbBoxContent').fadeOut('fast',function(){
						var tmp = $(this);
						tmp.html(data);
							//.fadeIn('slow');
						tmp.children('.bigThumb').children('img').load(function(){
							tmp.fadeIn('fast');
							$('.bigThumb').colorbox({});
							$('.tmpThumb').colorbox({});
							$('.bigThumbVideo').colorbox({},function(data){
								loadVideoFlash();
							});
							loading = false;
						});
						
					});
					
					
					$('#bigThumbBox_'+id).hover(
						function(){
							$('#bigThumbBox_'+id).children('.bigThumbBoxContent').children('.bigThumb').children('.thumbBigPlus').addClass('thumbBigHover');
						},
						function(){
							$('#bigThumbBox_'+id).children('.bigThumbBoxContent').children('.bigThumb').children('.thumbBigPlus').removeClass('thumbBigHover');
						}
					);
				}
				else
					loading = false;
		});
	}
	
}

function showPresseNewsletterBestellen(e){

	$(".tt_arrow").html("<div style='text-align:center;padding:5px;'><img src='/bitmaps/basic/loading.gif' alt='laden...' title='laden' /></div>");
	$.post("/presse/presseNewsletter.php", { func: 'anzeigen' },
		   										function cb(data){
													$(".tt_arrow").html(data);	
												});
	$("#verabreden").css("top","545px").css("display", "block");	
}

function closeTip(id)
{
	$('#'+id).toggle('fast');
}
