//currentaly opened sub tab name
var systemSizeSelectorOption = 0;

function contentLoad(url) {
	if (url != '#') $("div#content").load(url + " div#content > *");
	return false;
}

function contentDivIdLoad(url, id, afterCall) {
	if (url != '#') $("div#" + id).load(url, afterCall);
	return false;
}

function contentLoadInfoPane(url) {
	if (url != '#') $("div#info-panel").load(url + " div#info-panel > *");
	return false;
}


function submenuLoad(url) {
	if (url != '#') $("div#subnav ul").load(url + " div#subnav ul");
	return false;
}

function setOption(option, name) {
	$("#quote-option-id").attr("value", option);

	if (name == 'A') {
//		$(".size-selector-B").removeClass("opt-on");
//		$(".size-selector-C").removeClass("opt-on");
//		$(".size-selector-A").addClass("opt-on");
		systemSizeSelectorOption = 0;
	}

	if (name == 'B') {
//		$(".size-selector-A").removeClass("opt-on");
//		$(".size-selector-C").removeClass("opt-on");
//		$(".size-selector-B").addClass("opt-on");
		systemSizeSelectorOption = 1;
	}

	if (name == 'C') {
//		$(".size-selector-B").removeClass("opt-on");
//		$(".size-selector-A").removeClass("opt-on");
//		$(".size-selector-C").addClass("opt-on");
		systemSizeSelectorOption = 2;
	}
}

function activateTab(tabName) {
	$(tabName).idTabs(systemSizeSelectorOption);
}

//Env Benefits tab functionality
// how many milliseconds we should wait between resizing events
var resizeDelay = 10;
// how many pixels we should grow or shrink by each time we resize
var resizeIncrement = 5;

var imgCache = new Object();

function getCacheTag (imgElement) {
	return imgElement.src + "~" + imgElement.offsetLeft + "~" + imgElement.offsetTop;
}

function cachedImg (imgElement, increment) {
	this.img = imgElement;
	this.cacheTag = getCacheTag(imgElement);
	this.originalSrc = imgElement.src;

	var h = imgElement.height;
	var w = imgElement.width;
	this.originalHeight = h;
	this.originalWidth = w;

	increment = (!increment) ? resizeIncrement : increment;
	this.heightIncrement = Math.ceil(Math.min(1, (h / w)) * increment);
	this.widthIncrement = Math.ceil(Math.min(1, (w / h)) * increment);
}
function resizeImg (imgElement, percentChange, newImageURL) {
	var pct = (percentChange) ? percentChange / 100 : 1;
	var cacheTag = imgElement.getAttribute("cacheTag");
	if (!cacheTag) {
		cacheTag = getCacheTag(imgElement);
		imgElement.setAttribute("cacheTag", cacheTag);
	}
	var cacheVal = imgCache[cacheTag];
	if (!cacheVal) {
		imgCache[cacheTag] = new Array(new cachedImg(imgElement), pct);
	} else {
		cacheVal[1] = pct;
	}
	if (newImageURL)
		imgElement.src = newImageURL;
	resizeImgLoop(cacheTag);
	return true;
}
function resizeImgLoop (cacheTag) {
	var cacheVal = imgCache[cacheTag];
	if (!cacheVal)
		return false;
	var cachedImageObj = cacheVal[0];
	var imgElement = cachedImageObj.img;
	var pct = cacheVal[1];
	var plusMinus = (pct > 1) ? 1 : -1;
	var hinc = plusMinus * cachedImageObj.heightIncrement;
	var vinc = plusMinus * cachedImageObj.widthIncrement;
	var startHeight = cachedImageObj.originalHeight;
	var startWidth = cachedImageObj.originalWidth;

	var currentHeight = imgElement.height;
	var currentWidth = imgElement.width;
	var endHeight = Math.round(startHeight * pct);
	var endWidth = Math.round(startWidth * pct);

	if ( (currentHeight == endHeight) || (currentWidth == endWidth) )
		return true;

	var newHeight = currentHeight + hinc;
	var newWidth = currentWidth + vinc;
	if (pct > 1) {
		if ((newHeight >= endHeight) || (newWidth >= endWidth)) {
			newHeight = endHeight;
			newWidth = endWidth;
		}
	} else {
		if ((newHeight <= endHeight) || (newWidth <= endWidth)) {
			newHeight = endHeight;
			newWidth = endWidth;
		}
	}

	imgElement.height = newHeight;
	imgElement.width = newWidth;

	if ((newHeight == cachedImageObj.originalHeight) || (newWidth == cachedImageObj.originalwidth)) {
		imgElement.src = cachedImageObj.originalSrc;
	}
	setTimeout("resizeImgLoop('" + cacheTag + "')", resizeDelay);
}
function mouseOverEvent(imgid,src,time) {
	var img = $(imgid);
	resizeImg(img[0], time, src);
}
