﻿dojo.addOnLoad(function() {
	// Initialize the Orange Buttons
	dojo.query(".te-orange-btn").forEach(function(o) {
		new ButtonControl({
			"link" : o
		});
	});
});

// Controls the classnames of the orange buttons
// forces behavioral consistency across all browsers
function ButtonControl(cfg) {
	this.link = dojo.byId(cfg.link);
	dojo.connect(this.link, "onmouseover", this, function() { dojo.addClass(this.link, "te-orange-btn-hover"); });
	dojo.connect(this.link, "onmouseout", this, function() { dojo.removeClass(this.link, "te-orange-btn-hover"); });
	dojo.connect(this.link, "onmousedown", this, function() { dojo.addClass(this.link, "te-orange-btn-active"); });
	dojo.connect(this.link, "onmouseup", this, function() { dojo.removeClass(this.link, "te-orange-btn-active"); });
}

// Fix for IE since there's no CSS display : table-cell property
// forces divs to be the same height at the bottom of the homepage
function ForceConsistentHeight(cfg) {
	this.nodes = cfg.nodes;
	this.maxHeight = 0;
	this.fixed = true;
	this.updateMaxHeight();
	this.applyNewHeights();
	this.checkHeights();
	if(!this.fixed) {
		this.offset = 0;
		this.getOffset();
		this.fixHeights();
	}
};
ForceConsistentHeight.prototype.updateMaxHeight = function() {
	var h = 0;
	dojo.forEach(this.nodes, function(o) {
		h = dojo.coords(o).h;
		this.maxHeight = h > this.maxHeight ? h : this.maxHeight;
	}, this);
	this.applyNewHeights();
};
ForceConsistentHeight.prototype.applyNewHeights = function() {
	var h = 0;
	dojo.forEach(this.nodes, function(o) {
		h = dojo.coords(o).h;
		if(this.maxHeight > h) {
			dojo.style(o, {
				"height" : this.maxHeight + "px"
			});
		}
	}, this);
};
ForceConsistentHeight.prototype.checkHeights = function() {
	dojo.every(this.nodes, function(o) {
		if(dojo.coords(o).h != this.maxHeight && this.fixed) {
			this.fixed = false;
		}
	}, this);
};
ForceConsistentHeight.prototype.getOffset = function() {
	var h = 0;
	dojo.forEach(this.nodes, function(o) {
		h = dojo.coords(o).h;
		if(this.maxHeight > h) {
			this.offset = this.maxHeight - h;
		}
	}, this);
};
ForceConsistentHeight.prototype.fixHeights = function() {
	var h = 0;
	dojo.forEach(this.nodes, function(o) {
		h = dojo.coords(o).h;
		if(h == this.maxHeight) {
			dojo.style(o, {
				"height" : this.maxHeight + this.offset + "px"
			});
		}
	}, this);
};
function MiniMarquee(cfg) {
	this.widget = dojo.byId(cfg.widget);
	this.countContainer = dojo.query(".te-mini-marquee-count-current", this.widget)[0];
	this.marquee = dojo.query(".te-mini-marquee", this.widget)[0];
	this.imgLink = dojo.query(".te-mini-marquee-image-link", this.marquee)[0];
	this.img = dojo.query("img", this.imgLink)[0];
	this.header = dojo.query(".te-marquee-sub-header", this.widget)[0];
	this.paragraph = dojo.query("p", this.widget)[0];
	this.learnMore = dojo.query(".te-marquee-learn-more", this.widget)[0];
	this.data = cfg.data;
	this.current = 0;
	this.images = [];
	this.count = this.data[this.current].count;
	dojo.connect(dojo.query(".te-previous-control", this.marquee)[0], "onclick", this, function() { this.updateData("prev"); });
	dojo.connect(dojo.query(".te-next-control", this.marquee)[0], "onclick", this, function() { this.updateData("next"); });
	dojo.forEach(this.data, function(o) {
		this.images[this.images.length] = new Image();
		this.images[this.images.length - 1].src = o.image;
	}, this);
}
MiniMarquee.prototype.updateData = function(dir) {
	if(dir == "next") {
		this.current = this.current === this.count - 1 ? 0 : this.current + 1;
	} else {
		this.current = this.current === 0 ? this.count - 1 : this.current - 1;
	}
	this.countContainer.innerHTML = (this.current + 1);
	this.imgLink.href = this.data[this.current].url;
	this.img.src = this.data[this.current].image;
	this.img.alt = this.data[this.current].name;
	this.img.title = this.data[this.current].name;
	this.header.innerHTML = this.trim(this.data[this.current].name, 35);
	this.header.href = this.data[this.current].url;
	this.header.title = this.data[this.current].name;
	this.paragraph.innerHTML = this.trim(this.data[this.current].desc, 90);
	this.learnMore.href = this.data[this.current].url;
};
MiniMarquee.prototype.trim = function(str, max) {
	var newStr = "";
		
	if(str.length > max) {
		newStr = str.substring(0, max);
		newStr = newStr.substring(0, newStr.lastIndexOf(' ')) + "...";
	} else {
		newStr = str;
	}
		
	return newStr;
};
