/***************************************************************
*  Copyright notice
*
*  (c) 2009 Phillip Kroll <kroll@includemedia.de>
*  modded by NEUSTA GmbH 2010
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

var tweetsLoaded = false;
var selector = {
	main: '.tx-imtwitterajax-pi1',
    timeline: '.twitter-timeline',
    loading: '.tweetsLoad',
    profile: '.userProfile'
}

if(window.twitterProfiles === undefined){
	var twitterProfiles = new Array();
}

jQuery.noConflict();
jQuery(document).ready(function() {
	var mainCont = jQuery(selector.main);

	mainCont.hover(function() {}, function() {mainCont.hide();});
	jQuery('.socialNavi li:eq(1)').hover(function() {mainCont.show();}, function() {mainCont.hide();});

	//load tweets
	if(tweetsLoaded == 'undefined' || tweetsLoaded == false) {
		tweetsLoaded = true;
		loadData(0);
	}
});

function unique(selector, opt) {
    return '#twitterUser' + opt.id + ' ' + selector;
}

function isdefined(object, variable) {
    return (typeof(eval(object)[variable]) != 'undefined');
}

function loadData(index){
   var opt 			= twitterProfiles[index];
   var userProfile  = null;
   var userTimeline = null;

   if(opt === undefined){
	   return false;
   }

   jQuery(unique(selector.loading, opt)).show();

   jQuery.getJSON(opt.path, 'id=' + opt.id + '&type=timeline&period=' + opt.cache_period, function(json){
	   userTimeline = json;
       jQuery.getJSON(opt.path, 'id=' + opt.id + '&type=profile&period=' + opt.cache_period, function(json){
    	   userProfile = json;
    	   process(userTimeline, userProfile, opt, index);
        });
    });
}

function process(userTimeline, userProfile, opt, index){
	jQuery(unique(selector.loading, opt)).hide();
	jQuery(unique(selector.timeline, opt)).html('');
	jQuery(unique(selector.profile, opt)).html('');

	var maxEntries = opt.max;

	jQuery.each( userTimeline, function(i,item){
		if(i < maxEntries){
			// skip replies
			if(item.in_reply_to_user_id == null) {
				var d 	  	   = parseTwitterDate(item.created_at);
				var tweet_time = opt.show_time ? ' <span class="tweet-time">(' + d.toLocaleString() + ')</span>' : '';
				var licss 	   = '';

				if(i == 0) {
					licss = ' class="first"';
				} else if(i == (maxEntries - 1)) {
					licss = ' class="last"';
				}

				var listItem = jQuery('<li' + licss + '>' +  item.text + tweet_time + '</li>');
					listItem.linkize();
				jQuery(unique(selector.timeline, opt)).append(listItem);
		    } else {
		    	// if it's a reply add one to maxentries to get the next result
		    	maxEntries++;
		    }
		}
	});

	jQuery(unique(selector.profile, opt)).append(
		'<img src="' + userProfile.profile_image_url + '" />' +
		'<h1>' + userProfile.screen_name + ' (' + userProfile.name + ')</h1>' +
		'<span class="friends">following: <strong>' + userProfile.friends_count + '</strong></span> ' +
		'<span class="friends">followers: <strong>' + userProfile.followers_count + '</strong></span>'
	);

	if(opt.link_to_twitter){
		jQuery(unique(selector.profile, opt)).find('h1').wrap('<a target="_blank" href="http://www.twitter.com/' + userProfile.screen_name + '"></a>');
		jQuery(unique(selector.profile, opt)).find('img').wrap('<a target="_blank" href="http://www.twitter.com/' + userProfile.screen_name + '"></a>');
	}

	loadData(++index);
}

function parseTwitterDate(date) {
	var dateArray = date.split(' ');

	return new Date(
		Date.parse(dateArray[0] + ', ' + dateArray[2] + ' '
		+ dateArray[1] + ' ' + dateArray[3] + ' '
		+ dateArray[5].substring(0,4)));
}
