/**
 * Twitter - http://www.twitter.com
 * Copyright (C) 2009 Twitter
 * Author: Dustin Diaz (dustin@twitter.com)
 *
 * V 2.1.1 Twitter search/profile/faves/list widget
 * http://twitter.com/widgets
 */

var twitterWidget = new Class({
	options: {
		id:'twitter',		
  		rpp: 15,
  		width: '100%',
  		height: 'auto',
		periodical: 50, // refresh every 50 sec
  		theme: {
    			shell: {
      				background: '#333333',
      				color: '#ffffff'
    			},
    			tweets: {
      				background: '#474747',
      				color: '#ffffff',
      				links: '#4aed05'
    			}
  		},
  		features: {
    			scrollbar: true,
    			timestamp: true,
    			avatars: true
  		}     
	},
	initialize: function(e,b){
		this.setOptions(b);
		this.username = e;
		this.tweetsPeriodical = null;
		
		
		if(!this.username){ return; }
		if(!this.options.id){ return; }

		if(this.options.features.scrollbar){ 
			this.options.features.scrollbar = 'scrollbar';
			this.scrollbarClass = ' twtr-scroll';
		}
		else{
			this.scrollbarClass='';
		}
		if(this.options.features.timestamp) this.options.features.timestamp = 'timestamp';
		if(this.options.features.avatars) this.options.features.avatars = 'avatars';

		this.options.theme.shell.background = this.removeSy(this.options.theme.shell.background);
		this.options.theme.shell.color = this.removeSy(this.options.theme.shell.color);
		this.options.theme.tweets.background = this.removeSy(this.options.theme.tweets.background);
		this.options.theme.tweets.color = this.removeSy(this.options.theme.tweets.color);
		this.options.theme.tweets.links = this.removeSy(this.options.theme.tweets.links);

		
		
		var head = $$("head")[0];
		var stylesheet = new Element('link',{rel:'stylesheet',href:'http://widgets.twimg.com/j/2/widget.css',type:'text/css'});
		stylesheet.inject(head);
		
		this.css('#twtr-widget-1 .twtr-doc,#twtr-widget-1 .twtr-hd a,#twtr-widget-1 h3,#twtr-widget-1 h2 {background: #'+this.options.theme.shell.background+' !important;   color: #'+this.options.theme.shell.color+' !important;}#twtr-widget-1 .twtr-tweet a {color: #'+this.options.theme.tweets.links+' !important;  } #twtr-widget-1 .twtr-bd, #twtr-widget-1 .twtr-timeline i a,#twtr-widget-1 .twtr-bd p {color: #'+this.options.theme.tweets.color+' !important;} #twtr-widget-1 .twtr-new-results,#twtr-widget-1 .twtr-results-inner,#twtr-widget-1 .twtr-timeline {            background: #'+this.options.theme.tweets.background+' !important;          }');

		
	
		
		
		if(!this.options.features.avatars) this.css('#twtr-widget-1 .twtr-avatar, #twtr-widget-1 .twtr-user { display: none; } #twtr-widget-1 .twtr-tweet-text { margin-left: 0; }   ');
		if(!this.options.features.timestamp) this.css('#twtr-widget-1 i { display: none; }');
		
		this.tweeterContainer = new Element('div');
		this.tweeterContainer.injectInside($(this.options.id));
		this.xhtml();
		this.twts = this.tweeterContainer.getElement('div.twts');
		
		
		
		/*
		this.tweeterContainer.getElement('.twtr-doc').setStyles({
				background: '#'+this.options.theme.shell.background+' !important',
				color: '#'+this.options.theme.shell.color+' !important'
		});
		*/
		
		
		
		
		this.firstTime =1;
		
		var url = 'modules/mod_ice_twitter/includes/co.php?u='+this.username+'&shell='+this.options.theme.shell.background+','+this.options.theme.shell.color+'&tweets='+this.options.theme.tweets.background+','+this.options.theme.tweets.color+','+this.options.theme.tweets.links+'&features='+this.options.width+','+this.options.height+','+this.options.features.scrollbar+','+this.options.features.avatars+','+this.options.rpp+','+this.options.features.timestamp+'';
		this.doAjax(url);
		this.tweetsPeriodical = (function(){
			// $clear(this.tweetsPeriodical);			
			this.doAjax(url);	
		}).bind(this).periodical(this.options.periodical*1000);




		// this.genHTML();
	},
	xhtml: function(){
		this.tweeterContainer.innerHTML = '<div id="twtr-widget-1" class="twtr-widget twtr-widget-profile'+this.scrollbarClass+'"><div style="width: '+this.options.width+';" class="twtr-doc">            <div class="twtr-hd"><a class="twtr-profile-img-anchor" href="http://twitter.com/'+this.username+'" target="_blank"><img src="pic" class="twtr-profile-img" alt="profile"/></a><h2><a href="http://twitter.com/'+this.username+'" target="_blank">'+this.username+'</a></h2><div class="twtr-spinner twtr-active"> </div></div><div class="twtr-bd"><div style="height: '+this.options.height+';" class="twtr-timeline"><div class="twtr-tweets"><div class="twtr-reference-tweet"></div><div class="twts"></div></div></div></div><a href="http://twitter.com/'+this.username+'" class="twtr-join-conv" target="_blank">Unirse a la conversacion</a></div></div>';
	},
	removeSy: function(el){
		return el.replace(/[^A-Za-z0-9]+/, '');  
	},
	setupUser: function(){
		var el = this.twts.getElement('input[name=twtinfo]').getProperty('value').split('|');	
		this.tweeterContainer.getElement('img.twtr-profile-img').setProperty('src',el[1]);
		this.tweeterContainer.getElement('h2').getElement('a').setProperty('href',el[2]);
	},
	doAjax:function(url){
	this.tweeterContainer.getElement('div.twtr-spinner').removeClass('twtr-inactive').addClass('twtr-active');
	new Ajax(url, {
			method: 'get',
			onComplete: function(obj){
				this.twts.innerHTML = obj;
				this.tweeterContainer.getElement('div.twtr-spinner').removeClass('twtr-active').addClass('twtr-inactive');
				if(this.firstTime == 1){
					this.setupUser();
					this.firstTime = 2;
				}
			}.bind(this)
		}).request();
	},
	css: function(e){
		var ie = window.ie;
		var b = false;
		var d = document.createElement("style");
		d.type = "text/css";
		if (ie) {
			d.styleSheet.cssText = e;
		}
		else{
			 var f = document.createDocumentFragment();
			 f.appendChild(document.createTextNode(e));
			 d.appendChild(f);
		}
		
		function c() { document.getElementsByTagName("head")[0].appendChild(d) }
		
		if (!ie || b) {
			c();
		} else {
			c();
            window.attachEvent("onload", function () {
					b = true;
					c();
			});		
		}
		
		
	}
});

twitterWidget.implement(new Options);
