jQuery(document).ready(function() {

    $('#sbh_carousel br').remove();
    $('#sbh_carousel').cycle();

    /*
     * language switcher
     */
    $('#langs .en a').addClass('visible');
    $('div.it').slideUp('slow');

    $('#langs a').click(function() {
        console.log($(this).attr('class'));
        // controllo se il link cliccato NON ha la classe visible
        var $theClass = $(this).attr('class');
        console.log("hai cliccato la classe " + $theClass);

        if ($theClass != "visible") {
            var $theActiveLang = $(this).parent().parent().find('li .visible').parent().attr('class');
            // Rimuovo la classe .visible dal sibling
            $(this).parent().parent().find('li .visible').removeClass('visible');
            // Aggiungo la classe .visible
            $(this).addClass('visible');
            var $theLang = $(this).parent().attr('class');
            // nascondo il div di testo che è visibile
            $('div.' + $theActiveLang).slideUp('slow');
            $('div.' + $theLang).slideDown('slow');
        }
        return false;
    });
    /*
     * Ajax Pagination Section
     */
    $('.wp-pagenavi a').live('click', function(event) { // if not using wp-page-numbers, change this to correct ID
        event.preventDefault();
        var link = $(this).attr('href');
        $('#ajax_pagination_main').fadeOut(500).load(link + ' #ajax_pagination_posts', function() {
            $('#ajax_pagination_main').fadeIn(300);
        });
        $.scrollTo('#recent-news-block', 800);
    });

    /*
     * CSS fix Section
     */
    // let's check if span.published text is equal to "today"
    $('span.published:contains("Today")').addClass('today');

    /*
     * Scrollable
     */
    $(".scrollable").scrollable();

    /*
    * Link to comments in single.php should go to Disqus comments, no more to built in comments
    */
    $("a[href$='#comments']").attr('href', '#disqus_thread');

    /*
    * #28: Links in comments should open in an external windows keeping visitors on site
    */
    $('.dsq-comment-text a').live('mouseover', function() {
        $(this).attr('target', '_blank');
    });

    remove_double_highlight();
});

function remove_double_highlight() {
    if ($('#primary-nav > div > ul > li.current-post-ancestor').length > 1 ) {
        $('#primary-nav > div > ul > li.current-post-ancestor:first').removeClass('current-post-ancestor');
    }
}
