window.addEvent('domready', function() {
	
	// Swap add
	function ad_swap(target) {
		
		// Configuration
		var adSpots = Number(4);

		// If target is defined
		if(target && target > 0 && target <= adSpots) {
		
			// Get current spot & follower
			var currentRotationString = target.toString();
			var currentRotation = target;
			var currentAd = Number(4);
			if(currentRotation > 1) currentAd = currentRotation - 1;
			
			
			var currentAdID = Number($('homeSlide'+currentRotationString).get('alt'));
			
			// Determine next ad
			var nextRotation = currentRotation;
	
			// Update Link
			var nextLink = nextRotation.toString();
			var currentAdLink = $('homeLink'+nextLink).get('href');
			
			// Get next ad's ID
			var nextAd = Number($('homeSlide'+nextRotation).get('alt'));		
		
		} 
		
		// Else rotate in sequence
		else {
		
			// Get current spot & follower
			var currentRotationString = $('showLeft').get('alt');
			var currentRotation = Number(currentRotationString);
			var currentAd = Number(4);
			if(currentRotation > 1) currentAd = currentRotation - 1;
			
			
			var currentAdID = Number($('homeSlide'+currentRotationString).get('alt'));
			
			// Determine next ad
			var nextRotation = Number(1);
			if(currentRotation < adSpots) nextRotation = currentRotation + 1;
	
			// Update Link
			var nextLink = nextRotation.toString();
			var currentAdLink = $('homeLink'+nextLink).get('href');
			
			// Get next ad's ID
			var nextAd = Number($('homeSlide'+nextRotation).get('alt'));
		
		}

			
		// Begin transition
		
		if(!target || target == 0) {
			$('showcaseInner').tween('opacity', 1, 0);
		} else {
			$('showcaseInner').set('opacity', 0);
		}
		$('showLink').set('href', currentAdLink);
					
			// Move Tab
			$$('#showRight li').each(function(element) {
				element.set('class', 'showcaseOff');
			});
			$('homeSlide'+nextRotation).set('class', 'showcaseOn');				
			
			// If no target, execute crossfade
			if(target && target > 0 && target <= adSpots) {
				$('showLeft').setStyle('background-image', 'url(/data/bannersHome/main/'+nextAd+'.jpg)');
				$('showLeft').set('alt', nextRotation);
			} else {
				(function(){ }).delay(3000);
			
				$('showcaseInner').setStyle('background-image', 'url(/data/bannersHome/main/'+currentAdID+'.jpg)');
				$('showcaseInner').set('opacity', 1);
	
				$('showLeft').setStyle('background-image', 'url(/data/bannersHome/main/'+nextAd+'.jpg)');
				$('showLeft').set('alt', nextRotation);
			}		
	};			
		
	// Feature Rotator
	var erot = function rotator() {
		ad_swap();	
	};

	var erotor = erot.periodical(7000);

	// Click to jump to slide
	$$('.sliderLink').each(function(element) {
		element.addEvent('mouseover', function() {
			var erotor = erot.periodical(30000);
			
				var thisLinkString = this.get("id");
				thisLinkString = thisLinkString.replace("homeLink", "");
				thisLink = Number(thisLinkString);
				ad_swap(thisLink);
		});
	});

	
});


