$(document).ready(function() {
    
    function pageHighlight(loc, clsNm) {
        var pathname = window.location.pathname;
        if(pathname === loc) {
            clsNm.addClass('active');
        }
    }

    pageHighlight("/", $('.home'));
    pageHighlight("/about", $('.about'));
    pageHighlight("/category/blog", $('.blog'));
    pageHighlight("/category/projects", $('.projects'));
    pageHighlight("/category/freebies", $('.freebies'));
    pageHighlight("/github-repos", $('.github-repos'));
    pageHighlight("/contact", $('.contact'));
    
    $('.searchButtonActive').hide();
    
    function showHideSearch(el1, container, dur) {
        el1.bind('click', function() {
            if(container.css('display') === 'none') {
                el1.addClass('activeSearch');
                container.slideDown(dur);
            } else {
                el1.removeClass('activeSearch');
                container.slideUp(dur);
            }
        });    
    }
    
    showHideSearch($('.searchButton'), $('#searchContainer'), 250);
    
    $.getJSON("http://feeds.delicious.com/v2/json/tonybebber?recent&count=1&callback=?", function(data) {
        var deliciousData = data;
        $('#reading p').html('<a href="' + deliciousData[0].u + '">' + deliciousData[0].d + '</a>');
    }).error(function() {
        $('#reading p').html('Sorry...could not load the recommended article.');
    });

    $.getJSON("http://search.twitter.com/search.json?q=javascript&lang=en&rpp=1&result_type=popular&callback=?", function(data) {
        var twitterData = data;
        function convertLinks(text) {  
            var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;  
            return text.replace(exp,"<a href='$1'>$1</a>");
        }
        var textWithLinks = convertLinks(twitterData.results[0].text);
        $('#social p').html('<a href="http://twitter.com/' + twitterData.results[0].from_user + '"><img class="tweeter" src="' + twitterData.results[0].profile_image_url + '" width="20" height="20" /></a> <a href="http://twitter.com/' + twitterData.results[0].from_user + '">' + "@" + twitterData.results[0].from_user + '</a>: ' + textWithLinks);
    }).error(function() {
        $('#social p').html('Sorry...could not load the recommended tweet.');
    });
    
});
