function phoneSelectorControlLeft() {
	if (getStatusLeft()) {
		if (getCurrentPhoneIndex() != null) {
			currentPhoneIndex--;
			movePhones();
		}
	}
}

function phoneSelectorControlRight() {
	if (getStatusRight()) {
		if (getCurrentPhoneIndex() != null) {
			currentPhoneIndex++;
			movePhones();
		}
	}
}

function getStatusLeft() {
	if (statusLeft == null) {
		setStatusLeft();
	}
	return statusLeft;
}
function setStatusLeft() {
	if (getMaxPhoneIndex() > 1) {
		var ctlLeft = document.getElementById('phone-selector-control-left');
		if (getCurrentPhoneIndex() <= 0) {
			if (statusLeft == null || statusLeft) {
				if (ctlLeft != null) {
					ctlLeft.className = 'disabled';
				}
			}
			statusLeft = false;
		} else {
			if (statusLeft == null || !statusLeft) {
				if (ctlLeft != null) {
					ctlLeft.className = 'enabled';
				}
			}
			statusLeft = true;
		}
		if (statusLeft) {
			ctlLeft.innerHTML = "";
			var ctlLink = document.createElement("a");
			ctlLink.href = "javascript:phoneSelectorControlLeft();";
			ctlLink.innerHTML = "Previous";
			ctlLink.title = "Click to see the previous phone";
			ctlLeft.appendChild(ctlLink);
		} else {
			ctlLeft.innerHTML = "Previous";
		}
	}
}
function getStatusRight() {
	if (statusRight == null) {
		setStatusRight();
	}
	return statusRight;
}
function setStatusRight() {
	if (getMaxPhoneIndex() > 1) {
		var ctlRight = document.getElementById('phone-selector-control-right');
		if (getCurrentPhoneIndex() == getMaxPhoneIndex()-1) {
			if (statusRight == null || statusRight) {
				if (ctlRight != null) {
					ctlRight.className = 'disabled';
				}
			}
			statusRight = false;
		} else {
			if (statusRight == null || !statusRight) {
				if (ctlRight != null) {
					ctlRight.className = 'enabled';
				}
			}
			statusRight = true;
		}
		if (statusRight) {
			ctlRight.innerHTML = "";
			var ctlLink = document.createElement("a");
			ctlLink.href = "javascript:phoneSelectorControlRight();";
			ctlLink.innerHTML = "Next";
			ctlLink.title = "Click to see the next phone";
			ctlRight.appendChild(ctlLink);
		} else {
			ctlRight.innerHTML = "Next";
		}
	}
}
function getCurrentPhoneIndex() {
	if (currentPhoneIndex == null) {
		for (var i=0; i<getMaxPhoneIndex(); i++) {
			var phoneEl = document.getElementById('phone' + i);
			if (phoneEl.style.display == 'block' || phoneEl.className.indexOf("first") != -1) {
				currentPhoneIndex = i;
				return currentPhoneIndex;
			}
		}
	}
	return currentPhoneIndex;
}
function getMaxPhoneIndex() {
	if (maxPhoneIndex == null) {
		var phonesEl = document.getElementById('phoneselector');
		if (phonesEl != null) {
			maxPhoneIndex = phonesEl.getElementsByTagName("td").length;
			if (maxPhoneIndex != null && maxPhoneIndex > 0) {
				maxPhoneIndex /= 2; // Removes Text Elements from Count
			}
		}
	}
	return maxPhoneIndex;
}

function movePhones() {
	for (var i=0; i<getMaxPhoneIndex(); i++) {
		var phoneEl = document.getElementById('phone' + i);
		var phoneElTxt = document.getElementById('phone' + i + 'text');
		if (i == getCurrentPhoneIndex()) {
			phoneEl.style.display = 'block';
			phoneElTxt.style.display = 'block';
		} else {
			phoneEl.style.display = 'none';
			phoneElTxt.style.display = 'none';
		}
	}
	setStatusLeft();
	setStatusRight();
}

function initiatePhoneControls(resetVars) {
	if (resetVars) {
		maxPhoneIndex = null;
		currentPhoneIndex = null;
		statusLeft = null;
		statusRight = null;
	}
	if (getMaxPhoneIndex() != null) {
		var ctlLeft = document.getElementById('phone-selector-control-left');
		var ctlRight = document.getElementById('phone-selector-control-right');
		if (ctlLeft != null) {
			ctlLeft.onclick = phoneSelectorControlLeft;
		}
		if (ctlRight != null) {
			ctlRight.onclick = phoneSelectorControlRight;
		}

		setStatusLeft();
		setStatusRight();
	}
}

var maxPhoneIndex = null;
var currentPhoneIndex = null;
var statusLeft = null;
var statusRight = null;


/* Phone Selector Filter Control */
function updateFilter(newBrand) {
	var counter = 0;
	var scroller = document.getElementById('select-phone-scroll-window');
	var container = document.getElementById('select-phone-scroll-panel');
	var allPhones = container.getElementsByTagName('div');
	
	var debugStr = '';
	for (var i=0; i<allPhones.length; i++) {
		var elClass = allPhones[i].className.split(' ');
		var strBrand = '';
		for (var c=0; c<elClass.length; c++) {
			if (elClass[c].indexOf("brand-") != -1) {
				strBrand = elClass[c].split('-')[1];
			}
		}
		
		if (newBrand == '' || strBrand == newBrand) {
			if (elClass[0] == "select-phone-handset") {
				if (allPhones[i].className.indexOf("displayNone") != -1) {
					allPhones[i].className = allPhones[i].className.replace('displayNone', 'displayBlock');
				}
				counter ++;
			}
		} else {
			if (elClass[0] == "select-phone-handset") {
				if (allPhones[i].className.indexOf("displayBlock") != -1) {
					allPhones[i].className = allPhones[i].className.replace('displayBlock', 'displayNone');
				}
			}
		}
	}
	var newWidth = psMinWidth;
	if ((psHstWidth * counter) > newWidth) {
		newWidth = psHstWidth * counter;
	}
	scroller.scrollLeft = '1px';
	container.style.width = newWidth + 'px';
}

var psMinWidth = 740;
var psHstWidth = (isIE6) ? 122 : 131;
function setPhoneSelectorDimensions(minWidth, handsetWidth) {
	psMinWidth = minWidth;
	psHstWidth = (isIE6) ? handsetWidth-9 : handsetWidth;
}