/** 
 * Initialisation function for pagination
 */
function initPagination() {
    $(".paginationNumbers").pagination($('.paginate li').length, {
        callback: imagePageselectCallback,
        items_per_page: 5
    });
    $('.paginationNumbers > .pagination > span:not(.prev, .next)').after('<span class="color">&nbsp;/&nbsp;</span>');
    $('.paginationNumbers > .pagination > a:not(.prev, .next)').after('<span class="color">&nbsp;/&nbsp;</span>');
    $('.paginationNumbers > .pagination > span.color').last().remove();
}

function imagePageselectCallback(page_index, jq) {
    
    $('.paginate li:visible').hide();
    for (i = (page_index * 5); i < (page_index * 5 + 5); i++)
    {
        $('.paginate li:eq(' + i + ')').show();
    }
    $('.paginationNumbers > .pagination > span:not(.prev, .next)').after('<span class="color">&nbsp;/&nbsp;</span>');
    $('.paginationNumbers > .pagination > a:not(.prev, .next)').after('<span class="color">&nbsp;/&nbsp;</span>');
    $('.paginationNumbers > .pagination > span.color').last().remove();
    return false;     
}

var $timer;
$(document).ready(function()
{
    initPagination();

    $('#employeeList > li').mouseover(function()
    {
        $(this).children('.employeeOverlay').toggle();
    });
    
    $('#employeeList > li').mouseout(function()
    {
        $(this).children('.employeeOverlay').toggle();
    });
    
    $('#dansersList > li').mouseover(function()
    {
        $(this).children('.employeeSmallOverlay').toggle();
    });
    
    $('#dansersList > li').mouseout(function()
    {
        $(this).children('.employeeSmallOverlay').toggle();
    });    
    
    $("a[rel='framebox']").colorbox(
    {
        iframe:true, 
        width:500, 
        height:380
    });
    
	$("a[rel='lightbox']").colorbox({
	   maxWidth: '80%',
       maxHeight: '80%'
	});
    
    $('.innerfade').innerfade({
		speed: 750,
		timeout: 5000,
		type: 'random'
	});
    
    $(".validateForm, #validateNewsletterForm").validate({
        errorPlacement: function(error, element) {
			return;
		},
		// set this class to error-labels to indicate valid fields
		success: function(label) {
			// set &nbsp; as text for IE
			label.html("&nbsp;").addClass("checked");
		}
    });
    
    $('input[name=where]').change(function() {
        if ($(this).val() == 'new') {
            $('.target').hide();
            //$('.user').show();
        } else {
            $('.target').show();
            //$('.user').hide();
        }
    });
    
    $('#toMarketmail').click(function() {
        if ($('input[name=where]:checked').val() != undefined) {
            $.post('Mailing', {toMarketmail: true, html: $('#mailing').html(), where: $('input[name=where]:checked').val(), target: $('select[name=target]').val(), user: $('select[name=user]').val()}, function(response) {
                alert('Wijzigingen doorgevoerd in Marketmail!'); 
            });
        } else {
            alert('Selecteer een verwerk methode!');
        }
        return false;
    });
    
    $('#fotoList > li').click(function()
    {
        $tmp = $(this).attr('id').split('_', 2);
        $id = parseInt($tmp[1]);
        $('#pageHeader > ul > li:visible').fadeOut('750');
        $('#image_' + $id).fadeIn('750');
    });
    
    if ($('#buttons > li').length > 1)
    {
        $timer = setTimeout(function()
        {
            $tmp = $('#buttons > li.active').attr('id').split('_', 2);
            $active = parseInt($tmp[1]);
            switchImage($active, 0);
        }, 5000);
    }
    
    $('#buttons > li').click(function()
    {
        if (!$(this).hasClass('active'))
        {
            clearTimeout($timer);
            $tmp = $('#buttons > li.active').attr('id').split('_', 2);
            $active = parseInt($tmp[1]);
            
            $tmp = $(this).attr('id').split('_', 2);
            $to = parseInt($tmp[1]);
            
            switchImage($active, $to);
        }
    });
    
    $(".clearInput").focus(function(srcc)
    {
        if ($(this).val() == this.defaultValue)
        {
            $(this).val("");
        }
    });
    
    $(".clearInput").blur(function()
    {
        if ($(this).val() == "")
        {
            $(this).val(this.defaultValue);
        }
    });
    
    $(".clearInput").blur();
});

function switchImage($from, $to)
{
    if ($to == 0) // To is volgende
    {
        $to = $from + 1;
        if ($to > $('#buttons > li').length)
        {
            $to = 1;
        }
    }
    
    $('#image_' + $from).fadeOut('750');
    $('#image_' + $to).fadeIn('750');
    
    $('#button_' + $from).removeClass('active');
    $('#button_' + $to).addClass('active');
    
    $timer = setTimeout(function()
    {
        switchImage($to, 0);
    }, 5000);
}

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