var rdp_text = " via http://www.redditpics.com/";
var aval_length = 0;
var new_title = '';
var new_href = '';

function CompressURL(url,success)
{

	var link = 'http://to.ly/api.php?json=1&longurl=' + encodeURIComponent(url) + '&callback=?';

	$.getJSON(link, function(data){
	success && success(data.shorturl);
	});

}

function AppendShare(shortUrl) {
	new_href = shortUrl;
	aval_length = 140 - (rdp_text.length + new_href.length);
	if(new_title.length > aval_length || new_title.length + 3 > aval_length) {
		new_title = new_title.substring(0, aval_length - 4) + '...';
	}
	
	var share_twitter = ' - <a target="_blank" href="http://twitter.com/home?status='+new_title+' '+new_href+ rdp_text +'" title="Click to share this post on Twitter">Share on Twitter</a>';
	// var share_facebook = ' - <a href="http://www.facebook.com/sharer.php?u='+encodeURIComponent(new_href)+'&t='+encodeURIComponent(new_title+rdp_text)+'" title="Click to share this post on Facebook"> Share on Facebook</a>';
	$('#cboxTitle').append(share_twitter);
	// $('#cboxTitle').append(share_facebook);
	
}

$(document).ready(function(){
	$('a[title]').bt();
	$(".targetLink").colorbox(
		{
			width:"80%",
			height:"80%",
			iframe:true,
			title_attr: 'bt-xtitle',
			onComplete: function() {
				rdp_text = " via http://www.redditpics.com/";
				new_title = $(this).attr('bt-xtitle');
				new_href = this.href;
				
				CompressURL(new_href, AppendShare);
				
				// if(new_title.length > aval_length || new_title.length + 3 > aval_length) {
				// 	new_title = new_title.substring(0, aval_length - 4) + '...';
				// }
				// 
				// var share = '  <a target="_blank" href="http://twitter.com/home?status='+new_title+' '+new_href+ rdp_text +'" title="Click to share this post on Twitter">Share on Twitter</a>';
				// $('#cboxTitle').append(share);
			}					
		}
	);
	
	$(".commentsLink").colorbox(
		{
			width:"80%",
			height:"80%",
			iframe:true,
			title_attr: 'title'
		}
	);

	$('#footer .made_with_label').click(function(){
		$('#footer .made_with').slideToggle();
	});

    $('.image img').bind("load", function(){
        $(this).show();
    });
});

