/*
    Document   : frontend.js
    Created on : 23.02.2011, 13:16:26
    Author     : Mike vom Scheidt | CTS GmbH 2011
    Description: frontend js file for coelln event
*/

$(function(){
    if($.cookie("landingpage") != "true" && $("#landingpage").length > 0) {
        $("#landingpage").show();
    }
    $(".landingpage #wrapper").show();
    //move newsticker to the right end of the ticker range
    $('.movable').css("margin-left", $(".scrollable").outerWidth(false));
});

$(window).load(function () {
    setTimeout(function() {
        startAnimation();
    }, 3000);
    Shadowbox.init({
        skipSetup:true
    });
    Shadowbox.setup($('a[rel*="lightbox"]:not(.gallery_control a[rel*="lightbox"])'));
    setupGallery();
    newsticker();
    imageRotation();
});

/**
 * Setup the Gallery< br />
 */
var setupGallery = function() {
    //calculate the maximum width
    var width = 0;
    var containerWidth = $(".gallery_control > .slide").width();
    var singleElement = $(".slide > div").outerWidth(true);
    var elements = $('.gallery_large .odd, .gallery_large .even');
    var elementsLength = elements.length;

    $(".slide > div").each(function() {
        width += $(this).outerWidth(true);
    });
    //set the maximum width
    $(".gallery_control > .slide").width(width);

    $(".gallery_control a").click(function(ev){
        ev.preventDefault();
        var href = $(this).attr('href');
        $('.gallery_large a[href="' + href + '"]').parents(".odd, .even").fadeIn(500, function(){
            $('.gallery_large a:not(a[href="' + href + '"])').parents(".odd, .even").fadeOut(500).removeClass('active');
        }).addClass('active');
    });

    $("#previous").click(function(ev) {
        ev.preventDefault();
        //get the current margin
        var currentMargin = parseInt($(".gallery_control > .slide").css("margin-left"));
        //set new margin if newmargin is smaller or equal 0
        if(currentMargin + singleElement <= 0) {
            $(".gallery_control > .slide").stop().animate({
                marginLeft: currentMargin + singleElement + "px"
            }, 500, "swing");
        } else {
            $(".gallery_control > .slide").stop().animate({
                marginLeft: "0px"
            }, 500, "swing");
        }
    });

    $("#previous_image").click(function(ev){
        ev.preventDefault();
        if(!$('.gallery_large').hasClass('fading')) {
            var position = $('.gallery_large').children('.active').index();
            if(position < 1) {
                return;
            }
            $('.gallery_large').addClass('fading');

            $(elements[position]).removeClass('active');
            $(elements[position-1]).addClass('z_index').fadeIn(1000, function() {
                $(elements[position]).hide();
                $('.gallery_large').removeClass('fading');
                $(this).removeClass('z_index')
            }).addClass('active');
        }
    });
    
    $("#next").click(function(ev) {
        ev.preventDefault();
        //get the current margin
        var currentMargin = parseInt($(".gallery_control > .slide").css("margin-left"));
        //set new margin if newmargin is bigger or equal -containerWidth
        if(currentMargin - singleElement >= -width + containerWidth) {
            $(".gallery_control > .slide").stop().animate({
                marginLeft: currentMargin - singleElement + "px"
            }, 500, "swing");
        } else {
            $(".gallery_control > .slide").stop().animate({
                marginLeft: -width + containerWidth + "px"
            }, 500, "swing");
        }
    });

    $("#next_image").click(function(ev){
        ev.preventDefault();
        if(!$('.gallery_large').hasClass('fading')) {
            var position = $('.gallery_large').children('.active').index();
            if(position >= elementsLength-1) {
                return;
            }
            if(position == -1) {
                position = 0;
            }
            $('.gallery_large').addClass('fading');

            $(elements[position]).removeClass('active');
            $(elements[position+1]).addClass('z_index').fadeIn(500, function() {
                $(elements[position]).hide();
                $('.gallery_large').removeClass('fading');
                $(this).removeClass('z_index')
            }).addClass('active');
        }
    });
}

var newsticker = function() {
    var maxSize = 1;
    var containerSize = $(".scrollable").outerWidth(false);
    var itemCount = 0;

    $('#newsticker .news_ticker').each(function(){
        maxSize += $(this).outerWidth(true);
        itemCount++;
    });

    duration = ((containerSize + maxSize)  * 20);
    $('.movable').css('width', maxSize);

    var run = function() {
        $('.movable').animate({
            marginLeft: -maxSize
        }, duration, 'linear', function() {
            $('.movable').css("margin-left", containerSize);
            run();
        });
    };

    run();
};

var imageRotation = function() {
    if(typeof $('#slideshow li:first') != "undefined"){
        var el = $('#slideshow li:first');
        var origWidth = el.outerWidth(true);

        var run = setInterval(function()  {
            if(parseInt($('#slideshow .slide_inner').css('margin-left')) <= -origWidth) {
                el.appendTo('#slideshow ul');
                el = $('#slideshow li:first');
                $('#slideshow .slide_inner').css('margin-left', 0);
            } else {
                $('#slideshow .slide_inner').css('margin-left', parseInt($('#slideshow .slide_inner').css('margin-left')) - 1);
            }
        }, 30)
    }
};

var startAnimation = function() {
    if($("body.landingpage").length > 0 && $("#landingpage").length > 0 && $.cookie("landingpage") != "true") {
        $('#landingpage .upper, #landingpage .lower').animate({
            height: 'toggle'
        }, 2000, 'swing', function() {
            $('#landingpage').hide();
        });
        $.cookie("landingpage", "true");
    }
};
