function Carousel(type, imgWidth, imgHeight, intervalScroll, moduleId, module, contentId, additional)
{	
    //constructing
    this.type = type;
    this.intervalScroll = intervalScroll;
    this.moduleId = moduleId;
    this.module = module;
    this.contentId = contentId;
    this.additional = additional;
    this.imgWidth = imgWidth;
    this.imgHeight = imgHeight;
    
    //standaard settings
    this.ajaxScript = '/module/carousel/CarouselExternal.php';
    this.sizeFrame = 2;
    //this.animateSnapTime = 500;
    
    //dynamische waarden
    this.car;
    this.buttons = false;
    this.autoScroll = false;

    //initialisatie
    this.initCallback = function(carousel)
    {        
        this.car = carousel;
        
        //Scrolling uit als je met je muis erover heen gaat
        carousel.clip.hover(function() 
        {
            carousel.stopAuto();
        }, function() 
        {
            carousel.startAuto();
        });
    }
	
    this.itemLoadCallback = function(carousel, state)
    {    	
    	//Bestaan de items al (caching)
        if (carousel.has(carousel.first, carousel.last) || this.type == 'default') 
        {  
            // stop alle videos
        	jQuery('.videoPlayer').each(function()
    		{
            	this.sendEvent("STOP", true);
    		});
        	
        	return;
        }
        
        //de AJAX call
        jQuery.ajax({
            url: this.ajaxScript,
            data:
            {
                type: this.type,
                moduleid: this.moduleId,
                module: this.module,
                contentid: this.contentId,
                additional: this.additional
            },
            dataType: 'json',
            type: 'POST',
            success: function(json) 
            {
                self.itemAddCallback(carousel, type, json);
            }}
        );
    }

    itemAddCallback = function(carousel, type, json)
    {
        var optionValue;
        var visibleCount = 0;
        var optionVisible;

        // Controleer welke jaren er beschikbaar zijn. Degenen die niet beschikbaar
        // zijn gaan we verbergen.
        jQuery('#jcarousel-filter-year option[value!="0"]').each
        (
            function()
            {
                optionValue = jQuery(this).val();

                if (typeof json['yearsavailable'] != 'undefined' && typeof json['yearsavailable'][optionValue] != 'undefined')
                {
                    visibleCount++;
                    optionVisible = jQuery(this);
                    jQuery(this).show();
                }
                else
                {
                    jQuery(this).hide();
                }
            }
        );

        // In het geval dat er maar een enkel jaar beschikbaar is gaan we die
        // standaard selecteren.
        if (visibleCount == 1)
        {
            jQuery(optionVisible).attr('selected', 'selected');
        }

    	if (json['content'] != null)
    	{
            //reset carousel
            carousel.reset();

            //Zet de grootte aan aantal items
            carousel.size(json['content'].length);

            for (var iData = 0; iData < json['content'].length; iData++)
            {
                //voeg items toe aan carousel
                carousel.add(carousel.first + iData, json['content'][iData]);
            }
            
            if (json['content'].length <= 1)
            {
                $(carousel.buttonNext).attr('disabled', 'true');
                $(carousel.buttonNext).addClass('jcarousel-next-disabled');
                $(carousel.buttonNext).addClass('jcarousel-next-disabled-horizontal');
                carousel.stopAuto();
                //jQuery("#listcarousel").draggable( "option", "disabled", true );
            }
    	}
    }
    
    /* Speciale filter voor landen */
    this.filter = function(carousel, group, year, country)
    {
     	//de AJAX call
        jQuery.ajax({
            url: this.ajaxScript,
            data:
            {
                type: this.type,
                moduleid: this.moduleId,
                module: this.module,
                contentid: this.contentId,
                groups: group,
                year: year
            },
            dataType: 'json',
            type: 'POST',
            success: function(json) 
            {
                self.itemAddCallback(carousel, type, json);
            }}
        );
    }
    
    /* ---DRAGGING PURPOSES---
    this.getWidth = function()
    {
    	var width;
    	
        var aantal = Math.floor(this.car.options.size / this.car.options.scroll); //aantal slides
        var aantalImg = aantal * this.car.options.scroll; //totaal aantal nog te tonen plaatjes
    	var resImg = this.car.options.size - aantalImg; //verschil 
    	var scrollImg = (aantal - 1) * this.car.options.scroll;
        
    	width = ((scrollImg + resImg) * (this.imgWidth - this.sizeFrame)) + ((scrollImg + resImg) * this.sizeFrame);
    	
    	return width;

    }
    
    this.getLastSnapItems = function()
    {
    	var size = this.car.options.size;
    	var scroll = this.car.options.scroll;
    	
    	var aantal = Math.floor(size / scroll);
    	var res = size - (aantal * scroll);
    	
    	return res;
    }
    
    this.getSnapPoints = function()
    {
    	var snapPoints = Array();
    	var width = this.getWidth();
    	var totalImg = this.car.options.size;
    	var aantalImg = this.car.options.scroll;
    	var aantal = Math.floor(totalImg / aantalImg);
    	var resImg = totalImg - (aantal * aantalImg);
    	
    	var snapPoint = 0;

    	for(var i=0; i < aantal; i++)
    	{
    		if (i == 0)
    		{
    			snapPoints[i] = 0;
    		}
    		else
    		{
    			snapPoint = snapPoint + ((aantalImg * (this.imgWidth - this.sizeFrame)) + (aantalImg * this.sizeFrame));
    			
    			snapPoints[i] = snapPoint;
    		}
    	}
    	
    	snapPoints[i++] = snapPoint + ((resImg * (this.imgWidth - this.sizeFrame)) + (resImg * this.sizeFrame));

    	return snapPoints;
    }
    
    this.getClosestSnappoint = function(position)
    {
    	var snapPoints = this.getSnapPoints();
    	var verschil;
    	var rverschil;
    	var verschillen = Array();
    	var results = [];

    	for(var i=0; i < snapPoints.length; i++)
    	{
    		verschil = position + snapPoints[i]; //positie is altijd negatief of 0
    		
    		if (verschil < 0)
    		{
    			rverschil = 0 - verschil;
    		}
    		else
    		{
    			rverschil = 0 + verschil;
    		}
    		
    		verschillen[verschillen.length] = rverschil;

    		if (snapPoints[i] > 0)
    		{
    			results["verschil" + rverschil.toString()] = parseInt('-' + snapPoints[i]); //string key 
    		}
    		else
    		{
    			results["verschil" + rverschil.toString()] = snapPoints[i]; //string key
    		}
    	}
    	
    	verschillen.sort(sortNumber);
    	
    	return results['verschil' + verschillen[0].toString()];
    }
    
    this.snapToSnappoint = function(snappoint)
    {    	
    	var snapPoints = this.getSnapPoints();
    	
        //Animatie
        $("#listcarousel").animate({
            left: snappoint
        }, this.animateSnapTime);
        
    	if (0 > snappoint)
    	{
    		snappoint = snappoint.toString();
    		snappoint = parseInt(snappoint.substr(1));
    	}
    	
    	var snap = jQuery.inArray(snappoint, snapPoints);

    	if (snap >= 0)
    	{    		
    		if (snap > 0 && ((snap + 1) < snapPoints.length))
    		{
    			snap = 1 + (this.car.options.scroll * snap);
    			
    			this.car.buttonPrev.attr('disabled', 'false');
                
				prevFistSnap = snap - ((this.car.options.scroll * 2) - 1);
				prevLastSnap = snap - ((this.car.options.scroll) - 1);
    		}
    		else
    		{
    			snap = 1 + (this.car.options.scroll * (snap - 1)) + this.getLastSnapItems();
    			
				prevFistSnap = (snap - this.getLastSnapItems()) - (this.car.options.scroll + 1);
				prevLastSnap = (snap - this.getLastSnapItems()) - 1;
    			
    	        $(this.car.buttonPrev).attr('disabled', 'false');

    	        //voor laatste item standaard buttons uitzetten tenzij rotatie aanstaat
    		}
            
    		this.car.first = snap;
    		this.car.last = snap;
            
            if (snap == 0)
            {
                this.animatePijlers(1);
            }
            else
            {
                this.animatePijlers(snap);
            }
            
    		this.car.prevFirst = prevFistSnap;
    		this.car.prevLast = prevLastSnap;
    	}
    	else
    	{
    		this.car.first = 0;
    		this.car.last = 1;

            this.animatePijlers(1);
            
    		$(this.car.buttonPrev).attr('disabled', 'true');
    		$(this.car.buttonPrev).attr('disabled', 'false');
    	}
        
        this.car.buttons();
    }
    
    this.itemStopDragCallback = function(ev,ui)
    {            	
    	snappoint = this.getClosestSnappoint(ui.position.left);

        this.snapToSnappoint(snappoint);
    }
    
    this.itemDragging = function(ev,ui)
    {        
		var maxPos = this.getWidth();

    	if (ui.position.left > 0) ui.position.left = 0;
		if (ui.position.left < -maxPos) ui.position.left = -maxPos;
    }*/
}

