// Debugging-Function. Only active in Browser who support it
function pr() {
	if (!window.console) {
		return;
	}
	if (typeof window.console !== 'undefined' && typeof window.console.log === 'function') {
		console.log(pr.arguments);
	}
}

(function($) {
	// Produces correct URLs relative to Router::url('/)
	$.url = function(url) {
	  return $('meta[name=identifier-url]').attr('content')+url.substr(1);
	}

	$(function() {
		// Generate a Page-Object to bind events on
		window.Page = $('body');
		
		//Take care about the non-js fallback-version of the site 
		$('body').addClass('js').removeClass('no-js');

		// if ($('body').hasClass('start')) {
		// 	$('#menu,#content').hide();
		// 	$(document).one('mousemove.start keypress.start', function() {
		// 		timeout = 1;
		// 		if ($.browser.safari) {
		// 			timeout = 1000;
		// 		}
		// 		window.setTimeout(function() {
		// 			$('#menu,#content').fadeIn('slow');
		// 		}, timeout);
		// 		$(document).unbind('.start');
		// 	});
		// 	window.setTimeout(function() {
		// 		$(document).trigger('mousemove.start');
		// 	}, 1000);
		// }

		// Make external links open in a new window
		$('a[rel=external]').live('click', function() {
			window.open(this.href);
			return false;
		});

		twttr.anywhere(function (T) {
			// T('#follow-nemata2004').followButton("nemata2004");
			T('#follow-nemata2004').hovercards();
		});

		$('<div/>').attr('id', 'line').prependTo('body');
		$(window).bind('resize', function() {
			$('#line').width($('#page').offset().left);
			window.setTimeout(function() {
				$('#line').width($('#page').offset().left);
			}, 100);
		}).trigger('resize');

		$('.gallery').each(function() {
			var $slideshow = $(this).find('.slideshow');
			var $prev = $(this).find('.prev');
			var $next = $(this).find('.next');
			if ($slideshow.find('li').length < 2) {
				$prev.hide();
				$next.hide();
				return;
			}
			$(this).find('.slideshow').cycle({ 
				fx:			'scrollHorz',
				prev:		$prev,
				next:		$next,
				timeout:	0
			});
		});
		// And now all the project-specific stuff to be executed ON READY
		$.loadStreams();
	});


	// Definition of project-specific function
	$.loadStreams = function() {
		if ($('.section.facebook').length > 0) {
			$('.section.facebook p.content').html('<span class="loading"></span>');
			$.getJSON($.url('/facebook.json'), function(data) {
				if (typeof data.message != 'undefined') {
					$('.section.facebook p.content').html(data.message);
				} else if (data.name && data.link) {
					$('.section.facebook p.content').html($('<a />').attr('href', data.link).attr('rel', 'external').text(data.name));
				}
				//$('.section.facebook a.logo,.section.facebook .like a').attr('href', data.link);
			});
		}
		if ($('.section.twitter').length > 0) {
			$('.section.twitter p.content').html('<span class="loading"></span>');
			$.getJSON($.url('/twitter.json'), function(data) {
				$('.section.twitter p.content')
					.html(data.text)
					.attr('id', 'twitter-box');
					twttr.anywhere(function (T) {
						T("#twitter-box").hovercards();
					});
			});
		}
	};
})(jQuery);
