var defaultManualSpeed = 500;
var defaultAutoSpeed = 15000;
var defaultAutoSpeedEdge = 7500; // optimal - half of defaultAutoSpeed

function goToPoint(point, speed, gearbox, way) {
	if (!speed) speed = defaultManualSpeed;
	if (gearbox != 'auto') showIntroPart(point);
	var pix = itemsCenters[point] + 5;

	if (itemsCount == point + 1) {
		if (way == 'throughEdge') {
			if (gearbox == 'auto') {
				jQuery('.orbit-slider .dark').animate({
					backgroundPosition: timelineMargin + 'px bottom'
				}, defaultAutoSpeedEdge, function() {
					jQuery('.orbit-slider .dark').css({ backgroundPosition: '1175px bottom' });
					jQuery('.orbit-slider .dark').animate({
						backgroundPosition: pix + 'px bottom'
					}, defaultAutoSpeedEdge, function() {
						jQuery('.orbit-slider .timeline table tr.items td a').removeClass('active').filter('[rel='+ point +']').addClass('active');
						if (gearbox == 'auto') showIntroPart(point);
					});
				});

			} else {
				jQuery('.orbit-slider .dark').animate({
					backgroundPosition: timelineMargin + 'px bottom'
				}, 250, function() {
					jQuery('.orbit-slider .dark').css({ backgroundPosition: '1175px bottom' });
					jQuery('.orbit-slider .dark').animate({
						backgroundPosition: pix + 'px bottom'
					}, 250, function() {
						jQuery('.orbit-slider .timeline table tr.items td a').removeClass('active').filter('[rel='+ point +']').addClass('active');
						if (gearbox == 'auto') showIntroPart(point);
					});
				});

			}

		} else {
			jQuery('.orbit-slider .dark').animate({
				backgroundPosition: pix + 'px bottom'
			}, speed, function() {
				jQuery('.orbit-slider .timeline table tr.items td a').removeClass('active').filter('[rel='+ point +']').addClass('active');
				if (gearbox == 'auto') showIntroPart(point);
			});
		}
	} else if (0 == point) {
		if (way == 'throughEdge') {
			if (gearbox == 'auto') {
				jQuery('.orbit-slider .dark').animate({
					backgroundPosition: timelineMargin + sliderWidth + 'px bottom'
				}, defaultAutoSpeedEdge, function() {
					jQuery('.orbit-slider .dark').css({ backgroundPosition: timelineMargin + 'px bottom' });
					jQuery('.orbit-slider .dark').animate({
						backgroundPosition: pix + 'px bottom'
					}, defaultAutoSpeedEdge, function() {
						jQuery('.orbit-slider .timeline table tr.items td a').removeClass('active').filter('[rel='+ point +']').addClass('active');
						if (gearbox == 'auto') showIntroPart(point);
						jQuery('.orbit-slider ul.nav-buttons li.pause').hide().parent().parent().find('.play').show();
					});
				});

			} else {
				jQuery('.orbit-slider .dark').animate({
					backgroundPosition: timelineMargin + sliderWidth + 'px bottom'
				}, 250, function() {
					jQuery('.orbit-slider .dark').css({ backgroundPosition: timelineMargin + 'px bottom' });
					jQuery('.orbit-slider .dark').animate({
						backgroundPosition: pix + 'px bottom'
					}, 250, function() {
						jQuery('.orbit-slider .timeline table tr.items td a').removeClass('active').filter('[rel='+ point +']').addClass('active');
						if (gearbox == 'auto') showIntroPart(point);
					});
				});

			}
		} else {
			jQuery('.orbit-slider .dark').animate({
				backgroundPosition: pix + 'px bottom'
			}, speed, function() {
				jQuery('.orbit-slider .timeline table tr.items td a').removeClass('active').filter('[rel='+ point +']').addClass('active');
				if (gearbox == 'auto') showIntroPart(point);
			});
		}
	} else {
		jQuery('.orbit-slider .dark').animate({
			backgroundPosition: pix + 'px bottom'
		}, speed, function() {
			jQuery('.orbit-slider .timeline table tr.items td a').removeClass('active').filter('[rel='+ point +']').addClass('active');
			if (gearbox == 'auto') showIntroPart(point);
		});
	}
}

function introAutoPlay() {
	jQuery('.orbit-slider ul.nav-buttons li.play').hide().parent().parent().find('.pause').show();
	var activePointNo = 0;
	if (jQuery('.orbit-slider .timeline table tr.items td a.active').size() > 0) {
		var activePointNo = parseInt(jQuery('.orbit-slider .timeline table tr.items td a.active').attr('rel'));
	}
	jQuery('.orbit-slider .dark').css({ backgroundPosition: itemsCenters[activePointNo] + 'px bottom' });
	for (i = activePointNo + 1; i < itemsCount; i++) {
		goToPoint(i, defaultAutoSpeed, 'auto');
	}
	goToPoint(0, defaultAutoSpeed, 'auto', 'throughEdge');
}

function introStop() {
	jQuery('.orbit-slider .dark').stop(function() { jQuery('.orbit-slider .dark').stop(); });
	jQuery('.orbit-slider ul.nav-buttons li.pause').hide().parent().parent().find('.play').show();
}

function showIntroPart(part) {
	//if ( $.browser.msie )
	//{
		jQuery('.introPart').hide();
		jQuery('#intro' + part).show();
	//}
	/*else
	{
		jQuery('.introPart').fadeOut(defaultManualSpeed);
		jQuery('#intro' + part).fadeIn(defaultManualSpeed);
	}*/
}

jQuery(document).ready(function() {
	jQuery('.orbit-slider ul.nav-buttons li.play a').click(function() {
		introAutoPlay();
		return false;
	});
	
	jQuery('.orbit-slider ul.nav-buttons li.pause a').click(function() {
		introStop();
		return false;
	});

	jQuery('.orbit-slider ul.nav-buttons li.prev a').click(function() {
		introStop();
		var activePointNo = jQuery('.orbit-slider .timeline table tr.items td a.active').attr('rel');
		switch (activePointNo) {
			case '0':
				goToPoint(itemsCount - 1, defaultManualSpeed, '', 'throughEdge');
				break;
			default:
				goToPoint(activePointNo - 1);
				break;
		}
		return false;
	});
	
	jQuery('.orbit-slider ul.nav-buttons li.next a').click(function() {
		introStop();
		var activePointNo = parseInt(jQuery('.orbit-slider .timeline table tr.items td a.active').attr('rel'));
		switch (activePointNo) {
			case itemsCount - 1:
				goToPoint(0, defaultManualSpeed, '', 'throughEdge');
				break;
			default:
				goToPoint(activePointNo + 1);
				break;
		}
		return false;
	});

	jQuery('.orbit-slider .timeline table tr.items td a').click(function() {
		if (!jQuery(this).hasClass('active')) {
			introStop();
			goToPoint(jQuery(this).attr('rel'));
		}
		return false;
	});

	var items = jQuery('.orbit-slider .timeline table tr.line td');
	var width = 0;
	var i = 0;
	var lastPoint = 0;
	itemsCount = items.size();
	items.each(function () {
		width = parseFloat(jQuery(this).width());
		itemsCenters[i] = timelineMargin + lastPoint + parseFloat(width/2);
		lastPoint += width;
		i++;
	});
	sliderWidth = lastPoint;
	jQuery('.orbit-slider .dark').css({ backgroundPosition: timelineMargin + 'px bottom' });
});

var itemsCount = 0;
var timelineMargin = -1175;
var sliderWidth = 852;
var itemsCenters = new Array();

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

addLoadEvent(function() {
	//introAutoPlay();
});
