// JavaScript Document

/*
	twitter.js - communicates with getTwitter.php to receive a twitter feed in json format and inserts relevant 'p' elements
*/



var Tweet = new Class({
	
	category : '', // holds category
	username : '', // holds username
	loaded : false, //set to true when data is loaded
	
	options : {
		url : 'getTwitter.php', // url of php file
		limit : 2,
		container : 'ticker-inner',
		// EVENTS
		onComplete : $empty,
		onFail : $empty
		
	},
	
	Implements: Events,
	
/*
	CONSTRUCTOR
	new Tweet(username:string|array[, options:object] ):object
		perfomrms xhr request for json file
		params:
			username : the twitter username
			category : the 19 artist category
			options : the options object (see above)
*/
	
	initialize: function(username, category, options){
		this.setOptions(options);
		this.username = username;
		this.addConsole();
		var req = new Request.JSON({ url : this.options.url, onSuccess:this.success.bind(this), onFailure:this.fail.bind(this) });
		this.trace('id: ' +  username);
		req.get({ 'id' : username, 'format' : 'json', 'limit':this.options.limit });
	},
	
/*
	addConsole():void
		If debug is set to true, opens debuggin window
*/

	addConsole: function(){
		try{	
			window.console.log();
		}catch(e){
			if( this.options.debug ){
				var console = {};
				console.win = window.open('', 'Debugger', 'width=250,height=500,scrollbars=1,resizable=1');
				console.win.document.write('<h2>Debugging Console</h2>');
				console.log = function(m){
					try{console.win.document.write(m + '<br />');}catch(e){}
				}
			}
		}
	},
		
	
/*
	trace(message:string):void
		outputs debugging info to console, or debugging window
*/
	trace: function(message){
		if(this.debug){
			console.log(message);
		}
	},

/*
	getDateString( datestamp:datestamp):string
		returns string formatted date from datestamp
*/
	
	getDateString: function(datestamp){
		var d = datestamp.split(' ');
		var date = d[2].toInt();
		var month = d[1];
		var year = d[5];
		switch(date){
			case 1 || 21 || 31 : date = date + 'st'; break;
			case 2 || 22 : date = date + 'nd'; break;
			case 3 || 23 : date = date + 'rd'; break;
			default : date = date + 'th'; break;
		}
		return date + ' ' + month + ' ' + year;
	},
	

/*
	fail( xhr:object ):void
		Fired if xhr request fails
*/
	
	fail: function(xhr){
		this.trace('failed to load ' + this.username);
		this.loaded = true;
		this.fireEvent('fail');
	},
/*
	success(objs:array, str:string):void
		Fired if xhr request succeeds
		Params:
			objs : the parsed json response, as array of objects
			str : the json response as a string
*/
	
	success: function(obj, str){
		var els = [];
		var ps;
		for(var i=0; i<obj.length; i++){
			els[i] = new Element('p', {'class':'twitter' });
			sp = new Element('span', {'class':'grey'} );
			a = new Element('a', {href:'http://www.twitter.com/' + this.username, 'target':'_blank'} ); 
			sp.set('html', this.getDateString(obj[i].created_at) );
			a.set('html', obj[i].text);
			a.inject( els[i], 'top' );
			sp.inject( els[i], 'top' );
		}
		els.each( function(el){
			ps = $$('#' + this.options.container + ' p');
			el.inject( this.options.container, 'bottom');
		}, this);
		this.loaded = true;
		ticker.init();
	}
});

		
		
Tweet.implement(new Options);	
		