// JavaScript Document
var rotatingAds;
var rotatingLinks;

function $() {
	var elements = new Array();
	for (var i=0; i< arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == "string") {
			element = document.getElementById(element);
		}
		elements.push(element);
	}
	return (elements.length > 1)? elements : element;
}


function preloadImages(images) {
	var bufferedImages = new Array();
	for (var i=0; i<images.length; i++) {
		var imageBuffer = new Image();
		imageBuffer.src = images[i];
		bufferedImages.push(imageBuffer);
	}
	return bufferedImages;
}

var SlideShow = {
	element: null,
	images: null,
	imagesRelated: null,
	timeout: null,
	currentIndex: null,
	
	create: function(id, titleid, arrowsid, images, imagesRelated, timeout) {
		this.element = $(id);
		this.title = $(titleid);
		this.arrows = $(arrowsid);
		this.images = images;
		this.imagesRelated = imagesRelated;
		this.timeout = timeout;
	},
	
	start: function() {
		this.currentIndex = Math.floor(Math.random() * this.images.length);
		if (this.images.length > 0) SlideShow.showNext();
		if (this.images.length > 1) this.arrows.style.display = "block";
	},
	
	showNext: function() {
		this.currentIndex = (this.currentIndex + 1) % this.images.length;
		if (this.images.length > 1)	t=setTimeout(function() {
			SlideShow.showNext();							
		}, this.timeout);

		this.element.src = this.images[this.currentIndex];
		str = this.images[this.currentIndex];
		SlideShow.updateTitle(str);
		Effect.fade(this.element, 100, 0, 5);
	},

	jumpNext: function() {
		alert(this.currentIndex);
		if (this.currentIndex == (this.images.length -1)) this.currentIndex = 0;
		else this.currentIndex = this.currentIndex + 1;
		this.element.src = this.images[this.currentIndex];
		str = this.images[this.currentIndex];
		SlideShow.updateTitle(str);
		clearTimeout(t);
		if (this.images.length > 1)	t=setTimeout(function() {
			SlideShow.showNext();							
		}, this.timeout);
		alert(this.currentIndex);
		
	},

	jumpPrevious: function() {
		alert(this.currentIndex);
		if (this.currentIndex == 0) this.currentIndex = (this.images.length -1);
		else this.currentIndex = this.currentIndex - 1;
		this.element.src = this.images[this.currentIndex];
		str = this.images[this.currentIndex];
		SlideShow.updateTitle(str);
		clearTimeout(t);
		if (this.images.length > 1)	t=setTimeout(function() {
			SlideShow.showNext();							
		}, this.timeout);
		alert(this.currentIndex);

	},

	updateTitle: function(str) {
		if (str.match("Creative_Services")) this.title.innerHTML = "Creative Services";
		else if (str.match("Education_Childcare") != null) this.title.innerHTML = "Education & Child Care";
		else if (str.match("HOME__Cleaning__Repairs___Improvements") != null) this.title.innerHTML = "HOME: Cleaning, Repairs & Improvements";
		else if (str.match("Personal___Special_Occasion") != null) this.title.innerHTML = "Personal & Special Occasion";
		else if (str.match("Health___Beauty") != null) this.title.innerHTML = "Health & Beauty";
		else if (str.match("Automotive") != null) this.title.innerHTML = "Automotive";
		else if (str.match("Computers") != null) this.title.innerHTML = "Computers";
		else if (str.match("Food__Beverage___Entertainment") != null) this.title.innerHTML = "Food, Beverage & Entertainment";
		else if (str.match("Legal") != null) this.title.innerHTML = "Legal Services";
		else if (str.match("Leisure__Hobbies__Music__Clubs") != null) this.title.innerHTML = "Leisure, Hobbies, Music, Clubs";
		else if (str.match("Real_Estate") != null) this.title.innerHTML = "Real Estate";
		else if (str.match("Insurance_Finance") != null) this.title.innerHTML = "Insurance & Financial Services";
		else if (str.match("Travel") != null) this.title.innerHTML = "Travel";
	}
};

var Effect = {
	opacity: function(idObj, value) {
		var element = (typeof idObj == "string") ? $(idObj) : idObj;
		element.style.opacity = value / 100;
		element.style.filter = "alpha(opacity=" + value + ")";
	},
	
	fade: function(id, start, end, duration) {
		var steps = Math.round(duration / 100);
		var stepValue = Math.round((end - start) / steps);
		for (var i=0; i<steps; i++) {
			setTimeout("Effect.opacity('" + id + "', " + (start + i*stepValue) + ");", 100);
		}
	}
}