
$(document).ready(function(){
	$("a[rel^='prettyPhoto']").prettyPhoto({
		theme: 'light_square',
		opacity: 0
	});

	// $(".date").live('mouseover', function() {
	// 	$("#linklist").find(".bookmark-content").hide();
	// 	$("#linklist").find(".hover").removeClass("hover");		
	// 	$(this).parent().find(".bookmark-content").show();
	// 	$(this).parent().addClass("hover");
	// });

	$(".toggle-content").live('click', function() {
		$content = $(this).parent().find('.bookmark-content'); 
		$info = $(this).parent().parent().parent().find('.info'); 
		$date = $(this).parent().parent().parent().find('.date'); 
		if ($content.is(':visible')) {
			$(this).text('⇒');
			$content.hide();
			$('.info').removeClass('hover');
			$('.date').removeClass('hover');
		} else {
			$('.info').removeClass('hover');
			$('.date').removeClass('hover');
			$('.toggle-content').text('⇒');
			$(this).text('⇓');
			$(".bookmark-content").hide();
			$content.show();
			$info.addClass('hover');
			$date.addClass('hover');
		}
		return false;
	});

	// $(".bookmark").live('mouseover', function() {
	// 	$(this).find(".bookmark-content").show();
	// 	$(this).addClass("hover");
	// })
	//  
	// $(".bookmark").live('mouseout', function() {
	// 	$(this).find(".bookmark-content").hide();
	// 	$(this).removeClass("hover");
	// })
	
	$(window).scroll(function(){
		if  ($(window).scrollTop() == $(document).height() - $(window).height() && $("#more").length > 0) {
			start = start + perpage;
			$("#more").text('loading...');
			$.ajax({
				type: "POST",
				url: "/API/stream",
				data: {start: start, perpage: perpage, tag: stag, term: sterm},
				dataType: "json",
				success: function(data) {
					$("#more").remove();
					if (data.success) {
						$("#linklist").append(data.data);
						$("#linklist").append('<a id="more" href="#">mehr</a>');
					}
				}
			})
		}
	});
	
	$("#q").attr('autocomplete', 'off')
		.val("Suchen...")
		.css("color", "#ddd")
		.focus(function() {
			if ($("#q").val() == "Suchen...") {
				$("#q").val("").css("color", "#333");
			}
		})
		.blur(function() {
			if ($("#q").val() == "") {
				$("#q").val("Suchen...").css("color", "#ddd");
			}
		});

	$("#searchform").submit(function() {
		livesearch();
		return false;
	});

	$("#more").live('click', function() {
		start = start + perpage;
		$("#more").text('loading...');
		$.ajax({
			type: "POST",
			url: "/API/stream",
			data: {start: start, perpage: perpage, tag: stag, term: sterm},
			dataType: "json",
			success: function(data) {
				$("#more").remove();
				if (data.success) {
					$("#linklist").append(data.data);
					$("#linklist").append('<a id="more" href="#">mehr</a>');
				}
			}
		})
		return false;
	});

	function livesearch() {
		$("#more").remove();
		var q = $("#q").val();
		if (q.substr(0, 4) == 'tag:') {
			q = q.replace(/tag:/, '');
			data = {tag: q}
			stag = q;
		} else {
			data = {search: q}
			sterm = q;
		}
		if (q.length > 2) {
			start = 0;
			$('#q').attr('disabled', 'disabled');
			$.ajax({
				type: "POST",
				url: "/API/livesearch",
				data: data,
				dataType: "json",
				success: function(data) {
					$('#q').attr('disabled', '');
					if (data.success == true) {
						$("#content").html(data.data);
						$("a[rel^='prettyPhoto']").prettyPhoto({theme: 'light_square'});
					}
				}
			});
		}
	}
});
