/*
 * jTwitter 1.1.1 - Twitter API abstraction plugin for jQuery
 *
 * Copyright (c) 2009 jQuery Howto
 *
 * Licensed under the GPL license:
 *   http://www.gnu.org/licenses/gpl.html
 *
 * URL:
 *   http://jquery-howto.blogspot.com
 *
 * Author URL:
 *   http://jquery-howto.blogspot.com
 *
 */
(function( $ ){
                $.extend( {
                               jTwitter: function( username, numPosts, fnk ) {
                                               var info = {};
                                               
                                               // If no arguments are sent or only username is set
                                               if( username == 'undefined' || numPosts == 'undefined' ) {
                                                               return;
                                               } else if( $.isFunction( numPosts ) ) {
                                                               // If only username and callback function is set
                                                               fnk = numPosts;
                                                               numPosts = 1;
                                               }
                                               
                                               //var url = "http://twitter.com/status/user_timeline/" + username + ".json?count="+numPosts+"&callback=?";
                                               var url = "http://api.twitter.com/1/statuses/user_timeline.json?screen_name=" + username + "&callback=?";
;

                                               $.getJSON( url, function( data ){
                                                               if( $.isFunction( fnk ) ) {
                                                                              fnk.call( this, data );
                                                               }
                                               });
                               }
                });
})( jQuery );


//CALL ACTUAL DATA:

$(document).ready(function(){
    // Get latest 1 tweets by jQueryHowto
    $.jTwitter('HotelVroenhof', 1, function(data){
        $('#twitterPosts').empty();
        $counter = 0;
        $.each(data, function(i, post){
                
            var twitterDate_Pre = post.created_at;
            var twitterDate = new Date(twitterDate_Pre.replace(/^\w+ (\w+) (\d+) ([\d:]+) \+0000 (\d+)$/,"$1 $2 $4 $3 UTC"));
       		var curr_date = twitterDate.getDate();
            var curr_month = twitterDate.getMonth(); curr_month++;
            var curr_year = twitterDate.getFullYear();
            var curr_hour = twitterDate.getHours();
            curr_hour += '';
            curr_hour.length == 1 ? curr_hour = "0" + curr_hour : '';
            var curr_minutes = twitterDate.getMinutes();
            curr_minutes += '';
            curr_minutes.length == 1 ? curr_minutes = "0" + curr_minutes : '';
            currentDate = curr_date + "-" + curr_month + "-" + curr_year + " @ " + curr_hour + ":" + curr_minutes;
    
    		$varTekstInhoud = post.text;
    		$varTekstInhoudLang = post.text;
    		if($varTekstInhoud.length > 25) {
    			$varTekstInhoud = $varTekstInhoud.substring(0,120)+"...";
			}
			   
    		if($counter == 0){
    		
	    		//Parse http links:
				var regexp = /((ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi;
	        	$varTekstInhoud = $varTekstInhoud.replace(regexp,'<a href="$1">$1</a>')
	        	
	        	$tempInhoudArray = $varTekstInhoud.split("<a");
	        	for(i=1;i<$tempInhoudArray.length;i++){
	        		$tempString = $tempInhoudArray[i];
	        		$tempArray = $tempString.split("\">");
	        		$newTempString = $tempArray[0].replace("!...", "");
	        		$newTempString = $newTempString.replace("!..", "");
	        		$newTempString = $newTempString.replace("!.", "");
	        		$newTempString = $newTempString.replace("!", "");
	        		$newString = $newTempString + "\" target=\"_blank\"" + ">" + $tempArray[1];
	        		$tempInhoudArray[i] = $newString;
	        	}
	        	
	        	$varTekstInhoud = $tempInhoudArray.join("<a");
	        	
	        	//Build tweet:    		
	            $('#twitterPosts').append(
	                '<div class="twitterPost">'
	                +' <div class="twitterDate">'
	                // See output-demo.js file for details
	                +    currentDate
	                +' </div>'
	                +' <div class="twitterTxt" title="' + $varTekstInhoudLang + '">'
	                // See output-demo.js file for details
	                +    $varTekstInhoud
	                +' </div><div class="clear_rk"></div>'
	                +'</div>'
	            );
            }
            
            $counter++;
            
        });
        //IF NO RECENT TWEEDS ARE FOUND:
        if(data.length <= 0){
        	$('#twitterPosts').append(
                '<div class="twitterPost">'
                +' <div class="twitterDate">'
                // See output-demo.js file for details
                +    'Er zijn momenteel geen recente tweeds...'
                +' </div>'
                +' <div class="twitterTxt" title="' + '&nbsp;' + '">'
                // See output-demo.js file for details
                +    'Wilt u altijd op de hoogte blijven? Volg ons dan: klik links op "Volg ons op Twitter!".'
                +' </div><div class="clear_rk"></div>'
                +'</div>'
            );

        }
    });
});

