var jcarousel_itemLoadCallback = null;

(function($) {

	$(document).ready(function () {
			$('#photo-carousel,.photo-carousel').jcarousel2();
	});

	$.fn.jcarousel2 = function(o) {
		// return original object to support chaining
		return this.each(function(o) {

			var $container = $(this);

			// Плагину нужен вложенный контейнер <ul/>
			if($container.find("ul").length==0){$container.append("<ul/>")}

	    var defaults = {
				//itemVisibleOutCallback: {onAfterAnimation: function(carousel, item, i, state, evt) { carousel.remove(i); }},
				//size: 88,
				//size: 10,
				//wrap: 'circular',
				//vertical: true,
				//start: 1,
				//itemLoadCallback: jcarousel_itemLoadCallback
				//,scroll: 4
				mod: '',
				animation: 'fast', //normal, fast
				initCallback: initCallback,
				itemLoadCallback: itemLoadCallback
			}

			var opts = $.extend({}, defaults, o || {});
			opts = $.extend({}, opts, $container.metadata({ type: "attr", name: "photo-carousel" }) || {});

			console.info(opts);

			function initCallback(carousel) {
				// Enable mousewheel ...
				if($.fn.mousewheel) // Check if jQuery plugin is loaded
					$container
						.mousewheel(function(event, delta) {
						if (delta < 0) {
							carousel.next();
						}
						else if (delta > 0) {
							carousel.prev();
						}
						return false; // prevent default
					});

				// Enable swiping ...
				if($.fn.swipe) // Check if jQuery plugin is loaded
					$container
						.swipe({
							threshold: 10,
							allowPageScroll: "none",
							swipeStatus: function(event, phase, direction, distance) {
								if (phase=="end") {
									if (direction == "left") {
										carousel.next(Math.round(distance/100));
									}
									else if (direction == "right") {
										carousel.prev(Math.round(distance/100));
									}
								}
							}
						});
			}





			function itemLoadCallback(carousel, state) {

				var first = carousel.first;
				var last  = carousel.last;

				if (first < 1) first = 1;

				// Check if "first" is out of range if the size was already set
				var size = carousel.size();
				if (carousel.options.wrap != 'circular' && size && first > size) return;

				// Check if the requested items already exist
				if (carousel.has(first, last)) return;

				carousel.lock();
				jQuery.get(
					'/photo/jcarousel_ajax.asp',
					{
						div_id: opts.div_id,
						state: state,
						first: first,
						last: last,
						mod: opts.mod,
						ruser_id: opts.ruser_id
					},
					function(xml) {
						console.info(xml);
						if (state == 'init') {
							var data = $('data', xml);
							var size = $(data).attr('items');
							if(size>0) carousel.size(size - 1);
						}
						$('item', xml).each(function(i) {carousel.add(first + i, getItemHTML($(this)));});
						carousel.unlock();
					}, 'xml'
				);
			};



			// Call native jcarousel /jscript/jquery.jcarousel.js
			$container.jcarousel(opts);


			/**
			 * Item html creation helper.
			 */
			function getItemHTML(item) {
				if (opts.mod == 'blog') {
					return '<a href="/white/photo_show2.asp?id=' + item.attr("id") + '" title="' + (item.attr("title") ? item.attr("title") : "")  + '" style="background-image:url('+ item.attr("src") + ')"></a>';
				}	else {
					return '<a href="/photo/show2.asp?id=' + item.attr("id") + '" title="' + (item.attr("title") ? item.attr("title") : "")  + '" style="background-image:url('+ item.attr("src") + ')"></a>';
				}

			};

			function itemRemoveCallback(carousel, first, last) {
				if (!first || !last) return;
				for (var i = first; i <= last; i++)	carousel.remove(i);
			};

		});
	};

})(jQuery);
