/*
Description: 
      -create a class ShowTweets that extends Request.JSONP using script tag injection and handles callbacks
#method send(options) - make the request
*/
        var ShowTweets = new Class({
            Extends: Request.JSONP,
            options: {
                url: '',
                data: {
                   format: 'xml' 
                } 
            },
            initialize: function(username,options){
                 this.parent(options);
                 var root = "http://query.yahooapis.com/v1/public/yql?q=",
                     yql = 'select * from html where url="http://twitter.com/'+username+'" and xpath="//span[@class=\'entry-content\']"',
                     url = root + encodeURIComponent(yql);
                     this.options.url = url;
                     this.username = username;           
            },
            clean: function(text) {
                   text = text.replace('<span','<li').replace('</span>','</li>');
                   return text;
            }              
        });


window.addEvent('domready',function(){

        new ShowTweets('codepo8',{
              onComplete: function(o) {
                   if(window.console){console.log(o);}
                   var data = o.results; 
                   if(data && data.length>0){           
                     var output = '<ul>'; 
                     for(var i=0;i<data.length;i++) {
                         output += this.clean(data[i]);   
                     }
                    output += '</ul>';
                    document.id('tweets').set('html',output);
                   }else{document.id('tweets').set('text','No results found.');}
                   /* new request JSON for header username+avatar*/
                   var username = this.username;
                   new Request.JSONP({
                       url: "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Ftwitter.com%2F"+username+"%22%20and%20xpath%3D%22%2F%2Fh2%5B%40class%3D'thumb%20clearfix'%5D%22",
                       data: {format: 'json'},
                       onComplete: function(o){
                             if(window.console){console.log(o);}
                             var template = '<h2 class="thumb clearfix">'+
                                            '<a href="http://twitter.com{account}">'+
                                            '<img alt="" border="0" height="73" id="profile-image" src="{src}" valign="middle" width="73"/>'+
                                            '</a>{username}</h2>';
                             var data = o.query.results.h2; 
                             var h2 = data[0];
                             var username = h2.content; 
                             var href = h2.a.href;
                             var src = h2.a.img.src  
                             document.id('owner').set('html',template.replace('{src}',src).replace('{username}',username).replace('{account}',href));
                       },
                       onRequest: function(script) {
                              var badge = document.id('owner');
                              if(!badge) {return;}
                              badge.set('text','Loading...');
                       }

                   }).send(); 
            },
            onRequest: function(script) {
                var badge = document.id('tweets');
                if(!badge) {return;}
                badge.set('text','Loading...');
            }
        }).send();

});//end DOMReady