$.fn.zcarousel2 = function(options){

	var options = $.extend({}, $.fn.zcarousel2.defaults, options);
	return this.each(function(){

		obj = $(this).find('.items');

		var width_space = parseFloat(obj.children('li:first').css('margin-right').split('px')[0]);
		var width_element = obj.children('li:first').outerWidth();
		var width_arrow_left = obj.prev().outerWidth() + 16;
		var width_arrow_right = 32;
		
		obj.data('options', options)

		// breedte van ul
		var last_item = obj.children('li:last').index();
		obj.width((last_item + 1) * (width_space + width_element));

		obj.parent().find('.block').width((last_item + 1) * (width_space + width_element)-34);

		obj.next().children().click(function(){
			if($(this).is('.disabled')){
				return false;
			}
			var el = $(this);
			var obj = el.parent().prev();
			var e = elementcount(width_element, width_space, width_arrow_left, width_arrow_right, ($(window).width() - options.minuswidth))
/* 			debug.log(width_element + ' ' + width_space + ' ' + width_arrow_left + ' ' + width_arrow_right + ' ' + $(window).width() + ' ' + e) */
			
			var index = obj.children('li:not(.hidden):last').index()
			var type = obj.parent().attr('id');
			el.addClass('disabled')
			$.ajax({
				type: 'get',
				url: '/site/ajax/amount_left.php',
				data: 'offset='+index+'&type='+type,
				success: function(result){
					if(!options.ajax){
						result = obj.children(':hidden').length;
					}
					if(result <= e){
						e = parseFloat(result);
						el.addClass('disabled');
					} else {
						el.removeClass('disabled')
					}
										
					var width_visible = e * (width_element + width_space);
					
					var first = obj.children('li:visible:last').index() + 1;
					var last = obj.children('li:visible:last').index() + e + 1;
		
					obj.prev().children().removeClass('disabled')
					for(first; first<last; first++){
						if(obj.children('li').eq(first).length){
							obj.children('li').eq(first).show().removeClass('hidden');
						} else {
							if(!obj.data('items_left') || obj.data('items_left') > 0 ){
								obj.width(obj.width() + (width_space + width_element))
								obj.append('<li class="loading"><div class="top date"></div><div class="preloader"></div><div class="bot"></div></li>')
								obj.children('li:last').trigger('new')						
							}
						}
					}
					obj.animate({left: '-='+width_visible}, 400, function(){
						var first_old = obj.children('li:not(.hidden):first').index();
						var last_old = first_old + e;
						for(first_old; first_old<last_old; first_old++){
							obj.children('li').eq(first_old).addClass('hidden');
						}
					})
				}				
			})

			return false;
		})
		
		// show previous
		obj.prev().children().click(function(){
			if($(this).is('.disabled')){
				return true;
			}
			var el = $(this);
			var obj = $(this).parent().next();
			var e = elementcount(width_element, width_space, width_arrow_left, width_arrow_right, ($(window).width() - options.minuswidth))
			var amount_on_left_side = obj.children(':not(.hidden):first').index()
			el.addClass('disabled')
			if(amount_on_left_side < e){
				amount_to_move = amount_on_left_side;
			} else {
				amount_to_move = e;
			}

			var width_visible = amount_to_move * (width_element + width_space);
			
			obj.animate({left: '+='+width_visible}, 400, function(){
				if(obj.css('left') != '0px'){
					el.removeClass('disabled')
				} 

				obj.next().children().removeClass('disabled');
				
				var new_amount_items = obj.children('li:not(.hidden):first').index();
				var i = new_amount_items - amount_to_move;
				for(i; i<new_amount_items; i++){
					obj.children('li').eq(i).removeClass('hidden');
				}
			
				var i = obj.children('li:not(.hidden):first').index() + e;
				var old_amount_items = i + amount_to_move;
				for(i; i<old_amount_items; i++){
					obj.children('li').eq(i).addClass('hidden').hide();
				}
			})

		})

		// laden van content van toegevoegde items
		obj.find('li').live('new', function(){
			var el = $(this);
			var obj = el.parent();
			var index = $(this).index()
			$.ajax({
				type: 'get',
				url: obj.next().children().attr('href'),
				data: 'offset='+index+'&ajax=true',
				dataType: 'json',
				success: function(result){
					if(result.success == 'true'){
						el.removeClass('loading')
						el.html(result.article)
						el.addClass(result.classes)
						if(result.items_left <= 0){
							obj.next().addClass('disabled')
						}						
					} else {
						el.remove();
					}
				}
			})
		})
		$.fn.zcarousel2.recalculate($(this), 'init')
	})
}

