function mycarousel_initCallback(carousel) {
    $('div[class^=jcarousel-control] a').bind('click', function() {
        $('.control').removeClass('active');
        $(this).addClass('active');
        var temp = $(this).attr('id').split('_');
        
        carousel.scroll($.jcarousel.intval(temp[1]));
        return false;
    });
    
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });

    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });

    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};


function mycarousel_move(carousel, temp1, temp2, temp3)
{
    $('.control').removeClass('active');
    $('#counter_'+temp2).addClass('active');
}

$(document).ready(function()
{
    $('#dragContainer').draggable(
    {
        handle: '#moveContainer'
    });
    $("#mycarousel").jcarousel({
        scroll: 1,
        auto: 0,
        wrap: 'last',
        initCallback: mycarousel_initCallback,
        itemVisibleInCallback: {
            onAfterAnimation: mycarousel_move
        }
    });
    
    $("a[rel='colorbox']").colorbox({ 
        maxHeight: '90%',
        maxWidth: '90%'
    });
    
    $('.closeColorbox').click(function()
    {
        parent.window['jQuery'].colorbox.close();
        return false;
    });
    
    $("a[rel='framebox']").colorbox(
    {
        iframe:true, 
        width:799, 
        height:552
    });
    
    $('.innerfade').innerfade({
		speed: 750,
		timeout: 5000,
		type: 'random'
	});
    
    $(".validateForm").validate({
        errorPlacement: function(error, element) {
			error.prependTo( element.parent().next() );
		},
		// set this class to error-labels to indicate valid fields
		success: function(label) {
			// set &nbsp; as text for IE
			label.html("&nbsp;").addClass("checked");
		}
    });
    
    $(".clearInput").focus(function(srcc)
    {
        if ($(this).val() == this.defaultValue)
        {
            $(this).val("");
            $(this).css('color', '#4b4b4b');
        }
    });
    
    $(".clearInput").blur(function()
    {
        if ($(this).val() == "")
        {
            $(this).val(this.defaultValue);
        }
    });
    
    $(".clearInput").blur();
    
    $('#arrowLeft').click(function()
    {
        $activeId = parseInt($('#activeId').val());
        $show = $activeId - 1;
        if ($show == 0)
        {
            $show = parseInt($('#background > ul > li').size());
        }
        switchBackground($activeId, $show);
    });
    
    $('#arrowRight').click(function()
    {
        $activeId = parseInt($('#activeId').val());
        $show = $activeId + 1;
        if ($show > parseInt($('#background > ul > li').size()))
        {
            $show = 1;
        }
        switchBackground($activeId, $show);
    });
    
    $('.archive').click(function()
    {
        $('#archiveMenu').slideDown();
    });
    
    $('#downloadList').sweetPages({perPage:8});
    
    var controls = $('.swControls').detach();
	controls.appendTo('#downloadPages');
    
});

$(window).load(function() 
{
    $('.textHomeRechts').vAlign('bottom');    
});

(function ($) {
$.fn.vAlign = function($align) 
{
	return this.each(function(i)
    {
    	var ah = $(this).height();
    	var ph = $(this).parent().height();
        
        switch ($align)
        {
            case 'top':
                var mh = 0;
            break;
            case 'middle':
                var mh = Math.ceil((ph-ah) / 2);
            break;
            case 'bottom':
                var mh = Math.ceil(ph-ah);
            break;
        }
    	$(this).css('margin-top', mh);
	});
};
})(jQuery);

function switchBackground($active, $show)
{
    $('#activeId').val($show);
    $('#background_' + $active + ' > img').fadeOut();
    $('#text_' + $active).hide();
    $('#background_' + $show + ' > img').fadeIn();
    $('#text_' + $show).show();
}

function reloadCaptcha()
{
	document.getElementById('captcha').src = document.getElementById('captcha').src+ '?' +new Date();
}
