function twitter(txt, url, blog, page) {
	//twitter_url = "http://twitter.com/share?url=" + encodeURIComponent(url) + "&text=" + blog + ": \"" + encodeURIComponent(txt) + "\" (http%3A%2F%2Fpolurls.com%2F" + page + "%2F) link:";
	twitter_url = "http://twitter.com/share?url=" + encodeURIComponent(url) + "&text=" + blog + ": \"" + encodeURIComponent(txt) + "\" (via @polurls) link:";
	window.open(twitter_url, "twitter", "width=600, height=300, resizable");
}

function facebook(txt, url, blog, page) {
	facebook_url = "http://www.facebook.com/sharer.php?u=" + encodeURIComponent(url) + "&t=" + encodeURIComponent(txt) + " (via http://polurls.com/" + page + "/)";
	window.open(facebook_url, "facebook", "width=600, height=300, resizable");
}

var blue = 1;
var purple = 1;
var red = 1;

(function($){
	$.fn.fblikecount = function(options){
		var defaults = {
			baseUrl: 'http://graph.facebook.com/?ids='
		};
		var options = $.extend(defaults, options);
		var count = 0;
		return this.each(function(){
			var $this = $(this);
			$this.hide();
			var objLink = $(this).attr('title');
			if(objLink.indexOf('http') === 0){
				$.getJSON(defaults.baseUrl + objLink + '&callback=?', function(json){
					if(json[objLink] && json[objLink].shares){
						$this.html(json[objLink].shares);
						$this.show();
                        if (objLink == 'http://polurls.com/blue/') {
                            blue = json[objLink].shares;
                        } else if (objLink == 'http://polurls.com/purple/') {
                            purple = json[objLink].shares;
                        } else if (objLink == 'http://polurls.com/red/') {
                            red = json[objLink].shares;
                        }
                        if (blue > 0 && purple > 0 && red > 0) {
                            $('.piechart').sparkline([blue,purple,red], { type: 'pie', height: '80px', offset: '-90', sliceColors: ['blue','purple','red']} );
                        }
					}
				});				
			}
		});
	}
})(jQuery);

$(document).ready(function(){
    $('span.likebox').fblikecount();
    
	var back = false;
	var tooltips = false;
    $('#allurls ul li').tooltip({
				bodyHandler: function() {
		        	return $(this).find('div').html(); 
		    	},
		    	showURL: false
			});
    
    
	/* next / prev links */
	var switchUL = function(hidenode, shownode, event, context) { 
    	alert("switchUL!"); 
        var wrapper = $(context).parent().parent();
    	$(hidenode, wrapper).fadeOut('fast', function() {
    		$(hidenode, wrapper).removeClass('active');
    		$(hidenode, wrapper).css('display', 'none');
    		$(shownode, wrapper).fadeIn();
    		$(shownode, wrapper).addClass('active');
    	})
    	event.preventDefault();
    };

    $("a.urls-forward").click(function(event) { switchUL('.urls-page-1', '.urls-page-2', event, this) });
    $("a.urls-backward").click(function(event) { switchUL('.urls-page-2', '.urls-page-1', event, this) });
    
    /* pop link */
    $('a.urls-pop').click(function(event) {
        var targethref = $(this).attr('href').replace('-pop', '');
        var clonedul = $(targethref + " .active").clone();
        
        $('#clone *').remove();
        $('#clone').append(clonedul);
        $('#clone').removeClass('clone-page');
        $('#clone').addClass('clone-pop');
        $('#clone *').removeClass('hidden');

        //console.debug($(this).get()[0].href); 
        //$(event.target).faceboxdo();

        $(this).faceboxdo(null, '#clone', $(this).attr('rel'));
        event.preventDefault();
    }); 
    
    /* page link */
    toindex = function() {
    	$('#clone').fadeOut('fast');
    	$('#clone *').remove();
    	$('#allurls').fadeIn('fast');
    }
    
    $('a.urls-page').click(function(event) {
        var targethref = $(this).attr('href').replace('-full', '');
        var clonedul = $(targethref + " .active").clone();
        
        $('#allurls').fadeOut('fast');
        $('#clone *').remove();
        
        var backlink = '<a href="javascript: toindex();" class="red">Back to index</a><hr />'
        $('#clone').append(backlink);
        $('#clone').append(clonedul);
        $('#clone').append(backlink);
        $('#clone').removeClass('clone-pop');
        $('#clone').addClass('clone-page');
        $('#clone').fadeIn('fast');
        event.preventDefault();
    });
    
    /* tooltips */
    $('#action-tooltips').click(function(event) {
	    if (!tooltips) {
	    	$('#allurls ul li').tooltip({
				bodyHandler: function() {
		        	return $(this).find('div').html(); 
		    	},
		    	showURL: false
			});
		    $(this).text('disable tooltips');
		    tooltips = true;
	    } else {
	    	$('#allurls ul li').unbind();
	    	$(this).text('enable tooltips');
	    	tooltips = false;
	    }
	    event.preventDefault();
    })
    
    /* font links */
    var changeFont = function(ratio) {
    	var currentFontSize = $('#allurls').css('font-size');
    	var currentFontSizeNum = parseFloat(currentFontSize, 10);
    	var newFontSize = currentFontSizeNum*ratio;
    	$('#allurls').css('font-size', newFontSize);
    }
    $('#action-larger').click(function(event) {
    	changeFont(1.20);
    	event.preventDefault();
    	return false;
    })
    $('#action-smaller').click(function(event) {
    	changeFont(0.80);
    	event.preventDefault();
    	return false;
    })
    
    /* cufon actions */
	Cufon.replace('h2', { fontFamily: 'Museo 500' });
	Cufon.now();
});
