$(document).ready(function() {
	/**
	* Suchbox leeren
	*/
	$("#SearchTerm").click(function() {
		$(this).val('');
	})
	
	/**
	* Overlay
	*/
	$('a[rel]').overlay({
		onBeforeLoad: function() {
			$.ajax({
				type: 'GET',
				dataType: 'html',
				success: function (data, textStatus) {
					$("#content-wrap").html(data);
				},
				contentType: 'application/x-www-form-urlencoded',
				cache: false,
				url: this.getTrigger().attr("href")
			});			
		}
	});
	
	/**
	* Story versenden - Tooltip
	*/
	$('#SendFriendSendFriendForm').submit( function() {
	  $.post("/stories/send_friend");
	  return false;
	});
	
	/**
	* Story teilen - Tooltip
	*/
	$(".sharethis").click(function(e) {
		$(".social-media-tooltip").css({ 'left': e.pageX, 'top': e.pageY})
		$(".social-media-tooltip").toggle();
		return false;
	})
	
	$(".social-media-tooltip .close").click(function() {
		$(".social-media-tooltip").toggle();
		return false;
	});
});




function switchimage()
{
	if (index >= images.length)
	{
		index = 0;
	}
	$(".slider .items img").first().fadeIn(1500, function() {
		
	});		
	$(".slider .items img").last().fadeOut(1500, function() {
		$(".slider .items img").last().remove();
		$(".slider .items img").first().before(images[index]);
		index++;
		window.setTimeout("switchimage()", 5000);
	});	
}


var delay = 25;
var speed = 2;
var border = 2;
var direction = -1;
var pause = false;
function slideall() {
	if (pause != true)
	{
		marginLeft = parseInt($(".slideshow .items img").first().css("marginLeft"));
		if (direction == -1)
		{
			w = $(".slideshow .items img").first().width();
			if (marginLeft > -(w+border))
			{
				$(".slideshow .items img").first().css("marginLeft", (marginLeft-speed)+"px");
			}
			else
			{
				$(".slideshow .items img").last().after($(".slideshow .items img").first());
				$(".slideshow .items img").last().removeAttr("style");
			}	
		}
		else
		{
			if (marginLeft < border)
			{
				$(".slideshow .items img").first().css("marginLeft", (marginLeft+speed)+"px");
			}
			else
			{
				w = $(".slideshow .items img").last().width();
				$(".slideshow .items img").last().css("marginLeft", "-"+w+"px");
				$(".slideshow .items img").first().removeAttr("style");
				$(".slideshow .items img").first().before($(".slideshow .items img").last());
			}
		}			
		window.setTimeout("slideall()", delay);
	}
	else
	{
		window.setTimeout("slideall()", delay);
	}	
}


function initEndlessSlider()
{
	$("#slidestop").mouseenter(function(){ pause = true;});
	$("#slidestop").mouseleave(function(){ pause = false;});
	$("#direction-left").mouseenter(function(){ direction = -1;});
	$("#direction-right").mouseenter(function(){ direction = 1;});
	slideall();	
}