$.fn.zcarousel2.recalculate = function(obj, init){

		
	obj.each(function(){
		obj = $(this).find('.items')
		var options = obj.data('options');

		var width_space = parseFloat(obj.children('li:first').css('margin-right').split('px')[0]);
		
		
		var width_element = obj.children('li:first').outerWidth();
		
		var width_arrow_left = obj.prev().outerWidth() + 16;
		
		var width_arrow_right = 32;
		if($(window).width() < 960){
			var window_width = 960;
		} else {
			var window_width = $(window).width();
		}

		var e = elementcount(width_element, width_space, width_arrow_left, width_arrow_right, (window_width - options.minuswidth)) + obj.children('li:not(.hidden):first').index();
/* 		debug.log(width_element + ' ' + width_space + ' ' + width_arrow_left + ' ' + width_arrow_right + ' ' + window_width + ' ' + e + ' ' + options.minuswidth + ' ' + obj.children('li:not(.hidden):first').index()) */
		var last_item = obj.children('li:not(.hidden):last').index() + 1;

		// if loaded items is less then needed items disable next/prev
		if(e >= obj.children('li').length && init){
			obj.siblings('.art_nav').children().addClass('disabled')
		} 
		
		// breedte van de container
		obj.parent().width((e - obj.children('li:not(.hidden):first').index()) * (width_space + width_element) + width_arrow_left)
	//alert((e - obj.children('li:not(.hidden):first').index()) * (width_space + width_element) + width_arrow_left);
		if(last_item > e){
			// remove extra
			obj.children('li').each(function(index){
				if(index >= e) {
					$(this).hide().addClass('hidden');
				}
			})
		} else if(last_item < e) {
			// get extra
			if(obj.find('li:hidden:first').length){
				obj.find('li:hidden:first').show().removeClass('hidden');				
			} else {
				if(!obj.next().children().is('.disabled') && options.ajax){
					var index = obj.children('li:not(.hidden):last').index()
					var type = obj.parent().attr('id');
					$.ajax({
						type: 'get',
						url: '/site/ajax/amount_left.php',
						data: 'offset='+index+'&type='+type,
						success: function(result){
							obj = $('#' + type + ' .items');
							if(result <= 1){
								obj.next().children().addClass('disabled')
							}
						}				
					})

					obj.width(obj.width() + (width_space + width_element))
					obj.append('<li class="loading"><div class="preloader"></div></li>')
					obj.children('li:last').trigger('new')					
				}
			}
		}

		// when next is disabled while hiding an item on resize, enable next again
		if(obj.children(':hidden').length){
			obj.next().children().removeClass('disabled')
		}
		
		//position right arrow
		obj.next().css({left: (obj.find('li:visible:last').position().left - obj.find('li:not(.hidden):visible:first').position().left + width_element - width_space + width_arrow_left) - 16})
/*
		debug.log(obj.find('li:visible:first').position().left)
		debug.log(obj.find('li:visible:last').position().left + ' ' + width_element + ' ' + width_space + ' ' + width_arrow_left)
*/
	})

}

$.fn.zcarousel2.defaults = {
	minuswidth : 0,
	ajax: true
};

function elementcount(element,space,arrowleft,arrowright, maxwidth) {
	width = maxwidth - arrowleft - arrowright;
	x = 0;
	i = 0;
	while(x<width) {
		i++;
		x=(i*element)+((i-1)*space)	
	}
	return i-1;	
}
