var external = ' class="external" rel="external"';

//-------------------------------
// Get Yahoo! Pipe News feed
//-------------------------------
function fetchFeed(feeduri, ele) {
	if ( (typeof feeduri != 'undefined') && (ele.length > 0) ) {
		$.getJSON(feeduri, function(data) {
			var lnk = data.value.items[0].link,
				msg = data.value.items[0].title,
				output = '<a href="' + lnk + '"' + external +'>' + msg + '</a>';
			output = '<p class="status">' + output + '</p>';
			ele.prepend( output ).data('done', true);
		});
	}
}

//-------------------------------
// Random All-In-1 Feature output
//-------------------------------
function randFeature(ele) {
	// object of features & uri
	var feats = [
			{'id':'email',
			'title':'Send &amp; Track Email Campaigns to a Targeted Audience',
			'link':'/all-in-1/email-marketing'},
			{'id':'shop',
			'title':'Online Sales 24 Hrs a Day. SMS Notifications so you can feel the ROI',
			'link':'/all-in-1/ecommerce'}
			],
		pos = Math.floor(Math.random() * feats.length), // gen rand num
		lnk = feats[pos].link,
		msg = feats[pos].title,
		fid = feats[pos].id,
		output = '<a href="' + lnk + '" id="bcad_' + fid + '">' + msg + '</a>';
	output = '<p class="status">' + output + '</p>';
	ele.prepend( output ).data('done', true);
}

//-------------------------------
// Equal Extra Heights when done
//-------------------------------
var timercount = 1;
var extrasCheck = function() {
	var div1 = $('#twitter').data('done'),
		div2 = $('#smartcompany').data('done'),
		div3 = $('#bcfeatures').data('done'),
		timerID;
	if ( div1 && div2 && div3 ) { // final heights set
		$('.equal .status').equalCols();
	} else if ( timercount > 50 ) {
		// end - something is broke;
		clearInterval(timerID);
		return;
	} else {
		timercount++;
		timerID = setTimeout(extrasCheck, 100);
	}
};
setTimeout(extrasCheck, 100);

//-------------------------------
// Make links and @metions clickable
//-------------------------------
function phaseOutput(data) {
	// Links
	var exp = /(^|\s)((https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
	data = data.replace(exp, " <a href=\"$2\"" + external + ">$2</a>");

	// @metions
	exp = /[\@]+([A-Za-z0-9-_]+)/ig;
	data = data.replace(exp, "@<a href=\"http://twitter.com/$1\"" + external + ">$1</a>");

	return data;
}

//-------------------------------
// Get Twitter Feed and Display
//-------------------------------
function showTwitter(username, ele) {
	if ( (typeof username != 'undefined') && ($(ele).length > 0) ) {
		$.getJSON('http://twitter.com/statuses/user_timeline/' + username + '.json?callback=?', function(data) {
			var msg = phaseOutput( data[0].text ),
				output = '<p class="status">' + msg + '</p>';
			$(ele).prepend( output ).data('done', true);
		});
	}
}

//-------------------------------
// onload
//-------------------------------
$(function() {
	if ("https:" != document.location.protocol) { // doing on a secure connection breaks lock
		showTwitter('RMWPublishing', '#twitter');
		fetchFeed('http://pipes.yahoo.com/pipes/pipe.run?_id=mtaJxWM43hGGGI5Gbbsjiw&_render=json&_callback=?', $('#smartcompany'));
	}
	randFeature( $('#bcfeatures') );
});