var timer = null;

function init () {
	
}
		
function animBanner (imgID) {
				
			var $active = $('#slideshow img.active');
			
			if($active.length == 0) $active = $('#slideshow img:first');
			
			var $next = $active.next().length ? $active.next() : $('#slideshow img:first');
			if (imgID != undefined) $next = $('#'+imgID);
			
			if(timer != null) clearTimeout (timer);
			
			$active.addClass('last-active');
			$('#'+$active.attr('id')+'_btn').removeClass('selected');
			$('#'+$next.attr('id')+'_btn').addClass('selected');
			
			$next.css ({opacity:0.0})
				.show ()
				.addClass ('active')
				.animate ({opacity:1.0}, 1000, function () {
					$active.removeClass('active last-active');
					timer = setTimeout ("animBanner ()", 8000);
				});
}
		
function animLogo () {
				
			var $all_logo = $('#logos_all');
			var $height = $all_logo.height() / 2;
			$all_logo.animate ({top:-$height}, 30 * $height, 'linear', function (e){
				$(this).css({top:0});
				animLogo ();
			});
}

function activateBannerHover () {
	
	$('#slideshow img').hover(
				function ()
				{
					var $target = $('#banner_hover img:first');
					
					if (!$target.hasClass('active'))
					{
						$('#banner_hover').show();
						$target.css({left:0});
						$target.animate ({opacity:1.0}, 500, function ()
						{
							$(this).addClass('active');

						});
						
					}
				},
				function ()
				{
					var $target = $('#banner_hover img:first');
					
					if ($target.hasClass('active'))
					{
						$('#banner_hover').show();
						$target.animate({opacity:0}, 500, function()
						{
							$(this).css({left:-330});
							$(this).removeClass('active');
						});
					}
				}
			)
}

function getFB_Feed ()
{
	$.ajax({
		type:'get',
		url:'fb_token.php',
		dataType:'text',
		success:parseFB_Feed,
		error:function (e)
		{
			$('#fb_feed').append('<li>Connection Error!</li>');
		}
	});
}

function parseFB_Feed (token)
{
	$.getJSON("https://graph.facebook.com/179901898290/feed?date_format=U&access_token=" + token + "&limit=8&callback=?",
		function(data){
			
			var maxItem = 3;
			var item = 0;
			
			for (var i = 2; i < data.data.length; i++)
			{
				var pagename = data.data[0].from.name;
				var uid = data.data[i].from.id;
				var name = data.data[i].from.name;
				var postid = data.data[i].id.split ("_");
				var posttype = data.data[i].type;
				var created = new Date (data.data[i].created_time * 1000);
				var updated = new Date (data.data[i].updated_time * 1000);
				var totalComments = data.data[i].comments == null ? 0 : data.data[i].comments.count;
				var totalLikes = data.data[i].likes == null ? 0 : data.data[i].likes.count;
				
				switch (posttype)
				{
					case "status":
						if (data.data[i].message != undefined)
						{
							var message = data.data[i].message;
							var maxChars = 70;
							if (message.length > maxChars)
							{
								message = message.substring (0, maxChars);
								message = message.substring (0, message.lastIndexOf (" ")) + "...";
							}
							
							$('#fb_feed').append('<li><a href="http://www.facebook.com/profile.php?id=' + uid + '" target="_blank"><img src="http://graph.facebook.com/' + uid + '/picture?type=square" align="left"/> ' + name + '</a> posted <a href="http://www.facebook.com/permalink.php?story_fbid=' + postid[1] + '&id=' + postid[0] + '" target="_blank">' + message + '</a><br />' + totalComments + ' comments, ' + totalLikes + ' likes</li>');
							item ++;
						}
						break;
					case "video": case "link":
						$('#fb_feed').append('<li><a href="http://www.facebook.com/profile.php?id=' + uid + '" target="_blank"><img src="http://graph.facebook.com/' + uid + '/picture?type=square" align="left"/> ' + name + '</a> shared ' + posttype + ' <a href="http://www.facebook.com/permalink.php?story_fbid=' + postid[1] + '&id=' + postid[0] + '" target="_blank">' + data.data[i].name + '</a><br />' + totalComments + ' comments, ' + totalLikes + ' likes</li>');
						item ++;
						break;
					case "photo":
						$('#fb_feed').append('<li><a href="http://www.facebook.com/profile.php?id=' + uid + '" target="_blank"><img src="http://graph.facebook.com/' + uid + '/picture?type=square" align="left"/> ' + name + '</a> added ' + data.data[i].caption + ' to the album <a href="' + data.data[i].link + '" target="_blank">' + data.data[i].name + '</a><br />' + totalComments + ' comments, ' + totalLikes + ' likes</li>');
						item ++;
						break;
				}
				
				if (item >= maxItem) break;
			}

  		}
	);
}
