﻿/* AJAX events synchro test */
ajaxBoxesLoadedCount=0;


/*** Blogger ***/
// callback wrapper function
function recentpostsCallback(json) {
	return showrecentposts (json, 5);
}
// get and render items
function showrecentposts (json, numBlogPosts, numBlogPostChars) {
  // set values from args or defaults	
  var numBlogPosts=numBlogPosts || 10;
  var numBlogPostChars=numBlogPostChars || 100;

  // start a loop
  // in this loop we get the entry from the feed and parse it
  var postsList = []; 
  for (var i = 0; i < numBlogPosts; i++) {
    // get entry i from feed
    var entry = json.feed.entry[i];
    // get the posttitle
    var posttitle = entry.title.$t;
    // get the post url
    // check all links for the link with rel = alternate
    var posturl;
    if (i == json.feed.entry.length) break;
    for (var k = 0; k < entry.link.length; k++) {
      if (entry.link[k].rel == 'alternate') {
        posturl = entry.link[k].href;
        break;
      }
    }
    // get the postdate, take only the first 10 characters
    var postdate = entry.published.$t.substring(0,10);
    // get the post author
    var postauthor = entry.author[0].name.$t;
    // get the postcontent
    // if the Blogger-feed is set to FULL, then the content is in the content-field
    // if the Blogger-feed is set to SHORT, then the content is in the summary-field
    if ("content" in entry) {
      var postcontent = entry.content.$t;}
    else
    if ("summary" in entry) {
      var postcontent = entry.summary.$t;}
    else var postcontent = "";
    // strip off all html-tags
    var re = /<\S[^>]*>/g; 
    postcontent = postcontent.replace(re, "");
    // reduce postcontent to numchar characters
    if (postcontent.length > numBlogPostChars) postcontent = postcontent.substring(0,numBlogPostChars);
    // display the results

	postsList.push("<li>");
	postsList.push("<a href='"+posturl+"'>"+posttitle+"</a>");
	postsList.push("</li>");
	}
	postsStr = "<ul>" +postsList.join("")+"</ul>";

	jQuery(document).ready(
		function($) {
		$("#blogger-posts").html(postsStr).ready(
		function($) {
			ajaxBoxesLoadedCount++;
			if(ajaxBoxesLoadedCount==2) {
				$(".row2").equalHeights();			
			};
		}
	  );
		}
	)	
}


/*** Twitter ***/

function relative_time_cz(time_value) {
  var values = time_value.split(" ");
  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
  var parsed_date = Date.parse(time_value);
  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
  delta = delta + (relative_to.getTimezoneOffset() * 60);

  if (delta < 60) {
    return 'méně než před minutou';
  } else if(delta < 120) {
    return 'asi před minutou';
  } else if(delta < (60*60)) {
    return (parseInt(delta / 60)).toString() + ' minut zpátky';
  } else if(delta < (120*60)) {
    return 'zhruba před hodinou';
  } else if(delta < (24*60*60)) {
    return 'cca ' + (parseInt(delta / 3600)).toString() + ' hodin zpátky';
  } else if(delta < (48*60*60)) {
    return 'včera';
  } else {
    return (parseInt(delta / 86400)).toString() + ' dní zpátky';
  }
}

function twitterCallback2(twitters) {
  var statusHTML = [];
  for (var i=0; i<twitters.length; i++){
    var username = twitters[i].user.screen_name;
    var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
      return '<a href="'+url+'">'+url+'</a>';
    }).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
      return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
    });
    statusHTML.push('<li><span>'+status+'</span> <a style="font-size:85%" href="http://twitter.com/'+username+'/statuses/'+twitters[i].id+'">'+relative_time(twitters[i].created_at)+'</a></li>');
  }
  document.getElementById('twitter_update_list').innerHTML = statusHTML.join('');
}

function makeLinks(status) {
    var status = status.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
      return '<a href="'+url+'">'+url+'</a>';
    }).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
      return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'">'+reply.substring(1)+'</a>';
    }).replace(/\B#([_a-z0-9]+)/ig, function(tag) {
      return  '<a href="http://twitter.com/search?q=%23'+tag.substring(1)+'">'+tag+'</a>';
    });
	return status;
}



jQuery(document).ready(
	function($) {
/* jQuery code goes here */

// Twitter
	$.jTwitter('mpecka', 4, function(posts){
	  var tweetsList = []; 
	  for(var i=0; i<posts.length; i++){
		 tweetsList.push("<li>"+makeLinks(posts[i].text)+" <span>"+relative_time_cz(posts[i].created_at)+"</span></li>");
//		 $('#twitter-posts').append(posts[i].text);
	  }
	  $('#twitter-posts').html("<ul>"+tweetsList.join("")+"</ul>").ready(
		function($) {
			ajaxBoxesLoadedCount++;
			if(ajaxBoxesLoadedCount==2) {
				$(".row2").equalHeights();			
			};

		}
	  );
	});


	

	}
)

/*** page ***/
jQuery(document).ready(function($) {
	
	// columns equal height
	$(".row1").equalHeights();
	$(".row3").equalHeights();

	
	// Tooltip
	$("*").tooltip({
		showURL: false,
		track: true
	});
	

	// antispam email to link
	$('.tomail').unobfuscate({
		atstring: ' (šnek) ',
		dotstring: ' (tečka) '
	});
	
	
	
 }
)







