// YouTube miniplayer callback

var ytplayer;

function onYouTubePlayerReady(id) {
	ytplayer = document.getElementById('video-object');
	if (typeof ytplayer.pauseVideo == 'function') { return; }
	ytplayer = document.getElementById('video-embed');
}

// twitter 'sort' function (takes a tweet and parses + adds links)

function sortTweet(tweet) {
	var sorted = tweet.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
			return '<a href="'+url+'">'+url+'</a>';
	    }).replace(/\B@([_a-z0-9]+)/ig, function(link) {
			return '<a href="http://twitter.com/' + link.substring(1) + '">@' + link.substring(1) + '</a>';
	    }).replace(/\B#([_a-z0-9]+)/ig, function(link) {
	    	return '<a href="http://search.twitter.com/search?q=' + link.substring(1) + '">#' + link.substring(1) + '</a>';
	    });
	return sorted;
}

$(document).ready(function(){

	// slideshow script
	var slides = $('#slideshow li').length;			// number of slide elements on this page
	var changespeed = 6000;							// time between changes
	var fadespeed = 1000;							// fades over this time
	
	if (slides > 1) { // we've got a slideshow on our hands
		
		// get the timer going
		setInterval(function() {
			
			var id = parseInt($('#slideshow .current').attr('id').slice(4));
			var nextid = (id == slides) ? 1 : id + 1;
			
			$('#slideshow li').hide();
			$("#slideshow .current").css("zIndex", 2).show();
			$("#slideshow #side" + nextid).css("zIndex", 1).show();
			$("#slideshow .current").fadeOut(fadespeed).removeClass('current');
			$("#slideshow #side" + nextid).addClass('current');
			
		}, changespeed);
		
	}
	
	// add star-rating system
	
	if ($('#review-form #star-rating').length) {
	
		$('#review-form #star-rating a').bind('click', function() {
		
			var val = parseInt($(this).text());
			$('#review-form #rating').val(val);
			$('#review-form .current-rating').css('width', val*20 + "%");
			return false;
			
		});
	
	}
	
	// initialise shadowbox
	
	Shadowbox.init({
	    language: 'en',
	    continuous: true,
	    troubleElements: ["select", "canvas"]
	});
	
	// get tweets
	
	$('#twitter p').html("Loading a tweet...");
	
	var twitteruser = "7stories";
	var count = 1;
	var url = 'http://twitter.com/status/user_timeline/' + twitteruser + '.json?count=' + count + '&callback=?';
		
	$.getJSON(url, function(data) {
		var tweet = data[0].text;
		$('#twitter p').html(sortTweet(tweet));
	});
	
	// setup video popups
	
	$('a.vidbox').bind('click', function() {
		
		var vidurl = $('a.vidhelper').attr('href');
		// cancel the small player (if it's playing)
		if (typeof ytplayer.pauseVideo != 'function') {
			alert ("The player isn't ready!"); return false;
		}
		ytplayer.pauseVideo();
		// open the popup video
		Shadowbox.open({
		    content: vidurl,
		   	player: "swf"
		});
		return false; // don't follow that link
		
	});
	
	// $(".vidbox").jqvideobox();
	
});
