(function($)
{
    $.tagCloud = {
        iFadeOutSpeed: 100,
        iFadeInSpeed: 200
	};
	
	$.fn.tagCloud = function ()
    {
        return $(this).each(function()
        {
            $oThis = $(this);
            $oUl = $oThis.find('ul');
            $oList = $oThis.find('li');

            var $oSortAbc = $('<a href="javascript:void(0)" class="btn">Sort alphabetically</a>').toggle(
                function(){
                    $oList.fadeOut($.tagCloud.iFadeOutSpeed, function()
                    {
                         $oList.tsort({order: "asc"}).fadeIn($.tagCloud.iFadeInSpeed);
                    });
                },  
                function(){
                    $oList.fadeOut($.tagCloud.iFadeOutSpeed, function()
                    {
                         $oList.tsort({order: "desc"}).fadeIn($.tagCloud.iFadeInSpeed);
                    });
                }       
            );            
            var $oSortStrength = $('<a href="javascript:void(0)" class="btn">Sort by strength</a>').toggle(
                function(){
                    $oList.fadeOut($.tagCloud.iFadeOutSpeed, function()
                    {
                         $oList.tsort({order: "asc", attr: "class"}).fadeIn($.tagCloud.iFadeInSpeed);
                    });
                },  
                function(){
                    $oList.fadeOut($.tagCloud.iFadeOutSpeed, function()
                    {
                         $oList.tsort({order: "desc", attr: "class"}).fadeIn($.tagCloud.iFadeInSpeed);
                    });
                }       
            );
            
            $oList.tsort({order: "rand"});      
            $oUl.before($oSortAbc).before($oSortStrength).height($oUl.height() + 10);
        });
    };
})(jQuery);

$(document).ready(function()
{
    // tag cloud
    $('#tagcloudContainer_ctl div').tagCloud();
    
    // smooth scroll
    $('a[href*=#]').click(function()
    {
        if(location.pathname.replace(/^\//,'')==this.pathname.replace(/^\//,'') && location.hostname==this.hostname && this.hash.replace(/#/,''))
        {
            var hash = this.hash;
            var target = $(hash).offset().top;
            $("html:not(:animated),body:not(:animated)").animate({ scrollTop: target }, 500, 'swing', function() { /* location.hash=hash; */ });
            return false;
        }
    });
});
