/*
 * TwittixDS version 0.8
 * Developed by David Fuentes A.
 * wwww.davoscript.com
 * 
 * Description:
 * Show your latest tweets in your website.
 * 
 * Features:
 * - Fancy display with HTML and Mootools.
 * - Easy and quick installation.
 * - Friendly CSS customization.
 * - Multiple effects combinations.
 * - Tweeter API based.
 * - 100% Secure, no password needed.
 */  
 
 var twittix_container;
 var twittix_op;
 
 function twittix(user, qty, el, op){
   (typeof user == 'undefined') ? el.innerHTML='Twittix: You must enter a valid Twitter user.' : function(){};
   qty = (typeof qty != 'undefined' || !isNaN(qty)) ? qty : 5;
   (!$(el)) ? alert('Twittix: You must enter a valid div ID') : function(){};
   $(el).addClass('twittix_loading');
   op = (typeof op != 'undefined') ? op : {} ;
   op.name=(typeof op.name!='undefined') ? op.name : true;
   op.thumb=(typeof op.thumb!='undefined') ? op.thumb : true;
   op.followers=(typeof op.followers!='undefined') ? op.followers : true;
   op.twittsDate=(typeof op.twittsDate!='undefined') ? op.twittsDate : 'text';
   op.showHide=(typeof op.showHide != 'undefined') ? op.showHide : true;
   op.hideCaption=(typeof op.hideCaption != 'undefined') ? op.hideCaption : 'h i d e';
   op.showCaption=(typeof op.showCaption != 'undefined') ? op.showCaption : 's h o w';
   op.twittsHover=(typeof op.twittsHover != 'undefined') ? op.twittsHover : true;
   op.twittsOverStyle=(typeof op.twittsOverStyle != 'undefined') ? op.twittsOverStyle : {color:'#FFF'};
   op.twittsOutStyle=(typeof op.twittsOutStyle != 'undefined') ? op.twittsOutStyle : {color:'#000'};
   op.bottomLeftHTML=(typeof op.bottomLeftHTML != 'undefined') ? op.bottomLeftHTML : '';
   op.bottomRightHTML=(typeof op.bottomRightHTML != 'undefined') ? op.bottomRightHTML : '';
   twittix_op = op;
   twittix_container = $(el);
   var twitter_api_url = 'http://twitter.com/statuses/user_timeline/' + user + '.json?callback=twittix_build&count=' + qty;
   var twitter_api_script = new Element('script');
   twitter_api_script.setAttribute('type', 'text/javascript');
   twitter_api_script.setAttribute('src', twitter_api_url);
   document.body.appendChild(twitter_api_script);
 }
   
 function twittix_build(twitts){
   twittix_container.removeClass('twittix_loading');
   var box=new Element('div',{'class':'twittix_box'});
   var head=new Element('div',{'class':'twittix_head'});
   var foot=new Element('div',{'class':'twittix_foot'});
   var thumb=(twittix_op.thumb) ? new Element('div',{'class':'twittix_thumb'}) : null;
   var title=new Element('h3',{'class':'twittix_title'});
   var data=(twittix_op.followers) ? new Element('p',{'class':'twittix_data'}): null;
   var board=new Element('div',{'class':'twittix_board'});
   
   if (twittix_op.thumb){
     var pic = new Asset.image(twitts[0].user.profile_image_url, {
       'title': twitts[0].user.screen_name,
       'class': 'twittix_pic',
       onload: function(){
         this.injectInside(thumb);
       }
     });
   }
   
   title.innerHTML = '<a href=http://twitter.com/' + twitts[0].user.screen_name + '>@' + twitts[0].user.screen_name + '</a>';
   
   (twittix_op.followers) ? data.innerHTML = 'Followers: '+twitts[0].user.followers_count : function(){};
   
   twitts.each(function(t){
     var tb=(twittix_op.twittsDate=='tip') ? new Element('div',{'class':'twitt','title':'Said on :: '+t.created_at.split(' +')[0]}) : new Element('div',{'class':'twitt'});
     tb.innerHTML=t.text;
     (twittix_op.twittsDate=='text') ? tb.innerHTML+='<br/><span class="twitt_date">'+t.created_at.split(' +')[0]+'</span>' : function(){};
     tb.injectInside(board);
   });
   
   var hs=new Element('p',{'id':'twittix_hs'});
   hs.innerHTML=twittix_op.hideCaption;
   
   foot.innerHTML='';
   foot.innerHTML+='<div class="twittix_l">'+twittix_op.bottomLeftHTML+'</div>';
   foot.innerHTML+='<div class="twittix_r">'+twittix_op.bottomRightHTML+'</div>';

   (twittix_op.thumb) ? head.adopt(thumb) : function(){};
   
   if (twittix_op.name) {
     head.adopt(title);
   }
   
   (twittix_op.followers) ? head.adopt(data): function(){};

   box.adopt(head);
   box.adopt(board);
   if (twittix_op.showHide) {
     box.adopt(hs);
   }
   box.adopt(foot);

  twittix_container.adopt(box);
  
  /* Moo Effects */
 if(twittix_op.twittsDate=='tip'){
    var tdTip = new Tips($$('.twitt'),{className:'twittix'});
 }
 
  if (twittix_op.twittsHover) {
    var twittsBlocks = $$('.twitt');
    twittsBlocks.each(function(b){
      var tfx = new Fx.Styles(b,{duration: 200,wait: false});
      b.addEvent('mouseenter', function(){
        tfx.start(twittix_op.twittsOverStyle);
      });
      b.addEvent('mouseleave', function(){
        tfx.start(twittix_op.twittsOutStyle);
      });
    });
  }
  
  if(twittix_op.showHide){
    var bSlide = new Fx.Slide(board);
    hs.addEvent('click',function(){
    	bSlide.toggle();
      (hs.innerHTML==twittix_op.hideCaption) ? hs.innerHTML=twittix_op.showCaption : hs.innerHTML=twittix_op.hideCaption;
    });
  }
 }