var featuredListingTimer = null;
var currentFeaturedListing = null;
var totalFeaturedListings = 5;
var rotationDelay = 2000;
var featuredListings = [ { 'url': '', 'title': '' }, // always leave blank
	                       { 'url': 'buy/viewlisting.php?id=11556', 'title': 'Featured Listing #11556' },
	                       { 'url': 'buy/viewlisting.php?id=11040', 'title': 'Featured Listing #11040' },
	                       { 'url': 'buy/viewlisting.php?id=11565', 'title': 'Featured Listing #11565' },
	                       { 'url': 'buy/viewlisting.php?id=11174', 'title': 'Featured Listing #11174' },
	                       { 'url': 'buy/viewlisting.php?id=11564', 'title': 'Featured Listing #11564' } ];

function initPage() {
  // preload featured listing images
  var img = new Image();
  img.src = 'images/home/featured1.jpg';
  img.src = 'images/home/featured2.jpg';
  img.src = 'images/home/featured3.jpg';
  img.src = 'images/home/featured4.jpg';
  img.src = 'images/home/featured5.jpg';
  
  changeToFeaturedListing(1);
  startRotateTimer();
}

function startRotateTimer() {
	featuredListingTimer = setTimeout('rotateToTheNextFeaturedListing();', rotationDelay);
}

function stopRotateTimer() {
	clearTimeout(featuredListingTimer);
}

function rotateToTheNextFeaturedListing() {
	// move to the next listing
	var nextListing = currentFeaturedListing + 1;
	
	// if past the end, go to the start
	if (nextListing > totalFeaturedListings) {
	  nextListing = 1;
	}
	
	// change to the next featured listing
	changeToFeaturedListing(nextListing);
	
	// start the timer again
	startRotateTimer();
}

function changeToFeaturedListing(num) {
  // unselect the current featured listing
  if (currentFeaturedListing != null) {
	  var td = document.getElementById('featuredListing' + currentFeaturedListing);
	  td.className = '';
  }

	// change the selected featured listing
	currentFeaturedListing = num;
	var td = document.getElementById('featuredListing' + currentFeaturedListing);
	td.className = 'selected';
	
	// change the featured listing link
	var link = document.getElementById('featuredListingLink');
	if (featuredListings[currentFeaturedListing].url.length == 0) {
		link.href = '#';
		link.onclick = new Function('return false;');
	  link.title = featuredListings[currentFeaturedListing].title;
	} else {
	  link.href = featuredListings[currentFeaturedListing].url;
	  link.onclick = new Function();
	  link.title = featuredListings[currentFeaturedListing].title;
	}
	
	// change the featured listing image
	var img = document.getElementById('featuredListingImage');
	img.src = 'images/home/featured' + currentFeaturedListing + '.jpg';
	img.alt = featuredListings[currentFeaturedListing].title;
}
