var wnas = function() {
    var config = {},
    init = function() {
        inputCN();
		twitReact();
    },
    inputCN = function() {
        $('#input-test input').each(function() {
            var cn = $(this).attr('type');
            $(this).after('<span class="name">input-' + cn + '</span>');
        });
    },	
    twitReact = function(){
		var tr = $('p[data-twit]');
		tr.each(function(i){
			var term = $(this).attr('data-twit'),
				header = '<h1>Reactions with #'+term+'</h1>',
				note = '<p>Add your own reaction by tweeting with the #'+term+'.</p>';
			$(this).closest('.postcontent').after('<section class="twitterreactions" id="twitsection-'+i+'">'+header+'<ul id="twitlist-'+i+'"></ul>'+note);
			var url = 'http://search.twitter.com/search.json?&q='+term+'&rpp=100',
				T = $('twitsection-'+i),
				$there = $('#twitlist-'+i);
			$.ajax({ 
				// set the url
				url: url,
				// do a GET request
				type: 'GET',
				// set the dataType to jsonp
				dataType: 'jsonp',
				// try for one second
				timeout: 2000,
				// if it goes awry 
				error: function() {
					// we notice the user in a usefull manner
				    $there.html("<li>oops...</li>");
				},
				// Takes the JSON that is returned from the Search Query and Builds the HTML for our page.
				success: function(data) {
					// do something with each result
				//	console.log(data)
					$.each(data.results, function(i, item) {
					//	console.log(i);
						// build an array of words
						var words = item.text.split(' '),
							// reset the newtext
							newText='',
							// check out how many words there are.
							len = words.length;
							// for every word
							for( index in words) {
								// set the number of the word
								var i = words[index];
								// set a regexp for a url
								var urlCheck = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
								// if a word is an url
								if( urlCheck.test(i) ){
									// make the word into a link with a class of newWindow
									i = '<a href="'+i+'" class="newWindow">'+i+'</a>';
								};
								// if it is a hashtag, make a link of it.
								i = i.replace(/(^|\s|-)+#(\w+)/g, '<a href="http://twitter.com/#!/search?q=%23$&" class="newWindow"><em>$&</em></a>');
								// and do the same with a mention
								i = i.replace(/(^|\s|-)+@(\w+)/g, '<a href="http://twitter.com/$&" class="newWindow">$&</a>');
								// build the new text.
								newText = newText+i+' ';
							};
						// get the user
						var usr = item.from_user,
							// get the image
							img = item.profile_image_url,
							// set the text
							txt = newText,
							// time
							time = item.created_at,
							// unique url of the tweet.
							tweet = item.id_str,
							// start building a li (and hide it.)
							content = '<li style="display:none;" class="clearfix">';
							// set the avatar as a link to the users page
							content += '<a href="http://twitter.com/'+usr+'"><img src="'+img+'" /><strong>'+usr+'</strong></a>';
							// and create the rest of the content...
							content += '<p>'+txt+'</p><time><a href="http://twitter.com/'+usr+'/status/'+tweet+'" class="newWindow">'+time.substring(0,22)+'</a></time></li>';
						// append it to the list		
						$there.append(content);
						// fade the li slowly in.
						$there.find('li:last').fadeIn('slow');
					});
				}
			});
		});	
	};
	// make stuff public
    return {
        init: init
    };
} ();
// call function
wnas.init();



