/***************************************************************
*  Copyright notice
*
*  (c) 2009 Phillip Kroll <kroll@includemedia.de>
*  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 selector = {
    timeline: ".twitter-timeline",
    loading: ".tweetsLoad",
    reload: ".reloadTweets",
    profile: ".userProfile"
}

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

jQuery.noConflict();

jQuery(document).ready(function() {
     loadData( 0 ); 
});

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

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

function loadData( index ){

   var opt = twitterProfiles[ index ];
   if ( opt === undefined ){ return( null ); }

   jQuery( unique( selector.loading, opt ) ).show();
   jQuery( unique( selector.reload, opt ) ).hide();
   jQuery( unique( selector.reload, opt ) ).click( function(){ loadData( 0 ); return( false ); });
   
    var userProfile = null;
    var userTimeline = null;

   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.timeline, opt ) ).html("");
   jQuery( unique( selector.profile, opt ) ).html("");

    jQuery.each( userTimeline, function(i,item){
        if ( i < opt.max ){
            var licss = "";
            if( i == 0 ){ licss = " class='first'"   };
            if( i == opt.max - 1 ){ licss = " class='last'"   };
            
            //var d = new Date( Date.parse( item.created_at ) );
            var d = parseTwitterDate(item.created_at);
            var tweet_time = opt.show_time ? " <span class='small'>(" + d.toLocaleString() + ")</span>" : "";
            
            var listItem = jQuery("<div class='home3colBox-entry'><div class='home3colBox-image'><img src='" + userProfile.profile_image_url + "' /></div><div class='home3colBox-content'>" +  item.text + tweet_time + "</div><div class='clear'></div></div><div class='_small-col-item-spacer'></div>" );
            if ( opt.parse_urls ){ listItem.linkize(); }
            jQuery( unique( selector.timeline, opt ) ).append( listItem );
        }
    });

   jQuery( unique( selector.profile, opt ) ).append(
       "<img src='" + userProfile.profile_image_url + "' />"
    );

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

   loadData( ++index );
   jQuery( unique( selector.loading, opt ) ).hide();
   if ( opt.show_reload ){ jQuery( unique( selector.reload, opt ) ).show(); }

}

function parseTwitterDate(date) {
	var dateArray = date.split(' ');
	var tweetDatum = dateArray[2] +'.'+ dateArray[1] +'.'+ dateArray[5].substring(0,4);
	return tweetDatum;
}
