jQuery.noConflict();

jQuery(document).ready( function() {

    if (jQuery('body').hasClass('front')) {
        var img = new Image();
        jQuery(img).load( function() {
            jQuery(this).hide();
            jQuery('.front #container').css('background-image', 'url(/wp-content/themes/aukio/img/container_front_hq.jpg)');
        }).attr('src', '/wp-content/themes/aukio/img/container_front_hq.jpg');    
    } else {
        var img = new Image();
        jQuery(img).load( function() {
            jQuery(this).hide();
            jQuery('#container').css('background-image', 'url(/wp-content/themes/aukio/img/container_hq.jpg)');
        }).attr('src', '/wp-content/themes/aukio/img/container_hq.jpg');
    }

    if (jQuery('#navigation')) {
        jQuery('#navigation li:not(:last)').each( function() {
            var width = jQuery(this).children('a').width() + 11;

            jQuery(this).css({
                'background-image':    'url(/wp-content/themes/aukio/img/navigation.png)',
                'background-position': width +'px -146px',
                'background-repeat':   'no-repeat'
            });
        });
    }

    if (jQuery('#nosto ul')) {
		jQuery('#nosto li').show();
		jQuery('#nosto ul').newsTicker(4000);
	}

    if (jQuery('#sidebar-pages')) {
        jQuery('#sidebar-pages a').each( function(e) {
            if ( jQuery(this).attr('href').match('page_id=3291') ) {
                jQuery(this).attr('href', '/forum/');
            }
        });

        jQuery('#sidebar-pages').find('li>ul').each( function(i) {
            if ( ! jQuery(this).parent().hasClass('current_page_ancestor') ) {
                jQuery(this).hide().parent().children('a').prepend('<span class="closed">▶</span>');
            } else {
                jQuery(this).parent().children('a').prepend('<span class="open">▼</span>');
            }

            jQuery(this).parent().children('a').click( function(e) {
                if ( jQuery(this).next().length > 0 ) {
                    e.preventDefault();

                    var sym = jQuery(this).parent().children('a').children('span');

                    if ( sym.hasClass('closed') ) {
                        sym.addClass('open').removeClass('closed').html('▼');
                        jQuery(this).siblings('ul').show();
                    } else if ( sym.hasClass('open') ) {
                        sym.addClass('closed').removeClass('open').html('▶');
                        jQuery(this).siblings('ul').hide();
                    }
                }
            });
        });
    }

    if (jQuery('.like-post')) {
        jQuery('.like-post').click( function(e) {
            e.preventDefault();

            var postid = jQuery(this).attr('class').match(/post-(\d+)/);

            if (postid[1] > 0) {
                jQuery.get('/wp-like.php', { post: postid[1] },
                function(data) {
                    jQuery('span.like-'+ postid[1]).text('Suositeltu '+ data +' kertaa');
                });
            }
        });
    }
});