// Add js modifier class for css
$('body').addClass('hasJS');


/* Election ----------------------------------------------------------------- */

// Table link handlers
$('div#election-results table#results tbody tr.party').mouseover(function(){$(this).addClass('hover')});
$('div#election-results table#results tbody tr.party').mouseout(function(){$(this).removeClass('hover')});

$('div#election-results table#results tbody tr.party').click(function(){
    var oRows = $('div#election-results table#results tbody tr.candidate[rel='+$(this).attr('rel')+']');
    if(oRows.hasClass('visible')){
        oRows.removeClass('visible');
    }else{
        oRows.addClass('visible');
    }
    
});

/* Blog --------------------------------------------------------------------- */

// Config 
var locked = false;
var timing = 5000;
var currentCand = -1;
var scrollAnim = setTimeout(scrollNext,timing);

// Scrolls to specified anchor
function scrollTo(anchor){        
    
    // Do not animate if animation active
    if(locked)return false
    lock();
    clearTimeout(scrollAnim);
                    
    // Scroll/animate to correct anchor
    var divOffset = $('#blog-puff').offset().left;
    var pOffset = $('#blog-puff '+anchor).offset().left-$('#blog-puff').offset().left;
       
    
    // Animate if needed & release lock
    if(pOffset != 0){    
        $('#blog-puff').animate({scrollLeft: '+=' + pOffset + 'px'}, 1000,unlock);
    }else{
        unlock();
    }
}

// Scrolls to next in line
function scrollNext(){

    // Do not animate if only one entry
    if($('#blog-puff .blog-entry').size() == 1){
        clearTimeout(scrollAnim);
        return;
    }
    
    // Set next in anchor
    currentCand = (currentCand != $('#blog-puff .blog-entry').size()-1)?currentCand+1:0;
    scrollTo('#entry-'+currentCand);
    scrollAnim = setTimeout(scrollNext,timing);

    return true;
}

// lock / unlock so animation cant be interrupted
function lock(){locked = true;}
function unlock(){locked = false;scrollAnim = setTimeout(scrollNext,timing);}

// Load functions
$(document).ready(function() {

    // Make entire blog-entity clickable
    $('#blog-puff .blog-entry').addClass('link').click(function(){document.location.href = $(this).find('a').attr('href');});
    $('#blog-list .blog-list-entry').addClass('link').click(function(){document.location.href = $(this).find('a').attr('href');});    

    // Modifiers for HBL-lokalt    
    $('#hbl-lokalt ul a').click(function(){
        $('#hbl-lokalt ul a').removeClass('active');
        $(this).addClass('active');
        
        $('#hbl-lokalt #regions div').each(function(){        
            if(!$(this).hasClass('img')){
                $(this).css('display','none');
            }        
        });
        $('#hbl-lokalt '+$(this).attr('href')).css('display','block');
        return false;
            
    });
    $('#hbl-lokalt img').addClass('link');
    $('#hbl-lokalt img').click(function(){document.location.href = $(this).parent().parent().find('a').attr('href');});
    
    // Hide small ad-images
    $('.banner img[height=1]').addClass('hidden');
    $('.banner img[height=2]').addClass('hidden');
    $('#banner_main img[height=1]').addClass('hidden');
    $('#banner_main img[height=2]').addClass('hidden');    
        
});