// this Javascript can be found at: http://clagnut.com/sandbox/imagefades/

document.write("<style type='text/css'>#thephoto {visibility:hidden;}</style>");

function initImage() {
	var imageId = 'thephoto';
	var image = document.getElementById(imageId);
	setOpacity(image, 0);
	image.style.visibility = "visible";
	fadeIn(imageId,0);
}

function fadeIn(objId,opacity) {
	if (document.getElementById) {
		obj = document.getElementById(objId);
		if (opacity <= 100) {
			setOpacity(obj, opacity);
			opacity += 25;
			window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 100);
		}
	}
}

function setOpacity(obj, opacity) {
	opacity = (opacity == 100)?99.999:opacity;
	// IE/Win
	obj.style.filter = "alpha(opacity:"+opacity+")";
	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;
	// Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}

function displayCommentForm() {
	if (document.getElementById) {
		obj = document.getElementById('comment-form-wrapper');
		show = (obj.style.display == 'none')?'block':'none';
		obj.style.display = show;
	}
}

function commentResponse(responseText, statusText) { 
	if (responseText.success == true)
	{
		var name = responseText.data.name; 
		var message = responseText.data.message; 
		
		$('#comment-form').before('<div style="margin: 5px 20px 5px 70px;"><p>Danke ' + name + ' für Deinen Kommentar:</p><blockquote>' + message + '</blockquote><p style="color:red">Der Kommentar wird erst nach Freischaltung angezeigt.</p></div>');
		$('#comment-form').clearForm();
	}
	else
	{
		var error = responseText.data;
		alert("Es ist ein Fehler aufgetreten: " + error);
	}
}

function toggleFlickrStream()
{
	if ($('#flickrStream').length == 0) {
		$('#supernav').before('<div id="flickrStream"></div>');
		loadFlickrStream();
		$('#flickrStream').slideDown();
	} else {
		if ($('#flickrStream:visible').length == 1) {
			$('#flickrStream').slideUp();
		} else {
			$('#flickrStream').slideDown();
		}
	}
}


function loadFlickrStream()
{
	$.getJSON('http://deep-resonance.net/API/flickrstream',
		function(data){
			var html = '<div class="heading">jüngste Fotos vom <a href="http://www.flickr.com/photos/deep-resonance/">Flickr Photo Stream</a></div>';
				html += '<ul id="mycarousel" class="jcarousel-skin-flickrstream">';
			$.each(data, function(i, item){
				html += '<li><a href="'+item.link+'"><img src="'+item.thumbnail.url+'" width="75" height="75" alt="'+item.title+'" /></a></li>';
			});
			html += '</ul>';
			$('#flickrStream').html(html);
			jQuery('#mycarousel').jcarousel({
				scroll: 1,
				visible: 8
			});
		}
	);
}


window.onload = function() {
	initImage()
}


$(document).ready(function(){
	$("a#show-comments").click(function() {
		$(".commentstuff").slideToggle("slow");
		return false;
	});
	$("a.show-body").click(function() {
		$(".bodystuff").slideToggle("slow");
		return false;
	});
	
	var options = {
		success: commentResponse,
		url: '/API/add-comment',
		dataType: 'json'
	};
	$('#comment-form').ajaxForm(options);

	$('#supernav').html('<a href="#" id="openFlickrStream">Flickr Stream</a><br class="clr"/>');
	$('#openFlickrStream').click(function() {
		toggleFlickrStream();
		return false;
	})
});