$(document).ready(function(){

$.fn.cycle.defaults = { 
    timeout:       3000,  // milliseconds between slide transitions (0 to disable auto advance) 
    speed:         1000,  // speed of the transition (any valid fx speed value) 
    next:          null,  // id of element to use as click trigger for next slide 
    prev:          null,  // id of element to use as click trigger for previous slide 
    before:        null,  // transition callback (scope set to element to be shown) 
    after:         null,  // transition callback (scope set to element that was shown) 
    height:       'auto', // container height 
    sync:          1,     // true if in/out transitions should occur simultaneously 
    fit:           0,     // force slides to fit container 
    pause:         0,     // true to enable "pause on hover" 
    delay:         0,     // additional delay (in ms) for first transition (hint: can be negative) 
    slideExpr:     null   // expression for selecting slides (if something other than all children is required) 
};

$("area#photostream").click(function(){
	resetBlanks();
	$.getJSON('http://api.flickr.com/services/feeds/photos_public.gne?id=44025279@N07&format=json&jsoncallback=?', function(data){
		var images = "";
		$.each(data.items, function(i,item){
			images += '<a target="_blank" title="' + item.title + '" href="' + item.link + '" class="hide"><img src="' + item.media.m + '" class="flickr"></a>'
		});
		$("td#tr-window").html(images);
		$("td#tr-window").cycle();
	});
	return false;
});

$("area#pictures-moving").click(function(){
	resetBlanks();
	$("td#tl-window").html("<img src='images/anim_noise.gif' width='301' height='175'>");
	$.getJSON('http://vimeo.com/api/v2/sifter/videos.json?callback=?', function(data){
	var x = Math.floor(Math.random() * data.length);
	var ThisRandomVid = data[x - 1];
		$("td#tl-window").html(buildVimeo(ThisRandomVid.id));
	});
	return false;
});

$("area#pontifications").click(function(){
	resetBlanks();
	$.getJSON('http://api.twitter.com/1/statuses/user_timeline.json?screen_name=polsifter&callback=?', function(data){
		var tweets = "<ul>";
		$.each(data, function(i,item){
			tweets += '<li class="hide">'+ linkify(item.text) + '<span class="timeago"><a target="_blank" href="http://twitter.com/polsifter/status/' + item.id_str +'">' + H(item.created_at) + '</a></span></li>'
		});
		tweets += '</ul>';
		$('#tweets').html(tweets);
		$('#tweets span.timeago').timeago();
		$("#tweets ul").cycle();
	});
	return false;
});

$("area#philo").click(function(){
	resetBlanks();
	$('#philosophical').text('How long has it been since someone touched part of you other than your body? - Laurel Hoodwrit');
	return false;
});	

function buildVimeo(id){
	var code = "<iframe src='http://player.vimeo.com/video/" + id + "?color=71d100' width='301' height='175' frameborder='0'></iframe>";
	return code;
}

function resetBlanks(){
	$("td#tl-window").html('<img name="n960_r1_c1" src="images/960_r1_c1.png" width="301" height="175" border="0" id="n960_r1_c1" alt="" />');
	$("td#tr-window").html('<img name="n960_r1_c3" src="images/960_r1_c3.png" width="301" height="175" border="0" id="n960_r1_c3" alt="" />');
	$("#philosophical").html('');
	$("#tweets").html('');

}

function linkify(text){
    if (text) {
        text = text.replace(
            /((https?\:\/\/)|(www\.))(\S+)(\w{2,4})(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi,
            function(url){
                var full_url = url;
                if (!full_url.match('^https?:\/\/')) {
                    full_url = 'http://' + full_url;
                }
                return '<a href="' + full_url + '">' + url + '</a>';
            }
        );
    }
    return text;
}

// from http://widgets.twimg.com/j/1/widget.js
var K = function () {
    var a = navigator.userAgent;
    return {
        ie: a.match(/MSIE\s([^;]*)/)
    }
}();
 
var H = function (a) {
    var b = new Date();
    var c = new Date(a);
    if (K.ie) {
        c = Date.parse(a.replace(/( \+)/, ' UTC$1'))
    }
    var d = b - c;
    var e = 1000,
        minute = e * 60,
        hour = minute * 60,
        day = hour * 24,
        week = day * 7;
    if (isNaN(d) || d < 0) {
        return ""
    }
    if (d < e * 7) {
        return "right now"
    }
    if (d < minute) {
        return Math.floor(d / e) + " seconds ago"
    }
    if (d < minute * 2) {
        return "about 1 minute ago"
    }
    if (d < hour) {
        return Math.floor(d / minute) + " minutes ago"
    }
    if (d < hour * 2) {
        return "about 1 hour ago"
    }
    if (d < day) {
        return Math.floor(d / hour) + " hours ago"
    }
    if (d > day && d < day * 2) {
        return "yesterday"
    }
    if (d < day * 365) {
        return Math.floor(d / day) + " days ago"
    } else {
        return "over a year ago"
    }
};

}); // end docready

// http://vimeo.com/api/v2/activity/sifter/videos.json

// http://vimeo.com/api/v2/sifter/videos.json

// <iframe src="http://player.vimeo.com/video/18720640?color=71d100" width="400" height="225" frameborder="0"></iframe><p><a href="http://vimeo.com/18720640">Princes: Tinned tuna's secret catch</a> from <a href="http://vimeo.com/greenpeaceuk">Greenpeace UK</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
