
$(window).load(function() {
		var contentContainer = $('#content.home');
		if(contentContainer.length && !($.browser.msie && $.browser.version.substr(0,1)<7)) {
			var spinBox = $('#homeSpinBox'), controller = $('#homeSpinControl');
		
			controller.detach().css({
				'top':208,
				'left':12,
				'width':24
			}).appendTo('body');
			contentContainer.css({
				'position':'fixed',
				'top':'0px',
				'height':'100%',
				'overflow':'hidden'
			});
			spinBox.removeClass('noscript')
			
			if(!jQuery.browser.msie) {
				controller.find('a img').css({'opacity':0.0, 'visibility':'visible'});
			}
			spinBox.find('a img').css({'opacity':0.0, 'visibility':'visible'});
			
			var currentElement = spinBox.find('a:first'), nextElement = currentElement.next(), oldElement;
			
			var height = currentElement.height() -148;
			spinBox.css('marginTop', (-height)+'px');
			nextElement.children().fadeTo(400, 1.0);
			
			var nextController = $('#'+nextElement.attr('id').replace('Box', 'Clt')).children();
			if (jQuery.browser.msie) {
				nextController.css('visibility', 'visible');
			}
			else {
				nextController.fadeTo(400, 1.0);
			}
			controller.append('<div id="homenavSelected"></div>');
			var arrow = $('#homenavSelected'), arrowHalfHeight = arrow.height()/2;
			arrow.css('top', nextController.position().top + (nextController.height()/2) - arrowHalfHeight);
			numberOfEntries = spinBox.children('a').length;
			
			var timeout, timeStep = 4500, timeUser = 10000, doingNext = false, doingGoTo = false;
			
			var next = function(callback) {
				if(doingNext) return;
				doingNext = true;
				
				oldElement = currentElement;
				currentElement = nextElement;
				nextElement = currentElement.next();
				
				var oldController = $('#' + currentElement.attr('id').replace('Box', 'Clt')).children();
				if (jQuery.browser.msie) {
					oldController.css('visibility','hidden');
				}
				else {
					oldController.fadeTo(500, 0.0);
				}
				currentElement.children().fadeTo(500, 0.0, function() {
					var nextController = $('#'+nextElement.attr('id').replace('Box', 'Clt')).children();
					arrow.animate({'top':nextController.position().top + (nextController.height()/2) - arrowHalfHeight}, 500);
					spinBox.animate({marginTop: '-='+currentElement.height()+'px'}, function() {
						
						nextElement.children().fadeTo(500, 1.0);
						if (jQuery.browser.msie) {
							nextController.css('visibility', 'visible');
						}
						else {
							nextController.fadeTo(500, 1.0);
						}
						
						spinBox.css('marginTop', function(index, value){
							return (parseInt(value)+ oldElement.height())+'px';
						});
						oldElement.detach().appendTo(spinBox);

						if (typeof callback == 'function') {
							callback();
						}
						else {
							timeout = setTimeout(next, timeStep);
						}
						doingNext = false;
					});
				});
			};
			
			var goTo = function(number) {
				clearTimeout(timeout);
				var target = $('#cSBox'+number);
				var steps = target.prevAll().length - 1;
				if(steps == 0) {
					timeout = setTimeout(next, timeUser);
					doingGoTo = false;
				}
				else if(steps == 1) {
					next(function() {
						timeout = setTimeout(next, timeUser);
						doingGoTo = false;
					});
				}
				else if(steps == -1) {
					
					var skipElements = currentElement.nextAll(), targetPrev = spinBox.find('a:last'), targetNext = currentElement.next();
					var targetPrevClone = targetPrev.clone().attr('id', targetPrev.attr('id')+'clone').appendTo(spinBox);
					var targetClone = target.clone().attr('id', target.attr('id')+'clone').appendTo(spinBox);
					var targetNextClone = targetNext.clone().attr('id', targetNext.attr('id')+'clone').appendTo(spinBox);
					targetNextClone.children().css('opacity', 0);
					
					currentElement = nextElement;
					
					var oldController = $('#'+currentElement.attr('id').replace('Box', 'Clt')).children();
					if (jQuery.browser.msie) {
						oldController.css('visibility','hidden');
					}
					else {
						oldController.fadeTo(500, 0.0);
					}
				
					currentElement.children().fadeTo(500, 0.0, function() {
						var nextController = $('#'+target.attr('id').replace('Box', 'Clt')).children();
						arrow.animate({'top':nextController.position().top + (nextController.height()/2) - arrowHalfHeight}, 500);
						
						var distance = targetPrev.height();
						skipElements.each(function() {
							distance += $(this).height();
						});
						
						spinBox.animate({marginTop: '-='+distance+'px'}, function() {
							targetClone.children().fadeTo(500, 1.0);
							if (jQuery.browser.msie) {
								nextController.css('visibility', 'visible');
							}
							else {
								nextController.fadeTo(500, 1.0);
							}
							
							spinBox.css('marginTop', function(index, value){
								return (parseInt(value)+distance-targetPrev.height()+target.height())+'px';
							});
							skipElements.each(function() {
								$(this).detach().appendTo(spinBox);
							});
							targetNextClone.remove();
							targetPrev.remove();
							target.remove();
							targetPrevClone.attr('id', targetPrevClone.attr('id').replace('clone',''));
							targetClone.attr('id', targetClone.attr('id').replace('clone',''));
							
							currentElement = targetPrevClone;
							nextElement = targetClone;
							timeout = setTimeout(next, timeUser);
							
							doingGoTo = false;
						});
					});
					
					
				}
				else {
					currentElement = nextElement;
					var prevElements = currentElement.prevAll();
					oldElement = $('#cSBox'+(number > 1 ? number-1 : numberOfEntries));
					
					var skipElements = currentElement.add(currentElement.nextUntil(oldElement.selector));
					if(skipElements.length < (steps-1)) {
						skipElements = skipElements.add(spinBox.find('a:first').nextUntil(oldElement.selector));
					}
					
					var targetNext = target.next();
					if(!targetNext.length) targetNext = spinBox.find('a:first');
					
					var targetNextClone = targetNext.clone().attr('id', targetNext.attr('id')+'clone');
					target.after(targetNextClone);
					
					var oldController = $('#'+currentElement.attr('id').replace('Box', 'Clt')).children();
					if (jQuery.browser.msie) {
						oldController.css('visibility','hidden');
					}
					else {
						oldController.fadeTo(500, 0.0);
					}
					
					currentElement.children().fadeTo(500, 0.0, function() {
						var nextController = $('#'+target.attr('id').replace('Box', 'Clt')).children();
						arrow.animate({'top':nextController.position().top + (nextController.height()/2) - arrowHalfHeight}, 500);
						
						var distance = 0;
						skipElements.each(function() {
							distance += $(this).height();
						});
						
						spinBox.animate({marginTop: '-='+(distance+oldElement.height())+'px'}, function() {
							target.children().fadeTo(500, 1.0);
							if (jQuery.browser.msie) {
								nextController.css('visibility', 'visible');
							}
							else {
								nextController.fadeTo(500, 1.0);
							}
							
							prevElements.each(function() {
								distance += $(this).height();
							});
							spinBox.css('marginTop', function(index, value){
								return (parseInt(value)+distance)+'px';
							});
							skipElements.add(prevElements).each(function() {
								$(this).detach().appendTo(spinBox);
							});
							
							targetNextClone.remove();
							
							currentElement = target.prev();
							nextElement = target;
							
							timeout = setTimeout(next, timeUser);
							doingGoTo = false;
						});
					});
				}
			};
			
			controller.find('a').click(function(e){
				e.preventDefault();
				if(doingNext || doingGoTo) return;
				doingGoTo = true;
				goTo($(this).attr('id').replace('cSClt', ''));
			});
			//timeout = setTimeout(next, timeStep);
		}
});

