var scOSG = {};

scOSG.twitter = {};
scOSG.twitter.feeds = null;

scOSG.twitter.settings = {
	username: 'osgdigital',
	keyword: 'newOSGcomingsoon',
	wrapSelector: '.tweet em a',
	delay: 10 //seconds
}

scOSG.twitter.loadFeeds = function(){
	var _self = this;
	var delay = _self.settings.delay * 1000;
	$(_self.settings.wrapSelector).html(_self.feeds[0].text);
	window.setInterval(function(){
		var idx = _self.getRandom(0, _self.feeds.length - 1);
		var $wrapper = $(_self.settings.wrapSelector);
		$wrapper.fadeOut(200, function(){
			$(this).html(_self.feeds[idx].text);
			$(this).fadeIn(200);
		});
	}, delay);
}

scOSG.twitter.getRandom = function(from, to){
	return Math.floor(Math.random() * (to - from + 1) + from);
}

scOSG.twitter.init = function(){
	var _self = this;
	var url = 'http://search.twitter.com/search.json?q=%23' + this.settings.keyword + '%20from%3A'+ this.settings.username + '&callback=?';
	
	$.getJSON(url,function(response){
			if(response.results.length > 0){
				_self.feeds = response.results;
				_self.loadFeeds();
			}
	});
}

scOSG.debug = function(obj){
	if(typeof(console.log) == 'function'){
		console.log(obj);
	}
}

//Throw debug information to the console on error
$.ajaxSetup({"error":function(XMLHttpRequest,textStatus, errorThrown) {   
    scOSG.debug(textStatus);
    scOSG.debug(errorThrown);
    scOSG.debug(XMLHttpRequest.responseText);
}});

$(function(){
	scOSG.twitter.init();
});
