/**
 * SlideShow Class
 */

var loop = null;
var counter = 0;
var speed = 5000;

var im = new SlideShow();
function SlideShow() {
	var _l0 = this;
	$(document).ready(function() {
		_l0.build();
	});
};
			
SlideShow.prototype.build = function() {
	var _l0 = this;
	// SlideShow
	_l0.is_playing = true;
	_l0.slideshow_first_entry = true;
	_l0.is_mobile=false;
	
	jQuery.extend(jQuery.browser, {SafariMobile : navigator.userAgent.toLowerCase().match(/iP(hone|ad)/i) });
	if($.browser.SafariMobile){
		_l0.is_mobile = true;
	}
	
	$('#slideshow', _l0.doc).carousel('.first-item', '.last-item', function() { }, function() { });
	if(_l0.is_mobile == false) {
		loop = setInterval(
			function() {
				$('.last-item', _l0.doc).trigger('click');
				$('.slideshow-indicator .item').attr('class', 'item');
				if ($('#item_' + (counter+1)).length > 0) {
					counter++;
				} else {
					counter = 0;
				}
				$('#item_' + counter).attr('class', 'item current');
			},
		speed);
	}
	//
	$('.last-item').mouseup(function() {
		$('.slideshow-indicator .item').attr('class', 'item');
		if ($('#item_' + (counter+1)).length > 0) {
			counter++;
		} else {
			counter = 0;
		}
		$('#item_' + counter).attr('class', 'item current');
		if(_l0.is_mobile == false) {
			clearInterval(loop);
			loop = null;
			loop = setInterval(
				function() {
					$('.last-item', _l0.doc).trigger('click');
					$('.slideshow-indicator .item').attr('class', 'item');
					if ($('#item_' + (counter+1)).length > 0) {
						counter++;
					} else {
						counter = 0;
					}
					$('#item_' + counter).attr('class', 'item current');
				},
			speed);
		}
	});
	//
	$('.first-item').mouseup(function() {
		$('.slideshow-indicator .item').attr('class', 'item');
		if ($('#item_' + (counter-1)).length > 0) {
			counter--;
		} else {
			counter = max_num_images-1;
		}
		$('#item_' + counter).attr('class', 'item current');
		if(_l0.is_mobile == false) {
			clearInterval(loop);
			loop = null;
			loop = setInterval(
				function() {
					$('.last-item', _l0.doc).trigger('click');
					$('.slideshow-indicator .item').attr('class', 'item');
					if ($('#item_' + (counter+1)).length > 0) {
						counter++;
					} else {
						counter = 0;
					}
					$('#item_' + counter).attr('class', 'item current');
				},
			speed);
		}
	});
		
	//
	if(_l0.is_mobile == true) {		
		var swipeOptions = {
			swipeStatus: function (event, phase, direction, distance) {
				if (phase == "end") {
					if (direction == "left") {
						$('.last-item', _l0.doc).trigger('click');
						$('.slideshow-indicator .item').attr('class', 'item');
						if ($('#item_' + (counter+1)).length > 0) {
							counter++;
						} else {
							counter = 0;
						}
						$('#item_' + counter).attr('class', 'item current');
					} else
					if (direction == "right") {
						$('.first-item', _l0.doc).trigger('click');
						$('.slideshow-indicator .item').attr('class', 'item');
						if ($('#item_' + (counter-1)).length > 0) {
							counter--;
						} else {
							counter = max_num_images-1;
						}
						$('#item_' + counter).attr('class', 'item current');
					}
				}
			},
			triggerOnTouchEnd:false,
			threshold:50
		}
		$("#slideshow").swipe( swipeOptions );
		/*$('#slideshow', _l0.doc).swipe({
			swipeLeft: function() {
				$('.last-item', _l0.doc).trigger('click');
				$('.slideshow-indicator .item').attr('class', 'item');
				if ($('#item_' + (counter+1)).length > 0) {
					counter++;
				} else {
					counter = 0;
				}
				$('#item_' + counter).attr('class', 'item current');
			},
			swipeRight: function() {
				$('.first-item', _l0.doc).trigger('click');
				$('.slideshow-indicator .item').attr('class', 'item');
				if ($('#item_' + (counter-1)).length > 0) {
					counter--;
				} else {
					counter = max_num_images-1;
				}
				$('#item_' + counter).attr('class', 'item current');
			}
		});*/
	}
	//
	var position = 1;
	$('.slideshow-indicator .item').each(function() {
		$(this).data('position', position);
		$('a', this).css('cursor', 'pointer');
		position++;
	});
};
