/**
 * 
 * Slide Imagem
 */
function SlideImage(delay, imagePath, href, pageView) {
	this.delay     = delay;
	this.imagePath = imagePath;
	this.href      = href;
	this.pageView  = pageView;
	this.slideSet = null;
};

SlideImage.prototype.createElement = function() {
    var img = document.createElement("img");
    img.src = this.imagePath;
    img.border = 0;
    
    if (this.slideSet != null) {
	    img.width = this.slideSet.width;
	    img.height = this.slideSet.height;
    }

    if (this.href == null) {
    	return img;
    }
        
    var a = document.createElement("a");
	a.href = this.href;
	a.target = "_BLANK";
    a.appendChild(img);

    // pageTracker variável global criada para utilizar google-analytics
    if (this.pageView == null || pageTracker == null) {
    	return a;
    }
    
	a.onclick = function() {
		pageTracker._trackPageview(this.pageView);
	};
    
    return a;
};


/**
 SlideFlash
*/
function SlideFlash(delay, flashPath) {
	this.delay = delay;
	this.flashPath = flashPath;
	this.slideSet = null;
};

SlideFlash.prototype.createElement = function() {
	
	var embed = document.createElement("embed");
	embed.setAttribute("src", this.flashPath);
	if (this.slideSet != null) {
		embed.setAttribute("width", this.slideSet.width);
		embed.setAttribute("height", this.slideSet.height);
	}
	embed.setAttribute("quality", "high");
	embed.setAttribute("type", "application/x-shockwave-flash");
	embed.setAttribute("pluginspage", "http://www.adobe.com/go/getflashplayer");
	embed.setAttribute("allowScriptAccess", "sameDomain");
	embed.setAttribute("allowFullScreen", "false");

    return embed;
};



/**
 * SlideSet
 */
function SlideSet(elementId, width, height) {
	this.panel = document.getElementById(elementId);
	this.currentSlide = -1;
	this.nextSlide = 0;
	this.slides = new Array();
	this.running = false;
	this.timeout = null;
	this.width = width;
	this.height = height;
};

SlideSet.prototype.addFirst = function(slide) {
	slide.slideSet = this;
	this.slides.unshift(slide);
};

SlideSet.prototype.addLast = function(slide) {
	slide.slideSet = this;
	this.slides.push(slide);
};

SlideSet.prototype.shuffle = function() {
    for (var i = this.slides.length; i > 1; i--) {
        var j = Math.floor(Math.random() * i);
        var tmp = this.slides[i-1];
        this.slides[i-1] = this.slides[j];
        this.slides[j] = tmp;
    }
};

SlideSet.prototype.getSlide = function() {
	return this.slides[this.currentSlide];
};

SlideSet.prototype.isEmpty = function() {
	return this.slides.length == 0;
};

SlideSet.prototype.next = function() {
	this.currentSlide = this.nextSlide;
	this.nextSlide++;
	if (this.nextSlide >= this.slides.length) {
		this.nextSlide = 0;
	}
};

SlideSet.prototype.clear = function() {
	while (this.panel.childNodes.length > 0) {
		var child = this.panel.firstChild;
		this.panel.removeChild(child);
	}
};

SlideSet.prototype.draw = function() {
	this.clear();
    this.panel.appendChild(this.getSlide().createElement());
};

SlideSet.prototype.run = function() {
	if (this.isEmpty() || this.running) {
		return;
	}
	
	this.running = true;
	
    var self = this;
    var loop = function() {
		self.next();
		self.draw();
		if (self.running) {
			self.timeout = setTimeout(loop, self.getSlide().delay);
		}
	};
	loop();
};

SlideSet.prototype.clearTimeout = function() {
	if (this.timeout == null) {
		return;
	}
	clearTimeout(this.timeout);
	this.timeout = null;
};

SlideSet.prototype.stop = function() {
	if (this.running) {
		this.running = false;
		this.currentSlide = -1;
		this.nextSlide = 0;
		this.clearTimeout();
	}
};


