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()
{   
    $('#files').MultiFile({
        list: '#fileList',
        STRING: {
            remove: '<img src="images/delete.png" height="14" width="14" alt="delete" align="right"/>'
        }
    });
    
    $('.homeMonth').click(function()
    {
        $('.newsItems').slideUp();
        $(this).next('ul.newsItems').slideDown();
        return false;
    });
    
    $('.actHomeMonth').click(function()
    {
        $('.actItemList').slideUp();
        $(this).next('ul.actItemList').slideDown();
        return false;
    });
    
    $('#newsButtons > li').click(function()
    {
        $(this).siblings().toggleClass('active');
        $(this).toggleClass('active');
        $('.newsList').hide();
        $('#' + $(this).attr('id') + 'List').show();
    });
    
    
    $('.thumb').click(function()
    {
        $image = $(this).attr('src').split('&');
        $image = $image[0].replace('includes/imageCrop.php?image=../', '');
        $('#photoFilmTop').children('img').attr('src', $image);
    });
    
    var $activeId = 1;
    var $count = $('#photoFilmOverview').children().length;
    $('#goToPages > li a').click(function()
    {
        $href = $(this).attr('href').replace('#', '');
        switch ($href)
        {
            case 'previous':
                if ($activeId == 1)
                {
                    return false;
                }
                $show = $activeId - 1;
            break;
            case 'next':
                if ($activeId == $count)
                {
                    return false;
                }
                $show = $activeId + 1;
            break;
            default: // Page
                $show = $href;
            break;
        }
        
        $('#pagelink_' + $activeId).removeClass('active');
        $('#pagelink_' + $show).addClass('active');
        
        $('#overview_' + $activeId).hide();
        $('#overview_' + $show).show();
        $activeId = $show;
        
        
        
        if ($show == 1)
        {
            $('#previous').hide();
        }
        else
        {
            $('#previous').show();
        }
        
        if ($show == $count)
        {
            $('#next').hide();
        }
        else
        {
            $('#next').show();
        }
        
        return false;
    });
    
    
    $('.albumPicture').mouseover(function()
    {
        $(this).next('div').children('a').toggleClass('active')
    });
    
    $('.albumPicture').mouseout(function()
    {
        $(this).next('div').children('a').toggleClass('active')
    });
    
    $('.mapView').mouseover(function()
    {
        $.getJSON('includes/getMapView.php?map=' + $(this).attr('href').replace('#', ''), function(data) {
            $.each(data, function(key, val) {
                $('#' + key).html(val);
            });    
        });
        offset = $(this).areaOffset(false);
        offset.top = parseInt(offset.top) - 2;
        offset.left = parseInt(offset.left) + 38;
        $('#mapViewActiveArrow').css('top', offset.top + 'px');
        $('#mapViewActiveArrow').css('left', offset.left + 'px');
    });
    
    $('.mapView').click(function()
    {
        return false; 
    });
    
    $('#print').click(function()
    {
        window.print();
    });

    
    $('.homeMore').click(function()
    {
        $('#homeMoreText').slideDown();
        $('.homeLess').show();
        $(this).hide();
    });
    
    $('.homeLess').click(function()
    {
        $('#homeMoreText').slideUp();
        $('.homeMore').show();
        $(this).hide();
    });
    
    $("#mycarousel").jcarousel({
        scroll: 1,
        auto: 0,
        wrap: 'last',
        initCallback: mycarousel_initCallback,
        itemVisibleInCallback: {
            onAfterAnimation: mycarousel_move
        },
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null
    });
        
	$("a[rel='lightbox']").colorbox();
    
    $('.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");
		}
    });
    
    $('#downloadList > li > a').each(function() {
        $temp = this.pathname.split('.');
        $extension = $temp[($temp.length) - 1];
        $(this).after(' (' + $extension.toUpperCase() + ')');
    });

    
    $(".archiefListTitle").click(function() {
        if($(this).hasClass("expanded")) {
            $(this).next('.subArchiefList').slideToggle();
        } else {
            $(this).next('.subArchiefList').slideToggle();
        };
        $(this).toggleClass("expanded").toggleClass("collapsed");
        return false;
    });
    
    $('.styleswitch').click(function()
	{
		switchStylestyle(this.getAttribute("rel"));
		$(".styleswitch").removeClass('active');
		$(this).addClass('active');
		return false;
	});
	var c = readCookie('style');
	
	if (c) 
	{
        $(".styleswitch").removeClass('active');
		$('#changeFontSize > li > a#' + c + 'Letters').addClass('active');
		switchStylestyle(c);
	}
    
    $(".searchBar").focus(function(srcc)
    {
        if ($(this).val() == this.defaultValue)
        {
            $(this).val("");
        }
    });
    
    $(".searchBar").blur(function()
    {
        if ($(this).val() == "")
        {
            $(this).val(this.defaultValue);
        }
    });
    
    $(".searchBar").blur();
});

function switchStylestyle(styleName)
{
  jQuery('link[@rel*=style][title]').each(function(i)
  {
     this.disabled = true;
     if (this.getAttribute('title') == styleName) this.disabled = false;
  });
  createCookie('style', styleName, 365);
}

function createCookie(name,value,days)
{
	if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name)
{
	createCookie(name,"",-1);
}
