var RealEffects = function() {
};

RealEffects.fadeIn = function(id, duration) {
	$("#" + id).fadeIn(duration);
};

RealEffects.fadeOut = function(id, duration) {
	$("#" + id).fadeOut(duration);
};

// Only allow a single property to be manipulated at the moment
RealEffects.animate = function(id, duration, params) {
	$("#" + id).show();
	$("#" + id).animate(params, duration);
};

var ra = document.getElementById("realAd");
var rc = document.getElementById("realContent");
var BLANK_HTML = "target='_blank'";
var imageUrls = [];
var realSlides = function() {
	var slides = [];

	// Fade in the realAd div to ensure a bit of time to load images and separate the presentation
	// from the slide content, always start with this entry, feel free to vary times.
	slides.push(new Slide("realAd1", "realBG", "static/images/RealAd/NM_ad6_NMlogo.gif", RealEffects.fadeIn, null, 1, 2000));
	
	// PRESENTATION SLIDES START HERE
	slides.push(new Slide("realBG1", "realBG", "static/images/RealAd/NM_ad1.gif", RealEffects.fadeIn, null, 1000, 5000));
	slides.push(new Slide("schools", "call bottom", "static/images/RealAd/schools.png", RealEffects.animate, {"left": "-=468"}, 1000, 1500));
	slides.push(new Slide("realBG2", "realBG", "static/images/RealAd/NM_ad2.gif", RealEffects.fadeIn, null, 1500, 1500));
	slides.push(new Slide("walkable", "call mid", "static/images/RealAd/walkable.png", RealEffects.animate, {"left": "-=325"}, 1000, 1500));
	slides.push(new Slide("realBG3", "realBG", "static/images/RealAd/NM_ad3.gif", RealEffects.fadeIn, null, 1500, 1500));
	slides.push(new Slide("budget", "call top", "static/images/RealAd/budget.png", RealEffects.animate, {"left": "-=183"}, 1000, 1500));
	slides.push(new Slide("realBG4", "realBG", "static/images/RealAd/NM_ad4.gif", RealEffects.fadeIn, null, 1500, 4000));
	slides.push(new Slide("schools", "call bottom", "schools/budget.png", RealEffects.fadeOut, null, 1000, 0));
	slides.push(new Slide("walkable", "call mid", "static/images/RealAd/walkable.png", RealEffects.fadeOut, null, 1000, 0));
	slides.push(new Slide("budget", "call top", "static/images/RealAd/budget.png", RealEffects.fadeOut, null, 1000, 0));
	slides.push(new Slide("realPresents", "realBG", "static/images/RealAd/NM_ad5_realpresents.gif", RealEffects.fadeIn, null, 1000, 3500));
	slides.push(new Slide("realLogo", "realBG", "static/images/RealAd/NM_ad6_NMlogo.gif", RealEffects.fadeIn, null, 2000, 5000));
	// PRESENTATION SLIDES END HERE
	
	// Fade out the realAd div to ensure that we can destroy all of the created slides and repeat smoothly,
	// always end with this entry, feel free to vary the times
	slides.push(new Slide("realAd1", "realBG", "static/images/RealAd/NM_ad6_NMlogo.gif", RealEffects.fadeOut, null, 2000, 1));

	return slides;
};

function Slide(id, cssClass, imagePath, effect, params, duration, delayUntilNext, href, newWindow) {
	
	// Set delay until next slide for use with scheduler
	this.delay = delayUntilNext;
	// Set animation function for use with scheduler
	this.action = function() {
		//console.log("Executing action...");
		effect(id, duration, params);
	};

	this.html = [];
	if (href) {
		var blank = "";
		// Set the a attribute to the _blank value if the flag is present to open the link in a new window
		if (newWindow) {
			blank = BLANK_HTML;
		}
		// If we're passed an href, create the opening a tag to link the image
		this.html.push("<a href='" + href + "' + " + blank + ">");
	}
	
	// Check to see if layout element exists, do not add it if it's already in the DOM
	// Used only with images at the moment
	// Allows for multiple actions to be added for a single slide/DOM element
	if (!document.getElementById(id)) {
		this.html.push("<img id='" + id + "' class='" + cssClass + "' src='" + imagePath + "' />");
	}
	
	// Close link if href was passed in
	if (href) {
		this.html.push("</a>");
	}
	
	ra.innerHTML += this.html.join("");
}

// The scheduler object is initialized with an array of slides
// Calls itself recursively and iterates over the slides
// Depends on the slides to provide the delay in the recursive call
function scheduler(events) {
	this.events = events;
	this.eventHandles = [];
	this.currentEvent = 0;
	
	this.execute = function() {
		if (this.currentEvent >= this.events.length) {
			ra.innerHTML = "";
			this.currentEvent = 0;
			this.events = null;
			this.events = realSlides();
		}
		this.events[this.currentEvent].action();
		this.eventHandles[this.currentEvent] = setTimeout("myScheduler.execute()", this.events[this.currentEvent].delay);
		this.currentEvent++;
	};
}

var myScheduler = new scheduler(realSlides());
myScheduler.execute();