//filtering
jQuery(document).ready(function()
{
	jQuery('#jcarousel-filter-group').change(function()
	{		
		var group = jQuery(this).val();
                jQuery('#jcarousel-filter-year option[value="0"]').attr('selected', 'selected');
		
		photocarousel.filter(photocarousel.car, group);
	});
	
	jQuery('#jcarousel-filter-year').change(function()
	{
		var year = jQuery(this).val();
		var group = jQuery('#jcarousel-filter-group').val();
		
		photocarousel.filter(photocarousel.car, group, year);
	});
	
	jQuery("#content .jcarousel-next")
		.animate( { opacity: '0'}, 0)
		.animate( { opacity: '1'}, 2000)
		.delay(5000)
		.animate( { opacity: '0'}, 2000);
	
	jQuery("#content .jcarousel-prev")
		.animate( { opacity: '0'}, 0)
		.animate( { opacity: '1'}, 2000)
		.delay(5000)
		.animate( { opacity: '0'}, 2000);
	
	$('#content #jcarousel-container-album').mouseover(function() {
	  $('#content .jcarousel-next').stop().animate( { opacity: '1'}, 500);
	  $('#content .jcarousel-prev').stop().animate( { opacity: '1'}, 500);
	});
	$('#content #jcarousel-container-album').mouseout(function() {
	  $('#content .jcarousel-next').stop().animate( { opacity: '0'}, 1000);
	  $('#content .jcarousel-prev').stop().animate( { opacity: '0'}, 1000);
	});

    $('ul#photolistcarousel li.jcarousel-item div.options a').click(function(){
     	//de AJAX call
        jQuery.ajax({
            url: $(this).attr('href'),
            type: 'POST',
            context: $(this),
            success: function() 
            {
                $(this).parents("div.options").find("span").toggleClass("hide");
                //$(this).addClass("hide");
            }
        });
        return false;
    });

});

//Custom nummer sortering
function sortNumber(a,b)
{
	return a - b;
}
