$(document).ready(function(){




//////////////////////////////////////////////// NEWS //////////////////////////
////////////////////////////////////////////////////////////////////////////////

$(".news-prev-scroll").click(function(){
    $(".news-container").scrollTo("-=326px", 400);
    return false;
});
$(".news-next-scroll").click(function(){
    $(".news-container").scrollTo("+=326px", 400);
    return false;
});


//////////////////////////////////////////////// EXHIBITIONS ///////////////////
////////////////////////////////////////////////////////////////////////////////

$(".exhibitions-prev-scroll").click(function(){
    $(".exhibitions-container").scrollTo("-=200px", 800);
    return false;
});
$(".exhibitions-next-scroll").click(function(){
    $(".exhibitions-container").scrollTo("+=200px", 800);
    return false;
});



//////////////////////////////////////////////// PRESS /////////////////////////
////////////////////////////////////////////////////////////////////////////////

var pressScrollSpeed = 1000;
var pressScrollLength = "500px";
var scrollSpeed = 500;
var containerWidth = 1000;

// Adding classes and hiding the "prev" button
$(".PressContainer li:first").addClass("curImg").addClass("firstImg");
$(".PressContainer li:last").addClass("lastImg");
$(".prevPress").hide();
$(".PressContainer img").hide();
$(".show li").hide();
$(".show li:first").show().addClass("curShow");

// Set arbitrarily large width until images are loaded and we can apply the correct width
$(".PressContainer ul").css({ "width" : "10000px" });

// Wait till all images have loaded to run the following
$(window).load(function(){
    $(".PressContainer img").fadeIn();
    $(".PressContainer").css({ "background-position" : "0 -99999em" });
    // Setting left-margin on ".container ul" so first image is centered
    var firstWidth = $(".PressContainer li:first").width();
    var ulLeftMargin = (containerWidth-firstWidth)/2;
    $(".PressContainer ul").css({"margin-left" : ulLeftMargin + "px"});

    // Setting width of ".container ul"
    var ulWidth = $(".PressContainer li:last").width();
        ulWidth = (containerWidth-ulWidth)/2;
    $(".PressContainer li").each(function(){
        var w = $(this).width();
        ulWidth = ulWidth + w;
    });
    $(".PressContainer ul").css({ "width" : ulWidth + "px" });


});

function scrollImgRightPress() {
    // Calculate distance
    var curWidth =  $(".curImg").width();
    var nextWidth = $(".curImg").next().width();
    var dist = (curWidth+nextWidth)/2;
    $(".PressContainer").scrollTo("+=" + dist + "px", scrollSpeed );
    // Update classes
    $(".curImg").next().addClass("curImg").prev().removeClass("curImg");
    $(".prevPress").fadeIn();
    // If this is last image, fade out the button
    if ( $(".curImg").hasClass("lastImg") ) {
        $(".nextPress").fadeOut();
    }
    return false;
}

function scrollImgLeftPress() {
    // Calculate distance
    var curWidth =  $(".curImg").width();
    var prevWidth = $(".curImg").prev().width();
    var dist = (curWidth+prevWidth)/2;
    $(".PressContainer").scrollTo("-=" + dist + "px", scrollSpeed);
    // Update classes
    $(".curImg").prev().addClass("curImg").next().removeClass("curImg");
    $(".nextPress").fadeIn();
    // If this is first image, fade out the button
    if ( $(".curImg").hasClass("firstImg") ) {
        $(".prevPress").fadeOut();
    }
    return false;
}

// Controls for "nextImg" button
$(".controls a.nextPress").click(function(){
    scrollImgRightPress();
    return false;
});

// Controls for "prevImg" button
$(".controls a.prevPress").click(function(){
    scrollImgLeftPress();
    return false;
});






//////////////////////////////////////////////// FOR SALE //////////////////////
////////////////////////////////////////////////////////////////////////////////

// Variables worth messing with
var scrollSpeed = 500;
var containerWidth = 1000;

// Adding classes and hiding the "prev" button
$(".container li:first").addClass("curImg").addClass("firstImg");
$(".container li:last").addClass("lastImg");
$(".prevImg").hide();
$(".container img").hide();
$(".show li").hide();
$(".show li:first").show().addClass("curShow");

// Set arbitrarily large width until images are loaded and we can apply the correct width
$(".container ul").css({ "width" : "10000px" });

// Wait till all images have loaded to run the following
$(window).load(function(){
    $(".container img").fadeIn();
    $(".container").css({ "background-position" : "0 -99999em" });
    // Setting left-margin on ".container ul" so first image is centered
    var firstWidth = $(".container li:first").width();
    var ulLeftMargin = (containerWidth-firstWidth)/2;
    $(".container ul").css({"margin-left" : ulLeftMargin + "px"});

    // Setting width of ".container ul"
    var ulWidth = $(".container li:last").width();
        ulWidth = (containerWidth-ulWidth)/2;
    $(".container li").each(function(){
        var w = $(this).width();
        ulWidth = ulWidth + w;
    });
    $(".container ul").css({ "width" : ulWidth + "px" });
});

function scrollImgRight() {
    // Calculate distance
    var curWidth =  $(".curImg").width();
    var nextWidth = $(".curImg").next().width();
    var dist = (curWidth+nextWidth)/2;
    $(".container").scrollTo("+=" + dist + "px", scrollSpeed );
    // Update classes
    $(".curImg").next().addClass("curImg").prev().removeClass("curImg");
    $(".prevImg").fadeIn();
    // If this is last image, fade out the button
    if ( $(".curImg").hasClass("lastImg") ) {
        $(".nextImg").fadeOut();
    }
    $(".show li.curShow").next().addClass("curShow").prev().hide().removeClass("curShow").next().fadeIn();
    return false;
}

function scrollImgLeft() {
    // Calculate distance
    var curWidth =  $(".curImg").width();
    var prevWidth = $(".curImg").prev().width();
    var dist = (curWidth+prevWidth)/2;
    $(".container").scrollTo("-=" + dist + "px", scrollSpeed);
    // Update classes
    $(".curImg").prev().addClass("curImg").next().removeClass("curImg");
    $(".nextImg").fadeIn();
    // If this is first image, fade out the button
    if ( $(".curImg").hasClass("firstImg") ) {
        $(".prevImg").fadeOut();
    }
    $(".show li.curShow").prev().addClass("curShow").next().hide().removeClass("curShow").prev().fadeIn();
    return false;
}

// Controls for "nextImg" button
$(".controls a.nextImg").click(function(){
    scrollImgRight();
    return false;
});

// Controls for "prevImg" button
$(".controls a.prevImg").click(function(){
    scrollImgLeft();
    return false;
});



// When img is click
$(".container li").click(function(){
    if ( $(this).hasClass("curImg") ) {
        $(".show").fadeIn();
        $(".showBg").show();
    }
    if ( $(this).prev().hasClass("curImg") ) {
        scrollImgRight();
    }
    if ( $(this).next().hasClass("curImg") ) {
        scrollImgLeft();
    }
});

$(".showBg, .show li, .show img").click(function(){
    $(".showBg").fadeOut().prev().hide();
});


////////// CONTACT & HOME //////////////////////////////
////////////////////////////////////////////////////////

var brev1 = '<a href="mailto:agert';
var brev2 = 'ved@newgallery.dk">';
var brev3 = 'agertved@newg';
var brev4 = 'allery.dk</a>';
$("#brev").html(brev1 + brev2 + brev3 + brev4);


});


