var divAdvertisementLeft;
var divAdvertTitle;
var divAdvertDescription;

$(document).ready(function() {

	$(".es-MX").css("display", "none");
	
	var lnkLogo = $(".lnkHome");
	lnkLogo.attr("href", lnkLogo.html());
	lnkLogo.html("");
	
	var boxesDiv = $(".divBoxes");
	if (boxesDiv.length > 0)
	{
		/* I realize this is extremely lame, but all browsers *cough* IE7 *cough* don't support table DIVs properly */
		var fullHeight = $(".left-feature").height() + $(".left-article").height();
		boxesDiv[0].style.minHeight = Math.max(fullHeight - 280, 500) + "px";
	}
	
	divAdvertisementLeft = document.getElementById("divAdvertisementLeft");
	divAdvertTitle = document.getElementById("divAdvertTitle");
	divAdvertDescription = document.getElementById("divAdvertDescription");
		
	if (divAdvertisementLeft)
	{
		$("#divAdvertisementLeft a[rel='slide']").crossf4de({
			callBack: thermoconceptCallback,
			fadeDuration: 2000,
			pauseDuration: 5000
		});
	}
});

function setImageWidth()
{
	var divs = $("#divBigBox .divGalleries .gallery img");
			
	var tallestDiv = 0;
	for (var i = 0; i < divs.length; i++)
	{
		var thisDivWidth = $(divs[i]).width();		
		if (tallestDiv < thisDivWidth)
		{
			tallestDiv = thisDivWidth;
		}
	}
	
	if (tallestDiv > 0)
	{
		divs = $("#divBigBox .divGalleries");	
		divs.css("width", tallestDiv + "px");
	}
}

function setImageHeight()
{
	var divs = $("#divBigBox .divGalleries");
	var tallestDiv = 0;
	for (var i = 0; i < divs.length; i++)
	{	
		var thisDivHeight = $(divs[i]).height();		
		if (tallestDiv < thisDivHeight)
		{
			tallestDiv = thisDivHeight;
		}
	}
	
	if (tallestDiv > 0)
	{	
		divs.css("height", tallestDiv + "px");
	}
}

function setLoadImageWidth()
{
	var divs = $(".divGalleries .gallery img");
			
	var tallestDiv = 0;
	for (var i = 0; i < divs.length; i++)
	{
		var thisDivWidth = $(divs[i]).width();		
		if (tallestDiv < thisDivWidth)
		{
			tallestDiv = thisDivWidth;
		}
	}
	
	if (tallestDiv > 0)
	{
		divs = $(".divGalleries");	
		divs.css("width", tallestDiv + "px");
	}
}

function setLoadImageHeight()
{
	var divs = $(".divGalleries");
	var tallestDiv = 0;
	for (var i = 0; i < divs.length; i++)
	{	
		var thisDivHeight = $(divs[i]).height();		
		if (tallestDiv < thisDivHeight)
		{
			tallestDiv = thisDivHeight;
		}
	}
	
	if (tallestDiv > 0)
	{	
		divs.css("height", tallestDiv + "px");
	}
}

function setTabHeight()
{
	var tabs = $(".divTab");
	var tallestTab = 0;
	for (var i = 0; i < tabs.length; i++)
	{
		var thisTabHeight = $(tabs[i]).height();		
		if (tallestTab < thisTabHeight)
		{
			tallestTab = thisTabHeight;
		}
	}
	tabs.css("height", tallestTab + "px");
	
	tabs = $(".divTabs");
	tallestTab += 17;	
	tabs.css("height", tallestTab + "px");
	
	selectTab(tabs[0], 1);
	return tabs;
}

function selectTab(sender, load)
{
	var tabs = $(".divTab");
	if (tabs.length == 1)
		tabs.css("display", "none");
	var tabIndex = 0;
	for (var i = 0; i < tabs.length; i++)
	{
		var className = "divTab";
		if (tabs[i] == sender)
		{
			className += " divTabSelected";
			tabIndex = i;
		}
		tabs[i].className = className;
	}
	var tabContent = $(".divTabContent")[tabIndex];
	$("#divBigBox").html("<br style='clear: both' />" + tabContent.innerHTML + "<br style='clear: both' />");
	$("#divBigBox .gallery").fancybox({
	'transitionIn'	:	'elastic',
	'transitionOut'	:	'elastic',
	'speedIn'		:	600, 
	'speedOut'		:	200, 
	'overlayShow'	:	false
	});	
	  	 
	$(".divGalleries img").preloadImages();
		
}

$.fn.preloadImages = function() {
    var total = this.length;
    this.each(function(){
	total--;
	var imageUrl = this;
	if (this.tagName && this.tagName.toUpperCase() == "IMG")
		imageUrl = this.src;
        var img = new Image();
	if (total == 0) {
		img.onload = function() {
			setImageWidth();
			setImageHeight();
		}
	}
	img.src = imageUrl;
    });
}

$(window).load(function() {
 	setImageWidth();
	setImageHeight();
 });

function thermoconceptCallback(Items, Index)
{
	var link = Items[Index][1];
	
	divAdvertTitle.innerHTML = advertRecords[Index][0];
	divAdvertDescription.innerHTML = advertRecords[Index][1];
	
	var clickHandler = null;
	var mouseStyle = "";
	if (link)
	{
		clickHandler = function() { document.location = link };
		mouseStyle = "pointer";
	}
	divAdvertisementLeft.onclick = clickHandler;
	divAdvertisementLeft.style.cursor = mouseStyle;	
}

