var dxts;

function returnNew(state) {
	if (state == true) {
		return ' new';
	}
	else {
		return '';
	}
}

function grabAllDxts() {
	$.getJSON("/videos?sortby=carriage-desc", function(data) {
		if (data) {
			dxts = new TAFFY(data.results);
			dxts.forEach(function(val, i) {
				var tip = '<div class="tip"><p class="title">' + truncate(val.title, 40) + '</p><div class="smokeyimage" style="background-image: url(\'' + val.thumbsmall + '\');"><img src="/img/innersmoke.png" /></div><p class="author">' + val.author + '</p></div>';
				var el = $('<td class="carriage' + returnNew(val.isnew) + '" id="dxt' + val.carriage + '"><a class="carriage-' + Math.ceil(Math.random()*5) + '" href="/dxt/' + val.carriage + '">' + val.carriage + '</a>' + tip + '</td>');
				
				$("a",el).tooltip({
					offset: [30, 0]
				}).dynamic({
					bottom: {
						direction: 'down'
					}
				});
				$("#train tr").append(el);
				val.search = val.title + " " + val.description + " " + val.author + " " + val.tags;
			});
		}
		$("#searchsubmit").removeAttr("disabled");
		$("#q").bind('keypress', function() {
			timedFilter();
		});
	});
}

function grabPhotos() {
	$.getJSON("http://api.flickr.com/services/rest/?sort=date-posted-desc&safe_search=1&per_page=20&page=1&min_upload_date=1254200400&method=flickr.photos.search&extras=date_upload%2Cowner_name%2Cviews%2Ctags&content_type=1&api_key=908f1083ca93e6890753c3f2459c2f9a&text=dx2+AND+darlington&format=json&jsoncallback=?", function(data) {
		if (data) {
			var photos = data.photos.photo;
			$("#photogrid li").empty().remove();
			$.each(photos, function(i, val) {
				$("#photogrid").append('<li><a href="http://www.flickr.com/photos/' + val.owner + '/' + val.id + '"><img src="http://farm' + val.farm + '.static.flickr.com/' + val.server + '/' + val.id + '_' + val.secret + '_s.jpg" alt="' + val.title + '" /></a></li>')
			});
		}
	});
}

var filterTimer;

function timedFilter() {
	clearTimeout(filterTimer);
	filterTimer = setTimeout(filterCarriages, 400);
	$("#loader").css({'visibility' : 'visible'});
}

function filterCarriages() {
	var search = dxts.find({search:{"regexppass": new RegExp($("#q").attr('value'),"i")}});
			
	result = dxts.get(search);
	
	$("#train td.carriage").hide();
	$.each(result, function(i, val) {
		$("#dxt" + val.carriage).show();
	});
	$("#loader").css({'visibility' : 'hidden'});
}

var tweets = new Array();
var tweetIndex = 0;
var tweetTimeout;

function cycleTweets() {
	$("#tweet").fadeTo('fast', 0.1, function() {
		$("#tweet").html(tweets[tweetIndex]);
		$("#tweet").fadeTo('fast', 1);
	});
	if (tweetIndex == tweets.length) {
		tweetIndex = 0;
	}
	else {
		tweetIndex++;
	}
	tweetTimeout = setTimeout(cycleTweets, 6000);
}

$(function() {	

	$("#searchsubmit").attr({"disabled" : "disabled"});

	$("#tweet").hover(function() {
		if (tweetTimeout) {
			clearTimeout(tweetTimeout);
		}
	}, function() {
		if ((tweets) && (tweets.length > 0)) {
			tweetTimeout = setTimeout(cycleTweets, 3000);
		}
	});

	grabAllDxts();
	grabPhotos();
	
	$("#searchfilter").click(function(event) {
		filterCarriages();
	});

	$.getJSON('tweetaggregate', function(data) {
		var results = data;
		$.each(results, function(i,val) {
			if ((val.text) && (filterText(val.text))) {
				tweets.push('<a href="http://twitter.com/' + val.from_user + '">'
					+ '<img class="thumb" src="' + val.profile_image_url +'" alt="' + val.from_user + '" /></a>'
					+ '<span class="title">' + ify.clean(val.text) + '</span>'
					+ '<span class="small"> ~ ' + val.created_at + '</span>'
				);
			}
		});
		cycleTweets();
	});
});