jQuery.noConflict();
jQuery(document).ready(function($) {
    // Convert corepublish urls to bit.ly for twitter links
    $('div.favorites ul li').each(function() {
        if ($(this).children('a').attr('title') == 'Twitter') {
            var element = $(this).children('a');
            var url = $(this).children('a').attr('href').split('+');
            var defaults = {
                login: 'ingjerdj',
                apiKey: 'R_2f30314b1fbb5b6f7d7af0d26e76a0e1',
                url: encodeURIComponent(url[url.length-1])
            }
            var bitUrl = 'http://api.bit.ly/v3/shorten?'
            + 'login=' + defaults.login
            + '&apiKey=' + defaults.apiKey
            + '&longUrl=' + defaults.url;
            $.ajax({
                url: bitUrl,
                dataType: 'jsonp',
                success: function(response) {
                    if (typeof(response.status_code) != 'undefined') {
                        if (response.status_code == '200') {
                            url[url.length-1] = response.data.url;
                            element.attr('href', url.join('+'));
                        } else {
                            element.attr('rel', response.status_txt);
                        }
                    }
                },
                error: function(obj, t, e) {
                    element.attr('rel', t);
                }
            });
        }
    });
    $('.frontpage-banner ul.banner').advListRotator({
        effect: 'fade',
        effectTimer: 3000,
        helper: $('.frontpage-banner ul.helper'),
        stopOnInteraction: 'mouseover',
        startOnInteraction: 'mouseout'
    });    
    $('.frontpage-banner ul.helper').css('margin-left', '-' + Math.round(parseInt($('.frontpage-banner ul.helper').css('width'))/2) + 'px');
    $('.frontpage-banner span.text').bind('mouseover', function() {
        $(this).css('text-decoration', 'underline');
    });
    $('.frontpage-banner span.text').bind('mouseout', function() {
        $(this).css('text-decoration', 'none');
    });

    $('ul.norskdesign-gallery').advListRotator({
        effectTimer: 500,
        autoStart: false,
        disableRotationEngine: true,
        helperInteraction: 'click',
        helper: $('ul.norskdesign-gallery-helper'),
        effect: 'fade',
        unevenHeightsFix: true,
        helperInteractionAnimate: true,
        inactiveItemClass: 'norskdesign-gallery-inactive',
        activeItemClass: 'norskdesign-gallery-active'
    });
    
    /* Advisory */
    $('div.advisory ul.articlelist').children('li').each(function() {
        $(this).children('a:first').bind('click', function() {
            $('div.advisory ul.articlelist li.active').removeClass('active');
            $('div.advisory ul.articlelist li div.content').slideUp(200);
            $('div.advisory ul.articlelist li').removeClass('expanded');
            if ($(this).parent('li').children('div.content:first').is(':visible')) {
                $(this).parent('li').removeClass('expanded').addClass('collapsed');
                $(this).parent('li').children('div.content').slideUp(200);
            } else {
                $(this).parent('li').addClass('active').removeClass('collapsed').addClass('expanded');
                $(this).parent('li').children('div.content').slideDown(300);
            }
            return false;
        });
    });
    $('div.advisory ul.articlelist li.expanded div.content').show();
});
