$(function () {
    function showControls() {
        $('.controls').show();
    }
    function hideControls() {
        $('.controls').hide();
    }
    $('.prev').live('click', function() {
        prev();
        return false;
    });
    $('.next').live('click', function() {
        next();
        return false;
    });
    $('.small-images span').live('click', function() {
        if($(this).find('img').hasClass('selected') && !$('#biggallery').is(':hidden')){
            closeBiggallery();
        } else {
            openBiggallery();
        }
        display($(this).parent().attr('value'));
        if($('#small-gallery').length != 0){
            //$('html,body').animate({scrollTop: $('#SubPageWrapper').offset().top},'slow'); <-prøver annen id å scrolle til
            $('html,body').animate({scrollTop: $('#anbud').offset().top},'slow');
        }
        return false;
    });
    $('#backtoanbud span').live('click', function() {
        closeBiggallery();
    });

    $('.gallery .large-images li:not(:first)').hide();
});

function prev() {
    var total = $('#big-gallery .large-images li').length;
    if($('#business-gallery .large-images li').length){
        total = $('#business-gallery .large-images li').length;
    }
    var current = $('.large-images li:visible').attr('value');
    if(current > 1) {
        display(parseInt(current)-1);
    } else {
        display(total);
    }
}

function next() {
    var total = $('#big-gallery .large-images li').length;
    if($('#business-gallery .large-images li').length){
        total = $('#business-gallery .large-images li').length;
    }
    var current = $('.large-images li:visible').attr('value');
    if(current < total) {
        display(parseInt(current)+1);
    } else {
        display(1);
    }
}

function display(num) {
    $('.large-images li').hide();
    $('.large-images li[value="'+num+'"]').show();
    $('.small-images span img').removeClass('selected');
    var img = $('.small-images li[value="'+num+'"] span img').addClass('selected');
    if($('#business-gallery')){
        var li = img.parent().parent();
        var scroll = (li.position().left)-(li.width()/2);
        $('#business-gallery').scrollLeft(Math.round(scroll)-(($('#business-gallery').width()/2)-(li.width())));
    }       
}

function openBiggallery() {
    $('#biggallery').slideDown('slow');
}

function closeBiggallery() {
    $('#biggallery').slideUp('fast');
}
